Merge pull request #224 from himanshuladia31/himanshu/github/list-commits

feat: github: add capability to list commits of a branch
This commit is contained in:
Justin Spahr-Summers 2024-12-05 23:28:50 +00:00 • committed by GitHub
commit e5ace36a28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 88 additions and 1 deletions

View file

@ -170,6 +170,16 @@ MCP Server for the GitHub API, enabling file operations, repository management,
- `page` (optional number): Page number - `page` (optional number): Page number
- Returns: User search results - Returns: User search results
16. `list_commits`
- Gets commits of a branch in a repository
- Inputs:
- `owner` (string): Repository owner
- `repo` (string): Repository name
- `page` (optional string): page number
- `per_page` (optional string): number of record per page
- `sha` (optional string): branch name
- Returns: List of commits
## Search Query Syntax ## Search Query Syntax
### Code Search ### Code Search

View file

@ -25,12 +25,15 @@ import {
GitHubCreateUpdateFileResponseSchema, GitHubCreateUpdateFileResponseSchema,
GitHubForkSchema, GitHubForkSchema,
GitHubIssueSchema, GitHubIssueSchema,
GitHubListCommits,
GitHubListCommitsSchema,
GitHubPullRequestSchema, GitHubPullRequestSchema,
GitHubReferenceSchema, GitHubReferenceSchema,
GitHubRepositorySchema, GitHubRepositorySchema,
GitHubSearchResponseSchema, GitHubSearchResponseSchema,
GitHubTreeSchema, GitHubTreeSchema,
IssueCommentSchema, IssueCommentSchema,
ListCommitsSchema,
ListIssuesOptionsSchema, ListIssuesOptionsSchema,
PushFilesSchema, PushFilesSchema,
SearchCodeResponseSchema, SearchCodeResponseSchema,
@ -54,7 +57,7 @@ import {
type GitHubTree, type GitHubTree,
type SearchCodeResponse, type SearchCodeResponse,
type SearchIssuesResponse, type SearchIssuesResponse,
type SearchUsersResponse, type SearchUsersResponse
} from './schemas.js'; } from './schemas.js';
const server = new Server( const server = new Server(
@ -487,6 +490,40 @@ async function createRepository(
return GitHubRepositorySchema.parse(await response.json()); return GitHubRepositorySchema.parse(await response.json());
} }
async function listCommits(
owner: string,
repo: string,
page: number = 1,
perPage: number = 30,
sha?: string,
): Promise<GitHubListCommits> {
const url = new URL(`https://api.github.com/repos/${owner}/${repo}/commits`);
url.searchParams.append("page", page.toString());
url.searchParams.append("per_page", perPage.toString());
if (sha) {
url.searchParams.append("sha", sha);
}
const response = await fetch(
url.toString(),
{
method: "GET",
headers: {
"Authorization": `token ${GITHUB_PERSONAL_ACCESS_TOKEN}`,
"Accept": "application/vnd.github.v3+json",
"User-Agent": "github-mcp-server",
"Content-Type": "application/json"
},
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText}`);
}
return GitHubListCommitsSchema.parse(await response.json());
}
async function listIssues( async function listIssues(
owner: string, owner: string,
repo: string, repo: string,
@ -705,6 +742,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
description: "Create a new branch in a GitHub repository", description: "Create a new branch in a GitHub repository",
inputSchema: zodToJsonSchema(CreateBranchSchema), inputSchema: zodToJsonSchema(CreateBranchSchema),
}, },
{
name: "list_commits",
description: "Get list of commits of a branch in a GitHub repository",
inputSchema: zodToJsonSchema(ListCommitsSchema)
},
{ {
name: "list_issues", name: "list_issues",
description: "List issues in a GitHub repository with filtering options", description: "List issues in a GitHub repository with filtering options",
@ -924,6 +966,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
return { toolResult: comment }; return { toolResult: comment };
} }
case "list_commits": {
const args = ListCommitsSchema.parse(request.params.arguments);
const results = await listCommits(args.owner, args.repo, args.page, args.perPage, args.sha);
return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] };
}
default: default:
throw new Error(`Unknown tool: ${request.params.name}`); throw new Error(`Unknown tool: ${request.params.name}`);
} }

View file

@ -93,6 +93,25 @@ export const GitHubTreeSchema = z.object({
truncated: z.boolean(), truncated: z.boolean(),
}); });
export const GitHubListCommitsSchema = z.array(z.object({
sha: z.string(),
node_id: z.string(),
commit: z.object({
author: GitHubAuthorSchema,
committer: GitHubAuthorSchema,
message: z.string(),
tree: z.object({
sha: z.string(),
url: z.string()
}),
url: z.string(),
comment_count: z.number(),
}),
url: z.string(),
html_url: z.string(),
comments_url: z.string()
}));
export const GitHubCommitSchema = z.object({ export const GitHubCommitSchema = z.object({
sha: z.string(), sha: z.string(),
node_id: z.string(), node_id: z.string(),
@ -322,6 +341,15 @@ export const SearchRepositoriesSchema = z.object({
.describe("Number of results per page (default: 30, max: 100)"), .describe("Number of results per page (default: 30, max: 100)"),
}); });
export const ListCommitsSchema = z.object({
owner: z.string().describe("Repository owner (username or organization)"),
repo: z.string().describe("Repository name"),
page: z.number().optional().describe("Page number for pagination (default: 1)"),
perPage: z.number().optional().describe("Number of results per page (default: 30, max: 100)"),
sha: z.string().optional()
.describe("SHA of the file being replaced (required when updating existing files)")
});
export const CreateRepositorySchema = z.object({ export const CreateRepositorySchema = z.object({
name: z.string().describe("Repository name"), name: z.string().describe("Repository name"),
description: z.string().optional().describe("Repository description"), description: z.string().optional().describe("Repository description"),
@ -663,6 +691,7 @@ export type GitHubContent = z.infer<typeof GitHubContentSchema>;
export type FileOperation = z.infer<typeof FileOperationSchema>; export type FileOperation = z.infer<typeof FileOperationSchema>;
export type GitHubTree = z.infer<typeof GitHubTreeSchema>; export type GitHubTree = z.infer<typeof GitHubTreeSchema>;
export type GitHubCommit = z.infer<typeof GitHubCommitSchema>; export type GitHubCommit = z.infer<typeof GitHubCommitSchema>;
export type GitHubListCommits = z.infer<typeof GitHubListCommitsSchema>;
export type GitHubReference = z.infer<typeof GitHubReferenceSchema>; export type GitHubReference = z.infer<typeof GitHubReferenceSchema>;
export type CreateRepositoryOptions = z.infer< export type CreateRepositoryOptions = z.infer<
typeof CreateRepositoryOptionsSchema typeof CreateRepositoryOptionsSchema