协议版本: 2024-11-05
用户交互模型
Tools in MCP are designed to be 模型控制的, meaning that the language model can discover and invoke tools automatically based on its contextual understanding and the user’s prompts. 然而,实现可以自由通过任何适合其需求的接口模式来暴露工具——协议本身并不强制任何特定的用户交互模式。For trust & safety and security, there 应当 always
be a human in the loop with the ability to deny tool invocations.Applications 应当:
- 提供用户界面,清晰展示哪些工具正在暴露给AI模型
- 调用工具时插入清晰的可视化指示器
- 向用户展示操作确认提示,以确保人工参与其中
能力
Servers that support tools 必须 declare thetools capability:
listChanged indicates whether the server will emit notifications when the list of
available tools changes.
协议消息
列出工具
To discover available tools, clients send atools/list request. This operation supports
pagination.
请求:
调用工具
To invoke a tool, clients send atools/call request:
请求:
列表变更通知
当可用工具列表变更时,声明了listChanged
能力的服务器应当发送通知:
消息流
数据类型
工具
工具定义包含:name: 工具的独特标识description: 功能的人类可读描述inputSchema: JSON模式定义预期参数
工具结果
工具结果可包含多种不同类型的条目内容:文本内容
图像内容
嵌入资源
Resources 可以 被嵌入,用于通过一个可供客户端稍后订阅或再次获取的URI来提供额外的上下文或数据:错误处理
工具使用两种错误报告机制:-
协议错误: Standard JSON-RPC errors for issues like:
- 未知工具
- 无效参数
- 服务器错误
-
工具执行错误: Reported in tool results with
isError: true:- API失败
- 无效输入数据
- 业务逻辑错误
安全注意事项
-
服务器 必须:
- 验证所有工具输入
- 实施恰当的访问控制
- 限制工具调用频率
- 净化工具输出内容
-
Clients 应当:
- 在敏感操作上提示用户确认
- 在调用服务器之前向用户显示工具输入,避免恶意或意外的数据泄露
- 在传递给大语言模型LLM之前验证工具结果
- 为工具调用实现超时机制
- 记录工具使用情况以便审计