使用说明
mcp_github
MCP+语言图+流媒体启动器 –构建和使用MCP工具(GitHub+问候语)的最小堆栈,带有LangGraph代理和Streamlit聊天UI。
✨ 里面是什么?
- MCP服务器(stdio) 暴露:
greet–简单的问候工具gh_create_issue–创建GitHub问题gh_open_pull_request–打开拉取请求gh_list_open_issues–列出存储库问题(过滤PR)
- LangGraph代理 自动加载MCP工具并通过路由呼叫
tools_condition - 流线型UI 对于开发人员友好的聊天和工具运行者
- 原始MCP客户端 用于快速理智测试
📦 1) 安装依赖项
python -m venv .venv
# macOS/Linux
source .venv/bin/activate
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
如果你看到
ModuleNotFoundError: No module named 'mcp',确保你的virtualenv是 激活的 并且依赖项安装在该环境中。
🔐 2) 配置密钥
复制 .env.example 到 .env 并设置所需的变量。
cp .env.example .env
.env
# OpenAI (used by the agent)
OPENAI_API_KEY=sk-...
# Optional: override model (default: gpt-4o-mini)
OPENAI_MODEL=gpt-4o-mini
# GitHub (used server-side by GH tools)
# For public repos: 'public_repo' scope is enough; for private repos: 'repo' scope.
GITHUB_TOKEN=ghp_your_token_here
▶️ 3) 运行MCP服务器(可选)
如果你想单独测试服务器,可以直接运行它:
python mcp_server/mcp_server.py
客户端/代理/Streamlit脚本将 自动产卵 如果服务器尚未运行,则通过stdio连接。
🧪 4) 测试原始MCP客户端
python client/client.py
它的作用:
- 列出可用工具
- 呼叫
greet - 列出未决问题
- (可选)创建问题
预期控制台示例:
// tools //
- greet
- gh_create_issue
- gh_open_pull_request
- gh_list_open_issues
// greet //
Hello, Rohit! 👋
// gh_list_open_issues //
[ ... JSON list of issues ... ]
// gh_create_issue //
{
"number": 42,
"url": "https://github.com/rohit3773/mcp_github/issues/42",
"id": 1234567890,
"state": "open"
}
🤖 5) 运行LangGraph聊天循环
python agent/run_agent.py
您可以尝试的示例提示:
Say hello to Rohit using the greet tool.List open issues in rohit3773/mcp_github (limit 20).Create a GitHub issue in rohit3773/mcp_github titled "Dark mode theme breaks on settings page" with body "When switching to dark mode, some text remains black and becomes invisible" and labels ui, theme.
代理动态绑定MCP工具,并使用
tools_condition。您添加到服务器的任何新MCP工具都会立即被代理使用。
🖥️ 6) 启动Streamlit用户界面
streamlit run ui/streamlit_app.py
打开所示的URL(例如。,http://localhost:8501).
流线型UI截图
将这些文件放在
docs/screenshots/在README中呈现。
docs/screenshots/streamlit_home.png–聊天登录屏幕docs/screenshots/streamlit_tool_call.png–当代理调用MCP工具时docs/screenshots/streamlit_issue_list.png–成功创建GitHub问题
🧱 项目结构
.
├─ agent/
│ ├─ run_agent.py # LangGraph loop (loads MCP tools via stdio)
├─ client/
│ ├─ client.py # Raw MCP client for quick tests
├─ mcp_server/
│ ├─ mcp_server.py # MCP server (greet + GitHub tools)
├─ tests/
│ ├─ test_sample # Examples to test
├─ streamlit_app.py # Streamlit chat UI
├─ docs/
│ └─ screenshots/ # Place images used in README
├─ .env.example
├─ requirements.txt
└─ README.md
🧩 MCP服务器——可用工具
-
greet(name: str) -> str
致以友好的问候。 -
gh_create_issue(owner, repo, title, body="", labels=None, assignees=None) -> json
创建GitHub问题。需要GITHUB_TOKEN在服务器中.env. -
gh_open_pull_request(owner, repo, head, base, title, body="", draft=False, maintainer_can_modify=True) -> json
从打开拉取请求head进入base. -
gh_list_open_issues(owner, repo, labels=None, assignee=None, limit=50) -> json[]
列表 问题 (过滤掉PR)。支持labels,assignee(或"*"),以及limit(最多100)。
备注
- 工具使用
https://api.github.com随着Accept: application/vnd.github+json和API版本2022-11-28. - 错误以气泡形式显示,包括HTTP状态和错误有效载荷在内的有用消息。
🏗️ 架构(高级)
User ↔ Streamlit UI
│
▼
LangGraph Agent
(ChatOpenAI + tools_condition)
│ (stdio)
▼
MCP Server
(greet + GitHub tools)
│
▼
GitHub API
🧪 示例:cURL健全性检查问题(可选)
curl -H "Authorization: Bearer $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" "https://api.github.com/repos/rohit3773/mcp_github/issues?state=open&per_page=10"
GitHub
/issues端点返回问题和PR——您的MCP服务器会自动过滤掉PR。
💡 使用示例(复制粘贴)
创建问题
- 在rohit3773/mcp_GitHub中创建一个GitHub问题,标题为“设置页面上的黑暗模式主题中断”,正文为“切换到黑暗模式时,一些文本保持黑色并变得不可见”,并添加标签ui和theme
- “在rohit3773/mcp_github中打开一个题为“登录时API超时”的新问题,正文为“登录API在30秒后间歇性返回504网关超时”,并添加标签后端和bug。”
- “在repo rohit3773/mcp_github中,创建一个标题为“更新安装文档”的问题,正文为“文档没有提到Python 3.11要求”,并添加标签文档。”
打开PR
- “在rohit3773/mcp_github中打开一个pull请求,从branch feature/add-logging到main,标题为'添加结构化日志记录',正文为'在所有服务中实现JSON日志记录'。将其标记为草稿。”
- “在rohit3773/mcp_github中从分支修复/登录错误创建一个PR,标题为“修复登录重定向错误”,正文为“解决成功后登录重定向两次的问题”。”
混合
- “请在rohit3773/mcp_GitHub中创建一个名为'Refactor-utils.py'的GitHub问题,其正文为'utils.py太大,需要拆分为模块。'添加标签重构。”
- “现在,打开一个名为‘refactor utils.py into modules’的pull请求,从branch refactor utils到main,其正文为‘将助手拆分为多个文件,如前所述’。”
资源+工具组合(如果你在UI中显示问候语)
- “向Alice问好,然后在rohit3773/mcp_github中创建一个题为“欢迎消息拼写错误”的问题,其正文为“Greeting说你好,Alicee而不是你好,Alice”。将其标记为bug。”
🛠️ 故障排除
-
ModuleNotFoundError: No module named 'mcp'
确保正确的virtualenv处于活动状态并运行pip install -r requirements.txt(或uv pip install -r requirements.txt). -
GITHUB_TOKEN not set. Put it in your .env
添加GITHUB_TOKEN到服务器的.env并重新启动。对于私有仓库,确保令牌具有repo范围。 -
用于回购或权限的GitHub API 404
检查回购名称(例如。,mcp_github带有“s”)以及令牌范围。验证仓库的可见性。 -
速率限制
未经身份验证的请求具有速率限制——始终使用GITHUB_TOKEN在服务器上。 -
Windows激活
使用PowerShell:.venv\Scripts\Activate.ps1(或cmd.exe:.venv\Scripts\activate.bat).
📄 许可证
麻省理工学院(或您首选的许可证)。更新 LICENSE 因此。
🙌 鸣谢
- MCP Python SDK
- LangGraph
- 溪流
- GitHub REST API