🍔 食品聚集剂
比较不同地区的食品价格 Swiggy 和 佐马托 使用人工智能——由谷歌双子座、LangGraph和模型上下文协议(MCP)提供支持。
______________________________________________________________________
✨ 特性
- 🔍 跨平台搜索 --同时查询Swiggy和Zomato
- 🎟️ 优惠券比较 --获取并应用两个平台上的可用折扣
- 💬 React聊天界面 --带有多回合记忆的深色主题对话界面
- 📊 流量追踪 --代理推理、工具调用和LLM响应的完全可观察性
- ⚡ 令牌优化 -为减少API成本而调整的工具说明
- 🐳 可部署的 --包含多级Dockerfile+渲染蓝图
______________________________________________________________________
🏗️ 建筑
┌──────────────────────────────────────────────────┐
│ Render (Docker) │
│ │
│ ┌────────────────────────────────────────────┐ │
│ │ FastAPI (port 8000) │ │
│ │ ├── GET / → React static app │ │
│ │ ├── POST /api/connect → connect MCP │ │
│ │ ├── POST /api/chat → agent endpoint │ │
│ │ └── GET /api/status → health check │ │
│ └──────────┬─────────────────────────────────┘ │
│ │ │
│ ┌──────────▼─────────────────────────────────┐ │
│ │ LangGraph ReAct Agent (Gemini) │ │
│ │ ├── Swiggy MCP (via npx mcp-remote) │ │
│ │ └── Zomato MCP (via npx mcp-remote) │ │
│ └────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────┐ │
│ │ MLflow Tracking (localhost:5000) │ │
│ └────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────┘______________________________________________________________________
📁 项目结构
.
├── frontend/ # React chat frontend (Vite)
│ ├── src/
│ │ ├── App.jsx # Chat UI component
│ │ ├── index.css # Dark gradient theme
│ │ └── main.jsx # React entry point
│ ├── index.html # HTML template
│ ├── vite.config.js # Vite config with API proxy
│ ├── package.json # Frontend dependencies
│ └── dist/ # Built static files (after npm run build)
│
├── main.py # FastAPI backend (API + serves React)
├── agent.py # LangGraph ReAct agent with Gemini
├── mcp_client.py # MCP server connection manager
├── streamlit_app.py # Streamlit frontend (alternative)
│
├── requirements.txt # Python dependencies
├── .env.example # Environment variable template
├── .gitignore # Git ignore rules
│
├── Dockerfile # Multi-stage: Node builds React → Python serves all
├── render.yaml # Render Blueprint for one-click deploy
└── README.md # This file______________________________________________________________________
🚀 快速入门(逐步)
先决条件
| 要求 | 版本 | 检查命令 |
|---|---|---|
| Python | 3.11+ | python --version |
| Node.js | 18+ | node --version |
| npm | 9+ | npm --version |
| Gemini API密钥 | - | 在这里买一个 |
______________________________________________________________________
步骤1:克隆存储库
git clone
cd "Swiggy MCP POC"______________________________________________________________________
步骤2:设置Python后端
# Create virtual environment
python -m venv venv
# Activate it
source venv/bin/activate # macOS / Linux
# venv\Scripts\activate # Windows
# Install Python dependencies
pip install -r requirements.txt验证: 您应该看到安装的软件包没有错误。运行此程序以确认:
python -c "from agent import create_food_aggregator_agent; print('✅ Backend imports OK')"______________________________________________________________________
步骤3:设置React前端
# Navigate to frontend
cd frontend
# Install Node dependencies
npm install
# Build the React app
npm run build
# Go back to project root
cd ..验证: 检查生成输出是否存在:
ls frontend/dist/
# Should show: index.html assets/______________________________________________________________________
步骤4:配置环境变量
# Copy the template
cp .env.example .env现在编辑 .env 使用编辑器并添加Gemini API密钥:
# ─── REQUIRED ───
GEMINI_API_KEY="your_gemini_api_key_here"
# ─── OPTIONAL (defaults shown) ───
GEMINI_MODEL="gemini-3-flash-preview"
SWIGGY_MCP_URL="https://mcp.swiggy.com/food"
ZOMATO_MCP_URL="https://mcp-server.zomato.com/mcp"
MLFLOW_TRACKING_URI="http://127.0.0.1:5000"
MAX_TOOL_DESC_LENGTH="200"______________________________________________________________________
步骤5:运行应用程序
你有 3种方式 运行应用程序。选择一个:
选项A:React+FastAPI(推荐)
这是生产设置。FastAPI在单个端口上同时为API和React UI提供服务。
端子1 --启动服务器:
source venv/bin/activate
RUN_SERVER=1 uvicorn main:app --host 0.0.0.0 --port 8000打开: http://localhost:8000
用途:
- 点击 “连接到MCP服务器” 在侧边栏中
- 将为Swiggy OAuth打开一个浏览器窗口——授权它
- 另一个浏览器窗口将为Zomato OAuth打开——授权它
- 连接后,键入您的食物查询并聊天!
______________________________________________________________________
选项B:React开发模式(用于前端开发)
使用热重载运行React开发服务器,代理到FastAPI后端。
端子1 --启动FastAPI后端:
source venv/bin/activate
RUN_SERVER=1 uvicorn main:app --host 0.0.0.0 --port 80002号航站楼 --启动React开发服务器:
cd frontend
npm run dev打开: http://localhost:5173(Vite-dev服务器,热重载)
______________________________________________________________________
选项C:CLI模式(不需要UI)
用于直接在终端进行快速测试。
source venv/bin/activate
# Interactive chat mode
python main.py
# Or single query mode
python main.py -q "I want to order Narmada Chicken Biriyani"______________________________________________________________________
选项D:流光显示(替代UI)
source venv/bin/activate
streamlit run streamlit_app.py --server.port 8501打开: http://localhost:8501
______________________________________________________________________
步骤6:(可选)启动用于跟踪的MLW
打开A 独立终端:
source venv/bin/activate
mlflow ui --port 5000打开: http://localhost:5000
所有代理痕迹都将显示在 食品_聚合剂_试剂 实验。您可以看到:
- 每个LLM调用及其令牌
- 每次MCP工具调用及其参数/结果
- 完整的ReAct推理链
______________________________________________________________________
🔌 API 参考
FastAPI后端公开了这些端点:
POST /api/connect
连接到Swiggy和Zomato MCP服务器。聊天前先打一次电话。
curl -X POST http://localhost:8000/api/connect答复:
{
"connected": true,
"servers": [
{"name": "Swiggy", "status": "ok", "tools": 13},
{"name": "Zomato", "status": "ok", "tools": 11}
],
"total_tools": 24
}POST /api/chat
向代理发送消息。
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"query": "I want to order Narmada Chicken Biriyani", "session_id": "my-session-1"}'答复:
{
"response": "I found several options for Narmada Chicken Biriyani..."
}注: 使用相同 session_id 跨请求维护对话历史记录(多回合)。GET /api/status
检查MCP服务器是否已连接。
curl http://localhost:8000/api/status______________________________________________________________________
🚢 部署
选项1:渲染(免费--推荐)
Render提供了一个免费的基于Docker的web服务层。
- 将代码推送到GitHub
- 首选 render.com → 新 → Web服务
- 连接您的GitHub仓库
- 选择 码头工人 作为环境
- 添加环境变量:
GEMINI_API_KEY=您的钥匙 - 点击 部署
包括 render.yaml 文件还支持 蓝图部署 --只需在Render上单击“新建蓝图实例”并指向您的仓库。
选项2:Docker(任何地方)
# Build the image (this builds React + installs Python)
docker build -t food-aggregator .
# Run it
docker run -p 8000:8000 \
-e GEMINI_API_KEY="your_key_here" \
-e GEMINI_MODEL="gemini-3-flash-preview" \
food-aggregator打开: http://localhost:8000
选项3:谷歌云运行
gcloud builds submit --tag gcr.io/YOUR_PROJECT/food-aggregator
gcloud run deploy food-aggregator \
--image gcr.io/YOUR_PROJECT/food-aggregator \
--platform managed \
--allow-unauthenticated \
--set-env-vars "GEMINI_API_KEY=your_key"______________________________________________________________________
⚙️ 环境变量
| 变量 | 必填 | 默认 | 描述 |
|---|---|---|---|
GEMINI_API_KEY | ✅ | — | Google Gemini API密钥 |
GEMINI_MODEL | gemini-3-flash-preview | 使用Gemini模型 | |
SWIGGY_MCP_URL | https://mcp.swiggy.com/food | Swiggy MCP服务器端点 | |
ZOMATO_MCP_URL | https://mcp-server.zomato.com/mcp | Zomato MCP服务器端点 | |
MLFLOW_TRACKING_URI | http://127.0.0.1:5000 | 工作流跟踪服务器URL | |
MAX_TOOL_DESC_LENGTH | 200 | MCP工具描述的最大字符数(令牌优化) | |
RUN_SERVER | -- | 设置为 1 启动FastAPI服务器(否则启动CLI) |
______________________________________________________________________
📋 运作原理
- 连接 --点击按钮→ FastAPI生成
npx mcp-remote通过OAuth连接到Swiggy/Zomato远程HTTP MCP服务器的进程 - 加载工具 --加载MCP工具(搜索、菜单、购物车、优惠券、订单、跟踪);截断描述以保存令牌
- 聊天 --您的消息进入由Gemini驱动的LangGraph ReAct循环
MemorySaver用于多回合对话 - 搜索 --代理调用两个平台上的MCP工具,比较结果,应用可用优惠券
- 推荐 --代理会格式化一个比较价格的响应,并推荐最便宜的选项
______________________________________________________________________
🛠️ 技术栈
| 层 | 技术 |
|---|---|
| LLM | 谷歌双子座(langchain-google-genai) |
| 代理 | LangGraph重新激活+ MemorySaver 校验指针 |
| 主控程序 | langchain-mcp-adapters + npx mcp-remote |
| 后端 | FastAPI+Uvicorn |
| 前端 | React+Vite |
| 追踪 | MLW |
| 部署 | Docker/渲染/云运行 |
______________________________________________________________________
🐛 故障排除
| 问题 | 解决方案 |
|---|---|
ModuleNotFoundError | 确保venv已激活: source venv/bin/activate |
npx: command not found | 安装Node.js 18+: brew install node (macOS) |
| OAuth窗口未打开 | 检查浏览器的弹出窗口阻止程序 |
GEMINI_API_KEY not set | 将您的密钥添加到 .env --参见步骤4 |
frontend/dist not found | 快跑 cd frontend && npm run build --参见步骤3 |
| MLflow连接错误 | 先启动MLflow: mlflow ui --port 5000 |
| 令牌限制错误 | 减少 MAX_TOOL_DESC_LENGTH 在 .env (默认值:200) |
______________________________________________________________________
📄 许可证
麻省理工学院
