GitHub操作服务器

一个通过PyGithub与GitHub API交互的模型上下文协议服务器,提供管理问题、仓库和拉取请求等功能。

  • by AstroMined
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 astromined/pygithub-mcp-server

使用说明

PyGithub MCP服务器

模型上下文协议服务器,提供通过PyGithub与GitHub API交互的工具。该服务器使AI助手能够执行GitHub操作,如管理问题、存储库和拉取请求。

特性

  • 模块化工具架构:

    • 可启用/禁用的可配置工具组
    • 特定领域的组织(问题、存储库等)
    • 通过文件或环境变量灵活配置
    • 与模块化设计明确分离关注点
    • 易于扩展,模式一致
  • 完整的GitHub问题管理:

    • 创建和更新问题
    • 获取问题详细信息并列出存储库问题
    • 添加、列出、更新和删除评论
    • 管理问题标签
    • 处理指定人员和里程碑
  • 智能参数处理:

    • 用于可选参数的动态kwargs构建
    • GitHub对象的正确类型转换
    • 所有输入参数的验证
    • 清除无效输入的错误消息
  • 稳健实施:

    • 通过PyGithub实现面向对象的GitHub API交互
    • 集中式GitHub客户端管理
    • 正确的错误处理和速率限制
    • 通过MCP工具清理API抽象
    • 全面的分页支持
    • 调试详细日志记录

文档

综合指南可在docs/guides目录中找到:

  • error-handling.md:错误类型、处理模式和最佳实践
  • security.md:身份验证、访问控制和内容安全
  • tool-reference.md:带有示例的详细工具文档

有关使用PyGithub MCP服务器的详细信息,请参阅这些指南。

使用示例

问题操作

  1. 创建问题
{
  "owner": "username",
  "repo": "repository",
  "title": "Issue Title",
  "body": "Issue description",
  "assignees": ["username1", "username2"],
  "labels": ["bug", "help wanted"],
  "milestone": 1
}
  1. 获取问题详细信息
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1
}
  1. 更新问题
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1,
  "title": "Updated Title",
  "body": "Updated description",
  "state": "closed",
  "labels": ["bug", "wontfix"]
}

评论操作

  1. 添加评论
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1,
  "body": "This is a comment"
}
  1. 列表评论
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1,
  "per_page": 10
}
  1. 更新评论
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1,
  "comment_id": 123456789,
  "body": "Updated comment text"
}

标签操作

  1. 添加标签
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1,
  "labels": ["enhancement", "help wanted"]
}
  1. 删除标签
{
  "owner": "username",
  "repo": "repository",
  "issue_number": 1,
  "label": "enhancement"
}

所有操作都智能地处理可选参数:

  • 仅包括API调用中提供的参数
  • 将基元类型转换为GitHub对象(例如,将里程碑编号转换为里程碑对象)
  • 为无效参数提供明确的错误消息
  • 在适用的情况下自动处理分页

安装

  1. 创建并激活虚拟环境:
uv venv
source .venv/bin/activate
  1. 安装依赖项:
uv pip install -e .

配置

基本配置

将服务器添加到您的MCP设置中(例如。, claude_desktop_config.json 或 cline_mcp_settings.json):

{
  "mcpServers": {
    "github": {
      "command": "/path/to/repo/.venv/bin/python",
      "args": ["-m", "pygithub_mcp_server"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "your-token-here"
      }
    }
  }
}

工具组配置

服务器支持通过配置选择性地启用或禁用工具组。您可以通过两种方式进行配置:

1.配置文件

创建JSON配置文件(例如。, pygithub_mcp_config.json):

{
  "tool_groups": {
    "issues": {"enabled": true},
    "repositories": {"enabled": true},
    "pull_requests": {"enabled": false},
    "discussions": {"enabled": false},
    "search": {"enabled": true}
  }
}

然后在您的环境中指定此文件:

export PYGITHUB_MCP_CONFIG=/path/to/pygithub_mcp_config.json

2.环境变量

或者,使用环境变量配置工具组:

export PYGITHUB_ENABLE_ISSUES=true
export PYGITHUB_ENABLE_REPOSITORIES=true
export PYGITHUB_ENABLE_PULL_REQUESTS=false

默认情况下,只有 issues 工具组已启用。看 README.config.md 有关更详细的配置选项。

发展

测试

该项目包括一个全面的测试套件:

# Run all tests
pytest

# Run tests with coverage report
pytest --cov

# Run specific test file
pytest tests/test_operations/test_issues.py

# Run tests matching a pattern
pytest -k "test_create_issue"

注:目前许多测试都失败了,正在调查中。这是一个正在积极解决的已知问题。

MCP检验员测试

在开发过程中使用MCP检查器测试MCP工具:

source .venv/bin/activate  # Ensure venv is activated
npx @modelcontextprotocol/inspector -e GITHUB_PERSONAL_ACCESS_TOKEN=your-token-here uv run pygithub-mcp-server

使用MCP检查器的Web UI:

  • 使用可用工具进行实验
  • 使用真实的GitHub存储库进行测试
  • 验证成功和错误案例
  • 文件工作有效载荷

项目结构

tests/
├── unit/                # Fast tests without external dependencies
│   ├── config/          # Configuration tests
│   ├── tools/           # Tool registration tests
│   └── ...              # Other unit tests
└── integration/         # Tests with real GitHub API
    ├── issues/          # Issue tools tests
    └── ...              # Other integration tests
src/
└── pygithub_mcp_server/
    ├── __init__.py
    ├── __main__.py
    ├── server.py        # Server factory (create_server)
    ├── version.py
    ├── config/          # Configuration system
    │   ├── __init__.py
    │   └── settings.py  # Configuration management
    ├── tools/           # Modular tool system
    │   ├── __init__.py  # Tool registration framework
    │   └── issues/      # Issue tools
    │       ├── __init__.py
    │       └── tools.py # Issue tool implementations
    ├── client/          # GitHub client functionality
    │   ├── __init__.py
    │   ├── client.py    # Core GitHub client
    │   └── rate_limit.py # Rate limit handling
    ├── converters/      # Data transformation
    │   ├── __init__.py
    │   ├── parameters.py # Parameter formatting
    │   ├── responses.py # Response formatting
    │   ├── common/      # Common converters
    │   ├── issues/      # Issue-related converters
    │   ├── repositories/ # Repository converters
    │   └── users/       # User-related converters
    ├── errors/          # Error handling
    │   ├── __init__.py
    │   └── exceptions.py # Custom exceptions
    ├── operations/      # GitHub operations
    │   ├── __init__.py
    │   └── issues.py
    ├── schemas/         # Data models
    │   ├── __init__.py
    │   ├── base.py
    │   ├── issues.py
    │   └── ...
    └── utils/           # General utilities
        ├── __init__.py
        └── environment.py # Environment utilities

故障排除

  1. 服务器启动失败:

    • 验证MCP设置中的venv Python路径
    • 确保所有要求都安装在venv中
    • 检查GITHUB_PERSONAL_ACCES_TOKEN是否已设置且有效
  2. 构建错误:

    • 使用uv构建时的无构建隔离标志
    • 确保使用Python 3.10+
    • 验证是否已安装所有依赖项
  3. GitHub API 错误:

    • 检查令牌权限和有效性
    • 查看pygithub _mcp_server.log以获取详细的错误跟踪信息
    • 验证是否未超过速率限制

依赖项

  • Python 3.10+
  • MCP Python SDK
  • 派丹蒂克
  • PyGithub
  • UV包管理器

许可证

麻省理工学院