P

PyGithub AI助手管理器

@AstroMined/pygithub-mcp-server
0 Stars 324 次浏览 AstroMined 更新于 2026-08-23

通过PyGithub库启用AI助手与GitHub的交互,提供管理问题、存储库、拉取请求和其他GitHub操作的工具,并具备智能参数处理和错误管理功能。

该服务暂未提供标准配置,请参考 README 手动接入

服务介绍

PyGithub MCP 服务器

一个提供通过 PyGithub 与 GitHub API 交互工具的 Model Context Protocol 服务器。该服务器使 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 对象(例如,将里程碑编号转换为 Milestone 对象)
  • 为无效参数提供清晰的错误消息
  • 在适用的情况下自动处理分页

安装

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

配置

基本配置

将服务器添加到您的 MCP 设置中(例如 claude_desktop_config.jsoncline_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 Inspector 进行测试

在开发过程中使用 MCP Inspector 测试 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 Inspector 的 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_ACCESS_TOKEN 是否设置且有效
  2. 构建错误:

    • 使用 --no-build-isolation 标志与 uv build
    • 确保使用的是 Python 3.10+
    • 验证所有依赖项均已安装
  3. GitHub API 错误:

    • 检查令牌权限和有效性
    • 查看 pygithub_mcp_server.log 以获取详细的错误跟踪
    • 确认未超过速率限制

依赖项

  • Python 3.10+
  • MCP Python SDK
  • Pydantic
  • PyGithub
  • UV 包管理器

许可证

MIT

相关 MCP 服务