使用说明
🌈 MCP服务器的Hello World
📌 项目概述
这就是 MCP基础项目 --亲力亲为的Hello World 使用FastMCP实现模型上下文协议(MCP)。
它演示了所有三个MCP原语(工具、资源、提示), 三个传输层(STDIO、HTTP、内存),并与 LangChain ReAct代理商 和 LangGraph 打造智能 多服务器AI代理。
域名: 代理AI——MCP基础
语言: Python 3.10+
运输: 内存·STDIO·HTTP
代理框架: 语言链+语言图
🏗️ 建筑
┌──────────────────────────────────────────────────┐
│ LangChain ReAct Agent │
│ OpenAI GPT + LangGraph │
│ Multi-server MCP tool discovery & calling │
└──────────┬───────────────┬────────────────┬─────┘
│ │ │
In-Memory STDIO HTTP (8000)
│ │ │
┌──────────▼───────────────▼────────────────▼─────┐
│ CalculatorMCPServer │
│ │
│ 🔧 Tools │
│ ├── add(a, b) → a + b │
│ └── subtract(a, b) → a - b │
│ │
│ 📦 Resources │
│ ├── file:///endpoint/{name} → template docs │
│ └── file://endpoint2/{name} → actual files │
│ (from path/ directory) │
│ │
│ 💬 Prompts │
│ └── review_code(code) → Code Review prompt │
└──────────────────────────────────────────────────┘
📂 项目结构
mcp-hello-world/
│
├── stdio_server.py # Standalone STDIO server
├── FINAL Hello World of MCP Servers.ipynb # Complete Jupyter notebook
└── README.md
🛠️ 技术栈
| 组件 | 技术 | 版本 |
|---|---|---|
| MCP框架 | FastMCP | 2.12.2 |
| 语言 | Python | 3.10+ |
| 代理框架 | LangChain | 0.3.27 |
| MCP↔ LangChain桥接器 | LangChain mcp适配器 | 0.1.9 |
| 代理图 | LangGraph | 0.6.7 |
| LLM | OpenAI(通过语言链OpenAI) | 0.3.33 |
| 笔记本 | Jupyter | - |
🔧 MCP服务器——计算器MCPServer
工具(共2个)
| 工具 | 参数 | 返回值 | 描述 |
|---|---|---|---|
add |
a: int, b: int |
int |
将两个整数相加--返回a+b |
subtract |
a: int, b: int |
int |
从a中减去b--返回a-b |
资源(共2个)
| URI模板 | 源 | 描述 |
|---|---|---|
file:///endpoint/{name} |
内存中模板 | 返回格式化的模板字符串 |
file://endpoint2/{name} |
path/ 目录 |
从文件系统读取实际文件 |
提示(共1条)
| 提示 | 参数 | 描述 |
|---|---|---|
review_code |
code: str |
生成结构化代码审查提示 |
🚀 三种运输方式
1.️⃣ 内存传输(最简单)
服务器和客户端在同一个Python进程中运行:
from fastmcp import FastMCP, Client
mcp = FastMCP("CalculatorMCPServer")
client = Client(mcp) # Direct in-memory connection
2.️⃣ STDIO运输(本地)
服务器作为子进程运行——客户端通过stdin/stdout进行通信:
# Run server
python stdio_server.py
# Client connects via STDIO
client = Client("stdio_server.py")
3.️⃣ HTTP传输(远程)
服务器在端口8000上独立运行——支持多个客户端:
mcp.run(transport="http", host="127.0.0.1", port=8000)
client = Client("http://127.0.0.1:8000/mcp")
🤖 LangChain ReAct代理集成
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
# Load MCP tools into LangChain format
tools = await load_mcp_tools(session)
# Create ReAct agent with MCP tools
model = ChatOpenAI(model="gpt-4o-mini")
agent = create_react_agent(model, tools)
# Agent autonomously discovers & calls MCP tools
result = agent.invoke({"messages": "What is 15 + 27?"})
# → Agent calls add(15, 27) → Returns 42
⚙️ 如何跑步
步骤1——安装依赖项:
pip install fastmcp==2.12.2 langchain==0.3.27 langchain_mcp_adapters==0.1.9 langgraph==0.6.7 langchain_openai==0.3.33
步骤2-设置OpenAI API密钥:
export OPENAI_API_KEY="your-api-key-here"
步骤3——运行Jupyter记事本:
jupyter notebook "FINAL Hello World of MCP Servers.ipynb"
步骤4——或者直接运行STDIO服务器:
python stdio_server.py
📚 这个实验室涵盖了什么
| 主题 | 描述 |
|---|---|
| FastMCP基础 | 使用名称+说明创建MCP服务器 |
@mcp.tool |
将Python函数注册为可发现的工具 |
@mcp.resource |
通过URI模板公开数据 |
@mcp.prompt |
创建可重用的提示模板 |
| 内存传输 | 同一进程客户端-服务器连接 |
| STDIO传输 | 基于子进程的通信 |
| HTTP传输 | 远程网络通信 |
| 端口管理 | 在HTTP服务器之前检查端口可用性 |
| LangChain工具 | @tool 装饰器与MCP工具的比较 |
| MCP适配器 | 将MCP工具转换为LangChain格式 |
| ReAct代理 | 带MCP工具调用的LangGraph代理 |
| 多服务器客户端 | 同时连接到多个MCP服务器 |
🎓 技能展示
- 使用FastMCP创建MCP服务器
- 所有三个MCP原语——工具、资源、提示
- 三个传输层——内存、STDIO、HTTP
- LangChain工具定义
@tool装饰器 - 主控程序↔ LangChain适配器集成
- 使用MCP工具调用LangGraph ReAct代理
- 多服务器MCP客户端架构
- 异步Python——异步+等待模式
- MCP资源的URI模板设计
- 带错误处理的文件系统资源读取
- HTTP服务器的端口可用性检查
- 基于Jupyter笔记本的AI开发工作流程
📜 认证
| 认证 | 发卡机构 | 平台 |
|---|---|---|
| IBM数据科学专业证书 | IBM | Coursera |
| IBM生成人工智能专业证书 | IBM | Coursera |
| IBM RAG和代理人工智能专业证书 | IBM | Coursera |