|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + ghErrors "github.com/github/github-mcp-server/pkg/errors" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + "github.com/google/go-github/v77/github" |
| 12 | + "github.com/mark3labs/mcp-go/mcp" |
| 13 | + "github.com/mark3labs/mcp-go/server" |
| 14 | +) |
| 15 | + |
| 16 | +// TreeEntryResponse represents a single entry in a Git tree. |
| 17 | +type TreeEntryResponse struct { |
| 18 | + Path string `json:"path"` |
| 19 | + Type string `json:"type"` |
| 20 | + Size *int `json:"size,omitempty"` |
| 21 | + Mode string `json:"mode"` |
| 22 | + SHA string `json:"sha"` |
| 23 | + URL string `json:"url"` |
| 24 | +} |
| 25 | + |
| 26 | +// TreeResponse represents the response structure for a Git tree. |
| 27 | +type TreeResponse struct { |
| 28 | + SHA string `json:"sha"` |
| 29 | + Truncated bool `json:"truncated"` |
| 30 | + Tree []TreeEntryResponse `json:"tree"` |
| 31 | + TreeSHA string `json:"tree_sha"` |
| 32 | + Owner string `json:"owner"` |
| 33 | + Repo string `json:"repo"` |
| 34 | + Recursive bool `json:"recursive"` |
| 35 | + Count int `json:"count"` |
| 36 | +} |
| 37 | + |
| 38 | +// GetRepositoryTree creates a tool to get the tree structure of a GitHub repository. |
| 39 | +func GetRepositoryTree(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 40 | + return mcp.NewTool("get_repository_tree", |
| 41 | + 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")), |
| 42 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 43 | + Title: t("TOOL_GET_REPOSITORY_TREE_USER_TITLE", "Get repository tree"), |
| 44 | + ReadOnlyHint: ToBoolPtr(true), |
| 45 | + }), |
| 46 | + mcp.WithString("owner", |
| 47 | + mcp.Required(), |
| 48 | + mcp.Description("Repository owner (username or organization)"), |
| 49 | + ), |
| 50 | + mcp.WithString("repo", |
| 51 | + mcp.Required(), |
| 52 | + mcp.Description("Repository name"), |
| 53 | + ), |
| 54 | + mcp.WithString("tree_sha", |
| 55 | + mcp.Description("The SHA1 value or ref (branch or tag) name of the tree. Defaults to the repository's default branch"), |
| 56 | + ), |
| 57 | + mcp.WithBoolean("recursive", |
| 58 | + mcp.Description("Setting this parameter to true returns the objects or subtrees referenced by the tree. Default is false"), |
| 59 | + mcp.DefaultBool(false), |
| 60 | + ), |
| 61 | + mcp.WithString("path_filter", |
| 62 | + mcp.Description("Optional path prefix to filter the tree results (e.g., 'src/' to only show files in the src directory)"), |
| 63 | + ), |
| 64 | + ), |
| 65 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 66 | + owner, err := RequiredParam[string](request, "owner") |
| 67 | + if err != nil { |
| 68 | + return mcp.NewToolResultError(err.Error()), nil |
| 69 | + } |
| 70 | + repo, err := RequiredParam[string](request, "repo") |
| 71 | + if err != nil { |
| 72 | + return mcp.NewToolResultError(err.Error()), nil |
| 73 | + } |
| 74 | + treeSHA, err := OptionalParam[string](request, "tree_sha") |
| 75 | + if err != nil { |
| 76 | + return mcp.NewToolResultError(err.Error()), nil |
| 77 | + } |
| 78 | + recursive, err := OptionalBoolParamWithDefault(request, "recursive", false) |
| 79 | + if err != nil { |
| 80 | + return mcp.NewToolResultError(err.Error()), nil |
| 81 | + } |
| 82 | + pathFilter, err := OptionalParam[string](request, "path_filter") |
| 83 | + if err != nil { |
| 84 | + return mcp.NewToolResultError(err.Error()), nil |
| 85 | + } |
| 86 | + |
| 87 | + client, err := getClient(ctx) |
| 88 | + if err != nil { |
| 89 | + return mcp.NewToolResultError("failed to get GitHub client"), nil |
| 90 | + } |
| 91 | + |
| 92 | + // If no tree_sha is provided, use the repository's default branch |
| 93 | + if treeSHA == "" { |
| 94 | + repoInfo, repoResp, err := client.Repositories.Get(ctx, owner, repo) |
| 95 | + if err != nil { |
| 96 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 97 | + "failed to get repository info", |
| 98 | + repoResp, |
| 99 | + err, |
| 100 | + ), nil |
| 101 | + } |
| 102 | + treeSHA = *repoInfo.DefaultBranch |
| 103 | + } |
| 104 | + |
| 105 | + // Get the tree using the GitHub Git Tree API |
| 106 | + tree, resp, err := client.Git.GetTree(ctx, owner, repo, treeSHA, recursive) |
| 107 | + if err != nil { |
| 108 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 109 | + "failed to get repository tree", |
| 110 | + resp, |
| 111 | + err, |
| 112 | + ), nil |
| 113 | + } |
| 114 | + defer func() { _ = resp.Body.Close() }() |
| 115 | + |
| 116 | + // Filter tree entries if path_filter is provided |
| 117 | + var filteredEntries []*github.TreeEntry |
| 118 | + if pathFilter != "" { |
| 119 | + for _, entry := range tree.Entries { |
| 120 | + if strings.HasPrefix(entry.GetPath(), pathFilter) { |
| 121 | + filteredEntries = append(filteredEntries, entry) |
| 122 | + } |
| 123 | + } |
| 124 | + } else { |
| 125 | + filteredEntries = tree.Entries |
| 126 | + } |
| 127 | + |
| 128 | + treeEntries := make([]TreeEntryResponse, len(filteredEntries)) |
| 129 | + for i, entry := range filteredEntries { |
| 130 | + treeEntries[i] = TreeEntryResponse{ |
| 131 | + Path: entry.GetPath(), |
| 132 | + Type: entry.GetType(), |
| 133 | + Mode: entry.GetMode(), |
| 134 | + SHA: entry.GetSHA(), |
| 135 | + URL: entry.GetURL(), |
| 136 | + } |
| 137 | + if entry.Size != nil { |
| 138 | + treeEntries[i].Size = entry.Size |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + response := TreeResponse{ |
| 143 | + SHA: *tree.SHA, |
| 144 | + Truncated: *tree.Truncated, |
| 145 | + Tree: treeEntries, |
| 146 | + TreeSHA: treeSHA, |
| 147 | + Owner: owner, |
| 148 | + Repo: repo, |
| 149 | + Recursive: recursive, |
| 150 | + Count: len(filteredEntries), |
| 151 | + } |
| 152 | + |
| 153 | + r, err := json.Marshal(response) |
| 154 | + if err != nil { |
| 155 | + return nil, fmt.Errorf("failed to marshal response: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + return mcp.NewToolResultText(string(r)), nil |
| 159 | + } |
| 160 | +} |
0 commit comments