维基百科研究代理

一个基于Google Gemini AI的维基百科信息搜索与检索工具,通过MCP协议实现AI代理的研究功能。

  • by colmeabhi
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 colmeabhi/mcp_wikipedia_research_agent

使用说明

维基百科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_agent

2.创建虚拟环境

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

3.安装依赖项

pip install -r requirements.txt

如果 requirements.txt 不存在,请安装所需的软件包:

pip install mcp wikipedia langchain-google-genai langchain-mcp-adapters langgraph python-dotenv

4.设置环境变量

  1. 复制示例环境文件:
cp .env.example .env
  1. 获取您的Google AI API密钥:

  2. 编辑 .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 <prompt_name> "argument1" "argument2"
    
  • 列出可用资源:

    You: /resources
    
  • 查看特定资源:

    You: /resource <resource_name>
    
  • 退出代理:

    You: exit
    

    或使用 quit 或 q

示例会话

Wikipedia MCP agent is ready.
Type a question or use the following templates:
  /prompts                - to list available prompts
  /prompt <name> "args"   - to run a specific prompt
  /resources              - to list available resources
  /resource <name>        - 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)

运作原理

  1. MCP服务器 (mcp_server.py):

    • 提供维基百科搜索和检索工具
    • 实现模型上下文协议
    • 显示工具、提示和资源
  2. MCP客户端 (mcp_client.py):

    • 连接到MCP服务器
    • 使用Google Gemini 2.5 Flash进行人工智能响应
    • 将MCP工具与LangGraph集成
    • 提供交互式命令行界面
  3. 流动:

    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环境变量”

确保您:

  1. 创建了 .env 文件来自 .env.example
  2. 将您的实际API密钥添加到 .env
  3. 这 .env 文件与位于同一目录中 mcp_client.py

“API密钥过期”或“API_key_INVALID”

您的API密钥可能无效或过期:

  1. 首选 谷歌人工智能工作室
  2. 创建新的API密钥
  3. 更新您的 .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

安全说明

🔒 保护您的凭据:

  1. 永不承诺 .env 文件 -它们包含您的API密钥
  2. 这 .gitignore 文件阻止 .env 从承诺
  3. 只承诺 .env.example 带有占位符值
  4. 如果您不小心提交了凭据:
    • 立即吊销API密钥
    • 创建一个新的
    • 使用 git filter-branch 或BFG Repo Cleaner将其从历史记录中删除

贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

记得: 永远不要承诺你的 .env 文件!

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。

致谢

支持

对于问题、疑问或贡献,请在GitHub上打开问题。