GitHub MCP Server 接入指南
一步步配置 GitHub 官方 MCP Server,让 AI 助手直接读写 Issue、PR、代码搜索,告别复制粘贴 URL。
GitHub MCP Server 接入指南
Model Context Protocol(MCP)让 AI 助手能像调本地工具一样调用远端服务。GitHub 官方 MCP Server 把仓库、Issue、PR、代码搜索全部暴露给你的 LLM,使 AI 编程从「贴链接」走向「直接动手」。
你将获得
- AI 直接列出/创建/评论 Issue;
- AI 自助搜索仓库代码(基于 GitHub 的代码搜索 API);
- AI 拉取 PR diff 并给出 review;
- AI 自动维护项目看板。
前置条件
| 项 | 要求 |
|---|---|
| Node.js | ≥ 18 |
| GitHub Token | scopes:repo, read:org, workflow |
| 客户端 | Claude Desktop / Cursor / Hermes Agent 等支持 MCP 的客户端 |
1. 生成 GitHub Token
进入 github.com/settings/tokens → New token (classic):
- Note:
mcp-server - Expiration:90 days(建议)
- Scopes:
repo、read:org、workflow
复制生成的 token,下文记作 $GH_TOKEN。
2. 安装 MCP Server
npm install -g @modelcontextprotocol/server-github
也可以不全局安装、用 npx 直接拉取:
{
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"]
}
3. 配置客户端
以 Claude Desktop 为例,编辑 ~/Library/Application Support/Claude/claude_desktop_config.json(macOS):
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxxxxxxxxxxxxxxxxxxx"
}
}
}
}
Hermes Agent 用户:把同样的块写到
~/.hermes/config.yaml的mcp_servers下即可。
4. 验证连接
重启客户端,向 AI 发:
列出仓库 owner/repo 最近 5 个 open 的 PR,把标题和作者列出来。
正常应直接得到结果。如果报权限错误,回到第 1 步检查 token scopes。
常见问题
报 401:token 失效或没勾对 scope;重新生成。
报 403 rate limit:GitHub API 默认每小时 5000 次。可降低自动调用频率,或申请 GitHub App 凭证。
只能看公开仓库:token 没有 repo scope,只勾了 public_repo —— 改全。
进阶
- 把 token 写进密码管理器再用
op read/pass show注入,避免明文 config; - 多账号场景:为不同组织各起一个 server 实例(
github-personal/github-work)。