模型上下文协议(MCP)建立在灵活、可扩展的架构上,实现了 LLM 应用程序和集成之间的无缝通信。本文档涵盖了核心架构组件和概念。
MCP 遵循客户端-服务器架构,其中:
- 主机是启动连接的 LLM 应用程序(如 Claude Desktop 或 IDE)
- 客户端在主机应用程序内部与服务器维持 1:1 连接
- 服务器向客户端提供上下文、工具和提示
核心组件
协议层
协议层处理消息框架、请求/响应链接和高级通信模式。
class Protocol<Request, Notification, Result> {
// Handle incoming requests
setRequestHandler<T>(schema: T, handler: (request: T, extra: RequestHandlerExtra) => Promise<r>): void
// Handle incoming notifications
setNotificationHandler<T>(schema: T, handler: (notification: T) => Promise<void>): void
// Send requests and await responses
request<T>(request: Request, schema: T, options?: RequestOptions): Promise<T>
// Send one-way notifications
notification(notification: Notification): Promise<void>
}
class Protocol<Request, Notification, Result> {
// Handle incoming requests
setRequestHandler<T>(schema: T, handler: (request: T, extra: RequestHandlerExtra) => Promise<r>): void
// Handle incoming notifications
setNotificationHandler<T>(schema: T, handler: (notification: T) => Promise<void>): void
// Send requests and await responses
request<T>(request: Request, schema: T, options?: RequestOptions): Promise<T>
// Send one-way notifications
notification(notification: Notification): Promise<void>
}
class Session(BaseSession[RequestT, NotificationT, ResultT]):
async def send_request(
self,
request: RequestT,
result_type: type[Result]
) -> Result:
"""
Send request and wait for response. Raises McpError if response contains error.
"""
# Request handling implementation
async def send_notification(
self,
notification: NotificationT
) -> None:
"""Send one-way notification that doesn't expect response."""
# Notification handling implementation
async def _received_request(
self,
responder: RequestResponder[ReceiveRequestT, ResultT]
) -> None:
"""Handle incoming request from other side."""
# Request handling implementation
async def _received_notification(
self,
notification: ReceiveNotificationT
) -> None:
"""Handle incoming notification from other side."""
# Notification handling implementation
关键类包括:
传输层
传输层处理客户端和服务器之间的实际通信。MCP 支持多种传输机制:
-
标准输入输出传输
-
HTTP 与 SSE 传输
- 使用服务器发送事件(Server-Sent Events)进行服务器到客户端的消息传递
- 使用 HTTP POST 进行客户端到服务器的消息传递
所有传输都使用 JSON-RPC 2.0 交换消息。有关模型上下文协议消息格式的详细信息,请参阅规范。
消息类型
MCP 有以下主要消息类型:
-
请求期望从另一方获得响应:
interface Request {
method: string;
params?: { ... };
}
-
结果是对请求的成功响应:
interface Result {
[key: string]: unknown;
}
-
错误表示请求失败:
interface Error {
code: number;
message: string;
data?: unknown;
}
-
通知是不期望响应的单向消息:
interface Notification {
method: string;
params?: { ... };
}
连接生命周期
1. 初始化
- 客户端发送带有协议版本和功能的
initialize
请求
- 服务器响应其协议版本和功能
- 客户端发送
initialized
通知作为确认
- 开始正常的消息交换
2. 消息交换
初始化后,支持以下模式:
- 请求-响应:客户端或服务器发送请求,另一方响应
- 通知:任一方发送单向消息
3. 终止
任一方都可以终止连接:
- 通过
close()
进行干净关闭
- 传输断开连接
- 错误条件
错误处理
MCP 定义了以下标准错误代码:
enum ErrorCode {
// Standard JSON-RPC error codes
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603
}
SDK 和应用程序可以定义自己的错误代码(大于 -32000)。
错误通过以下方式传播:
- 对请求的错误响应
- 传输上的错误事件
- 协议级错误处理程序
实现示例
以下是实现 MCP 服务器的基本示例:
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server({
name: "example-server",
version: "1.0.0"
}, {
capabilities: {
resources: {}
}
});
// Handle requests
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: [
{
uri: "example://resource",
name: "Example Resource"
}
]
};
});
// Connect transport
const transport = new StdioServerTransport();
await server.connect(transport);
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server({
name: "example-server",
version: "1.0.0"
}, {
capabilities: {
resources: {}
}
});
// Handle requests
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: [
{
uri: "example://resource",
name: "Example Resource"
}
]
};
});
// Connect transport
const transport = new StdioServerTransport();
await server.connect(transport);
import asyncio
import mcp.types as types
from mcp.server import Server
from mcp.server.stdio import stdio_server
app = Server("example-server")
@app.list_resources()
async def list_resources() -> list[types.Resource]:
return [
types.Resource(
uri="example://resource",
name="Example Resource"
)
]
async def main():
async with stdio_server() as streams:
await app.run(
streams[0],
streams[1],
app.create_initialization_options()
)
if __name__ == "__main__":
asyncio.run(main)
最佳实践
传输选择
-
本地通信
- 对本地进程使用标准输入输出传输
- 对同一机器通信高效
- 简单的进程管理
-
远程通信
- 对需要 HTTP 兼容性的场景使用 SSE
- 考虑安全影响,包括身份验证和授权
消息处理
-
请求处理
- 彻底验证输入
- 使用类型安全的模式
- 优雅地处理错误
- 实现超时
-
进度报告
- 对长操作使用进度令牌
- 增量报告进度
- 在已知时包括总进度
-
错误管理
- 使用适当的错误代码
- 包含有用的错误消息
- 在错误时清理资源
安全考虑
-
传输安全
- 对远程连接使用 TLS
- 验证连接来源
- 在需要时实现身份验证
-
消息验证
- 验证所有传入消息
- 净化输入
- 检查消息大小限制
- 验证 JSON-RPC 格式
-
资源保护
- 实现访问控制
- 验证资源路径
- 监控资源使用
- 限制请求速率
-
错误处理
- 不泄露敏感信息
- 记录与安全相关的错误
- 实现适当的清理
- 处理 DoS 场景
调试和监控
-
日志记录
-
诊断
- 实现健康检查
- 监控连接状态
- 跟踪资源使用
- 性能分析
-
测试
- 测试不同的传输
- 验证错误处理
- 检查边缘情况
- 对服务器进行负载测试