MCP Server接入指南
更新时间:2026-03-06
概览
通过 MCP Server接入,当用户 Query 符合 MCP Tool 的能力描述时,Agent 将自动调用对应的 MCP Tool,从而实现工具能力的动态接入与复用。
当前 MCP Server 接入仅支持指令类操作,通用类 MCP 接入暂不支持。
使用说明
- 暂时支持单mcp server配置,即1个 Agent实例 暂不支持同时接入多个mcp server。
- 支持实例级(Instance Level)配置:在创建具体智能体实例时指定。 创建互动实例接口参数: 创建大模型互动实例
| mcp中字段 | 类型 | 是否必选 | 描述 |
|---|---|---|---|
| endpoint | String | 是 | mcp server 地址 |
| protocol | String | 是 | mcp server 使用协议类型,目前支持 HttpStreamable 和 SSE (区分大小写) |
| api_key | String | 否 | Bear Auth Token 值, 该配置可选 |
协议实现
- 支持SSE和HttpStreamable, 推荐使用HttpStreamable。
- 服务端需实现
initialize、tools/list和tools/call三个核心指令。可参考文档: https://modelcontextprotocol.io/docs/learn/architecture - 协议交互字段格式见mcp官网描述: MCP Schema
响应格式要求
- TTS 兼容性:目前仅支持 text 类型的 Content。
- 示例:
JSON
1{
2 "content": [{ "type": "text", "text": "这是给用户播报的内容" }]
3}
MCP Tools更新机制
系统目前暂不支持 MCP 标准的 Notification 自动通知机制 。若工具配置有变更,需通过以下方式手动触发 Agent 更新
- 服务端触发:通过 大模型互动实例发消息给sdk 接口 发送指令
[E]:[CMD]:[MCP_TOOLS_CHANGED]。 - SDK 触发:客户端通过 SDK 发送事件消息
[E]:[CMD]:[MCP_TOOLS_CHANGED]。
限制与约束
为了防止客户的 MCP Server 拖慢 Agent 响应:
- 超时限制:
tools/list必须在 3s 内返回,tools/call必须在 10s 内返回。 - 响应体大小:因为文本要转 TTS,建议限制单次返回的最大字符数(如 500 字以内),避免播报过长。
工具开发最佳实践
- mcp server 开发推荐使用 mcp sdk。 https://github.com/modelcontextprotocol
- 工具名称(Tool Name): 请尽量使用动宾结构,例如:
open_airconditioner,close_airconditioner - 描述词 (Description):请使用清晰的中文描述,例如"打开空调"。 触发mcp工具口令词列表可以放在description最后一行,用call_examples: 口令1, 口令2 表示,用半角符号,分割。 比如: call_examples: 打开空调,调到25度
- 参数设计:尽量使用枚举值来限制 LLM 的幻觉,参数数量不宜过多。 如需要设备ID,需要添加参数对应描述。设备ID通过指令传入:
[E]:[LIC]:[ACTIVE]:https://cloud.baidu.com/doc/RTC/s/sm9qgxvfq
MCP Server 代码举例
Python
1import sys
2from mcp.server.fastmcp import FastMCP
3
4# Initialize FastMCP server
5mcp = FastMCP(name="iot-mcp-server-demo", host='0.0.0.0', port=8988)
6
7@mcp.tool(
8 title="打开空调"
9)
10async def open_airconditioner(device_id: str) -> str:
11 """
12 打开空调
13
14 Args:
15 device_id: 设备ID
16
17 Returns:
18 打开空调结果
19
20 callExamples: 打开空调
21 """
22 # 打开空调的代码
23 return "空调已打开"
24
25@mcp.tool(
26 title="关闭空调"
27)
28async def close_airconditioner(device_id: str, value: str) -> str:
29 """
30 打开空调
31
32 Args:
33 device_id: 设备ID
34
35 Returns:
36 关闭空调结果
37
38 callExamples: 关闭空调
39 """
40 # 关闭空调的代码
41 return "空调已关闭"
42
43if __name__ == "__main__":
44 mcp.run(transport='streamable-http')
MCP 交互时序

评价此篇文章
