作为契约的工具 Schema:tool_use、tool_calls 与 JSON Schema
Agent 辅助工程检查
前置要求
Day 73 的控制循环;Python 区块中的基础 JSON 与函数签名。
学习目标
说明生产级模型 API 如何声明工具(name、description、JSON Schema 的 parameters)、tool_use / tool_calls 块如何携带名称与参数、MCP 如何把同一契约推广为 host/client/server 角色——同时明确有界装置未实现的范围。
操作指引
1. 工具 Schema 是一份契约:唯一的 name、自然语言的 description,以及列出 properties、类型、required 字段与 additionalProperties 策略的 JSON Schema parameters 对象。模型只能提案它见过 Schema 的调用。
2. Anthropic 方式:模型返回 tool_use 内容块 {id, name, input};客户端执行后以 tool_result 块回复。OpenAI 方式:模型返回 tool_calls[{id, function:{name, arguments(JSON 字符串)}}];客户端以 role:"tool" 消息回复。两者都把模型的请求与工具的执行分离——与本装置在 next_action 与 _tool_call 之间划的接缝相同。
3. MCP 一段话:host 进程内嵌 client;每个 client 与一个 server 维持一条连接,server 通过 JSON-RPC 2.0 暴露 tools、resources 与 prompts,并在 initialize 时做能力协商;传输层为 stdio 或 streamable HTTP。
4. 合规边界:projects.agent_learning 用手写参数检查硬编码了三个工具。它只演示分发与验证的形态——没有 JSON-RPC 帧、没有 initialize 握手、没有工具发现、没有 Schema 协商。把它当作循环形态来学,不要当成协议实现。
5. Schema 为什么重要:它是验证器强制的表面;畸形调用在工具运行前就会失败在 Schema 层。
自主演练与记录
把三个项目工具(list_notes、read_note、write_report)写成 Anthropic 风格的工具定义,每个含 name、description 与 input_schema 的 JSON Schema。
read_note 要求 filename 为不含路径分隔符的 basename 字符串;write_report 要求 content 为至多 4096 字符的字符串;list_notes 不接受任何 properties。
[
{"name": "list_notes", "description": "List available note files", "input_schema": {"type": "object", "properties": {}, "additionalProperties": false}},
{"name": "read_note", "description": "Read one note file by basename", "input_schema": {"type": "object", "properties": {"filename": {"type": "string", "pattern": "^[^/\\\\]+$"}}, "required": ["filename"], "additionalProperties": false}},
{"name": "write_report", "description": "Write the final report file", "input_schema": {"type": "object", "properties": {"content": {"type": "string", "maxLength": 4096}}, "required": ["content"], "additionalProperties": false}}
]
为三个工具全部产出有效工具定义,并说出玩具装置缺失的至少两项协议特性(JSON-RPC 帧、能力协商、发现机制、流式传输)。
权威源资源与权利证明
Anthropic Tool Use (Function Calling) Implementation Guide
对应章节 / 时间戳: Tool Use Overview, Schema Definitions, and Stop Reasons
验证版本与日期: Claude API Specification (Verified: 2026-10-30)
Detailed production reference for client-side tool definitions, tool_use content blocks, stop reason tool_use, and tool_result response formatting.
REST API concepts, JSON schema specification, and basic Python client programming.
Offline mock request/response cycles and tool payload schemas are provided in the repo.
对比候选资源与选型分析 (2)
Protobuf and REST function declaration schema; Anthropic documentation provides explicit structured message block mechanics (tool_use / tool_result).
Multi-step tool calling parameter definitions; Anthropic specification demonstrates clear separated content blocks essential for inspectable event traces.
OpenAI Function Calling and Structured Outputs Guide
对应章节 / 时间戳: Function Calling Architecture and Strict Outputs
验证版本与日期: OpenAI API Reference (Verified: 2026-10-30)
Widely adopted specification establishing tool choice parameters, JSON schema function descriptions, tool_calls arrays, and role: "tool" responses.
JSON data structures, chat completion roles, and schema validation.
Standard schema dictionaries and simulated tool call responses are bundled in the repository.
对比候选资源与选型分析 (2)
Follows OpenAI-compatible schema convention; OpenAI documentation serves as the primary originating reference for tools and tool_calls schemas.
Local model runtime implementation of function calling; OpenAI docs document the standard schema contract and strict output validation.