Skip to content

Commit 44f68ef

Browse files
add capability to list commits of a branch
1 parent b5ee3fe commit 44f68ef

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/github/index.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import {
4141
CreateIssueSchema,
4242
CreatePullRequestSchema,
4343
ForkRepositorySchema,
44-
CreateBranchSchema
44+
CreateBranchSchema,
45+
ListCommitsSchema
4546
} from './schemas.js';
4647
import { z } from 'zod';
4748
import { zodToJsonSchema } from 'zod-to-json-schema';
@@ -467,6 +468,40 @@ async function createRepository(
467468
return GitHubRepositorySchema.parse(await response.json());
468469
}
469470

471+
async function listCommits(
472+
owner: string,
473+
repo: string,
474+
page: number = 1,
475+
perPage: number = 30,
476+
sha?: string,
477+
): Promise<GitHubCommit[]> {
478+
const url = new URL(`https://api.github.com/repos/${owner}/${repo}/commits`);
479+
url.searchParams.append("page", page.toString());
480+
url.searchParams.append("per_page", perPage.toString());
481+
if (sha) {
482+
url.searchParams.append("sha", sha);
483+
}
484+
485+
const response = await fetch(
486+
url.toString(),
487+
{
488+
method: "GET",
489+
headers: {
490+
"Authorization": `token ${GITHUB_PERSONAL_ACCESS_TOKEN}`,
491+
"Accept": "application/vnd.github.v3+json",
492+
"User-Agent": "github-mcp-server",
493+
"Content-Type": "application/json"
494+
},
495+
}
496+
);
497+
498+
if (!response.ok) {
499+
throw new Error(`GitHub API error: ${response.statusText}`);
500+
}
501+
502+
return GitHubCommitSchema.array().parse(await response.json());
503+
}
504+
470505
server.setRequestHandler(ListToolsRequestSchema, async () => {
471506
return {
472507
tools: [
@@ -514,6 +549,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
514549
name: "create_branch",
515550
description: "Create a new branch in a GitHub repository",
516551
inputSchema: zodToJsonSchema(CreateBranchSchema)
552+
},
553+
{
554+
name: "list_commits",
555+
description: "Get list of commits of a branch in a GitHub repository",
556+
inputSchema: zodToJsonSchema(ListCommitsSchema)
517557
}
518558
]
519559
};
@@ -623,6 +663,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
623663
return { content: [{ type: "text", text: JSON.stringify(pullRequest, null, 2) }] };
624664
}
625665

666+
case "list_commits": {
667+
const args = ListCommitsSchema.parse(request.params.arguments);
668+
const results = await listCommits(args.owner, args.repo, args.page, args.perPage, args.sha);
669+
return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] };
670+
}
671+
626672
default:
627673
throw new Error(`Unknown tool: ${request.params.name}`);
628674
}

src/github/schemas.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ export const SearchRepositoriesSchema = z.object({
308308
perPage: z.number().optional().describe("Number of results per page (default: 30, max: 100)")
309309
});
310310

311+
export const ListCommitsSchema = z.object({
312+
owner: z.string().describe("Repository owner (username or organization)"),
313+
repo: z.string().describe("Repository name"),
314+
page: z.number().optional().describe("Page number for pagination (default: 1)"),
315+
perPage: z.number().optional().describe("Number of results per page (default: 30, max: 100)"),
316+
sha: z.string().optional()
317+
.describe("SHA of the file being replaced (required when updating existing files)")
318+
});
319+
311320
export const CreateRepositorySchema = z.object({
312321
name: z.string().describe("Repository name"),
313322
description: z.string().optional().describe("Repository description"),

0 commit comments

Comments
 (0)