GitHub PR评论MCP服务器

一个MCP服务器,允许大型语言模型(LLM)从GitHub拉取请求中获取并格式化评论。

  • by petems
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 cool-kids-inc/github-pr-review-mcp-server

使用说明

GitHub PR评论MCP服务器

这是一个模型上下文协议(MCP)服务器,允许像Claude这样的大型语言模型(LLM)从GitHub pull请求中获取和格式化评论。

⚠️ 安全通知:请阅读 安全.md 出于重要的安全考虑,特别是关于代理工作流和PR评论的自动化实现。

文档和快速入门

企业GitHub支持

此MCP服务器支持GitHub.com和GitHub企业服务器(GHES)。

配置

GITHUB_TOKEN 是必需的 -没有有效的非空令牌,服务器将无法启动。

对于GitHub.com(默认):

GITHUB_TOKEN=ghp_your_token_here

对于GitHub企业服务器:

GH_HOST=github.enterprise.com
GITHUB_TOKEN=your_enterprise_token

服务器基于 GH_HOST:

REST API:    https://{GH_HOST}/api/v3
GraphQL API: https://{GH_HOST}/api/graphql

高级:自定义API URL

对于非标准企业配置:

GITHUB_API_URL=https://custom.github.company.com/api
GITHUB_GRAPHQL_URL=https://custom.github.company.com/graphql

URL格式

服务器接受以下格式的PR URL:

https://{host}/owner/repo/pull/123

示例:

  • https://github.com/owner/repo/pull/123 (GitHub)
  • https://github.enterprise.com/owner/repo/pull/456 (GHES)

开发工作流程

代码质量和格式

此项目使用 拉夫 用于裁剪和格式化。所有配置均在中定义 pyproject.toml.

# Install development dependencies (if not already installed)
uv pip install -e ".[dev]"

# Run linting
ruff check .

# Run linting with auto-fix
ruff check . --fix

# Format code
ruff format .

# Run both linting and formatting
ruff check . --fix && ruff format .

测试

使用pytest运行测试:

# Run all tests
pytest

# Run tests with verbose output
pytest -v

# Run specific test file
pytest tests/test_integration.py

# Run tests with coverage (install pytest-cov first)
pytest --cov=. --cov-report=html

预承诺

此项目使用 预承诺 对暂存文件运行格式化、linting和测试。

# Install the git hook scripts
uv run --extra dev pre-commit install
uv run --extra dev pre-commit install --hook-type commit-msg

# Run all checks on every file
uv run --extra dev pre-commit run --all-files

如果 pre-commit install 由于您使用全局 core.hooksPath (例如, /usr/local/dd/global_hooks),通过pre-commit的install API将钩子直接安装到repo-hooks目录中。在这个仓库中,它创建了:

  • .git/hooks/pre-commit
  • .git/hooks/commit-msg

这些是相对于存储库根的示例路径,应该适应您的本地环境。这两个脚本都应该是可执行的。预计全局钩子链将调用这些回购级钩子。

运行MCP服务器

要启动MCP服务器,请在终端中运行以下命令:

# Using the installed script (after pip install -e .)
mcp-github-pr-review

# With UV
uv run mcp-github-pr-review

# Or use the helper script (uv-first)
./run-server.sh                 # starts via `uv run`
./run-server.sh --sync          # sync deps first
./run-server.sh --log           # also write logs/logs/mcp_server.log
./run-server.sh --register      # register with Claude CLI as `pr-review`
./run-server.sh --codex         # configure Codex CLI to use this server

在编辑器和CLI中安装/配置

选项A:通过快速设置 run-server.sh

  • 仅限交互式配置/显示配置:
    ./run-server.sh --config     # print instructions for all clients (no changes)
    
  • 一次性配置常见客户端(非交互式):
    ./run-server.sh --register --codex --gemini
    # add --desktop to also configure Claude Desktop
    

选项B:手动设置

从repo根目录运行这些命令,以便 $(pwd) 指向这个项目。

克劳德代码(CLI)

# Minimal (pass env vars if needed)
claude mcp add pr-review -s user -- \
  uv run --project "$(pwd)" -- mcp-github-pr-review

# Example with env var (GitHub token)
claude mcp add pr-review -s user -e GITHUB_TOKEN="$GITHUB_TOKEN" -- \
  uv run --project "$(pwd)" -- mcp-github-pr-review

Codex 命令行界面

附加 ~/.codex/config.toml:

[mcp_servers.pr-review]
command = "uv"
args = ["run", "mcp-github-pr-review"]

[mcp_servers.pr-review.env]
# Optional – provide your token here, or rely on your shell environment
# GITHUB_TOKEN = "your_github_token_here"
PATH = "/usr/local/bin:/usr/bin:/bin:/opt/homebrew/bin:$HOME/.local/bin:$HOME/.cargo/bin:$HOME/bin"

提示: ./run-server.sh --codex 我可以为你写这个。

双子星命令行工具

编辑 ~/.gemini/settings.json 并添加:

{
  "mcpServers": {
    "pr-review": {
      "command": "uv",
      "args": ["run", "mcp-github-pr-review"]
    }
  }
}

提示: ./run-server.sh --gemini 我可以为你写这个。

服务器监听 stdio 并且对连接的MCP客户端可用。

可用工具

服务器向LLM公开以下工具:

1. fetch_pr_review_comments

从给定的GitHub pull请求URL获取所有评论。该工具默认返回Markdown(针对人类/AI可读性进行了优化),并提供请求JSON或两者的选项。

  • 参数:

    • pr_url (str):拉取请求的完整URL(例如。, "https://github.com/owner/repo/pull/123").如果省略,服务器将尝试从当前仓库/分支自动解析。
    • output (str,可选):输出格式。之一 "markdown" (默认), "json",或 "both".
      • markdown:返回从注释呈现的单个Markdown文档(默认)。
      • json:返回一个包含原始注释列表的JSON字符串。
      • both:返回两条消息:首先是JSON,然后是Markdown。
    • per_page (int,可选):GitHub页面大小(1-100)。env中的默认值 HTTP_PER_PAGE.
    • max_pages (int,可选):页面上的安全帽。env中的默认值 PR_FETCH_MAX_PAGES.
    • max_comments (int,可选):总评论的安全上限。env中的默认值 PR_FETCH_MAX_COMMENTS.
    • max_retries (int,可选):针对暂时性错误重试预算。env中的默认值 HTTP_MAX_RETRIES.
  • 退货:

    • 当 output="markdown" (默认):包含Markdown的单个文本项。
    • 当 output="json":包含原始注释列表的JSON字符串的单个文本项。
    • 当 output="both":按顺序排列两个文本项——首先是JSON,然后是Markdown。
    • GraphQL结果包括 thread_id 因此,已解决的评论可以稍后关闭 resolve_pr_review_thread.

示例(Markdown默认):

{
  "name": "fetch_pr_review_comments",
  "arguments": { "pr_url": "https://github.com/owner/repo/pull/123" }
}

示例(JSON输出):

{
  "name": "fetch_pr_review_comments",
  "arguments": { "pr_url": "https://github.com/owner/repo/pull/123", "output": "json" }
}

2. resolve_open_pr_url(select_strategy?: str, owner?: str, repo?: str, branch?: str) -> str

使用git检测解析当前分支的打开PR URL。

  • 参数:
    • select_strategy (str,可选):自动解析PR时的策略(默认为“分支”)。选项:“分支”、“最新”、“第一”、“错误”。
    • owner (str,可选):覆盖回购所有者以进行PR解析。
    • repo (str,可选):覆盖PR解析的仓库名称。
    • branch (str,可选):覆盖PR解析的分支名称。
  • 退货:
    • 解析后的PR URL为字符串。

3. resolve_pr_review_thread(thread_id: str, host?: str, max_retries?: int) -> str

当代理修复了问题时,解决GitHub审查线程。

  • 参数:
    • thread_id (str,必填):GraphQL审查线程ID(可在 fetch_pr_review_comments 输出)。
    • host (str,可选):覆盖企业实例的GitHub主机。
    • max_retries (int,可选):为瞬时网络/API故障重试预算。
  • 退货:
    • JSON格式的字符串(文本),通过以下方式生成 json.dumps(...),不是原生JSON对象。
    • 当解析为JSON时,有效载荷包含 thread_id, is_resolved,以及 resolved_by 如果可用。
  • 权限:
    • 经典PAT: repo 范围(完全访问存储库)是必需的。
    • 细粒PAT: 对目标仓库的访问权限 拉取请求:读和写.

GitHub代币范围

使用最小权限 GITHUB_TOKEN:

  • 只读工具(fetch_pr_review_comments, resolve_open_pr_url)可以使用读取级拉取请求访问运行。
  • 经典PAT:
    • 公共存储库(只读工具): public_repo 足够了。
    • 私有存储库(只读工具): repo 是必需的。
    • resolve_pr_review_thread (突变): repo 是必需的。
  • 细粒PAT:
    • 存储库访问:选择目标存储库。
    • 只读工具:Pull请求→ 阅读权限(允许在以下网址阅读评论 GET /repos/{owner}/{repo}/pulls/{pull_number}/comments).
    • resolve_pr_review_thread:拉取请求→ 读和写。

除非工作流程需要,否则应避免更广泛的写入/管理范围。

警告:生产部署

不要在生产环境中使用开发服务器或启用调试运行此应用程序。如果暴露HTTP端点,请使用生产级WSGI/ASGI服务器(例如Gunicorn、Uvicorn),并确保禁用调试。

环境变量

这些变量可以在 .env (通过加载 python-dotenv)或您的环境:

  • GITHUB_TOKEN:用于API调用的GitHub PAT。细粒度代币使用 Bearer 方案;经典PAT会自动重试 token 401计划。
  • PR_FETCH_MAX_PAGES (默认值 50):获取评论时分页页面的安全上限。
  • PR_FETCH_MAX_COMMENTS (默认值 2000):提前停止前收集的评论总数的安全上限。
  • HTTP_PER_PAGE (默认值 100):GitHub API per_page 值(1–100)。
  • HTTP_MAX_RETRIES (默认值 3):瞬时请求错误和5xx响应的最大重试次数,具有回退+抖动。

对于GitHub Enterprise实例,请重写 .env:

GITHUB_API_URL=https://custom.github.company.com/api/v3
GITHUB_GRAPHQL_URL=https://custom.github.company.com/api/graphql

项目结构和工具

现代Python工具

该项目使用现代Python工具来增强开发人员体验:

  • 紫外线:超快Python包管理器和解析器
  • 拉夫:非常快的Python linter和格式化程序
  • pyproject.toml:现代Python项目配置文件
  • pytest:具有异步支持的现代测试框架

现代工具的好处

  • 速度:对于依赖关系分辨率,UV比pip快10-100倍
  • 可靠性:更好的依赖解析和锁文件生成
  • 代码质量:Ruff在毫秒内提供全面的linting和格式化
  • 开发者体验:更好的IDE集成和更快的反馈循环
  • 一致性:pyproject.toml中的标准化配置

传统支持

为了兼容性,原始 requirements.txt 文件被维护,但现代工作流使用 pyproject.toml 建议使用紫外线进行开发。

从旧安装程序迁移

如果您要从旧的基于pip的设置迁移:

# Remove old virtual environment
rm -rf venv

# Install UV
pip install uv

# Install dependencies with UV
uv pip install -e ".[dev]"

# Format and lint your code
ruff format . && ruff check . --fix

# Run tests to ensure everything works
pytest