add capability to list commits of a branch
This commit is contained in:
parent
b5ee3fe74d
commit
44f68efbdc
2 changed files with 56 additions and 1 deletions
|
|
@ -41,7 +41,8 @@ import {
|
||||||
CreateIssueSchema,
|
CreateIssueSchema,
|
||||||
CreatePullRequestSchema,
|
CreatePullRequestSchema,
|
||||||
ForkRepositorySchema,
|
ForkRepositorySchema,
|
||||||
CreateBranchSchema
|
CreateBranchSchema,
|
||||||
|
ListCommitsSchema
|
||||||
} from './schemas.js';
|
} from './schemas.js';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { zodToJsonSchema } from 'zod-to-json-schema';
|
import { zodToJsonSchema } from 'zod-to-json-schema';
|
||||||
|
|
@ -467,6 +468,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<GitHubCommit[]> {
|
||||||
|
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 GitHubCommitSchema.array().parse(await response.json());
|
||||||
|
}
|
||||||
|
|
||||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||||
return {
|
return {
|
||||||
tools: [
|
tools: [
|
||||||
|
|
@ -514,6 +549,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||||
name: "create_branch",
|
name: "create_branch",
|
||||||
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)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
@ -623,6 +663,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||||
return { content: [{ type: "text", text: JSON.stringify(pullRequest, null, 2) }] };
|
return { content: [{ type: "text", text: JSON.stringify(pullRequest, null, 2) }] };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -308,6 +308,15 @@ export const SearchRepositoriesSchema = z.object({
|
||||||
perPage: z.number().optional().describe("Number of results per page (default: 30, max: 100)")
|
perPage: z.number().optional().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"),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue