MCP 协议深度解析:给 AI 装上"万能接口",终于不用每个工具都写适配器了

你有没有这种体验:想让 AI 帮你查个数据库,得写一套适配器;想让它调用公司内部 API,又得写一套;想让它读个文件,还得写一套。每个 AI 工具(Claude、GPT、Gemini)的接口都不一样,同样的功能要重复开发 N 遍。

这就像早期的手机充电器——每家厂商一个接口,出门得带一堆线。

MCP 协议做的事情,就是给 AI 装上一个”USB-C 接口”。

一、MCP 是什么?

MCP = Model Context Protocol = 模型上下文协议

一句话定义:MCP 是一个开源标准,用于连接 AI 应用和外部系统。

用 MCP,AI 应用(比如 Claude、ChatGPT)可以连接到:

  • 数据源:本地文件、数据库、API
  • 工具:搜索引擎、计算器、代码执行器
  • 工作流:专业的提示词模板、自动化流程

官方比喻:AI 的 USB-C

MCP 官方用了一个很形象的比喻:

1
2
3
4
5
6
7
8
9
10
11
USB-C 之于硬件 = MCP 之于 AI 应用

USB-C:
- 一个接口,连接所有设备(U盘、键盘、鼠标、显示器)
- 即插即用,不用装驱动
- 统一标准,所有厂商都支持

MCP:
- 一个协议,连接所有工具(数据库、API、文件系统)
- 即插即用,不用写适配器
- 统一标准,所有 AI 都能用

MCP 解决了什么问题?

没有 MCP 之前

假设你有 M 个 AI 应用和 N 个外部工具,你需要写 M×N 个适配器。

1
2
3
4
5
6
         数据库    GitHub    Slack    邮件    文件系统
Claude ❌ ❌ ❌ ❌ ❌
GPT ❌ ❌ ❌ ❌ ❌
Gemini ❌ ❌ ❌ ❌ ❌

每个格子都要写一套代码,总共 3×5 = 15 套代码!

有了 MCP 之后

1
2
3
4
5
6
         数据库    GitHub    Slack    邮件    文件系统
Claude ✅ ✅ ✅ ✅ ✅
GPT ✅ ✅ ✅ ✅ ✅
Gemini ✅ ✅ ✅ ✅ ✅

每个工具写一个 MCP Server,总共只需要 5 套代码!

二、MCP 的核心架构

2.1 三个核心角色

MCP 采用客户端-服务器架构,有三个核心角色:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
┌─────────────────────────────────────────────────────────┐
│ MCP Host(宿主应用) │
│ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ MCP Client(客户端) │ │
│ │ 负责与 MCP Server 建立连接和通信 │ │
│ └─────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘

│ 标准化协议(JSON-RPC 2.0)

┌─────────────────────────────────────────────────────────┐
│ MCP Server(服务器) │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Tools │ │ Resources │ │ Prompts │ │
│ │ 工具 │ │ 资源 │ │ 提示词 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘


┌─────────────────────┐
│ 外部系统/服务 │
│ 数据库、API、文件 │
└─────────────────────┘
角色 说明 例子
MCP Host AI 应用,协调和管理多个 MCP Client Claude Desktop、Cursor、你的自定义应用
MCP Client 客户端组件,维护与 MCP Server 的连接 内置在 Host 中,一个 Host 可以有多个 Client
MCP Server 服务器,提供具体的工具和数据 文件系统 Server、数据库 Server、GitHub Server

2.2 连接方式

MCP 支持两种连接方式:

连接方式 说明 适用场景
STDIO 标准输入输出 本地 Server,一个 Client 对应一个 Server
Streamable HTTP HTTP 流式传输 远程 Server,一个 Server 服务多个 Client
1
2
3
4
5
6
7
8
9
10
11
12
13
STDIO 连接(本地):
┌─────────┐ stdin/stdout ┌─────────┐
│ Client │ ◄──────────────► │ Server │
└─────────┘ └─────────┘

Streamable HTTP 连接(远程):
┌─────────┐ ┌─────────┐
│ Client 1 │ ──── HTTP ────► │ │
├─────────┤ │ Server │
│ Client 2 │ ──── HTTP ────► │ │
├─────────┤ │ │
│ Client 3 │ ──── HTTP ────► │ │
└─────────┘ └─────────┘

三、MCP Server 的三大能力

MCP Server 可以向 AI 应用提供三种类型的能力:

3.1 Tools(工具)—— 让 AI 能”做事”

Tools 是 AI 可以调用的函数,通常有副作用(会改变外部状态)。

1
工具 = AI 的"手"

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"name": "query_database",
"description": "查询 PostgreSQL 数据库",
"inputSchema": {
"type": "object",
"properties": {
"sql": {
"type": "string",
"description": "SQL 查询语句"
},
"database": {
"type": "string",
"description": "数据库名称",
"default": "mydb"
}
},
"required": ["sql"]
}
}

AI 调用流程

1
2
3
4
5
6
7
8
9
10
11
用户:帮我查一下有多少用户

AI:我需要调用 query_database 工具

MCP Client:发送工具调用请求

MCP Server:执行 SQL 查询

结果:有 1024 个用户

AI:数据库里有 1024 个用户

常见工具类型

类型 示例
数据库操作 查询、插入、更新、删除
API 调用 发送邮件、调用第三方服务
文件操作 读写文件、创建目录
代码执行 运行脚本、编译代码
系统操作 执行命令、管理进程

3.2 Resources(资源)—— 让 AI 能”看”

Resources 是 AI 可以读取的数据源,类似 REST API 的 GET 端点。

1
资源 = AI 的"眼睛"

示例

1
2
3
4
5
6
{
"uri": "file:///etc/config.yaml",
"name": "应用配置文件",
"description": "应用的 YAML 配置文件",
"mimeType": "text/yaml"
}

AI 读取流程

1
2
3
4
5
6
7
8
9
10
11
用户:帮我看看配置文件里有什么

AI:我需要读取 file:///etc/config.yaml 资源

MCP Client:发送资源读取请求

MCP Server:读取文件内容

结果:配置文件内容

AI:配置文件里有以下内容...

常见资源类型

类型 示例
文件 配置文件、日志文件、代码文件
数据库 表结构、数据样本
API 用户信息、订单数据
文档 API 文档、使用说明

3.3 Prompts(提示词)—— 让 AI 能”用模板”

Prompts 是预定义的提示词模板,封装了最佳实践。

1
提示词 = AI 的"参考手册"

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"name": "code_review",
"description": "代码审查模板",
"arguments": [
{
"name": "code",
"description": "要审查的代码",
"required": true
},
{
"name": "language",
"description": "编程语言",
"required": false
}
]
}

AI 使用流程

1
2
3
4
5
6
7
8
9
用户:帮我审查这段代码

AI:我使用 code_review 提示词模板

MCP Client:获取提示词模板

MCP Server:返回模板内容

AI:根据模板审查代码

常见提示词类型

类型 示例
代码审查 检查代码质量、安全性
数据分析 分析数据、生成报告
文档生成 生成 API 文档、README
测试用例 生成单元测试、集成测试

四、MCP 的通信协议

4.1 JSON-RPC 2.0

MCP 使用 JSON-RPC 2.0 作为底层通信协议。

请求格式

1
2
3
4
5
6
7
8
9
10
11
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "query_database",
"arguments": {
"sql": "SELECT COUNT(*) FROM users"
}
}
}

响应格式

1
2
3
4
5
6
7
8
9
10
11
12
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [
{
"type": "text",
"text": "1024"
}
]
}
}

4.2 生命周期

MCP 连接有完整的生命周期:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. 初始化
Client → Server: initialize
Server → Client: initialize response
Client → Server: initialized

2. 正常通信
Client → Server: tools/list
Server → Client: tools list
Client → Server: tools/call
Server → Client: tool result

3. 关闭
Client → Server: shutdown
Server → Client: shutdown response

五、实战:从零搭建 MCP Server

5.1 环境准备

1
2
3
4
5
6
7
8
9
10
11
12
# 创建项目
mkdir my-mcp-server
cd my-mcp-server

# 初始化 Node.js 项目
npm init -y

# 安装 MCP SDK
npm install @modelcontextprotocol/sdk

# 安装 TypeScript(可选)
npm install -D typescript @types/node

5.2 完整示例:天气查询 Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";

// 1. 创建 MCP Server
const server = new Server(
{
name: "weather-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);

// 2. 定义工具列表
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "get_weather",
description: "获取指定城市的当前天气",
inputSchema: {
type: "object",
properties: {
city: {
type: "string",
description: "城市名称,例如:北京、上海、广州",
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
description: "温度单位",
default: "celsius",
},
},
required: ["city"],
},
},
{
name: "get_forecast",
description: "获取指定城市的未来天气预报",
inputSchema: {
type: "object",
properties: {
city: {
type: "string",
description: "城市名称",
},
days: {
type: "number",
description: "预报天数(1-7)",
default: 3,
},
},
required: ["city"],
},
},
],
};
});

// 3. 处理工具调用
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;

switch (name) {
case "get_weather": {
const city = args?.city as string;
const unit = (args?.unit as string) || "celsius";

// 这里可以调用真实的天气 API
const weather = await fetchWeather(city);

return {
content: [
{
type: "text",
text: JSON.stringify(
{
city: weather.city,
temperature:
unit === "celsius"
? `${weather.temp_c}°C`
: `${weather.temp_f}°F`,
condition: weather.condition,
humidity: `${weather.humidity}%`,
wind: `${weather.wind_kph} km/h`,
},
null,
2
),
},
],
};
}

case "get_forecast": {
const city = args?.city as string;
const days = (args?.days as number) || 3;

const forecast = await fetchForecast(city, days);

return {
content: [
{
type: "text",
text: JSON.stringify(forecast, null, 2),
},
],
};
}

default:
throw new Error(`Unknown tool: ${name}`);
}
});

// 4. 辅助函数:调用天气 API
async function fetchWeather(city: string) {
// 示例:使用 wttr.in API
const response = await fetch(
`https://wttr.in/${encodeURIComponent(city)}?format=j1`
);
const data = await response.json();
const current = data.current_condition[0];

return {
city: city,
temp_c: parseInt(current.temp_C),
temp_f: parseInt(current.temp_F),
condition: current.weatherDesc[0].value,
humidity: parseInt(current.humidity),
wind_kph: parseInt(current.windspeedKmph),
};
}

async function fetchForecast(city: string, days: number) {
const response = await fetch(
`https://wttr.in/${encodeURIComponent(city)}?format=j1`
);
const data = await response.json();

return data.weather.slice(0, days).map((day: any) => ({
date: day.date,
max_temp: `${day.maxtempC}°C`,
min_temp: `${day.mintempC}°C`,
condition: day.hourly[4].weatherDesc[0].value,
}));
}

// 5. 启动服务器
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Weather MCP Server running on stdio");
}

main().catch(console.error);

5.3 配置 Claude Desktop

在 Claude Desktop 的配置文件中添加:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json

1
2
3
4
5
6
7
8
{
"mcpServers": {
"weather": {
"command": "npx",
"args": ["tsx", "/path/to/weather-server/index.ts"]
}
}
}

5.4 测试使用

重启 Claude Desktop,然后你可以说:

“帮我查一下北京今天天气怎么样”

Claude 会自动调用你的天气 MCP Server,返回结果。


六、MCP 的生态系统

6.1 支持 MCP 的客户端

客户端 类型 说明
Claude Desktop AI 助手 Anthropic 官方客户端
Claude Code 编程工具 Anthropic 的编程 Agent
Cursor 代码编辑器 AI 驱动的 IDE
Windsurf 代码编辑器 Codeium 的 AI IDE
Cline VS Code 插件 开源 AI 编程助手
VS Code 代码编辑器 微软的 IDE,内置 MCP 支持
ChatGPT AI 助手 OpenAI 的 AI 助手

6.2 官方提供的 MCP Server

Server 功能 GitHub
filesystem 文件系统操作 @modelcontextprotocol/server-filesystem
github GitHub API 集成 @modelcontextprotocol/server-github
gitlab GitLab API 集成 @modelcontextprotocol/server-gitlab
google-drive Google Drive 集成 @modelcontextprotocol/server-gdrive
postgres PostgreSQL 数据库 @modelcontextprotocol/server-postgres
sqlite SQLite 数据库 @modelcontextprotocol/server-sqlite
slack Slack API 集成 @modelcontextprotocol/server-slack
puppeteer 浏览器自动化 @modelcontextprotocol/server-puppeteer
brave-search Brave 搜索 @modelcontextprotocol/server-brave-search
google-maps Google Maps @modelcontextprotocol/server-google-maps

6.3 社区 MCP Server

MCP 生态系统正在快速发展,社区贡献了大量 Server:

  • 数据库:MySQL、MongoDB、Redis、Elasticsearch
  • 云服务:AWS、GCP、Azure
  • 开发工具:Jira、Linear、Notion
  • 通讯工具:Discord、Telegram、Email
  • AI 工具:Hugging Face、Replicate

完整的 Server 列表:https://github.com/modelcontextprotocol/servers


七、MCP 的安全考虑

7.1 权限控制

MCP Server 应该实现严格的权限控制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 示例:限制文件访问范围
const ALLOWED_PATHS = ["/home/user/documents", "/home/user/projects"];

server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "read_file") {
const path = request.params.arguments?.path as string;

// 检查路径是否在允许范围内
if (!ALLOWED_PATHS.some(p => path.startsWith(p))) {
throw new Error("Access denied: path not allowed");
}

// 执行读取操作
return await readFile(path);
}
});

7.2 输入验证

1
2
3
4
5
6
7
8
9
10
11
// 使用 JSON Schema 验证输入
const schema = {
type: "object",
properties: {
sql: {
type: "string",
pattern: "^(SELECT|select)", // 只允许 SELECT 查询
},
},
required: ["sql"],
};

7.3 最佳实践

实践 说明
最小权限原则 只授予必要的权限
输入验证 验证所有输入参数
日志记录 记录所有工具调用
速率限制 防止滥用
敏感数据保护 不要在日志中记录敏感信息

八、MCP 与其他方案对比

8.1 MCP vs Function Calling

对比项 MCP Function Calling
标准化 ✅ 统一标准 ❌ 各模型不同
可复用 ✅ 一次开发 ❌ 每个模型重写
生态 ✅ 社区丰富 ⚠️ 依赖厂商
安全性 ✅ Server 端控制 ⚠️ 取决于实现
学习成本 ⚠️ 中等 ✅ 低

8.2 MCP vs 自定义适配器

对比项 MCP 自定义适配器
开发效率 ✅ 高 ❌ 低
维护成本 ✅ 低 ❌ 高
可复用性 ✅ 高 ❌ 低
标准化 ✅ 是 ❌ 否
灵活性 ⚠️ 中 ✅ 高

九、真实案例:OpenAI 用 MCP 构建语音 Agent

OpenAI 官方发布了 MCP-Powered Agentic Voice Framework,展示了 MCP 在企业级应用中的价值。

场景:保险公司的电话客服

用户打电话问:”我的保单覆盖哪些疾病?”

1
2
3
4
5
6
7
8
9
10
11
12
13
用户打电话:"我的保单覆盖哪些疾病?"

语音识别 → 文字

AI Agent 收到问题

通过 MCP 调用"知识库检索" Server

查询保单数据库,找到相关信息

AI 生成回答

语音合成 → 播放给用户

架构设计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
┌─────────────────────────────────────────────────────────┐
│ 用户(语音输入) │
└─────────────────────────┬───────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│ OpenAI Agents SDK(编排层) │
│ 负责协调多个 Agent 协同工作 │
└─────────────────────────┬───────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│ MCP 协议(中间件) │
│ 标准化接口,连接 Agent 和外部工具 │
└───────┬─────────────┬─────────────┬─────────────────────┘
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ 文本检索 │ │ Web搜索 │ │ SQLite │
│ MCP服务 │ │ MCP服务 │ │ MCP服务 │
└─────────┘ └─────────┘ └─────────┘

为什么用 MCP?

  1. Agent 和工具解耦:换工具不用改 Agent 代码
  2. 工具可插拔:想加新的数据源?装个新的 MCP Server 就行
  3. 多 Agent 协同:不同 Agent 可以共享同一套 MCP Server

十、学习资源

官方资源

资源 链接
官方文档 https://modelcontextprotocol.io/
GitHub https://github.com/modelcontextprotocol
MCP Server 列表 https://github.com/modelcontextprotocol/servers
MCP Inspector https://github.com/modelcontextprotocol/inspector

SDK 支持

语言 SDK
TypeScript @modelcontextprotocol/sdk
Python mcp
Java io.modelcontextprotocol:sdk
Kotlin io.modelcontextprotocol:sdk-kotlin
C# ModelContextProtocol

学习路线

阶段 内容 时间
1️⃣ 入门 理解概念、使用现成 Server 1-2 天
2️⃣ 实践 配置 Claude Desktop、体验 MCP 1-2 天
3️⃣ 开发 搭建自己的 MCP Server 3-5 天
4️⃣ 进阶 实现 Resources 和 Prompts 1 周
5️⃣ 生产 安全性、性能优化、部署 1-2 周

总结

MCP 解决了一个简单但重要的问题:让 AI 能用统一的方式调用各种外部工具

以前:每个 AI 模型一套规则,每个工具一套适配,混乱不堪。

现在:一个标准,所有 AI 都能用,所有工具都能接。

就像 USB-C 统一了充电接口,MCP 正在统一 AI 的工具调用接口。

如果你想让 AI 能做更多事情,MCP 是目前最好的方案。


参考资源