GitHub GraphQL API服务器

一个提供GitHub GraphQL API访问的Model Context Protocol (MCP)服务器,允许执行任意GraphQL查询和变更。

  • by QuentinCody
  • Python
  • 本地部署
  • 13 星标
  • 更新于
在 GitHub 查看 QuentinCody/github-graphql-mcp-server

使用说明

GitHub GraphQL MCP服务器

模型上下文协议(MCP)服务器,提供对GitHub的GraphQL API的访问。该服务器公开了一个单一的工具,允许针对GitHub的API执行任意GraphQL查询和突变。

特性

  • 针对GitHub的API执行任何GraphQL查询
  • 全面的错误处理和报告
  • 带有示例查询的详细文档
  • 支持GraphQL操作中的变量

先决条件

  • Python 3.10或更高版本
  • GitHub个人访问令牌(PAT)

安装

  1. 克隆此存储库
  2. 设置虚拟环境(推荐):
    # On macOS/Linux
    python3 -m venv .venv
    source .venv/bin/activate
    
    # On Windows
    python -m venv .venv
    .venv\Scripts\activate
    
  3. 安装依赖项:
    pip install -r requirements.txt
    

用法

运行服务器

# If using a virtual environment, make sure it's activated
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Run the server with your GitHub token
GITHUB_TOKEN=your_github_token_here python github_graphql_mcp_server.py

使用Claude for Desktop进行配置

将以下内容添加到您的Claude Desktop配置文件中:

{
  "github-graphql": {
    "command": "/absolute/path/to/your/.venv/bin/python",
    "args": [
        "/absolute/path/to/github_graphql_mcp_server.py"
    ],
    "options": {
        "cwd": "/absolute/path/to/repository"
    },
    "env": {
        "GITHUB_TOKEN": "your_github_token_here"
    }
  }
}

替换 /absolute/path/to/ 使用服务器文件的实际路径并添加GitHub令牌。

查询示例

获取存储库信息

query GetRepo($owner: String!, $name: String!) {
  repository(owner: $owner, name: $name) {
    name
    description
    stargazerCount
    url
    createdAt
    owner {
      login
      avatarUrl
    }
  }
}

变量:

{
  "owner": "octocat",
  "name": "Hello-World"
}

搜索存储库

query SearchRepos($query: String!, $first: Int!) {
  search(query: $query, type: REPOSITORY, first: $first) {
    repositoryCount
    edges {
      node {
        ... on Repository {
          name
          owner { login }
          description
          stargazerCount
          url
        }
      }
    }
  }
}

变量:

{
  "query": "language:python stars:1000",
  "first": 5
}

获取用户信息

query GetUserInfo($login: String!) {
  user(login: $login) {
    name
    login
    bio
    avatarUrl
    followers {
      totalCount
    }
    repositories(first: 5, orderBy: {field: STARGAZERS, direction: DESC}) {
      nodes {
        name
        description
        stargazerCount
      }
    }
  }
}

变量:

{
  "login": "octocat"
}

GitHub API速率限制

请注意GitHub的API速率限制:

  • 经过身份验证的请求:每小时5000个请求
  • 未经身份验证的请求:每小时60个请求

故障排除

如果您遇到问题:

  1. 检查您的GitHub令牌是否具有正确的权限
  2. 验证您的虚拟环境是否已正确设置和激活
  3. 确保您的令牌在环境变量中设置正确
  4. 如果使用Claude Desktop,请确保Python的路径正确(使用虚拟环境Python的绝对路径)
  5. 查看服务器日志中的错误消息
  6. 确保你的GraphQL查询对GitHub的架构有效
  7. 更改配置后重新启动Claude for Desktop

常见错误

spawn python ENOENT

  • 此错误表示找不到Python可执行文件
  • 解决方案:在虚拟环境中使用Python可执行文件的完整路径(例如。, /path/to/your/.venv/bin/python)

ModuleNotFoundError: No module named 'httpx' (或其他包装)

  • Python环境没有安装所需的依赖项
  • 解决方案:确保您已激活虚拟环境并运行 pip install -r requirements.txt

Error: GitHub token not found in environment variables

  • 服务器找不到您的GitHub令牌

  • 解决方案:确保已设置GITHUB_TOKEN环境变量

许可证

麻省理工学院