近距离作业
~作者:普里扬舒·库马尔·拉伊
📹 演示-https://www.loom.com/share/b64ce434bba94ff78f5f987faf5cdeb2
这是一个基于人工智能的应用程序,利用 模型上下文协议(MCP) 将大型语言模型(LLM)与各种本地和远程工具连接起来。它以现代风格为特色 溪流 用户界面,作为与能够跟踪费用、查看天气和与社交媒体交互的代理聊天的中心枢纽。
______________________________________________________________________
🎯 这个项目里面有什么?
该项目展示了一个完整的 多服务器MCP架构 三个主要部件协同工作:
1. 🖥️ MCP服务器 (后端服务)
该应用程序包括 三台MCP服务器 将工具暴露给LLM:
a) 数据库服务器(费用跟踪器)
- 地点:
DatabaseServer/ - 技术: FastMCP+FastAPI+SQLite
- MCP工具暴露:
- add_expense -通过自动分类添加新费用 - list_expenses -通过过滤检索费用历史记录 - summarize_expenses -获取费用分析和摘要
- FastAPI端点:
- POST /expenses -创建支出 - GET /expenses -列出所有费用 - GET /expenses/summary -获取费用汇总
- 它是如何工作的: MCP服务器封装FastAPI端点,并将其作为LLM的可调用工具公开
b) 天气服务器
- 地点:
WeatherServer/ - 技术: FastMCP+FastAPI+Open-Meteo API
- MCP工具暴露:
- get_forecast -获取任何地点的天气预报
- FastAPI端点:
- GET /forecast -获取天气数据
- 它是如何工作的: 与Open-Meteo API集成,通过MCP提供实时天气信息
c) 推特MCP服务器 (外部)
- 地点: 外部NPM包(
@enescinar/twitter-mcp) - 技术: Node.js MCP服务器
- MCP工具暴露:
- post_tweet -发布推文 - search_tweets -搜索推特 - get_user_timeline -获取用户推文
2. 🔌 FastAPI端点 (直接HTTP访问)
每台服务器还提供 直接HTTP访问 用于测试和调试:
数据库服务器 (\`http://
3. 🤖 MCP客户端 (流线型前端)
- 地点:
Client/main.py - 技术: Streamlit+LangChain+LangChain mcp适配器
- 功能:
- 连接到所有MCP服务器 使用 MultiServerMCPClient - 协调LLM交互 通过LangChain+Groq - 提供对话式用户界面 用于自然语言请求 - 自动工具调用 -LLM决定使用哪些MCP工具
客户如何工作:
User Input → Streamlit UI → LangChain Agent → Groq LLM → MCP Tools → Response客户:
- 启动时初始化与所有MCP服务器的连接
- 将MCP工具转换为LangChain兼容格式
- 向Groq LLM发送用户查询
- LLM分析查询并调用适当的MCP工具
- 结果显示在聊天界面中
交互示例:
- *“今天午餐加500卢比”* → Calls
add_expense工具 - *“巴黎的天气怎么样?”* → Calls
get_forecast工具 - *“显示我上周的开支”* → Calls
list_expenses工具 - *“发布推特问候”* → Calls
post_tweet工具
______________________________________________________________________
🏗 建筑
该项目遵循多服务器MCP架构:
- 客户(
/Client):连接到多个MCP服务器的Streamlit前端。使用 LangChain 协调用户、LLM(Groq)和可用工具之间的交互。 - 数据库服务器(
/DatabaseServer):当地人 快速API + 数据库 提供费用跟踪功能(添加、列出、汇总)的服务器。 - 天气服务器(
/WeatherServer)A. 快速API 从开放的meteo API获取实时天气数据的服务器。 - 推特MCP:外部MCP服务器(通过
npx)推特互动。
🚀 使用的技术
- 前端: 溪流 (带有固定布局仪表板的自定义CSS)
- AI/LLM编排:
- LangChain - langchain-mcp-adapters - langchain-groq
- 后端/服务器:
- FastMCP - uv 和 pip 用于依赖关系管理
- 数据库:SQLite(用于费用跟踪)
- 外部API:Open-Meteo(天气),Twitter API。
📂 项目结构
Inxtinct_MCP/
├── Client/
│ ├── main.py # Main Streamlit application & MCP Client
│ ├── test.py # Testing utilities
│ └── .env # Environment variables (create this)
├── DatabaseServer/
│ ├── server.py # FastAPI server for Expenses
│ ├── main.py # MCP Entrypoint
│ ├── categories.json # Expense categories configuration
│ └── expenses.db # SQLite Database (auto-generated)
├── WeatherServer/
│ ├── server.py # FastAPI server for Weather
│ └── main.py # MCP Entrypoint
├── pyproject.toml # Project metadata & dependencies
├── requirements.txt # Python dependencies
└── README.md # This file🛠️ 设置和安装
先决条件
在开始之前,请确保已安装以下内容:
- Python 3.13+ - 下载Python
- 紫外线 (包管理器)-通过以下方式安装:
# Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh- Node.js 18+ 和 npx - (npx捆绑销售)
- Git - 下载Git
分步安装
1.️⃣ 克隆存储库
git clone https://github.com/Devgambo/Inxtinct_MCP.git
cd Inxtinct_MCP2.️⃣ 再进行
使用以下命令安装所有必需的Python包 uv:
uv sync或者,如果你更喜欢使用pip:
pip install -r requirements.txt3.️⃣ 配置环境变量
创建一个 .env 文件在 Client/ 带有API密钥的目录:
cd Client
# Create .env file (Windows)
New-Item .env -ItemType File
# Create .env file (macOS/Linux)
touch .env将以下配置添加到您的 .env 文件:
# Groq API Configuration (Required)
GROQ_API_KEY=your_groq_api_key_here
# Twitter API Configuration (Optional - only if using Twitter features)
API_KEY=your_twitter_api_key
API_SECRET_KEY=your_twitter_api_secret_key
ACCESS_TOKEN=your_twitter_access_token
ACCESS_TOKEN_SECRET=your_twitter_access_token_secret如何获取API密钥:
- Groq API密钥:注册地址: console.groq.com
- Twitter API密钥:在以下网址创建开发人员帐户 developer.twitter.com
4️⃣ 重要提示:更新服务器路径 ⚠️
克隆后,你 必须 更新中的绝对文件路径 Client/main.py 以匹配您的本地设置。
打开 Client/main.py 并定位 SERVERS 配置(围绕第14-53行)。更新中的路径 args 部分:
之前(示例路径):
"ExpenseTracker": {
"args": [
"run",
"--with",
"fastmcp",
"fastmcp",
"run",
"C:\\Users\\priya\\OneDrive\\Desktop\\Inxtinct\\DatabaseServer\\main.py", # ← Change this
]
},
"weather-server": {
"args": [
"run",
"--with",
"fastmcp",
"--with",
"httpx",
"fastmcp",
"run",
"C:\\Users\\priya\\OneDrive\\Desktop\\Inxtinct\\WeatherServer\\main.py" # ← Change this
]
}之后(您的实际路径):
# Replace with your actual project path
# Windows example: C:\\Users\\YourUsername\\path\\to\\Inxtinct_MCP\\DatabaseServer\\main.py
# macOS/Linux example: /home/yourusername/path/to/Inxtinct_MCP/DatabaseServer/main.py
"ExpenseTracker": {
"args": [
"run",
"--with",
"fastmcp",
"fastmcp",
"run",
"YOUR_ABSOLUTE_PATH/DatabaseServer/main.py", # Update this path
]
},
"weather-server": {
"args": [
"run",
"--with",
"fastmcp",
"--with",
"httpx",
"fastmcp",
"run",
"YOUR_ABSOLUTE_PATH/WeatherServer/main.py" # Update this path
]
}💡 提示: 要获取当前目录路径,请执行以下操作:
- Windows PowerShell:运行
Get-Location或pwd - macOS/Linux:运行
pwd
运行应用程序
设置完成后,导航到客户端目录并启动应用程序:
# From the project root
cd Client
# Run the Streamlit application
uv run streamlit run main.py
# Alternative (if using pip)
streamlit run main.py应用程序将在默认浏览器中打开 http://localhost:8501
注: 数据库和天气MCP服务器由客户端作为子进程自动启动。你不需要单独运行它们。
✨ 特性
- 自然语言费用跟踪:“午餐加500”->自动分类并保存到数据库。
- 实时天气更新:“伦敦的天气怎么样?”->获取实时数据。
- Twitter集成:直接从聊天中发布和搜索推文。
- 持续聊天记录:在会话期间保持上下文。
- 现代用户界面:深色主题,固定布局界面,带有专用工具侧边栏。
🐛 故障排除
常见问题及解决方法
1.模块无边界错误或导入错误
问题: 缺少Python依赖项
解决方案:
# Reinstall dependencies
uv sync
# Or with pip
pip install -r requirements.txt2.“找不到GROQ_API_KEY”错误
问题: 未加载环境变量
解决方案:
- 验证
.env文件存在于Client/目录 - 检查
GROQ_API_KEY已正确设置.env - 重新启动Streamlit应用程序
3.服务器连接失败
问题: 中的服务器路径不正确 SERVERS 配置
解决方案:
- 仔细检查中的绝对路径
Client/main.py匹配您的系统 - 确保路径使用正确的分隔符(
\\对于Windows,/适用于macOS/Linux) - 验证
DatabaseServer/main.py和WeatherServer/main.py存在
4.推特工具不起作用
问题: Twitter API凭据丢失或无效
解决方案:
- 推特功能是可选的——没有这些功能,应用程序也能正常工作
- 如果需要,将所有四个Twitter API密钥添加到您的
.env文件 - 在以下位置验证凭据 developer.twitter.com
5.端口已在使用中
问题: Streamlit默认端口(8501)已占用
解决方案:
# Specify a different port
uv run streamlit run main.py --server.port 85026.未找到UV命令
问题: uv 未安装或不在PATH中
解决方案:
# Reinstall uv (Windows PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Restart your terminal after installation获取帮助
如果您遇到此处未列出的问题:
- 检查终端输出是否有特定的错误消息
- 验证所有必备组件是否已正确安装
- 确保你使用的是Python 3.13或更高版本:
python --version - 打开一个问题
📝 备注
- 受损的twitter api密钥已得到处理。
