使用说明
GitHub公关审查员MCP服务器
一个MCP(模型上下文协议)服务器,为Go存储库提供自动代码审查功能。此服务器使Claude能够分析pull请求、运行测试、执行静态分析以及将评论发布到GitHub。
特性
- 🔍 静态代码分析:在go代码上运行golangci-lint、gofmt和go-vet
- ✅ 自动化测试:使用覆盖率分析执行Go测试
- 📊 PR差异分析:获取并分析GitHub PR更改
- 💬 评审意见:自动将评论反馈发布到GitHub
- 🤖 全面审查:端到端PR审查工作流程编排
建筑
┌─────────────────┐
│ │
│ Claude.ai / │
│ Claude Desktop │
│ │
└────────┬────────┘
│ MCP Protocol
│
┌────────▼────────────────────────────┐
│ GitHub PR Reviewer MCP Server │
│ │
│ Tools: │
│ • github_pr_analyze_code │
│ • github_pr_run_tests │
│ • github_pr_get_diff │
│ • github_pr_post_review │
│ • github_pr_comprehensive_review │
└────────┬────────────────────────────┘
│
├──────────────┬──────────────┐
│ │ │
┌────▼───┐ ┌────▼────┐ ┌───▼─────┐
│ GitHub │ │ Local │ │ Go │
│ API │ │ Files │ │ Tools │
└────────┘ └─────────┘ └─────────┘
先决条件
系统要求
- Python 3.10或更高版本
- Go 1.20或更高(用于Go项目分析)
- Git
Go工具安装
安装所需的Go分析工具:
# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Ensure Go tools are in PATH
export PATH=$PATH:$(go env GOPATH)/bin
GitHub身份验证
您需要一个具有以下权限的GitHub个人访问令牌:
repo(用于私有存储库)pull_requests:write(用于发表评论)
在以下位置创建令牌:https://github.com/settings/tokens
安装
1.克隆存储库
git clone https://github.com/yourusername/github-pr-reviewer-mcp.git
cd github-pr-reviewer-mcp
2.安装Python依赖项
# Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
3.设置环境变量
# Set your GitHub token
export GITHUB_TOKEN="ghp_your_token_here"
对于持久配置,请添加 ~/.bashrc 或 ~/.zshrc:
echo 'export GITHUB_TOKEN="ghp_your_token_here"' >> ~/.bashrc
source ~/.bashrc
4.配置克劳德桌面
将服务器添加到Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
Linux: ~/.config/Claude/claude_desktop_config.json
{
"mcpServers": {
"github-pr-reviewer": {
"command": "python",
"args": ["/absolute/path/to/github-pr-reviewer-mcp/github_pr_mcp.py"],
"env": {
"GITHUB_TOKEN": "ghp_your_token_here"
}
}
}
}
替换 /absolute/path/to/github-pr-reviewer-mcp/ 与实际路径。
5.重新启动克劳德桌面
更新配置后,重新启动Claude Desktop以使更改生效。
用法
可用工具
1. github_pr_analyze_code
使用静态分析工具分析Go代码的质量问题。
参数:
file_path(string):Go文件或目录的路径analysis_type(string):“lint”、“fmt”、“vet”或“all”response_format(string):“markdown”或“json”
例子:
Can you analyze the code in /path/to/myproject/main.go?
2. github_pr_run_tests
运行带有可选覆盖率分析的Go测试。
参数:
package_path(string):转到包路径(默认:“./…”)verbose(bool):启用详细输出coverage(bool):生成覆盖率报告response_format(string):“markdown”或“json”
例子:
Run tests on my Go project at /path/to/myproject with coverage
3. github_pr_get_diff
获取GitHub pull请求的差异。
参数:
owner(string):存储库所有者repo(string):存储库名称pr_number(int):拉取请求号response_format(string):“markdown”或“json”
例子:
Show me the diff for PR #123 in owner/repo
4. github_pr_post_review
在GitHub pull请求中发布评论。
参数:
owner(string):存储库所有者repo(string):存储库名称pr_number(int):拉取请求号body(string):审阅评论文本event(string):“评论”、“批准”或“请求范围”commit_id(字符串,可选):具体提交审核
例子:
Post a review comment to PR #123 in owner/repo saying "LGTM! Great work on the refactoring."
5. github_pr_comprehensive_review
对GitHub pull请求进行全面的自动审查。
参数:
owner(string):存储库所有者repo(string):存储库名称pr_number(int):拉取请求号local_path(字符串,可选):克隆存储库的本地路径run_tests(bool):是否运行测试post_comments(bool):自动将评论发布到GitHubresponse_format(string):“markdown”或“json”
例子:
Review PR #456 in myorg/myrepo. The local clone is at /home/user/myrepo. Run tests and post the review.
示例工作流
工作流程1:快速静态分析
User: Analyze the Go code in my-project/handlers/api.go
Claude will:
1. Use github_pr_analyze_code tool
2. Run golangci-lint, gofmt, and go vet
3. Return formatted results with any issues found
工作流程2:测试本地更改
User: Run tests on my Go project at /home/user/awesome-app with coverage
Claude will:
1. Use github_pr_run_tests tool
2. Execute tests with coverage
3. Return test results and coverage percentages
工作流程3:审查GitHub PR
User: Review PR #42 in acme/rocket-engine. Local copy is at /home/user/rocket-engine.
Claude will:
1. Use github_pr_get_diff to fetch PR changes
2. Identify changed Go files
3. Use github_pr_analyze_code on each changed file
4. Use github_pr_run_tests to run the test suite
5. Generate comprehensive review summary
6. Optionally post review to GitHub (if requested)
工作流程4:完成自动审核
User: Do a full automated review of PR #789 in myorg/backend-api, local path is /projects/backend-api, and post the results.
Claude will:
1. Use github_pr_comprehensive_review tool
2. Fetch PR diff
3. Analyze all changed Go files
4. Run full test suite
5. Generate review with recommendations
6. Post review comment to GitHub PR
配置
环境变量
| 变量 | 必填 | 描述 |
|---|---|---|
GITHUB_TOKEN |
是 | GitHub个人访问令牌 |
工具特定设置
您可以通过修改MCP服务器代码来自定义分析行为:
# In github_pr_mcp.py
# Change default golangci-lint settings
lint_result = _run_command(
["golangci-lint", "run", "--config", ".golangci.yml", str(file_path)]
)
# Adjust test timeout
result = subprocess.run(
cmd,
timeout=600 # 10 minutes instead of 5
)
故障排除
问题:“未设置GITHUB_TOKEN环境变量”
解决方案: 确保在Claude Desktop配置中设置了令牌:
{
"mcpServers": {
"github-pr-reviewer": {
"env": {
"GITHUB_TOKEN": "ghp_your_actual_token"
}
}
}
}
问题:“golangci-lint:找不到命令”
解决方案: 安装golangci lint并确保它在PATH中:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
export PATH=$PATH:$(go env GOPATH)/bin
问题:MCP服务器未出现在Claude Desktop中
解决方案:
- 验证配置文件路径是否适用于您的操作系统
- 检查JSON语法(使用JSON验证器)
- 确保Python脚本路径是绝对的
- 完全重新启动克劳德桌面
- 检查Claude Desktop日志是否有错误
问题:测试失败,“包中没有Go文件”
解决方案: 确保从正确的目录运行测试:
- 集
local_path存储库根的参数 - 检查测试文件是否存在并具有
_test.go后缀
发展
运行测试
# Run Python tests
python -m pytest tests/
# Test MCP server directly
python github_pr_mcp.py
添加新工具
要添加新的分析工具,请执行以下操作:
- 定义Pydantic输入模型
- 为操作创建辅助函数
- 添加一个
@mcp.tool装饰功能 - 更新文档
例子:
class NewToolInput(BaseModel):
param: str = Field(..., description="Parameter description")
@mcp.tool(name="github_pr_new_tool")
async def new_tool(params: NewToolInput) -> str:
"""Tool description."""
# Implementation
pass
代码的风格
该项目如下:
- Python代码的PEP 8
- 为所有函数键入提示
- 用于输入验证的Pydantic模型
- 全面的文档字符串
安全最佳实践
- 从不提交代币:保持
GITHUB_TOKEN仅在环境变量中 - 使用最小权限:仅授予必要的GitHub令牌范围
- 验证输入:所有输入均通过Pydantic模型进行验证
- 限制文件访问:服务器仅读取/写入指定目录
- 超时操作:所有命令都有合理的超时
贡献
欢迎投稿!拜托:
- 分叉存储库
- 创建要素分支
- 添加新功能的测试
- 确保代码通过linting
- 提交拉取请求
许可证
MIT许可证-有关详细信息,请参阅许可证文件
支持
对于问题和疑问:
- GitHub问题:https://github.com/yourusername/github-pr-reviewer-mcp/issues
- MCP文件:https://modelcontextprotocol.io/
- 人类不和:https://discord.gg/anthropic
更新日志
v1.0.0(2025-02-07)
- 初始版本
- 静态代码分析支持(golangci-lint、gofmt、go-vet)
- 覆盖范围内的测试执行
- GitHub公关集成
- 全面审查工作流程
