|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/github/github-mcp-server/pkg/translations" |
| 9 | + "github.com/mark3labs/mcp-go/mcp" |
| 10 | + "github.com/mark3labs/mcp-go/server" |
| 11 | + "github.com/shurcooL/githubv4" |
| 12 | +) |
| 13 | + |
| 14 | +// Discussion represents a GitHub Discussion with its essential fields |
| 15 | +type Discussion struct { |
| 16 | + ID string `json:"id"` |
| 17 | + Number int `json:"number"` |
| 18 | + Title string `json:"title"` |
| 19 | + Body string `json:"body"` |
| 20 | + CreatedAt string `json:"createdAt"` |
| 21 | + UpdatedAt string `json:"updatedAt"` |
| 22 | + URL string `json:"url"` |
| 23 | + Category string `json:"category"` |
| 24 | + Author string `json:"author"` |
| 25 | + Locked bool `json:"locked"` |
| 26 | + UpvoteCount int `json:"upvoteCount"` |
| 27 | +} |
| 28 | + |
| 29 | +// GetRepositoryDiscussions creates a tool to fetch discussions from a specific repository. |
| 30 | +func GetRepositoryDiscussions(getGraphQLClient GetGraphQLClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 31 | + return mcp.NewTool("get_repository_discussions", |
| 32 | + mcp.WithDescription(t("TOOL_GET_REPOSITORY_DISCUSSIONS_DESCRIPTION", "Get discussions from a specific GitHub repository")), |
| 33 | + mcp.WithString("owner", |
| 34 | + mcp.Required(), |
| 35 | + mcp.Description("Repository owner"), |
| 36 | + ), |
| 37 | + mcp.WithString("repo", |
| 38 | + mcp.Required(), |
| 39 | + mcp.Description("Repository name"), |
| 40 | + ), |
| 41 | + ), |
| 42 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 43 | + owner, err := requiredParam[string](request, "owner") |
| 44 | + if err != nil { |
| 45 | + return mcp.NewToolResultError(err.Error()), nil |
| 46 | + } |
| 47 | + |
| 48 | + repo, err := requiredParam[string](request, "repo") |
| 49 | + if err != nil { |
| 50 | + return mcp.NewToolResultError(err.Error()), nil |
| 51 | + } |
| 52 | + |
| 53 | + categoryId, err := OptionalParam[string](request, "categoryId") |
| 54 | + if err != nil { |
| 55 | + return mcp.NewToolResultError(err.Error()), nil |
| 56 | + } |
| 57 | + |
| 58 | + pagination, err := OptionalPaginationParams(request) |
| 59 | + if err != nil { |
| 60 | + return mcp.NewToolResultError(err.Error()), nil |
| 61 | + } |
| 62 | + |
| 63 | + // Get GraphQL client |
| 64 | + client, err := getGraphQLClient(ctx) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to get GitHub GraphQL client: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Define GraphQL query variables |
| 70 | + variables := map[string]interface{}{ |
| 71 | + "owner": githubv4.String(owner), |
| 72 | + "name": githubv4.String(repo), |
| 73 | + "first": githubv4.Int(pagination.perPage), |
| 74 | + "after": (*githubv4.String)(nil), // For pagination - null means first page |
| 75 | + "categoryId": (*githubv4.ID)(nil), // For category ID - null means no filter |
| 76 | + } |
| 77 | + |
| 78 | + // For pagination beyond the first page |
| 79 | + // TODO: Fix this to use actual cursor values |
| 80 | + // This is a placeholder for the cursor logic |
| 81 | + // In a real implementation, you should store and use actual cursor values |
| 82 | + if pagination.perPage > 0 && pagination.page > 1 { |
| 83 | + if pagination.page > 1 { |
| 84 | + // We'd need an actual cursor here, but for simplicity we'll compute a rough offset |
| 85 | + // In real implementation, you should store and use actual cursor values |
| 86 | + cursorStr := githubv4.String(fmt.Sprintf("%d", (pagination.page-1)*pagination.perPage)) |
| 87 | + variables["after"] = &cursorStr |
| 88 | + } |
| 89 | + |
| 90 | + if categoryId != "" { |
| 91 | + variables["categoryId"] = githubv4.ID(categoryId) |
| 92 | + } |
| 93 | + |
| 94 | + // Define the GraphQL query structure |
| 95 | + var query struct { |
| 96 | + Repository struct { |
| 97 | + Discussions struct { |
| 98 | + TotalCount int |
| 99 | + Nodes []struct { |
| 100 | + ID githubv4.ID |
| 101 | + Number int |
| 102 | + Title string |
| 103 | + Body string |
| 104 | + CreatedAt githubv4.DateTime |
| 105 | + UpdatedAt githubv4.DateTime |
| 106 | + URL githubv4.URI |
| 107 | + Category struct { |
| 108 | + Name string |
| 109 | + } |
| 110 | + Author struct { |
| 111 | + Login string |
| 112 | + } |
| 113 | + Locked bool |
| 114 | + UpvoteCount int |
| 115 | + } |
| 116 | + PageInfo struct { |
| 117 | + EndCursor githubv4.String |
| 118 | + HasNextPage bool |
| 119 | + } |
| 120 | + } `graphql:"discussions(first: $first, after: $after, categoryId: $categoryId)"` |
| 121 | + } `graphql:"repository(owner: $owner, name: $name)"` |
| 122 | + } |
| 123 | + |
| 124 | + // Only include categoryId in the query if it was provided |
| 125 | + if categoryId == "" { |
| 126 | + // Redefine the query without the categoryId filter |
| 127 | + query.Repository.Discussions = struct { |
| 128 | + TotalCount int |
| 129 | + Nodes []struct { |
| 130 | + ID githubv4.ID |
| 131 | + Number int |
| 132 | + Title string |
| 133 | + Body string |
| 134 | + CreatedAt githubv4.DateTime |
| 135 | + UpdatedAt githubv4.DateTime |
| 136 | + URL githubv4.URI |
| 137 | + Category struct { |
| 138 | + Name string |
| 139 | + } |
| 140 | + Author struct { |
| 141 | + Login string |
| 142 | + } |
| 143 | + Locked bool |
| 144 | + UpvoteCount int |
| 145 | + } |
| 146 | + PageInfo struct { |
| 147 | + EndCursor githubv4.String |
| 148 | + HasNextPage bool |
| 149 | + } |
| 150 | + }{} |
| 151 | + } |
| 152 | + |
| 153 | + // Execute the GraphQL query |
| 154 | + err = client.Query(ctx, &query, variables) |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("failed to query discussions: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + // Convert the GraphQL response to our Discussion type |
| 160 | + discussions := make([]Discussion, 0, len(query.Repository.Discussions.Nodes)) |
| 161 | + for _, node := range query.Repository.Discussions.Nodes { |
| 162 | + discussion := Discussion{ |
| 163 | + ID: fmt.Sprintf("%v", node.ID), |
| 164 | + Number: node.Number, |
| 165 | + Title: node.Title, |
| 166 | + Body: node.Body, |
| 167 | + CreatedAt: node.CreatedAt.String(), |
| 168 | + UpdatedAt: node.UpdatedAt.String(), |
| 169 | + URL: node.URL.String(), |
| 170 | + Category: node.Category.Name, |
| 171 | + Author: node.Author.Login, |
| 172 | + Locked: node.Locked, |
| 173 | + UpvoteCount: node.UpvoteCount, |
| 174 | + } |
| 175 | + discussions = append(discussions, discussion) |
| 176 | + } |
| 177 | + |
| 178 | + // Create the response |
| 179 | + result := struct { |
| 180 | + TotalCount int `json:"totalCount"` |
| 181 | + Discussions []Discussion `json:"discussions"` |
| 182 | + HasNextPage bool `json:"hasNextPage"` |
| 183 | + EndCursor string `json:"endCursor"` |
| 184 | + }{ |
| 185 | + TotalCount: query.Repository.Discussions.TotalCount, |
| 186 | + Discussions: discussions, |
| 187 | + HasNextPage: query.Repository.Discussions.PageInfo.HasNextPage, |
| 188 | + EndCursor: string(query.Repository.Discussions.PageInfo.EndCursor), |
| 189 | + } |
| 190 | + |
| 191 | + // Marshal the result to JSON |
| 192 | + r, err := json.Marshal(result) |
| 193 | + if err != nil { |
| 194 | + return nil, fmt.Errorf("failed to marshal discussions result: %w", err) |
| 195 | + } |
| 196 | + |
| 197 | + return mcp.NewToolResultText(string(r)), nil |
| 198 | + } |
| 199 | +} |
0 commit comments