GitHub JSON-RPC服务

一个基于Rust的GitHub JSON-RPC服务器,提供GitHub API的标准化访问接口,适用于开发工具集成和自动化工作流。

  • by HautechAI
  • Rust
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 HautechAI/github-mcp

使用说明

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。

Inspector CLI(命令行界面)

  • 您可以使用MCP Inspector进行端到端验证:
    • npx @modelcontextprotocol/inspector-cli --cli ./target/release/github-mcp --method initialize
    • npx @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
  • 配置片段(粘贴到 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
  • 工作区范围(推荐):添加 .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
  • 工作区 .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。

输出整形

  • 默认行为旨在实现最小载荷:
    • 当结果未分页时,分页字段会被隐藏,并且 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 作为显示提示。