Skip to content

Commit

Permalink
Add support for listing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Jan 10, 2025
1 parent ac7592f commit a47abf5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ MCP Server for the GitHub API, enabling file operations, repository management,
- `expected_head_sha` (optional string): The expected SHA of the pull request's HEAD ref
- Returns: Success message when branch is updated

25. `get_pull_request_comments`
- Get the review comments on a pull request
- Inputs:
- `owner` (string): Repository owner
- `repo` (string): Repository name
- `pull_number` (number): Pull request number
- Returns: Array of pull request review comments with details like the comment text, author, and location in the diff

## Search Query Syntax

### Code Search
Expand Down
36 changes: 36 additions & 0 deletions src/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import {
SearchUsersResponseSchema,
SearchUsersSchema,
UpdateIssueOptionsSchema,
GetPullRequestCommentsSchema,
PullRequestCommentSchema,
type FileOperation,
type GitHubCommit,
type GitHubContent,
Expand Down Expand Up @@ -879,6 +881,29 @@ async function updatePullRequestBranch(
}
}

async function getPullRequestComments(
owner: string,
repo: string,
pullNumber: number
): Promise<z.infer<typeof PullRequestCommentSchema>[]> {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/comments`,
{
headers: {
Authorization: `token ${GITHUB_PERSONAL_ACCESS_TOKEN}`,
Accept: "application/vnd.github.v3+json",
"User-Agent": "github-mcp-server",
},
}
);

if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText}`);
}

return z.array(PullRequestCommentSchema).parse(await response.json());
}

async function getPullRequestStatus(
owner: string,
repo: string,
Expand Down Expand Up @@ -1048,6 +1073,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
name: "update_pull_request_branch",
description: "Update a pull request branch with the latest changes from the base branch",
inputSchema: zodToJsonSchema(UpdatePullRequestBranchSchema)
},
{
name: "get_pull_request_comments",
description: "Get the review comments on a pull request",
inputSchema: zodToJsonSchema(GetPullRequestCommentsSchema)
}
],
};
Expand Down Expand Up @@ -1298,6 +1328,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
return { content: [{ type: "text", text: "Pull request branch updated successfully" }] };
}

case "get_pull_request_comments": {
const args = GetPullRequestCommentsSchema.parse(request.params.arguments);
const comments = await getPullRequestComments(args.owner, args.repo, args.pull_number);
return { content: [{ type: "text", text: JSON.stringify(comments, null, 2) }] };
}

default:
throw new Error(`Unknown tool: ${request.params.name}`);
}
Expand Down
34 changes: 34 additions & 0 deletions src/github/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,5 +820,39 @@ export const UpdatePullRequestBranchSchema = z.object({
expected_head_sha: z.string().optional().describe("The expected SHA of the pull request's HEAD ref")
});

// Schema for PR comments
export const GetPullRequestCommentsSchema = z.object({
owner: z.string().describe("Repository owner (username or organization)"),
repo: z.string().describe("Repository name"),
pull_number: z.number().describe("Pull request number")
});

export const PullRequestCommentSchema = z.object({
url: z.string(),
id: z.number(),
node_id: z.string(),
pull_request_review_id: z.number().nullable(),
diff_hunk: z.string(),
path: z.string().nullable(),
position: z.number().nullable(),
original_position: z.number().nullable(),
commit_id: z.string(),
original_commit_id: z.string(),
user: GitHubIssueAssigneeSchema,
body: z.string(),
created_at: z.string(),
updated_at: z.string(),
html_url: z.string(),
pull_request_url: z.string(),
author_association: z.string(),
_links: z.object({
self: z.object({ href: z.string() }),
html: z.object({ href: z.string() }),
pull_request: z.object({ href: z.string() })
})
});

export type CombinedStatus = z.infer<typeof CombinedStatusSchema>;
export type UpdatePullRequestBranch = z.infer<typeof UpdatePullRequestBranchSchema>;
export type GetPullRequestComments = z.infer<typeof GetPullRequestCommentsSchema>;
export type PullRequestComment = z.infer<typeof PullRequestCommentSchema>;

0 comments on commit a47abf5

Please sign in to comment.