GitHub代码审查助手

一个基于MCP协议的智能GitHub代码审查服务器,提供PR分析、建议生成、标准合规检查等功能,帮助团队高效完成代码审查工作。

  • by sanjanaspanda
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 sanjanaspanda/mcp-server

使用说明

GitHub代码审查助手MCP服务器

一个全面的MCP(模型上下文协议)服务器,为GitHub pull请求代码审查提供智能工具。该服务器使AI助手能够分析PR,提出改进建议,检查模式,并确保与团队标准保持一致。

特性

  • 综合公关分析 -分析代码模式、复杂性和潜在问题
  • 评审管理 -创建评论、提交评论和管理反馈
  • 聪明的建议 -基于最佳实践的人工智能审查建议
  • 标准合规 -根据团队编码标准检查PR
  • 文件和差异分析 -详细检查变更及其影响
  • 工作流集成 -专为完整审查工作流程设计的工具

安装

先决条件

  • Python 3.8或更高版本
  • GitHub个人访问令牌 repo 范围
  • MCP兼容客户端(例如,Claude Desktop或任何MCP客户端)

设置

  1. 安装依赖项:
pip install mcp httpx pydantic
  1. 设置您的GitHub令牌:

    • 转到GitHub设置→ 开发人员设置→ 个人访问令牌
    • 使用生成新令牌 repo 范围
    • 安全保存令牌
  2. 运行服务器:

python github_code_review_mcp.py

Claude桌面配置

添加到您的Claude Desktop配置文件中:

{
  "mcpServers": {
    "github-code-review": {
      "command": "python",
      "args": ["/path/to/server.py"]
    }
  }
}

可用工具

1. github_list_pull_requests

列出存储库中具有全面筛选选项的拉取请求。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • state:按状态筛选(打开/关闭/全部)
  • sort:排序方式(创建/更新/流行/长期运行)
  • direction:排序方向(asc/desc)
  • base:按基分支筛选
  • head:按头部分支过滤
  • limit:最大结果(1-100)
  • page:分页页码
  • response_format:输出格式(markdown/json)

示例用法:

List all open PRs in facebook/react repository

2. github_get_pr_details

检索有关特定拉取请求的全面详细信息。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • include_reviews:包括审核信息(默认值:true)
  • include_checks:包括状态检查(默认值:true)
  • response_format:输出格式(markdown/json)

示例用法:

Get detailed information about PR #123 including reviews and checks

3. github_get_pr_files

列出拉取请求中更改的所有文件及其统计信息。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • limit:每页最大结果数
  • page:页码
  • response_format:输出格式(markdown/json)

示例用法:

Show me all files changed in PR #456

4. github_get_pr_diff

检索拉取请求的统一差异。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • file_path:特定文件的筛选器(可选)
  • context_lines:上下文行数(0-10)

示例用法:

Get the diff for PR #789, focusing on src/main.js

5. github_analyze_pr

对代码质量的拉取请求进行全面分析。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • check_patterns:检查代码模式(默认值:true)
  • check_complexity:分析复杂性(默认值:true)
  • check_security:基本安全检查(默认值:true)
  • response_format:输出格式(markdown/json)

示例用法:

Analyze PR #234 for code patterns, complexity, and security issues

6. github_get_pr_comments

检索拉取请求的所有注释。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • comment_type:评论类型(全部/问题/审查)
  • limit:最大结果
  • page:页码
  • response_format:输出格式(markdown/json)

示例用法:

Get all review comments for PR #567

7. github_create_review_comment

对拉取请求创建注释(常规或内联)。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • body:带有markdown支持的注释文本(必需)
  • commit_id:SHA承诺发表评论(可选)
  • path:内联注释的文件路径(可选)
  • line:内联注释的行号(可选)
  • side:差速器侧面(左/右)

示例用法:

Add a comment to line 42 of src/utils.js suggesting a performance improvement

8. github_create_pr_review

对pull请求提交正式审核。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • body:审阅摘要文本(可选)
  • event:审查行动(批准/请求_变更/评论)
  • comments:内联评论数组(可选)

示例用法:

Approve PR #890 with a comment about good test coverage

9. github_get_review_suggestions

为拉取请求生成基于AI的审核建议。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • focus_areas:重点领域(性能/安全性/可读性/测试/文档)
  • response_format:输出格式(markdown/json)

示例用法:

Generate review suggestions for PR #345 focusing on security and performance

10. github_check_team_standards

检查PR是否符合团队编码标准。

参数:

  • owner:存储库所有者(必填)
  • repo:存储库名称(必填)
  • github_token:GitHub访问令牌(必填)
  • pr_number:拉取请求号(必填)
  • standards_file:仓库中标准文件的路径(默认:.github/CODING_standards.md)
  • response_format:输出格式(markdown/json)

示例用法:

Check if PR #678 meets our team's coding standards

使用示例

示例1:完成PR审查工作流程

# 1. List open PRs to find ones needing review
github_list_pull_requests(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    state="open",
    sort="created"
)

# 2. Get details about a specific PR
github_get_pr_details(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=123
)

# 3. Analyze the PR for issues
github_analyze_pr(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=123
)

# 4. Get AI suggestions
github_get_review_suggestions(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=123,
    focus_areas=["security", "performance"]
)

# 5. Check team standards
github_check_team_standards(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=123
)

# 6. Submit review with comments
github_create_pr_review(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=123,
    body="Great work! A few suggestions for improvement...",
    event="APPROVE"
)

示例2:聚焦代码模式分析

# Get files changed
files = github_get_pr_files(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=456
)

# Get diff for specific analysis
diff = github_get_pr_diff(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=456,
    file_path="src/api/handler.js"
)

# Analyze for patterns
analysis = github_analyze_pr(
    owner="myorg",
    repo="myrepo",
    github_token="ghp_xxx",
    pr_number=456,
    check_patterns=True,
    check_security=True
)

最佳实践

对于审阅者

  1. 从概述开始:使用 github_get_pr_details 了解公关背景
  2. 先分析:运行 github_analyze_pr 人工审核前
  3. 检查标准:使用 github_check_team_standards 为了保持一致性
  4. 获取建议:使用 github_get_review_suggestions 获取全面反馈
  5. 建设性:在创建评论时,要具体并提出改进建议

公关作者

  1. 自我审查:在请求审核之前,使用您自己的PR分析工具
  2. 地址标准:提交前检查标准合规性
  3. 让公关人员保持专注:分析工具在较小、重点突出的变化上效果更好
  4. 包括测试:工具检查测试覆盖率
  5. 写出好的描述:工具分析公关描述的上下文

安全考虑

  1. 令牌安全:永远不要硬编码GitHub令牌。使用环境变量或安全凭据存储
  2. 权限:确保令牌具有适当的作用域(通常 repo 足够了)
  3. 速率限制:GitHub API有速率限制。这些工具可以很好地处理这个问题,但要注意局限性
  4. 私有仓库:如果需要,确保令牌可以访问私有存储库

模式检测

分析工具检测各种代码模式,包括:

  • 安全问题:硬编码的秘密、SQL注入风险、XSS漏洞
  • 性能问题:嵌套循环、SELECT*、异步代码中的同步操作
  • 代码质量:控制台日志、注释代码、空catch块
  • 最佳实践:缺少测试、大文件、缺少文档

团队标准集成

创建一个 .github/CODING_STANDARDS.md 使用团队的标准在存储库中创建文件。该工具将自动使用它进行合规性检查。示例格式:

# Coding Standards

## General Rules

- max_file_length: 500
- max_pr_size: 1000
- require_tests: true
- require_documentation: true

## Branch Naming

- Pattern: (feature|bugfix|hotfix|release)/description

## Commit Messages

- Format: type(scope): description
- Types: feat, fix, docs, style, refactor, test, chore

故障排除

常见问题

  1. 认证失败

    • 验证您的GitHub令牌是否有效
    • 检查令牌具有所需的作用域
    • 确保令牌未过期
  2. 速率限制

    • GitHub将API对已验证请求的调用限制为5000/小时
    • 工具将报告速率限制错误
    • 考虑对频繁访问的数据实施缓存
  3. 大PR

    • 非常大的PR可能会达到响应大小限制
    • 使用分页参数
    • 尽可能筛选到特定文件
  4. 网络错误

    • 检查互联网连接
    • 验证GitHub API是否可访问
    • 检查代理/防火墙问题

贡献

欢迎投稿!需要改进的地方:

  • 附加模式检测规则
  • 支持GitLab/Bitbucket
  • 增强的安全扫描
  • 与更多的CI/CD系统集成
  • 自定义规则定义
  • 缓存层可提高性能

许可证

MIT许可证-有关详细信息,请参阅许可证文件

致谢

构建使用:

支持

对于问题、疑问或建议:

  • 在GitHub上打开一个问题
  • 检查文档
  • 查看故障排除指南

备注:此工具旨在协助代码审查,但不应取代人工判断。在审查代码时,始终应用上下文和领域知识。