|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "reflect" |
| 11 | + |
| 12 | + ghErrors "github.com/github/github-mcp-server/pkg/errors" |
| 13 | + "github.com/github/github-mcp-server/pkg/translations" |
| 14 | + "github.com/google/go-github/v74/github" |
| 15 | + "github.com/google/go-querystring/query" |
| 16 | + "github.com/mark3labs/mcp-go/mcp" |
| 17 | + "github.com/mark3labs/mcp-go/server" |
| 18 | +) |
| 19 | + |
| 20 | +func ListProjects(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 21 | + return mcp.NewTool("list_projects", |
| 22 | + mcp.WithDescription(t("TOOL_LIST_PROJECTS_DESCRIPTION", "List Projects for a user or organization")), |
| 23 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{Title: t("TOOL_LIST_PROJECTS_USER_TITLE", "List projects"), ReadOnlyHint: ToBoolPtr(true)}), |
| 24 | + mcp.WithString("owner_type", mcp.Required(), mcp.Description("Owner type"), mcp.Enum("user", "organization")), |
| 25 | + mcp.WithString("owner", mcp.Required(), mcp.Description("If owner_type == user it is the handle for the GitHub user account. If owner_type == organization it is the name of the organization. The name is not case sensitive.")), |
| 26 | + mcp.WithString("query", mcp.Description("Filter projects by a search query (matches title and description)")), |
| 27 | + mcp.WithString("before", mcp.Description("Cursor for items before (backwards pagination)")), |
| 28 | + mcp.WithString("after", mcp.Description("Cursor for items after (forward pagination)")), |
| 29 | + mcp.WithNumber("per_page", mcp.Description("Number of results per page (max 100, default: 30)")), |
| 30 | + ), func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 31 | + owner, err := RequiredParam[string](req, "owner") |
| 32 | + if err != nil { |
| 33 | + return mcp.NewToolResultError(err.Error()), nil |
| 34 | + } |
| 35 | + ownerType, err := RequiredParam[string](req, "owner_type") |
| 36 | + if err != nil { |
| 37 | + return mcp.NewToolResultError(err.Error()), nil |
| 38 | + } |
| 39 | + queryStr, err := OptionalParam[string](req, "query") |
| 40 | + if err != nil { |
| 41 | + return mcp.NewToolResultError(err.Error()), nil |
| 42 | + } |
| 43 | + |
| 44 | + beforeCursor, err := OptionalParam[string](req, "before") |
| 45 | + if err != nil { |
| 46 | + return mcp.NewToolResultError(err.Error()), nil |
| 47 | + } |
| 48 | + afterCursor, err := OptionalParam[string](req, "after") |
| 49 | + if err != nil { |
| 50 | + return mcp.NewToolResultError(err.Error()), nil |
| 51 | + } |
| 52 | + perPage, err := OptionalIntParamWithDefault(req, "per_page", 30) |
| 53 | + if err != nil { |
| 54 | + return mcp.NewToolResultError(err.Error()), nil |
| 55 | + } |
| 56 | + |
| 57 | + client, err := getClient(ctx) |
| 58 | + if err != nil { |
| 59 | + return mcp.NewToolResultError(err.Error()), nil |
| 60 | + } |
| 61 | + |
| 62 | + var url string |
| 63 | + if ownerType == "organization" { |
| 64 | + url = fmt.Sprintf("/orgs/%s/projectsV2", owner) |
| 65 | + } else { |
| 66 | + url = fmt.Sprintf("/users/%s/projectsV2", owner) |
| 67 | + } |
| 68 | + projects := []github.ProjectV2{} |
| 69 | + |
| 70 | + opts := ListProjectsOptions{PerPage: perPage} |
| 71 | + if afterCursor != "" { |
| 72 | + opts.After = afterCursor |
| 73 | + } |
| 74 | + if beforeCursor != "" { |
| 75 | + opts.Before = beforeCursor |
| 76 | + } |
| 77 | + if queryStr != "" { |
| 78 | + opts.Query = queryStr |
| 79 | + } |
| 80 | + url, err = addOptions(url, opts) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("failed to add options to request: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + httpRequest, err := client.NewRequest("GET", url, nil) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + resp, err := client.Do(ctx, httpRequest, &projects) |
| 91 | + if err != nil { |
| 92 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 93 | + "failed to list projects", |
| 94 | + resp, |
| 95 | + err, |
| 96 | + ), nil |
| 97 | + } |
| 98 | + defer func() { _ = resp.Body.Close() }() |
| 99 | + |
| 100 | + if resp.StatusCode != http.StatusOK { |
| 101 | + body, err := io.ReadAll(resp.Body) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 104 | + } |
| 105 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list projects: %s", string(body))), nil |
| 106 | + } |
| 107 | + r, err := json.Marshal(projects) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + return mcp.NewToolResultText(string(r)), nil |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +type ListProjectsOptions struct { |
| 117 | + // A cursor, as given in the Link header. If specified, the query only searches for events before this cursor. |
| 118 | + Before string `url:"before,omitempty"` |
| 119 | + |
| 120 | + // A cursor, as given in the Link header. If specified, the query only searches for events after this cursor. |
| 121 | + After string `url:"after,omitempty"` |
| 122 | + |
| 123 | + // For paginated result sets, the number of results to include per page. |
| 124 | + PerPage int `url:"per_page,omitempty"` |
| 125 | + |
| 126 | + // Query Limit results to projects of the specified type. |
| 127 | + Query string `url:"q,omitempty"` |
| 128 | +} |
| 129 | + |
| 130 | +// addOptions adds the parameters in opts as URL query parameters to s. opts |
| 131 | +// must be a struct whose fields may contain "url" tags. |
| 132 | +func addOptions(s string, opts any) (string, error) { |
| 133 | + v := reflect.ValueOf(opts) |
| 134 | + if v.Kind() == reflect.Ptr && v.IsNil() { |
| 135 | + return s, nil |
| 136 | + } |
| 137 | + |
| 138 | + u, err := url.Parse(s) |
| 139 | + if err != nil { |
| 140 | + return s, err |
| 141 | + } |
| 142 | + |
| 143 | + qs, err := query.Values(opts) |
| 144 | + if err != nil { |
| 145 | + return s, err |
| 146 | + } |
| 147 | + |
| 148 | + u.RawQuery = qs.Encode() |
| 149 | + return u.String(), nil |
| 150 | +} |
0 commit comments