契約としてのツールスキーマ:tool_use・tool_calls・JSON Schema
Agent支援エンジニアリングチェック
前提条件
Day 73 の制御ループ。Python ブロックの基本 JSON と関数シグネチャ。
学習目標
本番モデル API がツールを宣言する方法(name・description・JSON Schema の parameters)、tool_use / tool_calls ブロックが name と引数を運ぶ仕組み、MCP が同じ契約を host/client/server 役割へ一般化する点を説明し、境界付きフィクスチャが実装していない範囲を明示します。
手順
1. ツールスキーマは契約です:一意の name、自然言語の description、そして properties・型・required・additionalProperties を列挙する JSON Schema の parameters オブジェクト。モデルは提示されたスキーマの呼び出ししか提案できません。
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 は JSON-RPC 2.0 でツール・リソース・プロンプトを公開する server へ 1 接続を維持し、initialize 時に能力ネゴシエーションを行います。トランスポートは stdio または streamable HTTP です。
4. 準拠境界:projects.agent_learning は 3 つのツールを手書き引数チェックでハードコードしています。ディスパッチと検証の形を示すだけで、JSON-RPC フレーミング・initialize ハンドシェイク・ツール発見・スキーマ交渉はありません。ループ構造の教材として学び、プロトコル実装として扱わないでください。
5. スキーマが重要な理由:スキーマはバリデータが強制する表面であり、不正な呼び出しはツール実行前にスキーマ層で失敗します。
演習と予測記録
3 つのプロジェクトツール(list_notes、read_note、write_report)を、name・description・input_schema の JSON Schema を持つ Anthropic 流ツール定義として表現しなさい。
read_note はパス区切りを含まない basename 文字列 filename を必須とし、write_report は 4096 文字以下の content 文字列を必須とし、list_notes はプロパティを取りません。
[
{"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}}
]
3 ツールすべての有効なツール定義を作成し、玩具ハーネスに欠けているプロトコル機能を 2 つ以上(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.