|
| 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 | +funcListProjects(getClientGetClientFn,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 21 | +returnmcp.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 | +iferr!=nil { |
| 33 | +returnmcp.NewToolResultError(err.Error()),nil |
| 34 | +} |
| 35 | +ownerType,err:=RequiredParam[string](req,"owner_type") |
| 36 | +iferr!=nil { |
| 37 | +returnmcp.NewToolResultError(err.Error()),nil |
| 38 | +} |
| 39 | +queryStr,err:=OptionalParam[string](req,"query") |
| 40 | +iferr!=nil { |
| 41 | +returnmcp.NewToolResultError(err.Error()),nil |
| 42 | +} |
| 43 | + |
| 44 | +beforeCursor,err:=OptionalParam[string](req,"before") |
| 45 | +iferr!=nil { |
| 46 | +returnmcp.NewToolResultError(err.Error()),nil |
| 47 | +} |
| 48 | +afterCursor,err:=OptionalParam[string](req,"after") |
| 49 | +iferr!=nil { |
| 50 | +returnmcp.NewToolResultError(err.Error()),nil |
| 51 | +} |
| 52 | +perPage,err:=OptionalIntParamWithDefault(req,"per_page",30) |
| 53 | +iferr!=nil { |
| 54 | +returnmcp.NewToolResultError(err.Error()),nil |
| 55 | +} |
| 56 | + |
| 57 | +client,err:=getClient(ctx) |
| 58 | +iferr!=nil { |
| 59 | +returnmcp.NewToolResultError(err.Error()),nil |
| 60 | +} |
| 61 | + |
| 62 | +varurlstring |
| 63 | +ifownerType=="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 | +ifafterCursor!="" { |
| 72 | +opts.After=afterCursor |
| 73 | +} |
| 74 | +ifbeforeCursor!="" { |
| 75 | +opts.Before=beforeCursor |
| 76 | +} |
| 77 | +ifqueryStr!="" { |
| 78 | +opts.Query=queryStr |
| 79 | +} |
| 80 | +url,err=addOptions(url,opts) |
| 81 | +iferr!=nil { |
| 82 | +returnnil,fmt.Errorf("failed to add options to request: %w",err) |
| 83 | +} |
| 84 | + |
| 85 | +httpRequest,err:=client.NewRequest("GET",url,nil) |
| 86 | +iferr!=nil { |
| 87 | +returnnil,fmt.Errorf("failed to create request: %w",err) |
| 88 | +} |
| 89 | + |
| 90 | +resp,err:=client.Do(ctx,httpRequest,&projects) |
| 91 | +iferr!=nil { |
| 92 | +returnghErrors.NewGitHubAPIErrorResponse(ctx, |
| 93 | +"failed to list projects", |
| 94 | +resp, |
| 95 | +err, |
| 96 | +),nil |
| 97 | +} |
| 98 | +deferfunc() {_=resp.Body.Close() }() |
| 99 | + |
| 100 | +ifresp.StatusCode!=http.StatusOK { |
| 101 | +body,err:=io.ReadAll(resp.Body) |
| 102 | +iferr!=nil { |
| 103 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 104 | +} |
| 105 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to list projects: %s",string(body))),nil |
| 106 | +} |
| 107 | +r,err:=json.Marshal(projects) |
| 108 | +iferr!=nil { |
| 109 | +returnnil,fmt.Errorf("failed to marshal response: %w",err) |
| 110 | +} |
| 111 | + |
| 112 | +returnmcp.NewToolResultText(string(r)),nil |
| 113 | +} |
| 114 | +} |
| 115 | + |
| 116 | +typeListProjectsOptionsstruct { |
| 117 | +// A cursor, as given in the Link header. If specified, the query only searches for events before this cursor. |
| 118 | +Beforestring`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 | +Afterstring`url:"after,omitempty"` |
| 122 | + |
| 123 | +// For paginated result sets, the number of results to include per page. |
| 124 | +PerPageint`url:"per_page,omitempty"` |
| 125 | + |
| 126 | +// Query Limit results to projects of the specified type. |
| 127 | +Querystring`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 | +funcaddOptions(sstring,optsany) (string,error) { |
| 133 | +v:=reflect.ValueOf(opts) |
| 134 | +ifv.Kind()==reflect.Ptr&&v.IsNil() { |
| 135 | +returns,nil |
| 136 | +} |
| 137 | + |
| 138 | +u,err:=url.Parse(s) |
| 139 | +iferr!=nil { |
| 140 | +returns,err |
| 141 | +} |
| 142 | + |
| 143 | +qs,err:=query.Values(opts) |
| 144 | +iferr!=nil { |
| 145 | +returns,err |
| 146 | +} |
| 147 | + |
| 148 | +u.RawQuery=qs.Encode() |
| 149 | +returnu.String(),nil |
| 150 | +} |