Skip to content

Commit 011ce96

Browse files
authored
Merge branch 'main' into docker
2 parents bc3a1cc + 8343fa5 commit 011ce96

File tree

5 files changed

+395
-6
lines changed

5 files changed

+395
-6
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[fork]: https://github.com/github/github-mcp-server/fork
44
[pr]: https://github.com/github/github-mcp-server/compare
5-
[style]: https://github.com/github/github-mcp-server/blob/main/.golangci.yaml
5+
[style]: https://github.com/github/github-mcp-server/blob/main/.golangci.yml
66

77
Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
88

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

@@ -310,6 +311,13 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
310311
- `branch`: Branch name (string, optional)
311312
- `sha`: File SHA if updating (string, optional)
312313

314+
- **list_branches** - List branches in a GitHub repository
315+
316+
- `owner`: Repository owner (string, required)
317+
- `repo`: Repository name (string, required)
318+
- `page`: Page number (number, optional)
319+
- `perPage`: Results per page (number, optional)
320+
313321
- **push_files** - Push multiple files in a single commit
314322

315323
- `owner`: Repository owner (string, required)
@@ -353,14 +361,21 @@ export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description
353361
- `branch`: New branch name (string, required)
354362
- `sha`: SHA to create branch from (string, required)
355363

356-
- **list_commits** - Gets commits of a branch in a repository
364+
- **list_commits** - Get a list of commits of a branch in a repository
357365
- `owner`: Repository owner (string, required)
358366
- `repo`: Repository name (string, required)
359367
- `sha`: Branch name, tag, or commit SHA (string, optional)
360368
- `path`: Only commits containing this file path (string, optional)
361369
- `page`: Page number (number, optional)
362370
- `perPage`: Results per page (number, optional)
363371

372+
- **get_commit** - Get details for a commit from a repository
373+
- `owner`: Repository owner (string, required)
374+
- `repo`: Repository name (string, required)
375+
- `sha`: Commit SHA, branch name, or tag name (string, required)
376+
- `page`: Page number, for files in the commit (number, optional)
377+
- `perPage`: Results per page, for files in the commit (number, optional)
378+
364379
### Search
365380

366381
- **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)