使用说明
使用自定义GitHub MCP工具的AI代理
一个完整的AI代理系统,带有用于GitHub存储库分析的自定义模型上下文协议(MCP)工具。使用LangGraph、Gemini AI、FastMCP和React构建。
🎯 这有什么作用
AI代理可以:
- 📊 分析GitHub存储库 -获取星星、叉子、问题、活动统计数据
- 📦 扫描依赖关系 -发现Python、Node.js、Go、Rust、Ruby、Java之间的依赖关系
- 💬 流响应 -通过web界面实时AI响应
- 🔧 使用自定义工具 -可扩展的MCP架构,用于添加新功能
🏗️ 建筑
┌─────────────────────────────────────────────────────────────────┐
│ Browser (localhost:5173) │
│ React + TypeScript Frontend │
└────────────────────────────┬────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────────┐
│ UI Backend (localhost:5003) │
│ Fastify + Node.js Proxy │
└────────────────────────────┬────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────────┐
│ Agent (localhost:8081) │
│ LangGraph + Gemini AI + LangChain │
└────────────────────────────┬────────────────────────────────────┘
│
↓
┌─────────────────────────────────────────────────────────────────┐
│ MCP Server (localhost:5001) │
│ FastMCP + Custom Tools │
│ • GitHub Stats Tool • GitHub Dependencies Tool │
│ • Multiply Tool • Code Review Tool │
└────────────────────────────┬────────────────────────────────────┘
│
↓
GitHub Public API
🚀 快速开始
先决条件
- Python 3.11+ 用于MCP服务器和代理
- Node.js 18+ 用于UI
- Gemini API密钥 从https://ai.google.dev/
步骤1:启动MCP服务器
cd template-mcp-server
# Install dependencies
pip install -e .
# Start the server
python -m template_mcp_server.src.main
# Server runs on http://localhost:5001
步骤2:启动代理
cd template-agent
# Create .env file
cat > .env << EOF
GEMINI_API_KEY="your-gemini-api-key-here"
MCP_SERVER_HOST=http://localhost:5001/mcp/
MCP_TRANSPORT_PROTOCOL=streamable_http
AGENT_HOST=http://127.0.0.1:8081
AGENT_PORT=8081
EOF
# Install dependencies
pip install -e .
# Start the agent
python -m template_agent.src.main
# Agent runs on http://127.0.0.1:8081
步骤3:启动用户界面
cd template-ui
# Create .env file
cat > .env << EOF
AGENT_HOST=http://127.0.0.1:8081
PORT=5003
AUTH_ENABLED=false
COOKIE_SIGN=super-secret-cookie-signing-key-minimum-32-chars-long
EOF
# Install dependencies
npm install
# Start both frontend and backend
npm run dev
# UI opens at http://localhost:5173
第四步:试试看!
打开http://localhost:5173并询问:
- “脸书/react的统计数据是什么?”
- “langchain ai/langgraph的依赖关系是什么?”
- “25乘以17是多少?”
📦 项目结构
Hackathon/
├── template-mcp-server/ # Custom MCP tools server
│ ├── template_mcp_server/
│ │ └── src/
│ │ ├── tools/
│ │ │ ├── github_stats_tool.py ⭐ Custom
│ │ │ └── github_dependencies_tool.py ⭐ Custom
│ │ ├── mcp.py # Tool registration
│ │ └── settings.py # Configuration
│ └── HACKATHON_SETUP.md # Detailed MCP docs
│
├── template-agent/ # AI Agent with LangGraph
│ ├── template_agent/
│ │ └── src/
│ │ ├── core/
│ │ │ ├── agent.py # Agent initialization
│ │ │ └── manager.py # Conversation management
│ │ └── routes/
│ │ └── stream.py # Streaming endpoint
│ ├── .env # Config (create this)
│ └── HACKATHON_SETUP.md # Detailed agent docs
│
├── template-ui/ # Web interface
│ ├── src/
│ │ ├── frontend/ # React app
│ │ └── server/ # Fastify backend
│ │ └── controllers/
│ │ └── v1/
│ │ └── agent.ts # Proxy to agent
│ ├── index.html # Frontend entry
│ ├── .env # Config (create this)
│ └── HACKATHON_SETUP.md # Detailed UI docs
│
└── README.md # This file
🔧 我们定制的工具
1.GitHub存储库统计数据
- 工具:
get_github_repo_stats - 输入:
owner/repo(例如。,microsoft/vscode) - 输出:星、叉、问题、PR、语言、日期、URL
- 文件:
template-mcp-server/template_mcp_server/src/tools/github_stats_tool.py
2.GitHub依赖性扫描程序
- 工具:
get_github_dependencies - 输入:
owner/repo - 输出:按生态系统划分的依赖关系(Python、Node.js、Go、Rust、Ruby、Java)
- 文件:
template-mcp-server/template_mcp_server/src/tools/github_dependencies_tool.py
🎨 主要特点
- ✅ 实时流媒体 -查看生成的AI响应
- ✅ 工具使用可视化 -观察客服决定使用哪些工具
- ✅ 自定义MCP工具 -轻松添加新功能
- ✅ 对话历史 -维护消息之间的上下文
- ✅ HTML渲染 -代理可以返回样式化的HTML响应
- ✅ 错误处理 -优美的回退和错误消息
🛠️ 技术栈
MCP服务器
- FastMCP -MCP协议实现
- Python 3.11 -运行时间
- 派丹蒂克 -配置管理
代理
- LangGraph -代理编排
- LangChain -LLM框架
- 谷歌双子座 -AI模型(双生-2.5-flash)
- 乌维科恩 -ASGI服务器
用户界面
- 反应18 -前端框架
- TypeScript -类型安全
- 维特 -构建工具和开发服务器
- 快车 -后端API服务器
- 尾风CSS -造型
📝 配置文件
MCP服务器
无需配置-使用默认值 settings.py
代理人: template-agent/.env
GEMINI_API_KEY="your-key-here" # Required
MCP_SERVER_HOST=http://localhost:5001/mcp/ # MCP endpoint
MCP_TRANSPORT_PROTOCOL=streamable_http # Must match server
AGENT_HOST=http://127.0.0.1:8081 # Agent endpoint
AGENT_PORT=8081 # Agent port
UI: template-ui/.env
AGENT_HOST=http://127.0.0.1:8081 # Agent endpoint
PORT=5003 # Backend port
AUTH_ENABLED=false # For development
COOKIE_SIGN=your-secret-key-here # Cookie signing
🐛 故障排除
MCP服务器无法启动
- 检查端口5001是否可用:
lsof -i :5001 - 验证Python 3.11+:
python --version - 检查终端输出中的日志
代理无法启动
- 验证Gemini API密钥是否设置在
.env - 检查MCP服务器是否在端口5001上运行
- 检查端口8081是否可用:
lsof -i :8081 - 查看日志:
tail -f agent.log
UI无法启动
- 检查端口5003和5173是否可用
- 验证Node.js 18+:
node --version - 检查代理是否在端口8081上运行
- 查看后端日志:
tail -f server.log
“超出429配额”错误
- 您已达到Gemini的免费等级限制(每天250个请求)
- 等待配额重置或升级到付费计划
- 从不同的Google帐户获取新的API密钥
UI显示“获取失败”
- 验证所有服务是否正在运行
- 检查浏览器控制台是否存在CORS错误
- 确保
.env文件具有正确的端口 - 尝试重新启动UI后端
📚 延伸阅读
📊 监控
查看日志
# MCP Server (stdout)
cd template-mcp-server && python -m template_mcp_server.src.main
# Agent
tail -f template-agent/agent.log
# UI Backend
tail -f template-ui/server.log
# UI Frontend
Check browser console (F12)
检查服务状态
# MCP Server
curl http://localhost:5001/
# Agent
curl http://127.0.0.1:8081/
# UI Backend
curl http://localhost:5003/
# UI Frontend
curl http://localhost:5173/
🎯 交互示例
GitHub存储库分析
用户:“比较facebook/react和vuejs/vue的统计数据”
代理:
- 呼叫
get_github_repo_stats("facebook/react") - 呼叫
get_github_repo_stats("vuejs/vue") - 综合与Gemini的比较
- 返回样式化HTML比较
依赖关系发现
用户:“langchain ai/langgraph使用哪些依赖项?”
代理:
- 呼叫
get_github_dependencies("langchain-ai/langgraph") - 作语法分析
pyproject.toml - 按类别返回有组织的列表
多工具使用
用户:“获取microsoft/vcode的统计数据并检查其依赖关系”
代理:
- 呼叫
get_github_repo_stats("microsoft/vscode") - 呼叫
get_github_dependencies("microsoft/vscode") - 将结果合并为综合报告
🚦 端口摘要
| 服务 | 端口 | URL |
|---|---|---|
| MCP服务器 | 5001 | http://localhost:5001 |
| 代理 | 8081 | http://127.0.0.1:8081 |
| UI后端 | 5003 | http://localhost:5003 |
| 用户界面前端 | 5173 | http://localhost:5173 |
🎓 我们的收获
- MCP协议:如何构建AI代理可以发现和使用的自定义工具
- LangGraph:使用工具调用进行代理编排
- 流媒体:通过多个代理层实时响应流
- 工具设计:构建工具输入/输出以优化人工智能使用
- 全栈AI:连接前端→ 后端→ 代理→ MCP工具→ 外部API
📄 许可证
请参阅单个项目的LICENSE文件。
在红帽黑客马拉松期间建造🎉