维基百科助手
一个通过MCP(模型上下文协议)框架使用维基百科搜索工具的对话式AI代理。该代理能够搜索维基百科、提取信息,并使用LangGraph和LangChain来回答问题。
项目结构
wikipedia_assistant/
├── mcp_server.py # MCP server with Wikipedia tools (do not modify)
├── mcp_client.py # Alternative client implementation (do not modify)
├── langgraph_state.py # LangGraph state definition
├── load_mcp_tools.py # Graph creation and tool loading
├── run_agent.py # Main entry point and CLI interface
├── requirements.txt # Python dependencies
└── README.md # This file特点/功能
- 维基百科搜索搜索并检索维基百科文章
- 交互式命令行界面(CLI)用于对话的命令行界面
- 提示系统支持带参数的结构化提示
- 工具集成使用MCP协议进行工具发现和执行
- 状态管理与LangGraph保持持续对话状态
设置
- 安装依赖项:
pip install -r requirements.txt- 设置OpenAI API密钥:
export OPENAI_API_KEY="your-openai-api-key-here"使用方法
运行交互式代理:
python3 run_agent.py可用命令
- 日常对话输入任何关于维基百科主题的问题
- 列出提示:
/prompts或者/list_prompts - 执行提示:
/prompt "arg1" "arg2" ... - 退出:
exit,quit,或者q
示例用法
You: Tell me about Alan Turing
AI: [Searches Wikipedia and provides information about Alan Turing]
You: /prompts
[Lists available prompts from the MCP server]
You: /prompt highlight_sections_prompt "Albert Einstein"
[Executes the prompt with the given argument]技术细节
建筑学
- MCP 服务器 (
mcp_server.py):通过MCP协议提供维基百科搜索工具 - LangGraph 代理 (
load_mcp_tools.py): 创建一个集成工具的对话图 - 状态管理 (
langgraph_state.py): 定义消息处理的共享状态 - 命令行界面(CLI) (
run_agent.py): 交互式命令行界面
依赖项
langchain-openaiOpenAI大型语言模型(LLM)集成langchain-coreLangChain的核心功能langchain-mcp-adaptersMCP协议适配器langgraph基于图的代理框架mcp模型上下文协议库wikipedia维基百科API封装器
故障排除
- 缺少API密钥确保
OPENAI_API_KEY环境变量已设置 - 导入错误安装所有依赖项,使用
pip install -r requirements.txt - 服务器连接确保Python可以执行
mcp_server.py
文件分析
所需文件
- ✅
langgraph_state.py- 对于状态管理至关重要 - ✅
load_mcp_tools.py- 图表创建的核心功能 - ✅(这个符号在中文中通常表示“正确”、“确认”或“完成”的意思,但直接翻译时可能无法完全传达其含义,因此在这里保留原符号并稍作解释)
run_agent.py- 主入口 - ✅
mcp_server.py- 所需的MCP服务器(如要求,保持不变) - ✅
requirements.txt- 依赖管理
重复文件/遗留文件
- ⚠️
mcp_client.py- 包含重复功能和安全问题(按要求保留)
该项目的主要文件已具备功能性 mcp_client.py 看起来这是一个包含重复代码和硬编码API密钥的旧版本,但根据您的要求已保留。
