内容
# github-mcp-server
一个手动构建的MCP服务器,封装了GitHub REST API。52种工具涵盖了日常使用GitHub的几乎所有操作。
## 工具
**身份验证**
- `get_authenticated_user`
**仓库**
- `list_my_repos`, `get_repo`, `search_repos`, `list_commits`
- `create_repo`, `update_repo`, `delete_repo` *(需要输入确认)*
**分支、标签、发布**
- `list_branches`, `get_branch`, `create_branch`
- `list_tags`, `list_releases`, `get_latest_release`, `create_release`
- `compare_commits`
**星标、关注、复刻**
- `star_repo`, `unstar_repo`, `list_starred_repos`
- `fork_repo`, `list_forks`
**问题**
- `list_issues`, `get_issue`, `create_issue`, `comment_on_issue`, `close_issue`
**标签和里程碑**
- `list_labels`, `create_label`, `add_labels_to_issue`
- `list_milestones`, `create_milestone`
**协作者**
- `list_collaborators`, `add_collaborator`
**拉取请求**
- `list_pull_requests`, `get_pull_request`, `create_pull_request`, `get_pull_request_diff_files`
- `merge_pull_request`, `list_pr_reviews`, `create_pr_review`, `request_pr_reviewers`
**搜索**
- `search_code`, `search_issues`, `search_users`
**代码片段**
- `list_my_gists`, `create_gist`
**GitHub Actions**
- `list_workflows`, `list_workflow_runs`, `trigger_workflow`
**文件**
- `get_file_contents`, `list_directory_contents`, `create_or_update_file`
## 1. 创建GitHub个人访问令牌
1. 前往 https://github.com/settings/tokens?type=beta (细粒度令牌)
2. 点击“生成新令牌”
3. 在“仓库访问”下,选择特定仓库或“所有仓库”
4. 在“权限”下,至少授予:
- 内容:读取和写入
- 问题:读取和写入
- 拉取请求:读取和写入
- 元数据:只读(通常自动需要)
5. 生成并复制令牌(以 `github_pat_` 或 `ghp_` 开头)—
您将无法再次看到它。
## 2. 配置服务器
在此文件夹中创建一个 `.env` 文件(永远不要提交此文件):
```
GITHUB_TOKEN=github_pat_xxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
## 3. 安装依赖项
```powershell
cd github-mcp-server
python -m venv venv
.\venv\Scripts\Activate.ps1
pip install -r requirements.txt
```
## 4. 独立测试
```powershell
mcp dev server.py
```
在检查器中,将命令设置为您的venv的python.exe,参数设置为
`server.py`(与我们在本地工具中使用的相同修复)。首先尝试 `get_authenticated_user`
,没有参数 - 它应该返回您的GitHub个人资料。
## 5. 连接到Claude Desktop
在Windows上,如果Claude Desktop是MSIX/Store版本,请在以下位置编辑配置:
```
%LOCALAPPDATA%\Packages\Claude_<id>\LocalCache\Roaming\Claude\claude_desktop_config.json
```
(不是文档中指定的 `%APPDATA%\Claude\` 路径 - 检查两者,并使用Claude Desktop实际读取的路径。参见下面的故障排除说明。)
将以下内容添加到 `mcpServers`(与文件中已有的内容合并):
```json
{
"mcpServers": {
"github-mcp-server": {
"command": "C:\\absolute\\path\\to\\github-mcp-server\\venv\\Scripts\\python.exe",
"args": ["C:\\absolute\\path\\to\\github-mcp-server\\server.py"]
}
}
}
```
完全重启Claude Desktop(在任务管理器中结束任务,然后重新打开),然后
通过聊天框中的“+”按钮 -> 连接器进行检查。
## 安全说明
- `.env` 被git忽略 - 永远不要提交您的令牌。
- 将令牌范围限定为仅您实际需要的仓库/权限。
- `create_or_update_file` 直接提交到您指定的分支 --
在调用工具之前,没有确认步骤,因此请像对待其他具有写入权限的工具一样谨慎。
- `delete_repo` 是永久且不可逆的。它要求您输入确切的仓库名称作为 `confirm_repo_name` --
这是对AI客户端可能错误调用的防范措施,而不是替代您仔细考虑要执行的操作。
## 扩展
这里的每个工具都遵循相同的模式:映射参数到GitHub REST
端点,调用 `_request()`,返回结果。GitHub的API文档
(https://docs.github.com/en/rest) 实质上是工具规范 - 要添加
新工具,请找到您要的端点并以相同的方式封装它,例如:
```python
@mcp.tool()
def list_branches(owner: str, repo: str) -> dict:
"""列出仓库中的分支。"""
return _request("GET", f"/repos/{owner}/{repo}/branches")
```