🌐 运行现有MCP服务器:STDIO和HTTP协议
______________________________________________________________________
📌 项目概述
这个项目是 深入了解MCP传输协议 — 演示如何将Python应用程序连接到现实世界的MCP服务器 两者都使用 工作室 和 流式HTTP 传输层。
使用 Context7 MCP服务器,该项目显示了LLM如何检索 库的最新编码文档,如 fastmcp, pandas, 和 scikit-learn --使AI编码助手真正成为最新的。
域名: 代理AI——MCP传输协议\ 语言: Python 3.12+\ MCP服务器: Context7(LLM的实时库文档)\ 运输: STDIO (npx)+可流式传输HTTP
______________________________________________________________________
🏗️ 建筑
┌──────────────────────────────────────────────────────┐
│ Python Client / Jupyter Notebook │
│ │
│ from fastmcp import Client │
│ from fastmcp.client.transports import │
│ StdioTransport, StreamableHttpTransport │
└──────────┬─────────────────────────┬────────────────┘
│ │
STDIO Transport HTTP Transport
(Local subprocess) (Remote cloud)
│ │
┌──────▼──────┐ ┌────────▼────────┐
│ npx runs │ │ Context7 Cloud │
│ Context7 │ │ MCP Server │
│ on your │ │ mcp.context7. │
│ machine │ │ com/mcp │
└─────────────┘ └─────────────────┘
│ │
Both expose identical MCP tools:
├── resolve-library-id → Find library ID
└── query-docs → Get LLM-ready docs______________________________________________________________________
📂 项目结构
Run-Existing-MCP-Servers/
│
├── Run Existing MCP Servers.ipynb # Complete Jupyter lab notebook
└── README.md______________________________________________________________________
🛠️ 技术栈
| 组件 | 技术 |
|---|---|
| 语言 | Python 3.12+ |
| MCP客户端 | FastMCP客户端 |
| STDIO传输 | 标准传输(npx @upstash/context7-mcp) |
| HTTP传输 | 流式HttpTransport |
| 外部服务器 | Context7 MCP服务器 |
| 笔记本 | Jupyter |
| async | 异步+异步/等待 |
______________________________________________________________________
🔌 传输协议详解
1.️⃣ STDIO运输(本地)
一切都在你的机器上运行:
stdio_transport = StdioTransport(
command="npx",
args=["-y", "@upstash/context7-mcp"]
)
stdio_client = Client(stdio_transport)Your Python Code ←→ [StdioTransport] ←→ Context7 Server (your machine)
(Client) (Bridge) (npx subprocess)它是如何工作的:
npx临时下载并运行Context7 MCP服务器StdioTransport桥接Python↔ 通过STDIN/STDOUT连接服务器- 一切都是本地的,不需要网络
______________________________________________________________________
2.️⃣ 流式HTTP传输(远程)
连接到云托管的MCP服务器:
http_transport = StreamableHttpTransport(
url="https://mcp.context7.com/mcp"
)
http_client = Client(http_transport)Your Python Code ←→ [StreamableHttpTransport] ←→ Context7 Cloud Server
(Client) (Bridge) (mcp.context7.com)______________________________________________________________________
🔧 Context7 MCP工具
| 工具 | 输入 | 输出 |
|---|---|---|
resolve-library-id | libraryName: str, query: str | Context7库ID(例如。 /pandas-dev/pandas) |
query-docs | libraryId: str, query: str | LLM优化文档、代码示例、API参考文献 |
工具调用模式
# Step 1 — Find library ID
async with stdio_client as client:
response = await client.call_tool("resolve-library-id", {
"libraryName": "fastmcp",
"query": "I want to create a new MCP server"
})
library_id = response.content[0].text
# Step 2 — Get documentation
async with stdio_client as client:
docs = await client.call_tool("query-docs", {
"libraryId": library_id,
"query": "How to create tools in FastMCP"
})______________________________________________________________________
📚 这个实验室涵盖了什么
| 主题 | 详细信息 |
|---|---|
| STDIO概念 | STDIN、STDOUT、STDERR——理论+实例 |
| StdioTransport | 通过npx将MCP服务器作为子流程启动 |
| StreamableHttpTransport | 连接到远程云MCP服务器 |
| FastMCP客户端 | 异步上下文管理器模式 |
| list_tools() | 从服务器发现可用工具 |
| call_tool() | 使用参数执行工具 |
| 工具检查 | 访问 .name, .description, .inputSchema |
| Context7工作流 | 解析库id→ 查询文档模式 |
| 查询库 | fastmcp、pandas、scikit-learn |
| 异步模式 | async with, await,非阻塞请求 |
______________________________________________________________________
⚙️ 如何跑步
步骤1——安装依赖项:
pip install fastmcp
npm install -g npx # For STDIO transport步骤2——打开Jupyter笔记本:
jupyter notebook "Run Existing MCP Servers.ipynb"步骤3——按顺序运行单元格:
- STDIO部分→ 通过本地子进程连接
- HTTP部分→ 通过云端点连接
- 工具列表→ 请参阅可用的Context7工具
- 工具调用→ 获取实时图书馆文档
______________________________________________________________________
🔑 关键概念——相同的接口,不同的传输
# STDIO — local process
async with Client(StdioTransport("npx", ["-y", "@upstash/context7-mcp"])) as client:
tools = await client.list_tools() # Identical API!
# HTTP — remote cloud
async with Client(StreamableHttpTransport("https://mcp.context7.com/mcp")) as client:
tools = await client.list_tools() # Same API!一旦连接,无论传输方式如何,MCP接口都是相同的! ✅
______________________________________________________________________
🎓 技能展示
- MCP传输协议比较——STDIO与HTTP
- 具有异步上下文管理器模式的FastMCP客户端
- StdioTransport——基于子流程的MCP服务器启动
- StreamableHttpTransport——远程云MCP连接
- 工具发现
list_tools() - 工具执行
call_tool()+参数 - 工具模式检查(名称、描述、inputSchema)
- Context7两步文档工作流程
- 面向LLM的实时图书馆文献检索
- STDIO基础——STDIN、STDOUT、STDERR
- 异步Python——异步、等待、非阻塞I/O
- 基于Jupyter笔记本的MCP开发
______________________________________________________________________
📜 认证
| 认证 | 发卡机构 | 平台 |
|---|---|---|
| IBM数据科学专业证书 | IBM | Coursera |
| IBM生成人工智能专业证书 | IBM | Coursera |
| IBM RAG和代理人工智能专业证书 | IBM | Coursera |
______________________________________________________________________
🤝 与我联系
  ](https://github.com/Leelaissakattaota)
