来源:modelcontextprotocol.io · Anthropic 公告 | 整理时间:2026-04-04
什么是 MCP?
Model Context Protocol(MCP) 是 Anthropic 于 2024 年 11 月发布的开放协议,旨在解决 LLM 与外部工具和数据源的集成问题。2025 年,MCP 被捐赠给 Linux 基金会,成为行业标准。
它解决什么问题?
在 MCP 出现之前,每个 AI 应用集成工具的方式都不同:
之前:
ChatGPT → 自定义插件系统
Claude → 自定义工具系统
Copilot → 自定义扩展系统
每个平台各自为战,工具无法复用
之后:
所有 LLM ←→ MCP 标准协议 ←→ 统一的工具生态
类比:MCP 之于 AI,就像 USB 之于电脑——一个标准接口连接所有设备。
协议架构
客户端-服务器模型
┌──────────────┐ MCP 协议 ┌──────────────┐
│ MCP 客户端 │ ←───────────→ │ MCP 服务器 │
│ (Claude等) │ JSON-RPC │ (工具提供者) │
└──────────────┘ └──────────────┘
│
┌──────┼──────┐
│ │ │
数据库 API 文件系统
核心原语
| 原语 | 说明 | 方向 | 类比 |
|---|---|---|---|
| Tools | 模型可调用的函数 | 服务器→模型 | API endpoint |
| Resources | 应用提供的上下文数据 | 服务器→模型 | GET 请求 |
| Prompts | 用户可调用的模板 | 服务器→用户 | 快捷指令 |
| Sampling | 服务器向 LLM 发送请求 | 服务器→模型 | 回调 |
传输方式
| 传输 | 场景 | 说明 |
|---|---|---|
| stdio | 本地进程 | 客户端启动服务器进程,通过 stdin/stdout 通信 |
| HTTP+SSE | 远程服务 | 通过 HTTP POST 发送请求,SSE 接收响应 |
协议细节
JSON-RPC 消息格式
请求(Client → Server):
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "query_database",
"arguments": {
"sql": "SELECT * FROM users LIMIT 10"
}
}
}
响应(Server → Client):
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [
{
"type": "text",
"text": "[{\"id\": 1, \"name\": \"Alice\"}, ...]"
}
]
}
}
能力协商
连接建立时,客户端和服务器交换各自支持的能力:
Client: 我支持 tools, resources, sampling
Server: 我提供 3 个 tools, 5 个 resources, 2 个 prompts
构建 MCP 服务器
TypeScript 示例
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "weather-server",
version: "1.0.0",
});
// 注册一个工具
server.tool(
"get_weather",
"获取指定城市的天气信息",
{ city: z.string().describe("城市名称") },
async ({ city }) => {
const weather = await fetchWeather(city);
return {
content: [{ type: "text", text: `${city}: ${weather.temp}°C, ${weather.condition}` }],
};
}
);
// 注册一个资源
server.resource(
"config",
"config://app",
async () => ({
contents: [{
uri: "config://app",
text: JSON.stringify(appConfig),
}],
})
);
// 注册一个提示模板
server.prompt(
"analyze-code",
{ code: z.string() },
async ({ code }) => ({
messages: [{
role: "user",
content: `分析以下代码的安全性和性能:\n\n${code}`,
}],
})
);
// 启动服务器
const transport = new StdioServerTransport();
await server.connect(transport);
Python 示例
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather-server")
@mcp.tool()
def get_weather(city: str) -> str:
"""获取指定城市的天气信息"""
weather = fetch_weather(city)
return f"{city}: {weather['temp']}°C, {weather['condition']}"
@mcp.resource("config://app")
def get_config() -> str:
"""获取应用配置"""
return json.dumps(app_config)
@mcp.prompt()
def analyze_code(code: str) -> str:
"""代码分析提示模板"""
return f"分析以下代码的安全性和性能:\n\n{code}"
mcp.run()
MCP 生态
官方服务器
Anthropic 和社区维护了一系列参考实现:
| 类别 | 服务器 | 功能 |
|---|---|---|
| 搜索 | Brave Search | 网页搜索 |
| 数据库 | PostgreSQL, SQLite | 数据库查询 |
| 文件 | Filesystem | 本地文件操作 |
| 代码 | GitHub | 仓库和 PR 管理 |
| 浏览器 | Playwright | 浏览器自动化 |
| 通信 | Slack | 消息收发 |
| 存储 | Google Drive | 文件访问 |
| 记忆 | Memory | 知识图谱 |
完整列表:https://github.com/modelcontextprotocol/servers
MCP 注册目录
社区维护的 MCP 服务器目录:https://mcp.so
支持的平台
| 平台 | MCP 支持 |
|---|---|
| Claude Desktop | 原生支持 |
| Claude Code | 原生支持 |
| VS Code (Copilot) | 支持 |
| Cursor | 支持 |
| 其他 IDE | 通过插件支持 |
MCP vs 其他协议
| 特性 | MCP | OpenAI Plugins | LangChain Tools |
|---|---|---|---|
| 标准化 | 开放标准 | 平台私有 | 框架私有 |
| 跨平台 | 所有 LLM | 仅 OpenAI | 仅 LangChain |
| 传输协议 | JSON-RPC | HTTP API | Python 函数 |
| 发现机制 | 能力协商 | 清单文件 | 注册表 |
| 社区治理 | Linux 基金会 | OpenAI | LangChain |
与其他协议的关系
A2A(Agent-to-Agent)
Google 推出的 A2A 协议解决 Agent 之间的通信,而 MCP 解决 Agent 与工具之间的连接。两者互补:
Agent A ←→ A2A 协议 ←→ Agent B
↓ ↓
MCP MCP
↓ ↓
工具/数据 工具/数据
安全考量
| 风险 | 缓解措施 |
|---|---|
| 恶意工具 | 只使用信任的 MCP 服务器 |
| 数据泄露 | 限制服务器访问的数据范围 |
| API Key 安全 | 通过环境变量传递 |
| 供应链攻击 | 审查服务器代码,锁定版本 |
相关链接
- 主流 Agent SDK 对比
- Agent 框架全景
- Claude Code MCP 集成
- MCP 官方文档:https://modelcontextprotocol.io/introduction
- Anthropic 公告:https://www.anthropic.com/news/model-context-protocol
- 官方服务器集合:https://github.com/modelcontextprotocol/servers
- TypeScript SDK:https://github.com/modelcontextprotocol/typescript-sdk
- Python SDK:https://github.com/modelcontextprotocol/python-sdk