作為契約的工具 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.