74 / 90

Day 74:ツールスキーマ・検証・プロトコル境界

ツールを JSON Schema 契約として定義し、検証失敗を層別に分類し、MCP の host/client/server 役割を位置づけます。玩具ハーネスからプロトコル準拠を主張しません。

エージェント 480 分(8時間)
01 120 分

契約としてのツールスキーマ: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 流ツール定義として表現しなさい。

独立習得基準

3 ツールすべての有効なツール定義を作成し、玩具ハーネスに欠けているプロトコル機能を 2 つ以上(JSON-RPC フレーミング・能力交渉・発見・ストリーミング)述べられること。

公式ソースリソースと権利証跡

linking Anthropic PBC copyright (no open license stated) / linked reference Linking only; no documentation text is copied

Anthropic Tool Use (Function Calling) Implementation Guide

著者・提供機関: Anthropic PBC | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
Google Gemini Function Calling Documentation ↗ compared_alternative [Creative Commons Attribution 4.0]

Protobuf and REST function declaration schema; Anthropic documentation provides explicit structured message block mechanics (tool_use / tool_result).

Cohere Tool Use and Multi-Step Agents Guide ↗ compared_alternative [Open Documentation]

Multi-step tool calling parameter definitions; Anthropic specification demonstrates clear separated content blocks essential for inspectable event traces.

linking OpenAI copyright (no open license stated) / linked reference Linking only; no documentation text is copied

OpenAI Function Calling and Structured Outputs Guide

著者・提供機関: OpenAI | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
Mistral AI Function Calling Guide ↗ compared_alternative [Open Documentation]

Follows OpenAI-compatible schema convention; OpenAI documentation serves as the primary originating reference for tools and tool_calls schemas.

Ollama Tools and Function Calling Documentation ↗ compared_alternative [MIT / Open Documentation]

Local model runtime implementation of function calling; OpenAI docs document the standard schema contract and strict output validation.

02 120 分

read_note 引数の JSON Schema を作成し適用する

独立習得チェック(AI 支援なし)

前提条件

Day 74 Activity 1:ツールスキーマの構造と契約の考え方。

学習目標

read_note 引数に対する JSON Schema(draft 2020-12)の object スキーマを独立に書き、スキーマ検証だけでは不十分なケースを含む 4 つの候補引数ペイロードを分類します。

手順

1. スキーマを書きます:type は object。properties.filename は string。required は [filename]。additionalProperties は false。任意でパス区切りを制限する pattern。
2. 4 つのペイロードを、あなたのスキーマとハーネス規則 Path(filename).name == filename の両方で分類します:
a) {"filename": "alpha.txt"} - スキーマ valid、ハーネス受理。
b) {"filename": "../outside.txt"} - 緩いスキーマでは文字列として通るが、basename チェックがトラバーサルを防ぐためハーネスは invalid_arguments で拒否。教訓:スキーマ検証は必要条件であって十分条件ではなく、ポリシーチェックは一段深い層にあります。
c) {"file": "alpha.txt"} - required 不足(filename なし)かつ additionalProperties 違反 - invalid_arguments。
d) {"filename": 42} - 型違反 - invalid_arguments。
3. 各判定を ペイロード / スキーマ判定 / ハーネス判定 / エラーコード の列で表に記録します。
4. アダプタの最初の障害経路が ../outside.txt を出すのはこのためです。修復はスキーマが静かに入力を矯正するのではなく、正直なエラー観測に依存します。

演習と予測記録

read_note.schema.json と、ペイロード a-d の 4 行判定表(スキーマ判定 / ハーネス判定 / エラーコード)を書きなさい。

独立習得基準

独立:required と additionalProperties を含むスキーマを自力で書き、スキーマは通るがハーネスが拒否するトラバーサル例を含む 4 判定すべてが正確であること。

公式ソースリソースと権利証跡

linking IETF Trust License / Open Standard

JSON Schema: A Media Type for Describing JSON Documents (Draft 2020-12)

著者・提供機関: Austin Wright, Henry Andrews, Ben Hutton, Greg Dennis | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 4 & 5 (Schema Objects and Keywords)

検証バージョンと日付: IETF Internet-Draft 2020-12 (Verified: 2026-10-30)

選定理由:

The normative standard governing parameter schemas, type validation, required constraints, additionalProperties restrictions, and descriptive metadata in tool definitions.

ソース前提知識:

JSON data interchange formats, data typing, and structural validation concepts.

安全フォールバック(オフライン回復性):

Compact schema validator examples and standard validation rules are embedded in local project code.

比較対象候補リソースと選定評価 (2)
OpenAPI Specification 3.1.0 (OAI) ↗ compared_alternative [Apache-2.0]

Comprehensive REST API specification format; JSON Schema Draft 2020-12 is the exact underlying core used by modern agent tool definition protocols.

JSON Schema Draft-07 Specification ↗ compared_alternative [IETF Open Standard]

Legacy JSON schema dialect; modern tool protocols standardise on 2020-12 with strict keyword support.

linking IETF Trust License / Open Standard

RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format

著者・提供機関: Tim Bray (Ed.) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 1-4 (JSON Syntax and Grammar)

検証バージョンと日付: IETF Standards Track RFC 8259 (Verified: 2026-10-30)

選定理由:

Foundational Internet standard defining strict JSON grammar, token parsing, and whitespace rules underpinning model tool calling serialization.

ソース前提知識:

Text encoding, UTF-8 parsing, and serialized data exchange.

安全フォールバック(オフライン回復性):

JSON grammar parsing rules and error recovery guidelines are documented in curriculum notes.

比較対象候補リソースと選定評価 (2)
RFC 7049: Concise Binary Object Representation (CBOR) ↗ compared_alternative [IETF Open Standard]

Binary serialization standard; text-based LLMs operate primarily on human-readable UTF-8 JSON interchange formats.

YAML 1.2 Specification (OASIS / yaml.org) ↗ compared_alternative [Open Standard]

Indentation-sensitive text format; JSON has universally standardized parsing across language runtimes and tool schemas.

03 120 分

障害語彙:unknown_tool・invalid_arguments・not_found・size_limit

Agent支援エンジニアリングチェック

前提条件

Day 74 Activity 2:スキーマ検証とハーネスのポリシーチェック。

学習目標

ハーネスが発行するすべての構造化エラーを、それを生成する検証層(存在・引数形状・リソース状態・ペイロードサイズ)へ対応付け、フラットな {ok:false,error} 形状をプロトコルレベルのエラー面と関連づけます。

手順

1. projects/agent_learning/agent.py の _tool_call を読み表にまとめます:
- name が ALLOWED_TOOLS にない -> unknown_tool(ディスパッチ層)。
- arguments が Mapping でない、またはキー過不足 -> invalid_arguments(スキーマ層)。
- read_note の filename が basename 規則違反 -> invalid_arguments(スキーマ層内のポリシー層)。
- ノートファイルがディスクにない -> not_found(ツールランタイム層)。
- write_report の content が 4096 バイト超 -> size_limit(リソース制限層)。
2. tempfile ワークスペース内の Python REPL で各エラーを実際に発生させ(先に create_fixture)、観測オブジェクトを記録します。
3. 不正な結果:実プロトコルではツールは直列化可能な結果を返す必要があり、非 JSON 出力ならプロトコル層自体がエラー(code と message を持つ JSON-RPC エラーオブジェクト)を出します。本ハーネスのフラットなエラー文字列はその面の教育的単純化です。
4. 構造化エラーが重要な理由:アダプタの修復ステップは観測から再計画します。曖昧な失敗ではループは推測するしかありません。

演習と予測記録

REPL で _tool_call に {"name": "delete_all", "arguments": {}} と、5000 文字 content の write_report を与え、両観測を記録してエラー層表に追加しなさい。

独立習得基準

4 つのエラーコードすべてを正しい層の帰属で再現し、不正結果リスクをプロトコルの用語で 1 段落説明できること。

公式ソースリソースと権利証跡

linking Apache-2.0 (specification; unrelicensed legacy contributions remain MIT) / Open Specification

Model Context Protocol (MCP) Specification

著者・提供機関: Anthropic PBC & Open Source Contributors | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Architecture Overview, Transports, Tools, and Resources

検証バージョンと日付: MCP Specification 2024-11-05 (Verified: 2026-10-30)

選定理由:

Normative open architecture specification defining Host, Client, and Server separation, resource subscriptions, tool listing/invoking primitives, and JSON-RPC 2.0 message framing.

ソース前提知識:

Client-server architecture, JSON-RPC 2.0 protocol, and standard IO communication.

安全フォールバック(オフライン回復性):

Architectural comparison tables, loop invariants, and message frame examples are self-contained in curriculum materials.

比較対象候補リソースと選定評価 (2)

Precursor JSON-RPC architecture for code intelligence in IDEs; MCP adapts similar client-server decoupling specifically for LLM context, tools, and prompts.

OpenAI Responses API Built-in Tools ↗ compared_alternative [Proprietary Documentation]

Hosted, vendor-managed built-in tool execution (successor to the Assistants API tools); MCP provides an open, decoupled local/remote client-server standard.

linking Anthropic PBC copyright (no open license stated) / linked reference Linking only; no documentation text is copied

Anthropic Tool Use (Function Calling) Implementation Guide

著者・提供機関: Anthropic PBC | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)
Google Gemini Function Calling Documentation ↗ compared_alternative [Creative Commons Attribution 4.0]

Protobuf and REST function declaration schema; Anthropic documentation provides explicit structured message block mechanics (tool_use / tool_result).

Cohere Tool Use and Multi-Step Agents Guide ↗ compared_alternative [Open Documentation]

Multi-step tool calling parameter definitions; Anthropic specification demonstrates clear separated content blocks essential for inspectable event traces.

04 120 分

評価:検証層とプロトコル準拠境界

独立習得チェック(AI 支援なし)

前提条件

Day 74 Activity 1-3 の完了。

学習目標

8 つの検証シナリオを層別に独立分類し、ルーブリックが要求する準拠境界ステートメントを書きます。

手順

1. 各シナリオを 直列化・スキーマ・ディスパッチ・ツールランタイム・リソース制限 のいずれか 1 層に分類し、観測可能なエラーを挙げます:(a) arguments テキストが有効な JSON でない。(b) 必須キー欠落。(c) レジストリにないツール名。(d) 要求ノートがディスクにない。(e) レポート内容が 4096 バイト超。(f) filename に ../ を含む。(g) arguments がオブジェクトでなくリスト。(h) 実プロトコルでツール結果が JSON 直列化不能。
2. 3 文の準拠境界ステートメントを書きます:ハーネスはループと検証構造を教えるもので、MCP クライアントではなく、プロトコル準拠を一切証明しない。欠けているプロトコル機能を 2 つ以上挙げること。
3. 独立ルーブリック:8 項目中 7 つ以上正確、境界ステートメント完備、無援助。
4. 援助付きルーブリック:Agent が表の下書きをしてよいが、すべての行を _tool_call のソースと照合して自分で検証すること。

演習と予測記録

agent.py を開かずに 8 行の分類表と 3 文の境界ステートメントを完成させ、その後ソースと照合しなさい。

独立習得基準

独立習得チェック:8 行中 7 行以上正確、かつ欠けているプロトコル機能を 2 つ挙げた境界ステートメントを無援助で述べられること。

公式ソースリソースと権利証跡

linking Apache-2.0 (specification; unrelicensed legacy contributions remain MIT) / Open Specification

Model Context Protocol (MCP) Specification

著者・提供機関: Anthropic PBC & Open Source Contributors | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Architecture Overview, Transports, Tools, and Resources

検証バージョンと日付: MCP Specification 2024-11-05 (Verified: 2026-10-30)

選定理由:

Normative open architecture specification defining Host, Client, and Server separation, resource subscriptions, tool listing/invoking primitives, and JSON-RPC 2.0 message framing.

ソース前提知識:

Client-server architecture, JSON-RPC 2.0 protocol, and standard IO communication.

安全フォールバック(オフライン回復性):

Architectural comparison tables, loop invariants, and message frame examples are self-contained in curriculum materials.

比較対象候補リソースと選定評価 (2)

Precursor JSON-RPC architecture for code intelligence in IDEs; MCP adapts similar client-server decoupling specifically for LLM context, tools, and prompts.

OpenAI Responses API Built-in Tools ↗ compared_alternative [Proprietary Documentation]

Hosted, vendor-managed built-in tool execution (successor to the Assistants API tools); MCP provides an open, decoupled local/remote client-server standard.

linking IETF Trust License / Open Standard

JSON Schema: A Media Type for Describing JSON Documents (Draft 2020-12)

著者・提供機関: Austin Wright, Henry Andrews, Ben Hutton, Greg Dennis | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 4 & 5 (Schema Objects and Keywords)

検証バージョンと日付: IETF Internet-Draft 2020-12 (Verified: 2026-10-30)

選定理由:

The normative standard governing parameter schemas, type validation, required constraints, additionalProperties restrictions, and descriptive metadata in tool definitions.

ソース前提知識:

JSON data interchange formats, data typing, and structural validation concepts.

安全フォールバック(オフライン回復性):

Compact schema validator examples and standard validation rules are embedded in local project code.

比較対象候補リソースと選定評価 (2)
OpenAPI Specification 3.1.0 (OAI) ↗ compared_alternative [Apache-2.0]

Comprehensive REST API specification format; JSON Schema Draft 2020-12 is the exact underlying core used by modern agent tool definition protocols.

JSON Schema Draft-07 Specification ↗ compared_alternative [IETF Open Standard]

Legacy JSON schema dialect; modern tool protocols standardise on 2020-12 with strict keyword support.