get_pr
Retrieve GitHub Pull Request details using commit hashes or PR numbers. Automatically detects repositories and extracts comprehensive PR information including title, description, author status, and reviews.
Instructions
Get PR details by commit hash or PR number. Extracts PR number from git commits (merge commits, squash commits) and returns full PR details including title, description, author, status, and reviews. Auto-detects repository from working directory.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| commit | No | Git commit hash (full or short), branch name, or any git reference. Use this OR pr_number. | |
| pr_number | No | PR number to look up directly. Use this OR commit. | |
| repo | No | GitHub repository in owner/repo format. If not provided, auto-detects from cwd. | |
| cwd | No | Working directory path to auto-detect the GitHub repository from git remote. |
Implementation Reference
- src/index.ts:190-240 (handler)The handler logic for the 'get_pr' tool, which resolves the PR number from a commit or uses the provided number, then calls getPRDetails to fetch information.
if (name === "get_pr") { const { commit, pr_number, repo: providedRepo, cwd } = args as { commit?: string; pr_number?: number; repo?: string; cwd?: string; }; if (!commit && !pr_number) { return { content: [ { type: "text", text: "Either 'commit' or 'pr_number' must be provided.", }, ], isError: true, }; } const repo = resolveRepo(providedRepo, cwd); let resolvedPrNumber: number | null = pr_number ?? null; if (!resolvedPrNumber && commit) { resolvedPrNumber = getPRNumberFromCommit(commit, repo); if (!resolvedPrNumber) { return { content: [ { type: "text", text: `No PR found for commit: ${commit} in ${repo}. This commit may not be associated with any pull request.`, }, ], isError: true, }; } } const prDetails = getPRDetails(resolvedPrNumber!, repo); return { content: [ { type: "text", text: JSON.stringify(prDetails, null, 2), }, ], }; } - src/index.ts:151-181 (registration)Registration of the 'get_pr' tool in the MCP server schema.
{ name: "get_pr", description: "Get PR details by commit hash or PR number. Extracts PR number from git commits (merge commits, squash commits) and returns full PR details including title, description, author, status, and reviews. Auto-detects repository from working directory.", inputSchema: { type: "object", properties: { commit: { type: "string", description: "Git commit hash (full or short), branch name, or any git reference. Use this OR pr_number.", }, pr_number: { type: "number", description: "PR number to look up directly. Use this OR commit.", }, repo: { type: "string", description: "GitHub repository in owner/repo format. If not provided, auto-detects from cwd.", }, cwd: { type: "string", description: "Working directory path to auto-detect the GitHub repository from git remote.", }, }, }, }, ], }; - src/index.ts:120-146 (helper)Helper function to get PR details from the GitHub CLI.
function getPRDetails(prNumber: number, repo: string): PRDetails { const prCmd = `gh pr view ${prNumber} --repo ${repo} --json number,title,body,state,url,author,createdAt,mergedAt,baseRefName,headRefName,labels,reviews`; const prResult = execCommand(prCmd); const pr = JSON.parse(prResult); return { number: pr.number, title: pr.title, body: pr.body || "", state: pr.state, url: pr.url, author: pr.author?.login || "unknown", createdAt: pr.createdAt, mergedAt: pr.mergedAt, baseRef: pr.baseRefName, headRef: pr.headRefName, labels: pr.labels?.map((l: { name: string }) => l.name) || [], reviews: pr.reviews?.map( (r: { author: { login: string }; state: string; submittedAt: string }) => ({ author: r.author?.login || "unknown", state: r.state, submittedAt: r.submittedAt, }) ) || [], }; }