list_pull_requests
Retrieve pull requests from a GitHub repository with filters for state, head branch, base branch, sorting, and pagination.
Instructions
List pull requests in a GitHub repository.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | Repository owner | |
| repo | Yes | Repository name | |
| state | No | Filter by state | |
| head | No | Filter by head user/org and branch | |
| base | No | Filter by base branch | |
| sort | No | Sort by | |
| direction | No | Sort direction | |
| per_page | No | Results per page (default 10, max 100) | |
| page | No | Page number (default 1) |
Implementation Reference
- src/tools/pullrequests.ts:97-201 (handler)The full handler definition for the 'list_pull_requests' tool, including the schema definition (lines 101-128) and async handler function (lines 129-201) that calls octokit.rest.pulls.list() and formats results as markdown.
// Tool: List Pull Requests server.tool( "list_pull_requests", "List pull requests in a GitHub repository.", { owner: z.string().describe("Repository owner"), repo: z.string().describe("Repository name"), state: z .enum(["open", "closed", "all"]) .optional() .describe("Filter by state"), head: z .string() .optional() .describe("Filter by head user/org and branch"), base: z.string().optional().describe("Filter by base branch"), sort: z .enum(["created", "updated", "popularity", "long-running"]) .optional() .describe("Sort by"), direction: z.enum(["asc", "desc"]).optional().describe("Sort direction"), 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, state, head, base, sort, direction, per_page, page, }) => { try { const response = await octokit.rest.pulls.list({ owner, repo, state, head, base, sort, direction, per_page, page, }) // Format the response as clean markdown const prs = response.data if (prs.length === 0) { return { content: [{ type: "text", text: "No pull requests found." }], } } let markdown = `# Pull Requests for ${owner}/${repo}\n\n` markdown += `Showing ${prs.length} pull request(s) - Page ${page}\n` if (prs.length === per_page) { markdown += `*Note: More results may be available. Use 'page' parameter to see next page.*\n` } markdown += `\n` prs.forEach(pr => { markdown += `## #${pr.number}: ${pr.title}\n\n` markdown += `- **State**: ${pr.state}\n` markdown += `- **Author**: ${pr.user?.login || "Unknown"}\n` markdown += `- **Created**: ${new Date(pr.created_at).toLocaleDateString()}\n` markdown += `- **Updated**: ${new Date(pr.updated_at).toLocaleDateString()}\n` markdown += `- **Branch**: ${pr.head.ref} → ${pr.base.ref}\n` if (pr.draft) { markdown += `- **Status**: Draft\n` } if (pr.labels && pr.labels.length > 0) { markdown += `- **Labels**: ${pr.labels.map(l => l.name).join(", ")}\n` } if (pr.assignees && pr.assignees.length > 0) { markdown += `- **Assignees**: ${pr.assignees.map(a => a.login).join(", ")}\n` } markdown += `- **URL**: ${pr.html_url}\n` markdown += `\n` }) return { content: [{ type: "text", text: markdown }], } } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.message}` }], } } }, ) - src/index.ts:14-18 (registration)The 'registerAllToolsAndResources' function calls 'registerPullRequestTools(server, octokit)' at line 18, which registers all pull request tools including 'list_pull_requests'.
export function registerAllToolsAndResources(server: McpServer, octokit: Octokit): void { registerSearchTools(server, octokit) registerIssueTools(server, octokit) registerRepositoryTools(server, octokit) registerPullRequestTools(server, octokit) - src/index.ts:5-5 (registration)Import of the registerPullRequestTools function from the pullrequests module.
import { registerPullRequestTools } from "./tools/pullrequests.js"