|
| 1 | +#!/usr/bin/env -S uv run --script |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.11" |
| 4 | +# dependencies = ["mcp"] |
| 5 | +# /// |
| 6 | +"""MCP server for collecting PR review comments and conclusion.""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from mcp.server.fastmcp import FastMCP |
| 13 | + |
| 14 | +server = FastMCP("pr-review") |
| 15 | + |
| 16 | +output_dir = Path(os.environ.get("REVIEW_OUTPUT_DIR", "/tmp")) |
| 17 | + |
| 18 | + |
| 19 | +def _append_comment(comment: dict) -> int: |
| 20 | + """Append a comment to the comments file and return the new total.""" |
| 21 | + comments_file = output_dir / "comments.json" |
| 22 | + comments = json.loads(comments_file.read_text()) if comments_file.exists() else [] |
| 23 | + comments.append(comment) |
| 24 | + comments_file.write_text(json.dumps(comments, indent=2)) |
| 25 | + return len(comments) |
| 26 | + |
| 27 | + |
| 28 | +@server.tool() |
| 29 | +def add_comment( |
| 30 | + path: str, |
| 31 | + line: int, |
| 32 | + body: str, |
| 33 | + suggestion: str | None = None, |
| 34 | + side: str = "RIGHT", |
| 35 | + start_line: int | None = None, |
| 36 | +) -> str: |
| 37 | + """Add a review comment on a specific line in the PR diff. |
| 38 | +
|
| 39 | + Args: |
| 40 | + path: The relative file path in the repository (e.g. "src/main.rs"). |
| 41 | + line: The line number in the file that the comment applies to. |
| 42 | + For added or modified lines, use the line number in the new version of the file (side=RIGHT). |
| 43 | + For deleted lines, use the line number in the old version of the file (side=LEFT). |
| 44 | + body: The review comment text (Markdown supported). |
| 45 | + suggestion: Optional replacement code for the line(s). When provided, GitHub renders an |
| 46 | + "Apply suggestion" button the author can click. The suggestion replaces the |
| 47 | + entire line (or range if start_line is set). |
| 48 | + side: Which version of the file the line number refers to. |
| 49 | + "RIGHT" for the new/modified version (default), "LEFT" for the old/deleted version. |
| 50 | + start_line: For multi-line comments, the first line of the range. When set, `line` is the last line. |
| 51 | + """ |
| 52 | + if suggestion is not None: |
| 53 | + body = ( |
| 54 | + f"{body}\n\n```suggestion\n{suggestion}\n```" |
| 55 | + if body |
| 56 | + else f"```suggestion\n{suggestion}\n```" |
| 57 | + ) |
| 58 | + |
| 59 | + comment = {"path": path, "line": line, "side": side, "body": body} |
| 60 | + if start_line is not None: |
| 61 | + comment["start_line"] = start_line |
| 62 | + comment["start_side"] = side |
| 63 | + |
| 64 | + total = _append_comment(comment) |
| 65 | + return f"Comment added on {path}:{line} ({total} total)." |
| 66 | + |
| 67 | + |
| 68 | +@server.tool() |
| 69 | +def finish_review(body: str = "") -> str: |
| 70 | + """Finish the review. |
| 71 | +
|
| 72 | + Args: |
| 73 | + body: Optional top-level review body (Markdown supported). Only include if it |
| 74 | + contains information not already covered by inline comments. Most reviews |
| 75 | + should leave this empty. |
| 76 | + """ |
| 77 | + conclusion = {"body": body, "event": "COMMENT"} |
| 78 | + conclusion_file = output_dir / "conclusion.json" |
| 79 | + conclusion_file.write_text(json.dumps(conclusion, indent=2)) |
| 80 | + return "Review finished." |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + server.run(transport="stdio") |
0 commit comments