Skip to content

Commit 62b0458

Browse files
Merge branch 'main' into main
2 parents 4e387dc + 01aefd3 commit 62b0458

File tree

5 files changed

+400
-6
lines changed

5 files changed

+400
-6
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@juruen @sammorrowdrums @williammartin @toby
1+
* @juruen @sammorrowdrums @williammartin @toby

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ automation and interaction capabilities for developers and tools.
1515
## Prerequisites
1616

1717
1. To run the server in a container, you will need to have [Docker](https://www.docker.com/) installed.
18-
2. [Create a GitHub Personal Access Token](https://github.com/settings/personal-access-tokens/new).
18+
2. Once Docker is installed, you will also need to ensure Docker is running.
19+
3. Lastly you will need to [Create a GitHub Personal Access Token](https://github.com/settings/personal-access-tokens/new).
1920
The MCP server can use many of the GitHub APIs, so enable the permissions that you feel comfortable granting your AI tools (to learn more about access tokens, please check out the [documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)).
2021

2122

@@ -331,6 +332,13 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
331332
- `branch`: Branch name (string, optional)
332333
- `sha`: File SHA if updating (string, optional)
333334

335+
- **list_branches** - List branches in a GitHub repository
336+
337+
- `owner`: Repository owner (string, required)
338+
- `repo`: Repository name (string, required)
339+
- `page`: Page number (number, optional)
340+
- `perPage`: Results per page (number, optional)
341+
334342
- **push_files** - Push multiple files in a single commit
335343

336344
- `owner`: Repository owner (string, required)
@@ -374,14 +382,21 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
374382
- `branch`: New branch name (string, required)
375383
- `sha`: SHA to create branch from (string, required)
376384

377-
- **list_commits** - Gets commits of a branch in a repository
385+
- **list_commits** - Get a list of commits of a branch in a repository
378386
- `owner`: Repository owner (string, required)
379387
- `repo`: Repository name (string, required)
380388
- `sha`: Branch name, tag, or commit SHA (string, optional)
381389
- `path`: Only commits containing this file path (string, optional)
382390
- `page`: Page number (number, optional)
383391
- `perPage`: Results per page (number, optional)
384392

393+
- **get_commit** - Get details for a commit from a repository
394+
- `owner`: Repository owner (string, required)
395+
- `repo`: Repository name (string, required)
396+
- `sha`: Commit SHA, branch name, or tag name (string, required)
397+
- `page`: Page number, for files in the commit (number, optional)
398+
- `perPage`: Results per page, for files in the commit (number, optional)
399+
385400
### Search
386401

387402
- **search_code** - Search for code across GitHub repositories

pkg/github/repositories.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,73 @@ import (
1313
"github.com/mark3labs/mcp-go/server"
1414
)
1515

16+
func GetCommit(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
17+
return mcp.NewTool("get_commit",
18+
mcp.WithDescription(t("TOOL_GET_COMMITS_DESCRIPTION", "Get details for a commit from a GitHub repository")),
19+
mcp.WithString("owner",
20+
mcp.Required(),
21+
mcp.Description("Repository owner"),
22+
),
23+
mcp.WithString("repo",
24+
mcp.Required(),
25+
mcp.Description("Repository name"),
26+
),
27+
mcp.WithString("sha",
28+
mcp.Required(),
29+
mcp.Description("Commit SHA, branch name, or tag name"),
30+
),
31+
WithPagination(),
32+
),
33+
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
34+
owner, err := requiredParam[string](request, "owner")
35+
if err != nil {
36+
return mcp.NewToolResultError(err.Error()), nil
37+
}
38+
repo, err := requiredParam[string](request, "repo")
39+
if err != nil {
40+
return mcp.NewToolResultError(err.Error()), nil
41+
}
42+
sha, err := requiredParam[string](request, "sha")
43+
if err != nil {
44+
return mcp.NewToolResultError(err.Error()), nil
45+
}
46+
pagination, err := OptionalPaginationParams(request)
47+
if err != nil {
48+
return mcp.NewToolResultError(err.Error()), nil
49+
}
50+
51+
opts := &github.ListOptions{
52+
Page: pagination.page,
53+
PerPage: pagination.perPage,
54+
}
55+
56+
client, err := getClient(ctx)
57+
if err != nil {
58+
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
59+
}
60+
commit, resp, err := client.Repositories.GetCommit(ctx, owner, repo, sha, opts)
61+
if err != nil {
62+
return nil, fmt.Errorf("failed to get commit: %w", err)
63+
}
64+
defer func() { _ = resp.Body.Close() }()
65+
66+
if resp.StatusCode != 200 {
67+
body, err := io.ReadAll(resp.Body)
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to read response body: %w", err)
70+
}
71+
return mcp.NewToolResultError(fmt.Sprintf("failed to get commit: %s", string(body))), nil
72+
}
73+
74+
r, err := json.Marshal(commit)
75+
if err != nil {
76+
return nil, fmt.Errorf("failed to marshal response: %w", err)
77+
}
78+
79+
return mcp.NewToolResultText(string(r)), nil
80+
}
81+
}
82+
1683
// ListCommits creates a tool to get commits of a branch in a repository.
1784
func ListCommits(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
1885
return mcp.NewTool("list_commits",
@@ -83,6 +150,69 @@ func ListCommits(getClient GetClientFn, t translations.TranslationHelperFunc) (t
83150
}
84151
}
85152

153+
// ListBranches creates a tool to list branches in a GitHub repository.
154+
func ListBranches(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
155+
return mcp.NewTool("list_branches",
156+
mcp.WithDescription(t("TOOL_LIST_BRANCHES_DESCRIPTION", "List branches in a GitHub repository")),
157+
mcp.WithString("owner",
158+
mcp.Required(),
159+
mcp.Description("Repository owner"),
160+
),
161+
mcp.WithString("repo",
162+
mcp.Required(),
163+
mcp.Description("Repository name"),
164+
),
165+
WithPagination(),
166+
),
167+
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
168+
owner, err := requiredParam[string](request, "owner")
169+
if err != nil {
170+
return mcp.NewToolResultError(err.Error()), nil
171+
}
172+
repo, err := requiredParam[string](request, "repo")
173+
if err != nil {
174+
return mcp.NewToolResultError(err.Error()), nil
175+
}
176+
pagination, err := OptionalPaginationParams(request)
177+
if err != nil {
178+
return mcp.NewToolResultError(err.Error()), nil
179+
}
180+
181+
opts := &github.BranchListOptions{
182+
ListOptions: github.ListOptions{
183+
Page: pagination.page,
184+
PerPage: pagination.perPage,
185+
},
186+
}
187+
188+
client, err := getClient(ctx)
189+
if err != nil {
190+
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
191+
}
192+
193+
branches, resp, err := client.Repositories.ListBranches(ctx, owner, repo, opts)
194+
if err != nil {
195+
return nil, fmt.Errorf("failed to list branches: %w", err)
196+
}
197+
defer func() { _ = resp.Body.Close() }()
198+
199+
if resp.StatusCode != http.StatusOK {
200+
body, err := io.ReadAll(resp.Body)
201+
if err != nil {
202+
return nil, fmt.Errorf("failed to read response body: %w", err)
203+
}
204+
return mcp.NewToolResultError(fmt.Sprintf("failed to list branches: %s", string(body))), nil
205+
}
206+
207+
r, err := json.Marshal(branches)
208+
if err != nil {
209+
return nil, fmt.Errorf("failed to marshal response: %w", err)
210+
}
211+
212+
return mcp.NewToolResultText(string(r)), nil
213+
}
214+
}
215+
86216
// CreateOrUpdateFile creates a tool to create or update a file in a GitHub repository.
87217
func CreateOrUpdateFile(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
88218
return mcp.NewTool("create_or_update_file",

0 commit comments

Comments
 (0)