GitHub操作协议服务器

一个Python实现的GitHub操作协议服务器,提供与Claude Code和其他MCP客户端的原生工具集成,支持问题、拉取请求、CI状态和批量操作等功能。

  • by rriesco
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 rriesco/github-mcp-server

使用说明

GitHub MCP服务器

用于GitHub操作的Python MCP(模型上下文协议)服务器,提供与Claude Code和其他MCP客户端的本地工具集成。

特性

  • 16个GitHub工具:问题、PR、CI状态、里程碑和批处理操作
  • 本地MCP集成:与Claude Code无缝协作
  • 结构化响应:类型安全、格式良好的工具输出
  • 批量操作:批量操作的并行执行
  • 基于环境的配置:通过环境变量配置默认存储库

安装

来自PyPI

pip install github-mcp-server
# or with uv
uvx github-mcp-server

来源

git clone https://github.com/rriesco/github-mcp-server.git
cd github-mcp-server
uv sync

配置

环境变量

变量 必填 描述
GITHUB_TOKEN 是 GitHub个人访问令牌 repo 范围
GITHUB_OWNER 否 所有操作的默认存储库所有者
GITHUB_REPO 否 所有操作的默认存储库名称

Claude代码配置

添加到MCP配置(~/.config/claude-code/mcp-config.json):

{
  "mcpServers": {
    "github-manager": {
      "type": "stdio",
      "command": "uvx",
      "args": ["github-mcp-server"],
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}",
        "GITHUB_OWNER": "your-username",
        "GITHUB_REPO": "your-repo"
      }
    }
  }
}

获取GitHub代币

  1. 首选https://github.com/settings/tokens
  2. 点击“生成新令牌(经典)”
  3. 选择范围: repo (完全访问存储库)
  4. 复制生成的令牌

可用工具

问题操作

工具 说明
create_issues 创建一个或多个问题(多个问题并行执行)
get_issue 获取完整的问题详细信息,包括正文
list_issues 查询过滤问题(状态、标签、里程碑、受让人)
close_issue 使用可选注释关闭问题

拉取请求操作

工具 说明
create_pr_with_content 使用结构化内容(问题、解决方案、变更)创建公关
get_pull_request 获取PR详细信息,包括可合并状态
update_pr 更新PR标题、正文、基础分支或州
merge_pr 将PR与方法选择合并(挤压/合并/重基)

CI/CD操作

工具 说明
check_ci_status 检查分支的工作流运行状态
get_ci_logs 获取调试失败作业的CI日志

批量操作

工具 说明
batch_update_issues 同时更新多个问题
batch_add_labels 为多个问题添加标签
batch_link_to_project GitHub项目链接问题(v2)

里程碑操作

工具 说明
create_milestone 创建具有可选截止日期的里程碑
list_milestones 列出具有过滤功能的里程碑

用法示例

创建问题

result = create_issues(
    issues=[{
        "title": "Implement feature X",
        "body": "## Description\n\nDetails here...",
        "labels": ["enhancement"],
        "milestone": 1
    }],
    owner="your-username",
    repo="your-repo"
)

检查CI状态

result = check_ci_status(
    branch="feature-branch",
    owner="your-username",
    repo="your-repo"
)

创建PR

result = create_pr_with_content(
    title="feat: add new feature",
    problem="Current behavior lacks X functionality",
    solution="Implement X using approach Y",
    key_changes="- Added X component\n- Updated Y module",
    issue=42,  # Optional: links to issue #42
    owner="your-username",
    repo="your-repo"
)

发展

先决条件

  • Python>=3.10
  • uv(推荐)或pip
  • GitHub个人访问令牌

设置

git clone https://github.com/rriesco/github-mcp-server.git
cd github-mcp-server
uv sync

运行测试

# Unit tests only (fast)
uv run pytest -m "not integration" -v

# Integration tests (requires GITHUB_TOKEN, TEST_OWNER, TEST_REPO)
uv run pytest -m integration -v

# All tests with coverage
uv run pytest --cov=src --cov-report=term-missing

类型检查

uv run mypy src/github_mcp_server --strict

建筑

Claude Code / MCP Client
      |
      | MCP Protocol (stdio)
      v
┌─────────────────────────────┐
│  Python FastMCP Server      │
│  - Tool Registry            │
│  - PyGithub Client          │
│  - Error Handling           │
│  - Type Validation          │
└─────────────┬───────────────┘
              |
              | PyGithub REST API
              v
        GitHub API

项目结构

github-mcp-server/
├── src/github_mcp_server/
│   ├── server.py              # Server entry point
│   ├── tools/
│   │   ├── issues.py          # Issue operations
│   │   ├── pulls.py           # PR operations
│   │   ├── ci.py              # CI operations
│   │   ├── milestones.py      # Milestone operations
│   │   └── batch_operations.py # Batch operations
│   ├── utils/
│   │   ├── github_client.py   # Singleton PyGithub client
│   │   ├── errors.py          # Structured error handling
│   │   └── types.py           # Type definitions
│   └── config/
│       └── defaults.py        # Environment-based defaults
└── tests/
    ├── test_*.py              # Unit tests
    └── integration/           # Integration tests

许可证

MIT许可证-请参阅 许可证 了解详情。

链接