来源:互联网 更新时间:2026-07-19 22:30
MCP 的架构定义得很清楚,三个角色分工明确,各管一摊。不妨先看看下面这张架构图,就能对整体的数据流向有个直观印象。

Host(宿主)→ 运行 LLM 推理的应用:Claude Desktop、Claude Code、自研 Agent→ 管理与一个或多个 MCP Server 的连接→ 决定把哪些工具/资源暴露给 LLMClient(客户端,内嵌于 Host)→ 与单个 MCP Server 保持一个 1:1 的连接→ 按 JSON-RPC 2.0 协议收发消息→ 维护会话状态(capabilities、已发现的工具列表)Server(服务端,独立进程)→ 暴露三类能力:Tools / Resources / Prompts→ 通过标准输入输出(stdio)或 HTTP 与 Client 通信→ 一个 Server 可以同时被多个 Host 连接
简单说:Host 是用户眼里的 AI 应用(比如 Claude Desktop),Client 是应用内部用来适配协议的翻译层,而 Server 才是真正提供工具和数据的地方。
MCP Server 对外暴露三种能力,各自用途不同:
Tools(工具) → LLM 主动调用的功能(action)→ 示例:搜索 Jira、执行 SQL、发送邮件→ 由 LLM 在推理过程中决定何时调用Resources(资源)→ LLM 可读取的数据源(data)→ 示例:当前 Sprint 状态、代码库文件树→ Host 决定何时注入到上下文Prompts(提示词)→ 预定义的 Prompt 模板(template)→ 示例:Bug 分析报告模板、代码 Review 模板→ 用户或 Host 直接调用,填入参数后生成完整 Prompt
这三种能力覆盖了 AI 应用最核心的需求:让模型能动手(Tools)、能看数据(Resources)、能按照规范交流(Prompts)。
Client 与 Server 之间怎么通信?目前支持三种方式:
stdio(标准输入输出)→ Server 作为子进程运行,通过 stdin/stdout 交换 JSON-RPC 消息→ 最简单,本地开发首选→ Claude Code 接入 MCP Server 的默认方式HTTP + SSE(Server-Sent Events)→ Server 作为独立 HTTP 服务运行→ Client → Server:HTTP POST→ Server → Client:SSE 流(支持 Server 主动推送)→ 适合远程 Server、多 Client 共享Streamable HTTP(新)→ HTTP POST + 可选 SSE 流→ 同时支持同步调用和流式推送→ 2025 年规范更新后的推荐远程方案
本文 demo 使用 stdio 传输,能最直观地展示协议本身是怎么跑的。
下面是用真实 MCP Server 跑出的 8 轮 JSON-RPC 消息,从头到尾完整走一遍协议交互。
每次会话的第一条消息,双方先交换自己支持的能力。就像两个陌生人见面先确认“你都会啥?”
Client → Server(请求):
{"jsonrpc": "2.0","id": 1,"method": "initialize","params": {"protocolVersion": "2024-11-05","capabilities": {"roots": {"listChanged": true},"sampling": {}},"clientInfo": {"name": "demo-client","version": "1.0.0"}}}
Server → Client(响应):
{"jsonrpc": "2.0","id": 1,"result": {"protocolVersion": "2024-11-05","capabilities": {"experimental": {},"prompts": {"listChanged": false},"resources": {"subscribe": false, "listChanged": false},"tools": {"listChanged": false}},"serverInfo": {"name": "mcp-protocol-demo","version": "1.13.1"}}}
解读:
protocolVersion:双方协商使用的协议版本capabilities:双方各自声明支持什么。Server 这里说 tools.listChanged: false,意味着工具列表不会动态变化,Client 不需要订阅变更通知id 字段:initialize 是请求-响应类型,有 idinitialize 完成后,Client 发一条通知(notification)确认就绪:
{"jsonrpc": "2.0", "method": "notifications/initialized"}
注意这条消息没有 id 字段——这是 JSON-RPC 的 Notification 类型,fire-and-forget,不等响应。有意思的设计:它告诉 Server “我已经准备好接收后续消息了”。
请求:
{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}
响应:
{"jsonrpc": "2.0","id": 2,"result": {"tools": [{"name": "echo","description": "Repeats the input message back. Useful for testing connectivity.","inputSchema": {"type": "object","properties": {"message": {"type": "string", "description": "The message to echo back"}},"required": ["message"]}},{"name": "add","description": "Adds two numbers and returns the result.","inputSchema": {"type": "object","properties": {"a": {"type": "number", "description": "First number"},"b": {"type": "number", "description": "Second number"}},"required": ["a", "b"]}}]}}
仔细看 inputSchema,它用的是标准 JSON Schema。Host 拿到它之后直接传给 LLM,模型读到这个 Schema 就能决定什么时候调用工具、填什么参数——本质上和 Function Calling 的 schema 结构完全一致。
echo 调用:
// 请求{"jsonrpc": "2.0", "id": 3, "method": "tools/call","params": {"name": "echo", "arguments": {"message": "Hello from MCP client!"}}}// 响应{"jsonrpc": "2.0", "id": 3,"result": {"content": [{"type": "text", "text": "Echo: Hello from MCP client!"}],"isError": false}}
add 调用:
// 请求{"jsonrpc": "2.0", "id": 4, "method": "tools/call","params": {"name": "add", "arguments": {"a": 42, "b": 58}}}// 响应{"jsonrpc": "2.0", "id": 4,"result": {"content": [{"type": "text", "text": "42 + 58 = 100"}],"isError": false}}
解读:响应结构里有两个重要字段:
content:数组,支持多个内容块(text / image / resource),可以一次返回多种格式isError:布尔值。工具执行失败时设为 true,结果里写错误信息。这里有个值得注意的设计——工具报错使用的是正常响应(result),而不是 JSON-RPC 协议错误(error)。为什么?因为这样 LLM 就能读到错误详情并自适应调整,而不是直接断掉通信。列举资源:
// 响应{"result": {"resources": [{"name": "Server Information","uri": "info://server-info","description": "Metadata about this MCP server: name, version, capabilities","mimeType": "application/json"}]}}
读取资源:
// 请求{"method": "resources/read", "params": {"uri": "info://server-info"}}// 响应{"result": {"contents": [{"uri": "info://server-info","mimeType": "text/plain","text": "{"name": "mcp-protocol-demo", "version": "1.0.0", ...}"}]}}
这里 URI scheme 是自定义的(info://),Server 自己定义格式。常见 scheme 还有 file://(本地文件)、jira://(Jira 工单)、github://(代码仓库)。Resources 是纯粹的数据层,只读,不执行动作。
列举模板:
{"result": {"prompts": [{"name": "summarize","description": "Summarize a piece of text concisely","arguments": [{"name": "text", "description": "The text to summarize", "required": true},{"name": "max_words", "description": "Maximum words in the summary", "required": false}]}]}}
渲染模板(填入参数):
// 请求{"method": "prompts/get","params": {"name": "summarize","arguments": {"text": "MCP defines a standard protocol for connecting AI models to tools and data sources.","max_words": "20"}}}// 响应{"result": {"description": "Summarization prompt","messages": [{"role": "user","content": {"type": "text","text": "Summarize the following text in at most 20 words:nnMCP defines a standard protocol for connecting AI models to tools and data sources."}}]}}
Prompts 是服务端维护的 Prompt 模板。Host 调用 prompts/get 得到渲染后的消息列表,可以直接拿来构建 LLM 的 messages。企业场景下,这特别有用——统一维护各类业务分析模板,所有 Agent 共用同一份,不用每个 Agent 单独写 Prompt。
MCP 用的就是标准 JSON-RPC 2.0,消息分三类:
请求(Request):有 id,需要响应{"jsonrpc":"2.0", "id": N, "method": "...", "params": {...}}响应(Response):有 id,与请求对应{"jsonrpc":"2.0", "id": N, "result": {...}}← 成功{"jsonrpc":"2.0", "id": N, "error": {...}} ← JSON-RPC 级错误通知(Notification):无 id,fire-and-forget{"jsonrpc":"2.0", "method": "notifications/initialized"}
关键区别:工具执行失败不使用 error 响应——那是 JSON-RPC 传输层的错误(比如方法不存在)。工具执行失败通过 result.isError: true 来表达,这样 LLM 就能读到具体错误内容,自己决定下一步怎么做。
conda activate llm_basepip install mcpcd llm-in-action/mcp-02-protocol# 方式 A:命令行查看原始 JSON-RPC 消息(8 步协议交互)python demo_protocol_client.py# 方式 B:MCP Inspector 可视化界面(需要 Node.js)npx @modelcontextprotocol/inspector python demo_mcp_server.py
腾讯ima怎么把微信内容一键导入知识库?
黄金价格不断创新高!黄金稳定币XAU、PAXG市值达11亿美元
CC币价格预测(2026-2035):Canton币今日价格走势+长期价格预测
腾讯ima怎么创建共享知识库?
今日比特币暴涨分析:Metaplanet的比特币BTC投资推动股价上涨17%
新浪互联网热点小时报丨2026年07月26日16时_今日实时互联网热点速递
新浪机器学习热点小时报丨2026年07月25日18时_今日实时机器学习热点速递
新浪人工智能热点小时报丨2026年07月30日18时_今日实时人工智能热点速递
Celestia价格预测2026-2032:TIA币能否引领山寨币上涨行情?历史价格回顾
比特币(BTC)核心周期指标复刻历史走势 价格或跌破5.8万美元关键支撑位
蚂蚁庄园今日答案7月21日(今日已更新) 蚂蚁庄园今天正确答案是什么呢
车载冰箱重置到出厂设置几步?
短剧《史上最强洪荒修为》剧情介绍
TRUMP价格走势与WEPE预售进展解析
结婚家电首选:Leader懒人三筒Ultra热泵洗烘一体
抖音怎么取消申请退货退款?抖音上取消退货怎么操作
腾讯ima知识库怎么分类管理?
WorkBuddy微信版怎么获得积分?
Windy卫星云图怎么看?云层变化识别技巧
短剧《仙人跳获透视,古玩玉器我全拿捏》剧情介绍
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc