-
Couldn't load subscription status.
- Fork 2.9k
Add Repository Tree Navigation Tool #1164
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
Open
natagdunbar
wants to merge
5
commits into
main
Choose a base branch
from
natagdunbar/add-repo-nav-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "annotations": { | ||
| "title": "Get repository tree", | ||
| "readOnlyHint": true | ||
| }, | ||
| "description": "Get the tree structure (files and directories) of a GitHub repository at a specific ref or SHA", | ||
| "inputSchema": { | ||
| "properties": { | ||
| "owner": { | ||
| "description": "Repository owner (username or organization)", | ||
| "type": "string" | ||
| }, | ||
| "path_filter": { | ||
| "description": "Optional path prefix to filter the tree results (e.g., 'src/' to only show files in the src directory)", | ||
| "type": "string" | ||
| }, | ||
| "recursive": { | ||
| "default": false, | ||
| "description": "Setting this parameter to true returns the objects or subtrees referenced by the tree. Default is false", | ||
| "type": "boolean" | ||
| }, | ||
| "repo": { | ||
| "description": "Repository name", | ||
| "type": "string" | ||
| }, | ||
| "tree_sha": { | ||
| "description": "The SHA1 value or ref (branch or tag) name of the tree. Defaults to the repository's default branch", | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "owner", | ||
| "repo" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "name": "get_repository_tree" | ||
| } |
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 | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -677,6 +677,147 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t | |||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // GetRepositoryTree creates a tool to get the tree structure of a GitHub repository. | ||||||||||||||||||||||||||||||||||||||
| func GetRepositoryTree(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewTool("get_repository_tree", | ||||||||||||||||||||||||||||||||||||||
| mcp.WithDescription(t("TOOL_GET_REPOSITORY_TREE_DESCRIPTION", "Get the tree structure (files and directories) of a GitHub repository at a specific ref or SHA")), | ||||||||||||||||||||||||||||||||||||||
| mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||||||||||||||||||||||||||||||||||||||
| Title: t("TOOL_GET_REPOSITORY_TREE_USER_TITLE", "Get repository tree"), | ||||||||||||||||||||||||||||||||||||||
| ReadOnlyHint: ToBoolPtr(true), | ||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||
| mcp.WithString("owner", | ||||||||||||||||||||||||||||||||||||||
| mcp.Required(), | ||||||||||||||||||||||||||||||||||||||
| mcp.Description("Repository owner (username or organization)"), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| mcp.WithString("repo", | ||||||||||||||||||||||||||||||||||||||
| mcp.Required(), | ||||||||||||||||||||||||||||||||||||||
| mcp.Description("Repository name"), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| mcp.WithString("tree_sha", | ||||||||||||||||||||||||||||||||||||||
| mcp.Description("The SHA1 value or ref (branch or tag) name of the tree. Defaults to the repository's default branch"), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| mcp.WithBoolean("recursive", | ||||||||||||||||||||||||||||||||||||||
| mcp.Description("Setting this parameter to true returns the objects or subtrees referenced by the tree. Default is false"), | ||||||||||||||||||||||||||||||||||||||
| mcp.DefaultBool(false), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| mcp.WithString("path_filter", | ||||||||||||||||||||||||||||||||||||||
| mcp.Description("Optional path prefix to filter the tree results (e.g., 'src/' to only show files in the src directory)"), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||
| func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||||||||||||||||||||||||||||||||||
| owner, err := RequiredParam[string](request, "owner") | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError(err.Error()), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| repo, err := RequiredParam[string](request, "repo") | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError(err.Error()), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| treeSHA, err := OptionalParam[string](request, "tree_sha") | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError(err.Error()), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| recursive, err := OptionalBoolParamWithDefault(request, "recursive", false) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError(err.Error()), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| pathFilter, err := OptionalParam[string](request, "path_filter") | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError(err.Error()), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| client, err := getClient(ctx) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError("failed to get GitHub client"), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // If no tree_sha is provided, use the repository's default branch | ||||||||||||||||||||||||||||||||||||||
| if treeSHA == "" { | ||||||||||||||||||||||||||||||||||||||
| repoInfo, _, err := client.Repositories.Get(ctx, owner, repo) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return mcp.NewToolResultError(fmt.Sprintf("failed to get repository info: %s", err)), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| treeSHA = *repoInfo.DefaultBranch | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Get the tree using the GitHub Git Tree API | ||||||||||||||||||||||||||||||||||||||
| tree, resp, err := client.Git.GetTree(ctx, owner, repo, treeSHA, recursive) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
| return ghErrors.NewGitHubAPIErrorResponse(ctx, | ||||||||||||||||||||||||||||||||||||||
| "failed to get repository tree", | ||||||||||||||||||||||||||||||||||||||
| resp, | ||||||||||||||||||||||||||||||||||||||
| err, | ||||||||||||||||||||||||||||||||||||||
| ), nil | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| defer func() { _ = resp.Body.Close() }() | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Filter tree entries if path_filter is provided | ||||||||||||||||||||||||||||||||||||||
| var filteredEntries []*github.TreeEntry | ||||||||||||||||||||||||||||||||||||||
| if pathFilter != "" { | ||||||||||||||||||||||||||||||||||||||
| for _, entry := range tree.Entries { | ||||||||||||||||||||||||||||||||||||||
| if entry.Path != nil && strings.HasPrefix(*entry.Path, pathFilter) { | ||||||||||||||||||||||||||||||||||||||
natagdunbar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
| filteredEntries = append(filteredEntries, entry) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| filteredEntries = tree.Entries | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Create a simplified response structure | ||||||||||||||||||||||||||||||||||||||
| type TreeEntryResponse struct { | ||||||||||||||||||||||||||||||||||||||
| Path string `json:"path"` | ||||||||||||||||||||||||||||||||||||||
| Type string `json:"type"` | ||||||||||||||||||||||||||||||||||||||
| Size *int `json:"size,omitempty"` | ||||||||||||||||||||||||||||||||||||||
| Mode string `json:"mode"` | ||||||||||||||||||||||||||||||||||||||
| SHA string `json:"sha"` | ||||||||||||||||||||||||||||||||||||||
| URL string `json:"url"` | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| type TreeResponse struct { | ||||||||||||||||||||||||||||||||||||||
| SHA string `json:"sha"` | ||||||||||||||||||||||||||||||||||||||
| Truncated bool `json:"truncated"` | ||||||||||||||||||||||||||||||||||||||
| Tree []TreeEntryResponse `json:"tree"` | ||||||||||||||||||||||||||||||||||||||
| TreeSHA string `json:"tree_sha"` | ||||||||||||||||||||||||||||||||||||||
| Owner string `json:"owner"` | ||||||||||||||||||||||||||||||||||||||
| Repo string `json:"repo"` | ||||||||||||||||||||||||||||||||||||||
| Recursive bool `json:"recursive"` | ||||||||||||||||||||||||||||||||||||||
| Count int `json:"count"` | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| treeEntries := make([]TreeEntryResponse, len(filteredEntries)) | ||||||||||||||||||||||||||||||||||||||
| for i, entry := range filteredEntries { | ||||||||||||||||||||||||||||||||||||||
| treeEntries[i] = TreeEntryResponse{ | ||||||||||||||||||||||||||||||||||||||
| Path: *entry.Path, | ||||||||||||||||||||||||||||||||||||||
| Type: *entry.Type, | ||||||||||||||||||||||||||||||||||||||
| Mode: *entry.Mode, | ||||||||||||||||||||||||||||||||||||||
| SHA: *entry.SHA, | ||||||||||||||||||||||||||||||||||||||
| URL: *entry.URL, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
natagdunbar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
| if entry.Size != nil { | ||||||||||||||||||||||||||||||||||||||
| treeEntries[i].Size = entry.Size | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
natagdunbar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| response := TreeResponse{ | ||||||||||||||||||||||||||||||||||||||
| SHA: *tree.SHA, | ||||||||||||||||||||||||||||||||||||||
| Truncated: *tree.Truncated, | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| response := TreeResponse{ | |
| SHA: *tree.SHA, | |
| Truncated: *tree.Truncated, | |
| var sha string | |
| if tree.SHA != nil { | |
| sha = *tree.SHA | |
| } else { | |
| sha = "" | |
| } | |
| var truncated bool | |
| if tree.Truncated != nil { | |
| truncated = *tree.Truncated | |
| } else { | |
| truncated = false | |
| } | |
| response := TreeResponse{ | |
| SHA: sha, | |
| Truncated: truncated, |
natagdunbar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
natagdunbar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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
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.
Does the client handle
recursive: falsesuch that it won't return recursive entries? The docs suggest that we'll need to omit therecursiveparameter completely in order to prevent recursion: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.
Ah, yep, it looks like the client will only include the recursive parameter if it's
true: https://github.com/google/go-github/blob/46f1bf23e6f9659d04f9eaebff5d25902cddfd8e/github/git_trees.go#L102-L104👍