|
| 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 | +typeTreeEntryResponsestruct { |
| 18 | +Pathstring`json:"path"` |
| 19 | +Typestring`json:"type"` |
| 20 | +Size*int`json:"size,omitempty"` |
| 21 | +Modestring`json:"mode"` |
| 22 | +SHAstring`json:"sha"` |
| 23 | +URLstring`json:"url"` |
| 24 | +} |
| 25 | + |
| 26 | +// TreeResponse represents the response structure for a Git tree. |
| 27 | +typeTreeResponsestruct { |
| 28 | +SHAstring`json:"sha"` |
| 29 | +Truncatedbool`json:"truncated"` |
| 30 | +Tree []TreeEntryResponse`json:"tree"` |
| 31 | +TreeSHAstring`json:"tree_sha"` |
| 32 | +Ownerstring`json:"owner"` |
| 33 | +Repostring`json:"repo"` |
| 34 | +Recursivebool`json:"recursive"` |
| 35 | +Countint`json:"count"` |
| 36 | +} |
| 37 | + |
| 38 | +// GetRepositoryTree creates a tool to get the tree structure of a GitHub repository. |
| 39 | +funcGetRepositoryTree(getClientGetClientFn,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 40 | +returnmcp.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 | +iferr!=nil { |
| 68 | +returnmcp.NewToolResultError(err.Error()),nil |
| 69 | +} |
| 70 | +repo,err:=RequiredParam[string](request,"repo") |
| 71 | +iferr!=nil { |
| 72 | +returnmcp.NewToolResultError(err.Error()),nil |
| 73 | +} |
| 74 | +treeSHA,err:=OptionalParam[string](request,"tree_sha") |
| 75 | +iferr!=nil { |
| 76 | +returnmcp.NewToolResultError(err.Error()),nil |
| 77 | +} |
| 78 | +recursive,err:=OptionalBoolParamWithDefault(request,"recursive",false) |
| 79 | +iferr!=nil { |
| 80 | +returnmcp.NewToolResultError(err.Error()),nil |
| 81 | +} |
| 82 | +pathFilter,err:=OptionalParam[string](request,"path_filter") |
| 83 | +iferr!=nil { |
| 84 | +returnmcp.NewToolResultError(err.Error()),nil |
| 85 | +} |
| 86 | + |
| 87 | +client,err:=getClient(ctx) |
| 88 | +iferr!=nil { |
| 89 | +returnmcp.NewToolResultError("failed to get GitHub client"),nil |
| 90 | +} |
| 91 | + |
| 92 | +// If no tree_sha is provided, use the repository's default branch |
| 93 | +iftreeSHA=="" { |
| 94 | +repoInfo,repoResp,err:=client.Repositories.Get(ctx,owner,repo) |
| 95 | +iferr!=nil { |
| 96 | +returnghErrors.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 | +iferr!=nil { |
| 108 | +returnghErrors.NewGitHubAPIErrorResponse(ctx, |
| 109 | +"failed to get repository tree", |
| 110 | +resp, |
| 111 | +err, |
| 112 | +),nil |
| 113 | +} |
| 114 | +deferfunc() {_=resp.Body.Close() }() |
| 115 | + |
| 116 | +// Filter tree entries if path_filter is provided |
| 117 | +varfilteredEntries []*github.TreeEntry |
| 118 | +ifpathFilter!="" { |
| 119 | +for_,entry:=rangetree.Entries { |
| 120 | +ifstrings.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 | +fori,entry:=rangefilteredEntries { |
| 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 | +ifentry.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 | +iferr!=nil { |
| 155 | +returnnil,fmt.Errorf("failed to marshal response: %w",err) |
| 156 | +} |
| 157 | + |
| 158 | +returnmcp.NewToolResultText(string(r)),nil |
| 159 | +} |
| 160 | +} |