MCP 全称 Model Context Protocol (模型上下文协议),MCP 其实最主要的能力就是规范了应用程序如何向模型提供上下文的方式。
架构介绍
MCP 的核心架构是 Client- Server,Host Client 也可以连接到多个服务器;
MCP Client :与服务器保持 1:1 连接的协议客户端;
MCP Server:轻量的中转服务,通过标准化模型上下文协议公开特定的工具能力;
Local 数据源:访问的本地的文件、数据库和服务
Remote 服务:通过互联网(例如 API)连接到的外部系统
传输及协议介绍
协议层
负责消息的格式化与中间层的路由能力,包括 Protocol,Client,Server 部分
传输层
处理客户端和服务器之间的实际通信,目前支持两种:
- Stdio transport(适用本地),客户端通过启动服务端子进程(如命令行工具),利用操作系统的管道机制(stdin/stdout)进行数据传输。同步阻塞模型,通信基于顺序处理,需等待前一条消息完成传输后才能处理下一条,适合简单的本地批处理任务。
- HTTP with SSE transport(适用远端服务的 SEE 长链接),异步事件驱动,服务端通过SSE长连接主动推送数据,客户端通过HTTP POST端点发送请求,支持实时或准实时交互,适合分布式系统或需要高并发的场景。
传输层中,所有传输均使用标准格式的 JSON-RPC 2.0 来交换消息。同时,MCP 允许开发者实现其他传输方式(如 WebSocket、gRPC),但需满足JSON-RPC 2.0的消息格式和生命周期管理。
更多参阅:https://modelcontextprotocol.io/docs/concepts/transports
消息类型
Requests
interface Request {
method: string;
params?: { ... };
}
Results·
interface Result {
[key: string]: unknown;
}
Errors
interface Error {
code: number;
message: string;
data?: unknown;
}
Notifications
interface Notification {
method: string;
params?: { ... };
}
上下文类型介绍
资源
将服务器中的数据和内容公开给 LLM,资源是模型上下文协议 (MCP) 中的核心原语,是由客户端通过 resources/read 读取服务端 resources/list 列取的资源并直接用作 LLM 交互上下文的数据和内容。包括但是不限于文件内容,数据库记录,日志内容等;
每个资源都通过 URI 标识,例如
file:///home/user/documents/report.pdf
postgres://database/customers/schem
提示词
创建可重复使用的提示模板和工作流程,服务器能够定义可重复使用的提示模板和工作流程,客户端可以轻松地调用服务端内容,并将其呈现给用户和 LLM。其功能包括接受动态参数,指定资源上下文信息等等。
工具
允许 LLM 通过您的服务器执行对应操作,允许服务端通过 tools/list 向客户端公开可执行功能,并客户端 tools/call 调用工具,通过工具LLM 可以与外部系统交互。
更多
Sampling 和 Roots 内容过少,不再赘述,可通过官网文档查看相关内容
https://modelcontextprotocol.io/docs/concepts/sampling
MCP 调研总结
个人观点,请自辨之。
之前的 Function Calling 想要调用,需要写一堆的适配代码,为每个工具专门定制。而 MCP 协议,帮助我们将上下文的入参格式实现统一,封装出一套新的很类似“通用接口”的调用范式,开源侧可无负担的贡献各种服务端,使其无痛调用各个工具,从而让整个 AI 生态更繁荣。
同时 MCP 协议通过 C-S 架构,让跨设备、跨网络等通信变得简单。现在只需要将阅读 PDF 的工具封装成一个 MCP Server,运行在本地,从其它地方连接这个 MCP Server 就可以分析 Server 所在机器上的 PDF,不用上传云端,这中间可附加一些安全验证,安全性会提高很多。
附:客户端支持介绍
Client |
Roots |
Notes |
||||
|
|
|
|
|
Full support for all MCP features |
|
|
|
|
|
|
Supports tools. |
|
|
|
|
|
|
Supports tools in agentic workflows. |
|
|
|
|
|
|
Supports tools and resources. |
|
|
|
|
|
|
Full support for all MCP features |
|
|
|
|
|
|
Supports tools. |
|
|
|
|
|
|
Supports tools in Emacs. |
|
|
|
|
|
|
Supports resource list and lookup through tools. |
|
|
|
|
|
|
Supports tools. |
|
|
|
|
|
|
Supports tools. |
|
|
|
|
|
|
Supports tools for Agents |
|
|
|
|
|
|
Supports tools, server connection management, and agent workflows. |
|
|
|
|
|
|
Supports tools. |
|
|
|
|
|
|
Supports tools and resources. |
|
|
|
|
|
|
Supports resources through OpenCTX |
|
|
|
|
|
|
Supports tools |
|
|
|
|
|
|
Supports tools for Agents in Theia AI and the AI-powered Theia IDE |
|
|
|
|
|
|
Supports tools with AI Flow for collaborative development. |
|
|
|
|
|
|
Prompts appear as slash commands |
|
|
|
|
|
|
Supports tools for Typescript AI Agents |
|
|
|
|
|
|
Supports tools in OpenSumi |
|
|
|
|
|
|
Support for drop in Servers to Daydreams agents |