# 第七阶段 · 模块七 · 第一节:MCP 协议概述
什么是 MCP(Model Context Protocol)?MCP 的架构是什么?Claude Code 如何集成 MCP?
Claude Code 全局架构
┌─────────────────────────────────────────────────────────────────────┐
│ MCP 系统 ← 本节 │
│ │
│ MCP Client ──> 连接 MCP 服务器 │
│ MCP Tools ──> 注册为 Claude Code 工具 │
│ MCP Resources ──> 提供上下文资源 │
└─────────────────────────────────────────────────────────────────────┘
MCP = Model Context Protocol
一种让 AI 模型与外部工具/资源交互的标准协议
┌─────────────────────────────────────────────────────────────────────┐
│ Claude Code │
│ │
│ ┌─────────────┐ MCP ┌─────────────┐ │
│ │ Client │◄────────────►│ Server │ │
│ └─────────────┘ └─────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Tools │ │ Resources │ │
│ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
| 方面 | MCP | 传统 API |
| 标准化 | 统一协议 | 各家不同 |
| 工具发现 | 自动发现 | 手动配置 |
| 认证 | 内置 OAuth | 自定义 |
| 实时性 | 推送支持 | 轮询 |
问 1:MCP 解决什么问题?
// 问题:每个工具都需要单独的集成代码
// 传统方式
claude.use(toolA); // 工具 A
claude.use(toolB); // 工具 B
claude.use(toolC); // 工具 C
// MCP 方式
claude.connect(mcpServer); // 自动发现所有工具
源码位置:`src/services/mcp/client.ts`
// MCP Client 来自 @modelcontextprotocol/sdk
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
// 传输层
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
// 1. HTTP + SSE(服务器推送)
const sseTransport = new SSEClientTransport(
new URL('https://mcp-server.example.com/sse')
);
// 2. STDIO(本地进程)
const stdioTransport = new StdioClientTransport({
command: 'npx',
args: ['mcp-server', '--stdio'],
});
// 3. Streamable HTTP
const httpTransport = new StreamableHTTPClientTransport(
new URL('https://mcp-server.example.com/mcp')
);
问 1:什么时候用哪种传输?
// SSE:远程服务器,支持推送
// → 需要实时通知的场景
// STDIO:本地工具,通过子进程通信
// → 文件系统工具、CLI 工具
// Streamable HTTP:云服务,RESTful
// → API 型服务
源码位置:`src/services/mcp/client.ts`
// 创建 MCP Client
const client = new Client(
{
name: 'claude-code',
version: '1.0.0',
},
{
capabilities: {
tools: {},
resources: {},
prompts: {},
},
}
);
// 连接到服务器
await client.connect(transport);
// 列出服务器提供的工具
const tools = await client.request(
{ method: 'tools/list' },
ListToolsResultSchema
);
console.log(tools);
// {
// tools: [
// { name: 'filesystem_read', description: 'Read a file', ... },
// { name: 'filesystem_write', description: 'Write a file', ... }
// ]
// }
问 1:MCP 工具有什么特点?
// MCP 工具格式
interface MCPTool {
name: string; // 工具名
description: string; // 描述
inputSchema: { // JSON Schema
type: 'object';
properties: { ... };
required?: string[];
};
}
┌─────────────────────────────────────────────────────────────────────┐
│ Claude Code │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ MCPConnectionManager │ │
│ │ │ │
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
│ │ │ Client1 │ │ Client2 │ │ ClientN │ ← 每个 MCP 服务器 │ │
│ │ └────┬────┘ └────┬────┘ └────┬────┘ │ │
│ │ │ │ │ │ │
│ │ └────────────┼────────────┘ │ │
│ │ ▼ │ │
│ │ ┌─────────────┐ │ │
│ │ │ Tools Registry│ ← 转换为 Claude Code 工具 │ │
│ │ └─────────────┘ │ │
│ └──────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
源码位置:`src/services/mcp/MCPConnectionManager.tsx`
// MCP 工具注册为 Claude Code 工具
class MCPConnectionManager {
async connect(server: MCPServerConfig): Promise<void> {
// 1. 创建客户端
const client = new Client(...);
// 2. 连接到服务器
await client.connect(transport);
// 3. 发现工具
const { tools } = await client.request(
{ method: 'tools/list' },
ListToolsResultSchema
);
// 4. 注册为 Claude Code 工具
for (const tool of tools) {
registerTool({
name: `mcp__${server.name}__${tool.name}`,
handler: async (input) => {
return client.request(
{ method: 'tools/call', params: { name: tool.name, arguments: input } },
CallToolResultSchema
);
},
});
}
}
}
问 1:MCP 工具命名规则?
// 格式:mcp__{服务器名}__{工具名}
// 避免命名冲突
// 示例
'mcp__filesystem__read' // filesystem 服务器的 read 工具
'mcp__github__create_issue' // github 服务器的 create_issue 工具
// MCP 服务器需要实现这些端点
interface MCPServer {
// 工具
'tools/list': () => ListToolsResult;
'tools/call': (params: { name: string; arguments: object }) => CallToolResult;
// 资源
'resources/list': () => ListResourcesResult;
'resources/read': (params: { uri: string }) => ReadResourceResult;
// 提示
'prompts/list': () => ListPromptsResult;
'prompts/get': (params: { name: string }) => GetPromptResult;
}
Claude Code 内置了一些 MCP 服务器:
// 文件系统
// Git
// GitHub
// 数据库
// 搜索
答案:
| 方面 | MCP | 插件 |
| 协议 | 标准化 MCP 协议 | Claude Code 专有 |
| 工具发现 | 自动发现 | 手动配置 |
| 部署 | 可以远程 | 本地 |
| 用途 | 扩展 AI 能力 | 扩展 CLI 功能 |
答案:
// 1. 查看连接状态
claude mcp list
// 2. 测试工具调用
claude mcp call <server>.<tool> --args '{...}'
// 3. 查看日志
claude mcp --verbose
答案:
// Claude Code 支持多个 MCP 服务器
// 每个服务器独立连接
// 限制因素:
// 1. 系统资源
// 2. 服务器处理能力
// 3. API 限流
// 建议:按需连接,不要同时连接过多服务器
| 资源 | 说明 |
| MCP 协议规范 | https://modelcontextprotocol.io |
| MCP SDK | @modelcontextprotocol/sdk |
| MCP 服务器示例 | https://github.com/modelcontextprotocol/servers |
下一节我们将深入 MCP 客户端实现:
- MCP 客户端的创建和连接
- 工具调用流程
- 错误处理和重试
*- 第一轮:□ 事实准确性*
*- 第二轮:□ 深度与洞见*
*- 第三轮:□ 可读性与价值*