本项目借助 FastAPI 实现了一个 模型上下文协议(MCP)服务器,能让 AI 助手(如 Claude)与 GitHub 进行交互。服务器提供了一系列 API 端点,可用于获取用户信息、管理仓库、创建问题以及自动化 GitHub 工作流。
git clone https://github.com/snehaapratap/mcp-server.git
cd mcp-server
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
在根目录下创建一个 .env 文件,并添加你的 GitHub 个人访问令牌 (PAT):
GITHUB_TOKEN=your_personal_access_token
uvicorn server:app --reload
服务器将在 http://127.0.0.1:8000 上运行。
| 方法 | 端点 | 描述 |
|---|---|---|
| GET | / |
检查 MCP 服务器健康状态 |
| GET | /github/user |
获取 GitHub 用户详情 |
| GET | /github/repos |
获取认证用户的所有仓库 |
| POST | /github/create-issue |
在仓库中创建问题 |
| GET | /github/list-issues |
列出仓库中的所有问题 |
| PUT | /github/star-repo |
星标一个仓库 |
| POST | /github/summarize-issues |
带 AI 功能的问题摘要(可选) |
curl -X GET "http://127.0.0.1:8000/github/user"
curl -X GET "http://127.0.0.1:8000/github/repos"
curl -X POST "http://127.0.0.1:8000/github/create-issue" \
-H "Content-Type: application/json" \
-d '{"owner":"username", "repo":"repository", "title":"Issue Title", "body":"Issue Body"}'
curl -X GET "http://127.0.0.1:8000/github/list-issues?owner=username&repo=repository"
curl -X PUT "http://127.0.0.1:8000/github/star-repo" \
-H "Content-Type: application/json" \
-d '{"owner":"username", "repo":"repository"}'
如果出现错误,例如无效的 GitHub 令牌,服务器将返回以下格式的消息:
{
"error": "Invalid GitHub Token",
"message": "Please provide a valid GitHub OAuth token."
}
这有助于开发者快速识别和解决错误。
这个项目展示了如何使用 FastAPI 快速构建一个与 GitHub 交互的 API 服务器。通过配置环境变量和简单的命令,开发者可以轻松启动并运行该服务器,开始与 GitHub 进行交互。