Skip to main content
Glama
sweta2503

GitHub Analytics MCP Server

by sweta2503

GitHub Analytics MCP Server

一个用于 GitHub 分析的生产级 Model Context Protocol (MCP) 2.x 服务器

本项目超越基础 MCP 教程,演示了如何使用你在实际应用中真正需要的工程模式来构建 MCP 服务器:

  • 异步 HTTP

  • 连接池

  • 显式超时

  • TTL 缓存

  • GitHub 速率限制感知

  • 带指数退避的重试

  • 结构化日志

  • 输入验证

  • 并行 API 请求

  • 清晰的 MCP 生命周期管理

  • 真实的端到端 MCP 协议测试

本项目是 Agentic Data LabProduction AI Engineering 系列的一部分。

🎥 YouTube: Agentic Data Lab


这个 MCP 服务器做什么?

该服务器通过 MCP 工具提供 GitHub 仓库分析功能。

兼容 MCP 的 AI 客户端可以使用它来:

  • 查看仓库元数据

  • 获取最近的提交

  • 分析贡献者

  • 查看未关闭的 issue

  • 分析提交活动

  • 比较两个仓库

  • 查看 GitHub API 速率限制

示例:

User:
Compare pallets/flask and django/django.

Which repository looks more active?

AI 客户端可以调用:

compare_repos

并通过此 MCP 服务器获取实时 GitHub 数据。


架构

                ┌─────────────────────┐
                │   MCP Client / AI   │
                │ Claude / MCP Client │
                └──────────┬──────────┘
                           │
                           │ MCP stdio
                           ▼
                ┌─────────────────────┐
                │ GitHub Analytics    │
                │     MCP Server      │
                └──────────┬──────────┘
                           │
                 ┌─────────┴─────────┐
                 │                   │
                 ▼                   ▼
          Input Validation       TTL Cache
                                     │
                           ┌─────────┴─────────┐
                           │                   │
                      CACHE HIT          CACHE MISS
                           │                   │
                           │                   ▼
                           │          Async HTTP Client
                           │                   │
                           │          Retry + Backoff
                           │                   │
                           │                   ▼
                           │            GitHub REST API
                           │                   │
                           └───────────◄───────┘
                                       │
                                       ▼
                               Structured MCP Result

已实现的 7 种生产级模式

1. 异步 HTTP + 连接池

服务器使用:

httpx.AsyncClient

而不是同步 HTTP 请求。

HTTP 客户端在 MCP 服务器生命周期内只创建一次,并在各工具调用之间复用。

这提供了:

  • 非阻塞 I/O

  • 连接复用

  • 更好的并发性

  • 显式超时控制

服务器为以下场景配置了各自的超时时间:

connect
read
write
pool

Related MCP server: ship-it-mcp

2. TTL 缓存

AI 客户端可能在一次对话中多次调用同一个 MCP 工具。

服务器不会每次都请求 GitHub,而是在内存中缓存响应。

示例:

First request

MCP Client
    │
    ▼
MCP Server
    │
    ▼
GitHub API
    │
    ▼
Cache

第二次请求:

MCP Client
    │
    ▼
MCP Server
    │
    ▼
CACHE HIT

无需再次发起 GitHub 请求。

不同工具根据其数据变化的快慢使用不同的 TTL。

工具

缓存 TTL

get_repo_overview

5 分钟

list_recent_commits

2 分钟

get_contibutors

10 分钟

list_open_issues

2 分钟

get_commit_activity

30 分钟

compare_repos

5 分钟

get_rate_limit_status

30 秒


3. GitHub 速率限制感知

GitHub 通过响应头暴露速率限制信息。

服务器会跟踪:

X-RateLimit-Limit
X-RateLimit-Used
X-RateLimit-Remaining
X-RateLimit-Reset
Retry-After

服务器可以检测 GitHub 是否真的在限制请求,并返回有用的 MCP 工具错误,而不是暴露原始异常。


4. 重试 + 指数退避

瞬时网络故障和上游 5xx 响应会自动重试。

重试序列:

Attempt 1
   │
   └── failure
        │
        ▼
      wait 1s

Attempt 2
   │
   └── failure
        │
        ▼
      wait 2s

Attempt 3
   │
   └── final result

退避公式为:

2 ** (attempt - 1)

服务器不会盲目重试正常的 4xx 客户端错误。


5. 结构化日志

MCP stdio 使用 stdout 进行协议通信。

因此,操作日志通过 Python logging 单独写入。

示例:

2026-08-27T14:14:03 | INFO | Starting GitHub Analytics MCP server
2026-08-27T14:14:04 | INFO | GET /repos/facebook/react → 200
2026-08-27T14:14:04 | INFO | CACHE HIT /repos/facebook/react

这样可以方便地查看:

  • API 请求

  • HTTP 状态

  • 延迟

  • 重试尝试

  • 缓存命中

  • 验证失败

  • 速率限制警告


6. 输入验证

在发起任何网络请求之前,会对仓库所有者和仓库名称进行验证。

有效名称可以包含:

letters
numbers
.
-
_

例如:

face../../book

会在本地被拒绝,而不会成为 GitHub API 请求的一部分。


7. 并行 API 请求

compare_repos 这个 MCP 工具需要来自两个独立仓库的信息。

它不是按顺序获取:

repo_a = await get_repo_a()
repo_b = await get_repo_b()

而是让两个请求并发运行:

repo_a, repo_b = await asyncio.gather(
    get_repo_a(),
    get_repo_b(),
)

概念上:

Sequential

Repo A ───────────────► Done
                        Repo B ───────────────► Done


Parallel

Repo A ───────────────► Done
Repo B ───────────────────► Done

当请求相互独立时,这样可以减少等待时间。


可用的 MCP 工具

服务器当前提供 7 个 MCP 工具

get_repo_overview

返回:

  • stars(星标数)

  • forks(复刻数)

  • open issues(未关闭的 issue 数)

  • watchers(关注者数)

  • language(语言)

  • topics(主题)

  • licnse(许可证)

  • last push date(最后推送日期)

  • homepage(主页)

  • repository size(仓库大小)

示例:

get_repo_overview(
    owner="facebook",
    repo="react"
)

list_recent_commits

返回仓库最近的提交。

示例:

list_recent_commits(
    owner="vuejs",
    repo="core",
    limit=5
)

get_contibutors

返回仓库的主要贡献者。

示例:

get_contributors(
    owner="django",
    repo="django",
    limit=10
)

list_open_issues

返回 GitHub 上未关闭的 issue,并排除拉取请求。

示例:

list_open_issues(
    owner="pallets",
    repo="flask",
    limit=10
)

get_commit_activity

返回仓库的提交活动,包括:

  • 总提交数

  • 每周平均提交数

  • 峰值活动

  • 最近每周活动


compare_repos

并排比较两个仓库。

示例:

compare_repos(
    owner1="pallets",
    repo1="flask",
    owner2="django",
    repo2="django"
)

返回的字段包括:

stars
forks
open issues
language
last push

get_rate_limit_status

返回 GitHub API 速率限制信息以及本地 MCP 服务器计数器。

示例:

{
  "limit": 60,
  "used": 4,
  "remaining": 56,
  "resets_in_seconds": 3599,
  "server_outbound_http_requests": 4,
  "server_cache_hits": 1
}

实际值取决于你当前的 GitHub API 使用情况。


项目结构

mcp-github-analytics/
│
├── server.py
│   └── Main MCP server and GitHub tools
│
├── demo_mcp.py
│   └── Real end-to-end MCP client demo
│
├── requirements.txt
│   └── Python dependencies
│
├── .env.example
│   └── Environment variable template
│
└── .gitignore

设置

1. 克隆仓库

git clone https://github.com/sweta2503/mcp-github-analytics.git

进入项目目录:

cd mcp-github-analytics

2. 创建虚拟环境

python -m venv .venv

m/cOS / Linux

source .venv/bin/activate

Windows

.venv\Scripts\activate

3. 安装依赖

pip install -r requirements.txt

项目使用:

mcp[cli]==2.1.1
httpx==0.28.1
python-dotenv==1.2.3

GitHub Token 设置

如果只使用公共仓库,GitHub token 是可选的,但建议使用。

复制示例环境文件:

cp .env.example .env

添加你的 GitHub token:

GITHUB_TOKEN=your_github_token_here

不要提交你的真实 .env 文件或 token。


运行真实的 MCP 演示

运行:

python demo_mcp.py

这是一个真实的 MCP 端到端测试。

demo_mcp.py 并非简单地导入 server.py 中的函数。

相反,它会:

1. Starts server.py as an MCP subprocess
2. Connects using MCP stdio
3. Negotiates the MCP protocol
4. Discovers the MCP tools
5. Calls the tools through MCP
6. Receives structured MCP responses

你应该会看到类似下面的输出:

MCP CONNECTED — discover the real server tools

Negotiated protocol: ...
Tools discovered (7):
get_repo_overview
list_recent_commits
get_contributors
list_open_issues
get_commit_activity
compare_repos
get_rate_limit_status

测试缓存

演示脚本会调用:

get_repo_overview(facebook/react)

两次。

第一次调用会请求 GitHub。

第二次应该显示:

CACHE HIT

并且返回速度明显更快。


测试并行仓库比较

演示脚本还会运行:

compare_repos(
    pallets/flask,
    django/django
)

两个上游 GitHub 请求通过以下方式并发发出:

asyncio.gather(...)

捕获演示输出和服务器日志

你可以分别捕获 MCP 客户端输出和服务器日志:

python demo_mcp.py > demo_output.txt 2> server.log

这会创建:

demo_output.txt

用于 MCP 客户端响应;以及:

server.log

用于服务器端日志。

服务器日志包含有用的信息,例如:

GET /repos/facebook/react → 200
CACHE HIT /repos/facebook/react
GET /repos/pallets/flask → 200
GET /repos/django/django → 200

直接运行服务器

你可以通过以下命令启动 MCP 服务器本身:

python server.py

服务器通过 MCP stdio 运行。

通常情况下,兼容 MCP 的客户端会自动启动该进程。


将服务器连接到 Claude Desktop

你无需在此仓库中保留特定于机器的 claude_desktop_config.json

相反,请将服务器添加到本地 Claude Desktop 配置中。

示例:

{
  "mcpServers": {
    "github-analytics": {
      "command": "/ABSOLUTE/PATH/TO/mcp-github-analytics/.venv/bin/python",
      "args": [
        "/ABSOLUTE/PATH/TO/mcp-github-analytics/server.py"
      ],
      "env": {
        "GITHUB_TOKEN": "YOUR_GITHUB_TOKEN"
      }
    }
  }
}

替换:

/ABSOLUTE/PATH/TO/mcp-github-analytics

为你计算机上实际的项目位置。

切勿提交你真实的 GitHub token。

重启 Claude Desktop 后,GitHub 分析工具应该对 Claude 可用。

示例提示词:

Compare pallets/flask and django/django.

Which repository appears more active?

Use the GitHub MCP tools and explain which data you used.

端到端请求流程

User
 │
 ▼
Claude / MCP Client
 │
 │ MCP tool call
 ▼
GitHub Analytics MCP Server
 │
 ├── Validate input
 │
 ├── Check TTL cache
 │
 ├── Cache hit ──────────────► Return result
 │
 └── Cache miss
          │
          ▼
     Async HTTP
          │
     Retry / Backoff
          │
          ▼
     GitHub REST API
          │
          ▼
       Response
          │
          ▼
       TTL Cache
          │
          ▼
 Structured MCP Response
          │
          ▼
     AI / MCP Client

本地与分布式生产环境下的 MCP

本项目特意使用内存 TTL 缓存,因为它是作为清晰的本地/stdio MCP 示例而设计的。

对于多实例远程 MCP 部署,你通常会用以下基础设施替换进程本地状态:

Redis
PostgreSQL
distributed rate limiting
centralized observability
authentication
tracing

本仓库演示的这些模式是进入下一阶段的基础构件。


观看完整构建过程

我会在我的 YouTube 频道上讲解架构、代码、缓存、重试逻辑、验证、并行请求以及真实的 MCP 演示:

️ Agentic Data Lab

https://www.youtube.com/@agenticdatalab

在频道上,我会介绍:

  • Production AI Engineering

  • MCP

  • AI 智能体

  • LangGraph

  • RAG

  • AI 评估

  • Agent 可观测性

  • AI 系统设计

  • 数据工程 + AI

  • 生产级基准测试与实验

如果你有兴趣构建超越教程演示的 AI 系统,请考虑订阅。

👉 YouTube: Agentic Data Lab


贡献

欢迎提交 issue、改进建议和拉取请求。

如果你用另一个有用的 GitHub 分析工具扩展了这个 MCP 服务器,欢迎提交 PR。


支持本项目

如果这个仓库对你有帮助:

  • ⭐ 给仓库加 Star

  • 🍴 Fork 它并构建你自己的 MCP 工具

  • ▶️ 订阅 Agentic Data Lab

更多生产级 AI 工程项目即将推出。

Maintenance

ActivityMaintained
ResponsivenessSyncing

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    C
    maintenance
    Enables to interact with GitHub repositories directly from Claude, supporting actions like viewing repos, checking status, committing and pushing changes, and managing pull requests.
  • F
    license
    A
    quality
    D
    maintenance
    Enables Claude to access and manage GitHub repositories dynamically at runtime, including private repos, with tools for browsing files, searching code, and viewing commits, pull requests, and issues.
    11
    1

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sweta2503/mcp-github-analytics'

If you have feedback or need assistance with the MCP directory API, please join our Discord server