使用说明
GitHub PR评论解析器-MCP服务器
一个生产级本地MCP(模型上下文协议)服务器,通过GitHub API获取评论并为基于LLM的代码解析生成结构化提示,从而自动解析GitHub PR审核评论。
🎯 特性
- 基于FastAPI的MCP服务器 具有RESTful端点
- GitHub API集成 获取PR评论和文件内容
- 多格式提示生成 (游标、JSON、Markdown)
- 机器人过滤 以及用户特定的评论处理
- 文件上下文增强 带有差异块和代码片段
- CLI接口 用于命令行使用
- 后台处理 用于自动更改应用程序
- 生产准备就绪 通过适当的错误处理、日志记录和配置
🚀 快速开始
1.安装
# Clone and setup
git clone <your-repo>
cd cursor-pr-fix
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
2.配置
# Copy environment template
cp .env.example .env
# Edit .env with your GitHub token
GITHUB_TOKEN=your_github_token_here
获取GitHub令牌:
- 首选 GitHub设置>开发者设置>个人访问令牌
- 使用创建新令牌
repo范围 - 将令牌复制到
.env文件
3.使用选项
选项A:CLI(建议首次使用)
# Generate Cursor-compatible prompt
python cli.py generate-prompt https://github.com/owner/repo/pull/123
# With options
python cli.py generate-prompt https://github.com/owner/repo/pull/123 \
--format-type cursor \
--skip-bots \
--dry-run
# Apply changes directly (placeholder implementation)
python cli.py apply-changes https://github.com/owner/repo/pull/123 --dry-run
# Check status
python cli.py status
选项B:MCP服务器
# Start server
python cli.py start-server
# Or directly
python server.py
服务器将在上运行 http://127.0.0.1:8000 API文档 /docs
选项C:API调用
# Generate prompt via API
curl -X POST "http://127.0.0.1:8000/generate-cursor-task" \
-H "Content-Type: application/json" \
-d '{
"pr_url": "https://github.com/owner/repo/pull/123",
"format_type": "cursor",
"skip_bots": true
}'
📁 项目结构
cursor-pr-fix/
├── server.py # FastAPI MCP server
├── cli.py # Command-line interface
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── handlers/
│ ├── __init__.py
│ └── github.py # GitHub API interactions
├── prompt/
│ ├── __init__.py
│ └── generator.py # Prompt generation logic
├── utils/
│ ├── __init__.py
│ └── file_manager.py # File operations and change application
└── pr_prompts/ # Generated prompt files (auto-created)
🔧 配置
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
GITHUB_TOKEN |
GitHub个人访问令牌 | 必填 |
SERVER_HOST |
服务器主机地址 | 127.0.0.1 |
SERVER_PORT |
服务器端口 | 8000 |
LOG_LEVEL |
日志记录级别 | INFO |
API终点
| 端点 | 方法 | 描述 |
|---|---|---|
/ |
GET | 健康检查 |
/generate-cursor-task |
POST | 生成结构化提示 |
/apply-changes |
POST | 将建议应用于文件 |
/status |
GET | 服务器状态 |
📋 请求/响应格式
生成提示请求
{
"pr_url": "https://github.com/owner/repo/pull/123",
"dry_run": false,
"only_user": "username",
"skip_bots": true,
"auto_apply": false,
"format_type": "cursor"
}
响应
{
"success": true,
"message": "Generated prompt for 5 comments",
"comments_found": 5,
"prompt_file": "cursor_prompt_repo_123.txt",
"changes_applied": [],
"errors": []
}
🎨 输出格式
1.光标格式(默认)
针对Cursor IDE进行了优化,包括:
- 结构化降价部分
- 带有语法高亮显示的代码上下文
- 可采取行动的建议
- 最佳实践指南
2.JSON格式
用于程序化处理的结构化数据:
{
"task": "resolve_pr_comments",
"metadata": { ... },
"comments": [ ... ]
}
3.Markdown格式
清理标记以备文件或审查。
🔄 工作流集成
使用游标IDE
-
生成提示:
python cli.py generate-prompt <PR_URL> -
复制和粘贴:
- 打开生成
.txt文件 - 将内容复制到Cursor聊天
- 让LLM分析和应用更改
- 打开生成
-
审查变更:
- 法学硕士将解释推理
- 应用建议的修改
- 提交前审查差异
自动化工作流程
# Generate and apply in one step (when implemented)
python cli.py generate-prompt <PR_URL> --auto-apply
🛠️ 发展
添加自定义处理程序
-
新评论处理器:
# In handlers/custom.py class CustomHandler: async def process_comment(self, comment: Dict) -> Dict: # Custom logic return processed_comment -
新提示格式:
# In prompt/generator.py async def _generate_custom_prompt(self, comments, pr_info): # Custom format implementation return formatted_prompt
测试
# Test with dry run
python cli.py generate-prompt <PR_URL> --dry-run
# Check status
python cli.py status
# Test API
curl http://127.0.0.1:8000/status
🔐 安全
- GitHub令牌存储在环境变量中
- 日志中没有敏感数据
- 遵守GitHub API的速率限制
- CORS仅配置用于本地开发
📊 日志记录
日志包括:
- API请求/响应详细信息
- GitHub API交互
- 文件操作结果
- 带有上下文的错误详细信息
通过配置 LOG_LEVEL 环境变量。
🤝 贡献
- 分叉存储库
- 创建特征分支
- 添加新功能的测试
- 提交拉取请求
📄 许可证
MIT许可证-有关详细信息,请参阅许可证文件。
🆘 故障排除
常见问题
-
GitHub 令牌错误:
- 验证令牌是否具有
repo范围 - 支票令牌尚未过期
- 确保令牌在
.env文件
- 验证令牌是否具有
-
API费率限制:
- GitHub API有速率限制
- 服务器尊重分页
- 考虑GitHub Apps以获得更高的限制
-
找不到文件:
- 确保PR分支文件在本地存在
- 检查注释中的文件路径是否与本地结构匹配
调试模式
python cli.py --verbose generate-prompt <PR_URL>
如需更多帮助,请查看 /docs 服务器运行时的端点。
