list_commits
Retrieve a paginated list of commits from a specified branch in a GitHub repository by providing the repository owner, name, and branch reference.
Instructions
Get list of commits of a branch in a GitHub repository
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | Repository owner | |
| repo | Yes | Repository name | |
| sha | No | SHA or Branch name | |
| per_page | No | Results per page (default 10, max 100) | |
| page | No | Page number (default 1) |
Implementation Reference
- src/tools/repositories.ts:254-333 (handler)The main implementation of the 'list_commits' tool. It registers the tool via server.tool() with the name 'list_commits', defines a Zod schema for parameters (owner, repo, sha, per_page, page), and contains the async handler that calls octokit.rest.repos.listCommits() and formats the response as markdown.
// Tool: List Commits server.tool( "list_commits", "Get list of commits of a branch in a GitHub repository", { owner: z.string().describe("Repository owner"), repo: z.string().describe("Repository name"), sha: z.string().optional().describe("SHA or Branch name"), per_page: z .number() .optional() .default(10) .describe("Results per page (default 10, max 100)"), page: z .number() .optional() .default(1) .describe("Page number (default 1)"), }, async ({ owner, repo, sha, per_page, page }) => { try { const response = await octokit.rest.repos.listCommits({ owner, repo, sha, per_page, page, }) const commits = response.data if (commits.length === 0) { return { content: [{ type: "text", text: "No commits found." }], } } // Format as clean markdown let markdown = `# Commits for ${owner}/${repo}` if (sha) { markdown += ` (${sha})` } markdown += `\n\n` markdown += `Showing ${commits.length} commit(s) - Page ${page}\n` if (commits.length === per_page) { markdown += `*Note: More commits may be available. Use 'page' parameter to see next page.*\n` } markdown += `\n` commits.forEach(commit => { const shortSha = commit.sha.substring(0, 7) const message = commit.commit.message.split("\n")[0] // First line only const author = commit.commit.author?.name || commit.author?.login || "Unknown" const date = new Date( commit.commit.author?.date || "", ).toLocaleDateString() markdown += `## ${shortSha}: ${message}\n\n` markdown += `- **Author:** ${author}\n` markdown += `- **Date:** ${date}\n` if (commit.commit.comment_count > 0) { markdown += `- **Comments:** ${commit.commit.comment_count}\n` } markdown += `- **URL:** ${commit.html_url}\n` markdown += `\n` }) return { content: [{ type: "text", text: markdown }], } } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.message}` }], } } }, ) - src/tools/repositories.ts:254-272 (schema)The input schema for 'list_commits' using Zod: owner (string), repo (string), sha (optional string), per_page (optional number, default 10), page (optional number, default 1).
// Tool: List Commits server.tool( "list_commits", "Get list of commits of a branch in a GitHub repository", { owner: z.string().describe("Repository owner"), repo: z.string().describe("Repository name"), sha: z.string().optional().describe("SHA or Branch name"), per_page: z .number() .optional() .default(10) .describe("Results per page (default 10, max 100)"), page: z .number() .optional() .default(1) .describe("Page number (default 1)"), }, - src/tools/repositories.ts:254-256 (registration)Registration of the 'list_commits' tool via server.tool() call inside the registerRepositoryTools function.
// Tool: List Commits server.tool( "list_commits", - src/index.ts:17-17 (registration)The entry point where registerRepositoryTools (which contains list_commits) is called.
registerRepositoryTools(server, octokit)