-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add ListProjects tool #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add ListProjects tool #1113
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c21aee
Bump go-viper/mapstructure
JoannaaKL 2d3db3a
Update github.com/go-viper/mapstructure/v2 version in licenses
JoannaaKL c3bad93
Add tool to list projects
JoannaaKL ec5502a
Merge branch 'main' into add-projects
JoannaaKL 7437464
Fix ordering
JoannaaKL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"annotations": { | ||
"title": "List projects", | ||
"readOnlyHint": true | ||
}, | ||
"description": "List Projects for a user or organization", | ||
"inputSchema": { | ||
"properties": { | ||
"after": { | ||
"description": "Cursor for items after (forward pagination)", | ||
"type": "string" | ||
}, | ||
"before": { | ||
"description": "Cursor for items before (backwards pagination)", | ||
"type": "string" | ||
}, | ||
"owner": { | ||
"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.", | ||
"type": "string" | ||
}, | ||
"owner_type": { | ||
"description": "Owner type", | ||
"enum": [ | ||
"user", | ||
"organization" | ||
], | ||
"type": "string" | ||
}, | ||
"per_page": { | ||
"description": "Number of results per page (max 100, default: 30)", | ||
"type": "number" | ||
}, | ||
"query": { | ||
"description": "Filter projects by a search query (matches title and description)", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"owner_type", | ||
"owner" | ||
], | ||
"type": "object" | ||
}, | ||
"name": "list_projects" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"reflect" | ||
|
||
ghErrors "github.com/github/github-mcp-server/pkg/errors" | ||
"github.com/github/github-mcp-server/pkg/translations" | ||
"github.com/google/go-github/v74/github" | ||
"github.com/google/go-querystring/query" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
) | ||
|
||
func ListProjects(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
return mcp.NewTool("list_projects", | ||
mcp.WithDescription(t("TOOL_LIST_PROJECTS_DESCRIPTION", "List Projects for a user or organization")), | ||
mcp.WithToolAnnotation(mcp.ToolAnnotation{Title: t("TOOL_LIST_PROJECTS_USER_TITLE", "List projects"), ReadOnlyHint: ToBoolPtr(true)}), | ||
mcp.WithString("owner_type", mcp.Required(), mcp.Description("Owner type"), mcp.Enum("user", "organization")), | ||
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.")), | ||
mcp.WithString("query", mcp.Description("Filter projects by a search query (matches title and description)")), | ||
mcp.WithString("before", mcp.Description("Cursor for items before (backwards pagination)")), | ||
mcp.WithString("after", mcp.Description("Cursor for items after (forward pagination)")), | ||
mcp.WithNumber("per_page", mcp.Description("Number of results per page (max 100, default: 30)")), | ||
), func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
owner, err := RequiredParam[string](req, "owner") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
ownerType, err := RequiredParam[string](req, "owner_type") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
queryStr, err := OptionalParam[string](req, "query") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
beforeCursor, err := OptionalParam[string](req, "before") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
afterCursor, err := OptionalParam[string](req, "after") | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
perPage, err := OptionalIntParamWithDefault(req, "per_page", 30) | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
client, err := getClient(ctx) | ||
if err != nil { | ||
return mcp.NewToolResultError(err.Error()), nil | ||
} | ||
|
||
var url string | ||
if ownerType == "organization" { | ||
url = fmt.Sprintf("/orgs/%s/projectsV2", owner) | ||
} else { | ||
url = fmt.Sprintf("/users/%s/projectsV2", owner) | ||
} | ||
projects := []github.ProjectV2{} | ||
|
||
opts := ListProjectsOptions{PerPage: perPage} | ||
if afterCursor != "" { | ||
opts.After = afterCursor | ||
} | ||
if beforeCursor != "" { | ||
opts.Before = beforeCursor | ||
} | ||
if queryStr != "" { | ||
opts.Query = queryStr | ||
} | ||
url, err = addOptions(url, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to add options to request: %w", err) | ||
} | ||
|
||
httpRequest, err := client.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
resp, err := client.Do(ctx, httpRequest, &projects) | ||
if err != nil { | ||
return ghErrors.NewGitHubAPIErrorResponse(ctx, | ||
"failed to list projects", | ||
resp, | ||
err, | ||
), nil | ||
} | ||
JoannaaKL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
return mcp.NewToolResultError(fmt.Sprintf("failed to list projects: %s", string(body))), nil | ||
} | ||
r, err := json.Marshal(projects) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
} | ||
|
||
return mcp.NewToolResultText(string(r)), nil | ||
} | ||
} | ||
|
||
type ListProjectsOptions struct { | ||
// A cursor, as given in the Link header. If specified, the query only searches for events before this cursor. | ||
Before string `url:"before,omitempty"` | ||
|
||
// A cursor, as given in the Link header. If specified, the query only searches for events after this cursor. | ||
After string `url:"after,omitempty"` | ||
|
||
// For paginated result sets, the number of results to include per page. | ||
PerPage int `url:"per_page,omitempty"` | ||
|
||
// Query Limit results to projects of the specified type. | ||
Query string `url:"q,omitempty"` | ||
} | ||
|
||
// addOptions adds the parameters in opts as URL query parameters to s. opts | ||
// must be a struct whose fields may contain "url" tags. | ||
func addOptions(s string, opts any) (string, error) { | ||
v := reflect.ValueOf(opts) | ||
if v.Kind() == reflect.Ptr && v.IsNil() { | ||
return s, nil | ||
} | ||
|
||
u, err := url.Parse(s) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
qs, err := query.Values(opts) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
u.RawQuery = qs.Encode() | ||
return u.String(), nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The code duplicates the addOptions function that already exists in the go-github library. Consider using github.addOptions from the go-github package instead of implementing a custom version to reduce code duplication.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes genius, it exists in go-github but is not exported.