维基百科MCP研究代理
一种模型上下文协议(MCP)服务器和客户端实现,使AI代理能够使用谷歌的Gemini AI从维基百科中搜索和检索信息。
特性
- 🔍 通过MCP服务器进行维基百科搜索和文章检索
- 🤖 使用Google Gemini 2.5 Flash的人工智能研究代理
- 🛠️ MCP工具与LangGraph的集成
- 💬 交互式命令行界面
- 📚 通过MCP协议支持提示和资源
先决条件
- Python 3.13或更高版本
- 谷歌人工智能API密钥(Gemini API)
- Git
安装
1.克隆存储库
git clone https://github.com/yourusername/mcp_wikipedia_research_agent.git
cd mcp_wikipedia_research_agent2.创建虚拟环境
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate3.安装依赖项
pip install -r requirements.txt如果 requirements.txt 不存在,请安装所需的软件包:
pip install mcp wikipedia langchain-google-genai langchain-mcp-adapters langgraph python-dotenv4.设置环境变量
- 复制示例环境文件:
cp .env.example .env- 获取您的Google AI API密钥:
- 访问 谷歌人工智能工作室 - 点击“创建API密钥” - 复制API密钥
- 编辑
.env并添加您的API密钥:
# Open .env in your editor
nano .env
# Add your API key (replace with your actual key):
GOOGLE_API_KEY=your-actual-api-key-here⚠️ 重要提示:切勿承诺 .env 文件到GitHub! 它已经列在 .gitignore.
用法
运行维基百科MCP代理
启动交互代理:
python mcp_client.py可用命令
代理运行后,您可以使用以下命令:
- 自然地提问:只需键入您的问题
You: Tell me about Python programming- 列出可用提示:
You: /prompts- 运行特定提示:
You: /prompt
"argument1" "argument2"- 列出可用资源:
You: /resources- 查看特定资源:
You: /resource - 退出代理:
You: exit或使用 quit 或 q
示例会话
Wikipedia MCP agent is ready.
Type a question or use the following templates:
/prompts - to list available prompts
/prompt "args" - to run a specific prompt
/resources - to list available resources
/resource - to run a specific resource
You: What is machine learning?
AI: [Agent searches Wikipedia and provides information about machine learning]
You: exit项目结构
mcp_wikipedia_research_agent/
├── mcp_server.py # MCP server implementation with Wikipedia tools
├── mcp_client.py # Interactive client with Gemini AI agent
├── .env.example # Example environment variables (safe to commit)
├── .env # Your actual API keys (DO NOT commit)
├── .gitignore # Git ignore rules (protects credentials)
├── README.md # This file
└── requirements.txt # Python dependencies (if created)运作原理
- MCP服务器 (
mcp_server.py):
- 提供维基百科搜索和检索工具 - 实现模型上下文协议 - 显示工具、提示和资源
- MCP客户端 (
mcp_client.py):
- 连接到MCP服务器 - 使用Google Gemini 2.5 Flash进行人工智能响应 - 将MCP工具与LangGraph集成 - 提供交互式命令行界面
- 流动:
User Question → Gemini AI → MCP Tools → Wikipedia → AI Response支持的型号
客户使用 双子座2.5闪光灯 默认情况下。其他支持的型号包括:
gemini-2.5-flash(默认值-最佳性价比)gemini-2.5-flash-lite(最快且最具成本效益)gemini-2.5-pro(复杂任务的高级推理)
要更改模型,请编辑 mcp_client.py:
llm = ChatGoogleGenerativeAI(model="gemini-2.5-pro", temperature=0, google_api_key=api_key)故障排除
“未设置GOOGLE_API_KEY环境变量”
确保您:
- 创建了
.env文件来自.env.example - 将您的实际API密钥添加到
.env - 这
.env文件与位于同一目录中mcp_client.py
“API密钥过期”或“API_key_INVALID”
您的API密钥可能无效或过期:
- 首选 谷歌人工智能工作室
- 创建新的API密钥
- 更新您的
.env使用新密钥的文件
“找不到models/gemini-x.x-xxx”
您可能正在使用已弃用的型号名称。更新到当前型号:
- 使用
gemini-2.5-flash而不是gemini-1.5-flash - 请参阅上面的“支持的型号”部分
导入错误
确保安装了所有依赖项:
pip install -r requirements.txt或手动安装:
pip install mcp wikipedia langchain-google-genai langchain-mcp-adapters langgraph python-dotenv安全说明
🔒 保护您的凭据:
- 永不承诺
.env文件 -它们包含您的API密钥 - 这
.gitignore文件阻止.env从承诺 - 只承诺
.env.example带有占位符值 - 如果您不小心提交了凭据:
- 立即吊销API密钥 - 创建一个新的 - 使用 git filter-branch 或BFG Repo Cleaner将其从历史记录中删除
贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
记得: 永远不要承诺你的 .env 文件!
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
致谢
支持
对于问题、疑问或贡献,请在GitHub上打开问题。
