使用说明
MCP聊天机器人🤖
MCP聊天机器人-通过模型上下文协议集成实现智能自动化。具有浏览器自动化、网络搜索和旅行计划功能,并具有强大的错误处理功能。使用Google Gemini LLM,优雅地处理服务器故障,以实现可靠运行。
GitHub存储库:https://github.com/naakaarafr/MCP-Chatbot
一个使用模型上下文协议(MCP)构建的强大聊天机器人,通过浏览器控制、网络搜索和旅行计划功能提供智能自动化。
概述
该聊天机器人利用模型上下文协议集成多个专用服务器,为网络自动化、搜索和旅行相关任务创建统一的界面。它使用谷歌的Gemini模型作为底层LLM,可以通过单独测试每个MCP服务器来优雅地处理服务器故障。
特性
🌐 浏览器自动化(剧作家MCP)
- 导航到网站
- 截图
- 点击元素并与网页交互
- 自动化基于web的任务
🔍 网络搜索(DuckDuckGo MCP)
- 执行网络搜索
- 获取实时信息
- 研究主题并找到答案
🏠 旅行计划(Airbnb MCP)
- 搜索住宿
- 查找酒店和租赁
- 获取旅行建议
🛡️ 稳健的错误处理
- 启动时的单个服务器测试
- 服务器发生故障时性能下降
- 有限模式下的回退响应
- 清除错误报告
先决条件
系统要求
- Python 3.8+
- Node.js和npm(用于MCP服务器)
- UV包管理器(适用于Python MCP服务器)
所需套餐
# Python dependencies
pip install asyncio python-dotenv langchain-google-genai mcp-use
# Node.js MCP servers
npm install -g @playwright/mcp
npm install -g @openbnb/mcp-server-airbnb
# Python MCP servers
uvx install duckduckgo-mcp-server
安装
-
克隆或下载项目文件:
# Ensure you have both app.py and browser_mcp.json -
安装Python依赖项:
pip install -r requirements.txt或单独安装:
pip install asyncio python-dotenv langchain-google-genai mcp-use -
安装MCP服务器:
# Playwright for browser automation npm install -g @playwright/mcp # Airbnb for travel search npm install -g @openbnb/mcp-server-airbnb # DuckDuckGo for web search uvx install duckduckgo-mcp-server -
设置环境变量: 创建一个
.env项目根目录中的文件:GOOGLE_API_KEY=your_google_api_key_here -
配置MCP服务器: 这
browser_mcp.json文件已配置默认设置。必要时进行修改:{ "mcpServers": { "playwright": { "command": "npx", "args": ["@playwright/mcp@latest"] }, "airbnb": { "command": "npx", "args": ["-y", "@openbnb/mcp-server-airbnb"] }, "ddg-search": { "command": "uvx", "args": ["duckduckgo-mcp-server"] } } }
用法
启动聊天机器人
python app.py
可用命令
help-显示可用命令和示例查询tools-列出所有可用的MCP工具quit或exit-结束对话
查询示例
浏览器自动化
"Take a screenshot of google.com"
"Navigate to wikipedia.org and search for Python"
"Click the login button on the website"
网页搜索
"What's the weather like today?"
"Search for the latest news about AI"
"Find information about renewable energy"
旅行计划
"Find hotels in New York City"
"Search for Airbnb rentals in San Francisco"
"What are the best accommodations in Tokyo?"
一般援助
"Find the best restaurants in downtown Chicago"
"Help me research Python programming tutorials"
"Take a screenshot of the current stock market trends"
建筑
核心组件
-
MCPChatbot类:主要聊天机器人编排器
- 初始化和管理MCP服务器
- 处理用户交互
- 提供回退响应
-
服务器测试:单个服务器验证
- 启动时测试每个MCP服务器
- 报告工作和故障服务器
- 使用可用服务器继续运行
-
代理集成:LangChain+MCP集成
- 使用Google Gemini 2.0 Flash作为LLM
- 与MCP客户端集成以访问工具
- 支持多达20个推理步骤
错误处理
聊天机器人实现了强大的错误处理:
- 服务器故障:单独的服务器测试可防止完全失败
- 有限模式:在没有可用服务器时提供基本响应
- 故障弱化:继续运行,部分功能正常
- 用户反馈:清除错误消息和状态报告
配置
MCP服务器配置
编辑 browser_mcp.json 添加或修改MCP服务器:
{
"mcpServers": {
"your-server-name": {
"command": "command-to-run",
"args": ["arg1", "arg2"]
}
}
}
环境变量
必填项 .env 文件:
GOOGLE_API_KEY:Gemini访问的Google AI API密钥
LLM配置
修改 app.py 如果需要:
self.llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash-exp", # Model selection
temperature=0.3 # Creativity level
)
故障排除
常见问题
-
“未找到browser_mcp.json”
- 确保配置文件与位于同一目录中
app.py
- 确保配置文件与位于同一目录中
-
“没有MCP服务器正在工作”
- 检查所有MCP服务器是否已正确安装
- 验证Node.js和UV是否已安装且可访问
- 检查服务器日志中的特定错误消息
-
Google API关键问题
- 确保您的
.env文件包含有效GOOGLE_API_KEY - 验证API密钥是否可以访问Gemini模型
- 确保您的
-
安装问题
- 确保已安装Node.js和npm
- 安装UV包管理器:
pip install uv - 尝试单独安装MCP服务器以隔离问题
调试模式
聊天机器人提供详细的启动信息:
- 服务器测试结果
- 工作服务器列表
- 故障服务器的错误消息
贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 使用各种MCP服务器进行测试
- 提交拉取请求
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
请检查各个MCP服务器许可证的相应条款。
支持
关于以下问题:
- MCP协议:查看MCP官方文件
- 特定MCP服务器:请参阅单个服务器文档
- LangChain集成:请参阅LangChain文档
- 谷歌双子座:查看Google AI文档
致谢
- 创新协议的模型上下文协议(MCP)团队
- 浏览器自动化功能的剧作家团队
- LangChain团队负责集成框架
- 谷歌提供Gemini AI模型