wikipedia
🚀 FastAPI + FastMCP + LangChain Wikipedia Agent
一个由 AI 驱动的 Wikipedia 研究代理,演示了 FastAPI、FastMCP、LangChain 和 LangGraph 如何协同工作,构建一个模块化的工具使用型 AI 应用。
该项目通过 MCP(模型上下文协议)服务器 暴露 Wikipedia 能力,并允许 LangChain/LangGraph 代理自动发现和使用这些工具。
🏗️ 架构
┌──────────────────────┐
│ User │
│ Natural Language │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ FastAPI │
│ Application API │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ LangChain Agent │
│ LangGraph │
└──────────┬───────────┘
│
MCP Protocol
│
▼
┌──────────────────────┐
│ FastMCP │
│ MCP Server │
└──────────┬───────────┘
│
┌──────────┴───────────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Wikipedia │ │ Wikipedia │
│ Search │ │ Page │
└──────────────┘ └──────────────┘Related MCP server: MCP Wikipedia Server
✨ 功能特性
🤖 由 LLM 驱动的 Wikipedia 研究
🔌 模型上下文协议(MCP)集成
⚡ FastAPI 应用层
🧠 LangChain 代理集成
🔄 LangGraph ReAct 代理
🔎 Wikipedia 搜索工具
📄 Wikipedia 页面检索工具
🔗 MCP 工具发现
📡 基于 stdio 的 MCP 通信
🧩 模块化架构,可轻松支持更多工具
🛠️ 技术栈
技术 | 用途 |
Python | 核心编程语言 |
FastAPI | API/应用层 |
FastMCP | MCP 服务器与工具实现 |
LangChain | LLM 与工具集成 |
LangGraph | 代理工作流 |
Requests | Wikipedia API 请求 |
Wikipedia REST API | 外部知识源 |
🔧 MCP 工具
FastMCP 服务器暴露了两个工具。
search_wikipedia
搜索指定主题的 Wikipedia 内容。
@mcp.tool
def search_wikipedia(query: str):
...示例:
Search Wikipedia for Albert Einstein代理可以自动决定调用:
search_wikipedia("Albert Einstein")get_wikipedia_page
检索特定 Wikipedia 页面的内容。
@mcp.tool
def get_wikipedia_page(title: str):
...示例:
Get the Wikipedia page for Artificial Intelligence代理可以调用:
get_wikipedia_page("Artificial Intelligence")🔄 MCP 在此项目中的工作原理
MCP 服务器使用以下方式运行:
mcp.run(transport="stdio")LangChain 客户端连接到 MCP 服务器:
client = MultiServerMCPClient(
{
"wikipedia": {
"command": "python",
"args": ["server.py"],
"transport": "stdio",
}
}
)客户端随后发现可用的 MCP 工具:
tools = await client.get_tools()这些工具被传递给 LangGraph 代理:
agent = create_react_agent(
model,
tools
)然后,LLM 可以根据用户的请求决定使用哪个工具。
⚙️ 安装
1. 克隆仓库
git clone https://github.com/YOUR_USERNAME/langchain-mcp-wikipedia.gitcd langchain-mcp-wikipedia2. 创建虚拟环境
Windows:
python -m venv venv
venv\Scripts\activateLinux/macOS:
python3 -m venv venv
source venv/bin/activate3. 安装依赖
pip install -r requirements.txt🔐 环境变量
创建 .env 文件:
OPENAI_API_KEY=your_openai_api_key🧠 为什么选择 MCP?
传统的工具集成通常会将 LLM 应用与各个 API 紧密耦合。
MCP 提供了一种标准化的方式,将能力以工具的形式暴露出来。
LLM
│
▼
LangChain / LangGraph
│
▼
MCP Client
│
▼
MCP Server
│
├── Wikipedia
├── Search
├── Database
├── APIs
└── Custom Tools这种分离使得工具可以在不同的 AI 应用和代理之间复用。
📌 演示的关键概念
该项目有助于学习:
模型上下文协议(MCP)
FastMCP
MCP 服务器
MCP 客户端
stdio 传输
LangChain 工具集成
LangGraph 代理
ReAct 代理
FastAPI
外部 API 集成
LLM 工具调用
模块化 AI 代理架构
This server cannot be deployed
Maintenance
Related MCP Connectors
Search Wikipedia, read summaries and full text, target sections, find nearby pages, list languages.
Google search, news, maps, scholar and public webpages for AI agents, with Markdown results.
Web search, point-in-time crypto market data, verified strategy track records and 160+ agent tools.
461Direct access to 60+ scraping and search tools. Extract structured data from Google (Search, Maps, Trends), Amazon, Airbnb, Social Media, and any web page directly into your AI agent.
Related MCP Servers
- AlicenseBqualityCmaintenanceA Model Context Protocol server that retrieves information from Wikipedia to provide context to LLMs, allowing users to search articles, get summaries, full content, sections, and links from Wikipedia.224,717 PyPI299MIT
- FlicenseNot gradedqualityDmaintenanceA production-ready server that provides Wikipedia search and content retrieval tools through the Model Context Protocol, enabling AI assistants to search for articles, list sections, and retrieve specific content.-
- AlicenseAqualityCmaintenanceEnables LLMs to search for keywords and fetch full page content from Wikipedia across various languages. It provides direct access to Wikipedia information through search and fetch tools.24MIT
- AlicenseNot gradedqualityFmaintenanceEnables AI agents to search, read, and explore Wikipedia articles via tools like summaries, categories, and random articles, with no API keys required.1MIT