使用说明
GitHub-MCP
GitHub MCP 服务器(Rust,stdio JSON-RPC)。
快速入门:
- 先决条件:Rust 1.90.0+,cargo;已设置
GITHUB_TOKEN或者GH_TOKEN。 - 构建:
cargo build - 运行(stdio JSON-RPC,NDJSON 框架):
- 初始化
echo '{"jsonrpc":"2.0","method":"initialize","id":1}' | cargo run -- --log-level warn
- 工具列表
echo '{"jsonrpc":"2.0","method":"tools/list","id":2}' | cargo run -- --log-level warn
- Ping 工具(可选)
- 直接JSON-RPC方法
ping已被移除。使用tools/call和;与GITHUB_MCP_ENABLE_PING=true如果你需要健康检查。 - 示例(启用时):
echo '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"ping","arguments":{"message":"hello"}},"id":3}' | cargo run -- --log-level warn- 回复随MCP工具结果信封一同附上
content并且structuredContent。
- 回复随MCP工具结果信封一同附上
- 直接JSON-RPC方法
- 初始化
Inspector CLI(命令行界面)
- 您可以使用MCP Inspector进行端到端验证:
npx @modelcontextprotocol/inspector-cli --cli ./target/release/github-mcp --method initializenpx @modelcontextprotocol/inspector-cli --cli ./target/release/github-mcp --method tools/list- 期待
result.protocolVersion以及MCP工具的结果信封(或结果集合)与content和structuredContent。
配置
- 代币:
GITHUB_TOKEN(备用方案GH_TOKEN)。 - 终点(或:研究终点):
GITHUB_API_URL(默认 https://api.github.com),GITHUB_GRAPHQL_URL(默认 https://api.github.com/graphql)。 - API版本头部:
GITHUB_API_VERSION(默认 2022-11-28)。 - HTTP 超时:
GITHUB_HTTP_TIMEOUT_SECS(默认值为30)。 - 用户代理(User-Agent):
github-mcp/<version>(自动设置)。 - 特性标志:
GITHUB_MCP_ENABLE_PING当为真值时(1/true/yes/on), 内置的ping工具已列出且可调用。默认关闭;当禁用时,tools/call ping返回 JSON-RPC 错误-32601并且tools/list省略ping。
与MCP客户端一起使用
假设
- 你构建或安装了二进制文件,并且它已添加到系统路径(PATH)中,作为
github-mcp。 - 通过环境变量设置身份验证令牌:
GITHUB_TOKEN(或GH_TOKEN)。 - 服务器默认使用stdio;无需子命令。
Claude Desktop(可译为“Claude桌面版”或保持原样,若“Desktop”在此处特指某种软件或平台的桌面版本,则可根据具体语境灵活翻译)
- 配置文件路径
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json - Linux(社区构建版):
~/.config/Claude/claude_desktop_config.json
- macOS:
- 配置片段(粘贴到
claude_desktop_config.json)
{
"mcpServers": {
"github-mcp": {
"command": "github-mcp",
"args": [],
"env": {
"GITHUB_TOKEN": "${env:GITHUB_TOKEN}"
}
}
}
}
- 重新加载:完全退出 Claude 桌面应用(Cmd+Q/Alt+F4),然后重新启动。
- 文档链接:https://modelcontextprotocol.io/docs/develop/connect-local-servers(中文可表述为:“文档:https://modelcontextprotocol.io/docs/develop/connect-local-servers”)
- 注:在 macOS 上,您可能需要在“隐私与安全性”中允许未签名的二进制文件。
光标
- 配置文件路径
- 项目:
<project>/.cursor/mcp.json - 全球:
~/.cursor/mcp.json(Windows:%USERPROFILE%\\.cursor\\mcp.json)
- 项目:
- 配置片段(项目或全局)
mcp.json)
{
"mcpServers": {
"github-mcp": {
"type": "stdio",
"command": "github-mcp",
"args": [],
"env": {
"GITHUB_TOKEN": "${env:GITHUB_TOKEN}"
}
}
}
}
- 重新加载:如果工具未显示,请关闭并重新打开光标。
- 文档链接:https://cursor.com/docs/context/mcp(中文可表述为:“文档地址:https://cursor.com/docs/context/mcp”)
- 注:优先使用PATH解析的(路径)
github-mcp; 如果在Windows上使用包含空格的绝对路径,请确保使用正确的引号。
Continue.dev(适用于VS Code/JetBrains)
- 全局配置
- macOS/Linux:
~/.continue/config.yaml - Windows:
%USERPROFILE%\\.continue\\config.yaml
- macOS/Linux:
- 工作区范围(推荐):添加
.continue/mcpServers/github-mcp.yaml在你的项目中。 - 工作区YAML文件(推荐)
mcpServers:
- name: github-mcp
command: github-mcp
args: []
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- 或者在下方添加相同的区块
mcpServers:在你的全球范围内~/.continue/config.yaml. - 重新加载:打开继续选项,切换到代理模式以使用MCP工具;如有需要,请点击“重新加载配置”。
- 文档链接:https://docs.continue.dev/customize/deep-dives/mcp(中文表述为:文档:https://docs.continue.dev/customize/deep-dives/mcp)
- 注:将您的令牌存储在
~/.continue/.env(或工作区.continue/.env) 作为GITHUB_TOKEN以避免泄露秘密。
VS Code Copilot Chat(MCP)
- 配置位置
- 工作区:
.vscode/mcp.json - 用户:使用命令面板 -> “MCP: 打开用户配置”(创建
mcp.json(在你的 VS Code 用户文件夹下)- macOS:
~/Library/Application Support/Code/User/mcp.json - Linux:
~/.config/Code/User/mcp.json - Windows:
%APPDATA%\Code\User\mcp.json
- macOS:
- 工作区:
- 工作区
.vscode/mcp.json示例
{
"servers": {
"github-mcp": {
"command": "github-mcp",
"args": [],
"env": {
"GITHUB_TOKEN": "${input:github_token}"
}
}
},
"inputs": [
{
"type": "promptString",
"id": "github_token",
"description": "GitHub token for github-mcp",
"password": true
}
]
}
- 可选:通过 settings.json 启用对 Claude Desktop 配置的发现功能:
{ "chat.mcp.discovery.enabled": true }。 - 重新加载:保存
mcp.json然后,点击编辑器中的“开始”代码片段,或者使用命令面板 -> “MCP: 启动服务器”。打开Copilot聊天窗口,并切换到代理模式。 - 文档链接:https://docs.github.com/copilot/customizing-copilot/using-model-context-protocol/extending-copilot-chat-with-mcp(译文:GitHub Copilot 文档:自定义 Copilot/使用模型上下文协议/通过 MCP 扩展 Copilot 聊天功能)
服务器调用和认证(适用于所有客户端)
- 命令:
github-mcp - 参数:无需任何参数(stdio 为默认设置)。可选择添加
--log-level warn. - 环境:设置
GITHUB_TOKEN(备用方案:GH_TOKEN)。 示例(macOS/Linux):export GITHUB_TOKEN=ghp_...示例(Windows PowerShell):setx GITHUB_TOKEN "ghp_..."并重新启动你的Shell。
使用示例
-
问题 → 列出问题
echo '{"jsonrpc":"2.0","method":"tools/call","id":11,"params":{"name":"list_issues","arguments":{"owner":"octo","repo":"hello","limit":10,"include_author":true}}}' | cargo run -- --log-level warn
-
拉取请求 → 获取拉取请求
echo '{"jsonrpc":"2.0","method":"tools/call","id":12,"params":{"name":"get_pull_request","arguments":{"owner":"octo","repo":"hello","number":1}}}' | cargo run -- --log-level warn
-
拉取请求 → 列出轻量级的PR文件(包含补丁)
echo '{"jsonrpc":"2.0","method":"tools/call","id":13,"params":{"name":"list_pr_files_light","arguments":{"owner":"octo","repo":"hello","number":1,"per_page":50,"page":1,"include_patch":true}}}' | cargo run -- --log-level warn
-
操作 → 获取工作流作业日志(显示最后几行 + 时间戳)
echo '{"jsonrpc":"2.0","method":"tools/call","id":14,"params":{"name":"get_workflow_job_logs","arguments":{"owner":"octo","repo":"hello","job_id":123456,"tail_lines":200,"include_timestamps":true}}}' | cargo run -- --log-level warn
分页
- GraphQL:使用
cursor和limit(最多100个)。回复包括meta.next_cursor和meta.has_more. - REST:服务器使用Link头部来检测
has_more并返回一个不透明的对象next_cursor编码{page, per_page}(base64 URL安全编码)。客户端可以传递cursor< 返回page/per_page在某些工具上也被接受。
错误模型
- 在失败时,响应包括
error包含字段:code(例如。,bad_request,unauthorized,forbidden,not_found,conflict,rate_limited,upstream_error,server_error)和retriable(对429/5xx状态码适用)。 meta.rate通过REST头部信息和GraphQL进行填充rateLimit(当其)存在时。
注释
- 请参阅docs/methods.md以获取权威的工具输入/输出信息;本服务器实现了这些格式。
- 待办事项:
- GraphQL(一种查询语言和运行时用于API)
rateLimit在可行的情况下,会将其包含在内而不使查询复杂化;某些查询可能仍然会省略它。 - REST 分页依赖于 Link 头;当 GitHub 对于小结果集省略 Link 时,
has_more可能为假,无(特定条件或结果)next_cursor。
- GraphQL(一种查询语言和运行时用于API)
输出整形
- 默认行为旨在实现最小载荷:
- 当结果未分页时,分页字段会被隐藏,并且
meta对象可以完全省略。 - 默认情况下,速率限制元数据会被排除。
- 当结果未分页时,分页字段会被隐藏,并且
- 每次调用时添加的可选加入率元数据
_include_rate: true到顶级工具参数。当设置时,meta.rate已包含;分页键仅在相关时包含。
示例
- 请求(含费率):
{"jsonrpc":"2.0","method":"tools/call","id":1,
"params":{"name":"list_workflow_runs_light",
"arguments":{"owner":"octo","repo":"hello","per_page":30,"_include_rate":true}}}
- 回复(节选):
{
"result": {
"content": [{"type":"text","text":"0 workflow runs"}],
"structuredContent": {
"items": [],
"meta": {
"has_more": true,
"next_cursor": "...",
"rate": {"remaining": 4999, "used": 1, "reset_at": "..."}
}
}
}
}
当 _include_rate 被省略或 false, meta.rate 被省略了。如果 has_more 是错误的, has_more 并且 next_cursor 也被省略了,并且 meta 可能会被完全移除。
MCP响应信封(重大变更)
- 工具/调用结果已封装:
content包含一个元素的数组{type:"text", text:"..."}为便于人类查看而设计的模块/区块。structuredContent为程序化客户端保留了之前的结构化JSON有效载荷。isError现在和true当包含一个工具级别的错误时structuredContent.error。
- 示例(ping,当启用时):
{
"jsonrpc":"2.0",
"id":3,
"result":{
"content":[{"type":"text","text":"hello"}],
"structuredContent":{"message":"hello"}
}
}
- 程序化客户端应切换至读取模式
result.structuredContent并且对待result.content[0].text作为显示提示。