Back to the curriculum

L3-M02 · Level 3 · ~15 h

Tool Calling and Tool Contract Testing

What you build: Tool validation test suite

lessons
5
lesson hours (estimate)
≈ 10.5 h
0/5 lessons marked done · Progress is saved in this browser only. No account, nothing is sent to a server.
In review. The curriculum, module overviews and Levels 1–3 lessons are drafts written with AI assistance and reviewed by AI. Human review is still pending. English translation: AI-assisted, not human-reviewed.

Levels 1–3 lessons, exercises and quizzes are published as drafts, in Vietnamese with an AI-assisted English translation. They may change after human review.

Module path

  1. Lesson 1 · L3-M02-L01≈ 2 h

    Tool definitions as contracts

    What you build: Contract tests for tool schemas

    Needs: L3-M01-L05

    Key terms (5)
    • Tool definitionĐịnh nghĩa tool: tên, mô tả, schema tham số (và kết quả)
    • Tool contractHợp đồng tool: điều ứng dụng và model cùng dựa vào ở định nghĩa; nửa cho máy, nửa cho model
    • Well-formed / conformĐúng cấu trúc (schema tự nó) / thỏa (giá trị thỏa schema)
    • Unsupported keywordTừ khóa schema validator chưa cài; được báo, không bị bỏ qua
    • Rug pullĐổi định nghĩa tool sau khi người đã duyệt [R5]
  2. Lesson 2 · L3-M02-L02≈ 2 h

    Testing tool selection

    What you build: Cases where the right tool, the wrong tool or no tool is expected

    Needs: L3-M02-L01

    Key terms (5)
    • Tool selectionChọn tool: quyết định của một lượt, gọi tool nào, không gọi, hay hỏi lại
    • Missed call / spurious callGọi hụt (cần tool mà không gọi) / gọi thừa (không cần mà gọi)
    • Ambiguous caseCa mơ hồ: có nhiều đáp án chấp nhận được, ghi trong `acceptable`
    • Order sensitivityĐộ nhạy thứ tự: lựa chọn đổi khi đổi thứ tự liệt kê tool
    • Consistency (over paraphrases)Độ nhất quán qua cách diễn đạt khác
  3. Lesson 3 · L3-M02-L03≈ 2 h

    Argument validation and malicious tool inputs

    What you build: Adversarial argument cases

    Needs: L3-M02-L02

    Key terms (2)
    • Adversarial caseCa đối kháng: đầu vào chọn có chủ đích để khiến một bước kiểm cho kết quả sai
    • Indirect prompt injectionChèn chỉ thị gián tiếp: chỉ thị nằm trong nội dung ngoài mà model đọc [R2]
  4. Lesson 4 · L3-M02-L04≈ 2 h

    Mock tools and sandboxes

    What you build: Sandboxed tool layer with side-effect logging

    Needs: L3-M02-L03

    Key terms (6)
    • Mock toolTool giả: thay tool thật bằng hành vi định sẵn để test
    • Effect (read, write, external)Tác động: thay đổi trạng thái ngoài giá trị trả về; ba loại
    • Effect logNhật ký tác động: ghi mỗi lời gọi cùng trạng thái trước và sau; chỉ ghi thêm
    • Idempotency keyKhóa idempotency: lặp lại cùng yêu cầu không tác động thêm
    • Dry-runChạy thử không tác động: chạy tool đọc, giả lập tool ghi
    • ApprovalDuyệt: điều kiện của người cho một yêu cầu cụ thể (theo khóa)
  5. Lesson 5 · L3-M02-L05≈ 2.5 h

    Model Context Protocol (MCP) basics for testers

    What you build: Tests a small MCP server's tool listing and calls

    Needs: L3-M02-L04

    Key terms (5)
    • MCP server / client / hostServer MCP (cung cấp tool) / client MCP (của host, nói với một server) / host
    • CapabilityNăng lực: khai báo về tính năng hỗ trợ, trong `server/discover` hoặc `_meta` của request
    • Protocol error vs tool execution errorLỗi giao thức (JSON-RPC error) và lỗi thực thi tool (`isError: true`)
    • StatelessKhông trạng thái: mỗi request tự chứa mọi thứ cần để xử lý [R2]
    • Legacy / modernThế hệ cũ (có `initialize`, đến 2025-11-25) / mới (từ 2026-07-28) [R4]

A. Module overview

Field Value
Module ID L3-M02
Level 3 - AI Agent Quality Engineer
Description Testing the tool layer of an agent: defining a tool as a two-half contract (a schema for the machine, a name and description for the model) with contract tests and lint; tool selection (cases for the right tool, the wrong tool, no tool and ambiguous cases, sensitivity to listing order, consistency); checking parameters and malicious input (adversarial cases for type, bounds, size, Unicode, paths, injection-like strings, and instructions planted in tool results); a fake tool layer in a sandbox with an effect log, dry-run, idempotency keys, allowlist and denylist, approval, and rate and budget limits; and checking the list and calls of a small MCP server. The module lab is a simulated tool layer for the fictional product ThuVien Mini (a school library assistant), deterministic and offline: no model calls, no real server, no MCP SDK, no API key; tools run only in memory and in a temporary directory. What is known about MCP and the tool use API is quoted from sources read on 2026-11-04: the MCP specification revision 2026-07-28 (the Tools, Overview, Discovery, Versioning, Architecture pages and the changelog) and two MCP security pages, the Claude documentation on tool definitions, handling tool calls and strict tool use, OpenAI's function calling documentation, and three OWASP pages (LLM01, LLM05, LLM06). The reading level of each source is recorded in §12 of each lesson.
Why this module matters Most agent failures happen where the model touches the world: choosing the wrong tool, filling in or being steered into wrong parameters, or a write tool running twice. The tool layer lives in the team's code, so ordinary tests can check it if you have the right case set and log. The module also lays the groundwork for trajectories (L3-M04), reliability (L3-M06) and agent safety (L3-M07).
Target professional competency Level 3, learning outcome 2: "Test tool contracts, tool selection, arguments, permissions and side effects with sandboxed tools". The permissions part is only at the level of allowlist, denylist and approval on request; per-user authorization is not in the lab.
Estimated duration 4 lessons × 120 minutes + 1 lesson × 150 minutes (L05, the longest lesson) = 10.5 hours, plus the module assignment (~4 hours) and quiz (~0.5 hours): ≈ 15 hours for someone who has completed L3-M01. An estimate, not yet measured with real learners.
Prerequisites L3-M01 (the first lesson needs L3-M01-L05). The module reuses, without repeating: tool definitions, the tool_use and tool_result protocol, parameter checks and the four protective layers (L1-M05-L03 and L1-M05-L04), prompt injection and excessive agency (L1-M04), the tool layer in a sandbox, traces and the risk map (L3-M01-L01, L04, L05), contract tests (L1-M01-L04). No API key needed.
Module output (deliverable) A tool validation test suite: contract tests and lint for a tool registry, a tool selection case set (right, wrong, none, ambiguous), a corpus of adversarial cases for parameters, tests for a fake tool layer with an effect log, and tests for the list and calls of a simulated MCP server; plus the small products of each lesson
Cost 0 VND

What learners build. Step by step, in code-labs/l3-m02: (L01) a subset JSON Schema validator using the standard library, a contract lint for names, descriptions, schemas, effects and examples, and a digest and diff of the tool list; (L02) five simulated tool selection policies, a fifteen-case set, a scoring function with five verdicts, order sensitivity and consistency; (L03) a parameter check chain (name, strict JSON, hidden characters, schema), a corpus of seventy-three adversarial cases, safe_path, flagging instructions in tool results; (L04) a fake tool layer with a hash-chained effect log, dry-run, idempotency, approval, limits and reconcile; (L05) a simulated MCP server and client following the message shapes of the 2026-07-28 specification, checking the list, calls, error codes and list pinning. The lab is independent of the earlier labs and needs only pytest.

What the module does not do. It does not measure real models or agents, it states no price, speed or reliability for any model, server or vendor, and it does not rank or recommend any framework or provider: every number in the lab describes the simulation. It does not run shell commands, network calls or files outside the temporary directory; it does not use an MCP SDK; it does not check transport, authentication or MCP features other than tools. It does not create or edit an automatically running workflow in the repository. It gives no legal opinion. Lints and validators only flag; the loop guard and the limits lint block the run (fail closed); a person decides the thresholds, whether to accept a description change, and every real-world effect.

Terms reused with a different meaning (each lesson has a sentence that separates the senses). tool and function are used interchangeably (L01); schema is the JSON Schema of a tool's parameters, which differs from the schema check of L2-M03-L01; contract here includes the half meant for the model, which is wider than the contract test of L1-M01-L04; validation is checking against a schema, and does not include business rules or authorization (L03); an ambiguous case (several acceptable answers, L02) differs from an edge case (right at the threshold of a constraint, L03); a mock is a fake tool for tests, a sandbox is an isolated zone, and the lab's sandbox differs from an operating-system sandbox (L04); an effect (L04) is a state change beyond the return value; the effect log differs from the trace of L3-M01-L04; server and client in L05 are an MCP server and client, which differ from a web server and the client of a model API; a capability is one side's declaration of the features it supports; stateless is in the sense of the MCP specification revision 2026-07-28; oracle is still the mechanism that decides pass or fail (in L02 it is the expectation table of each case).

B. Learning objectives

ID Objective
MO1 Explain a tool definition as a two-half contract, run a contract lint and a validator, and state what they do not check.
MO2 Design and run a tool selection case set with right, wrong, no-tool and ambiguous cases, and read the result as fractions and per-case verdicts without inferring anything about a real model.
MO3 Write adversarial cases for tool parameters by risk family, check paths and hidden characters, and flag instructions planted in tool results.
MO4 Build and test a fake tool layer with an effect log: dry-run, idempotency, allowlist and denylist, approval, limits and budget.
MO5 Check the list and calls of a simulated MCP server: message shape, error codes, version, determinism of the list, and detection of a changed description.

C. Lessons

ID Lesson Duration Prerequisite Concepts Practical output
L3-M02-L01 Defining a tool as a contract 120' L3-M01-L05 two-half contract, well-formed and conform, contract lint, digest and diff Contract tests for a tool's schema
L3-M02-L02 Testing tool selection 120' L01 three kinds of expectation, five verdicts, no-tool and ambiguous cases, order sensitivity Right-tool, wrong-tool and no-tool cases
L3-M02-L03 Checking parameters and malicious input 120' L02 the model as an untrusted input, check chain, adversarial case families, safe_path, instructions in tool results Adversarial cases for parameters
L3-M02-L04 Fake tools and a sandbox with an effect log 120' L03 effect kinds, dry-run, idempotency, approval, limits, hash-chained log, reconcile A sandboxed tool layer with an effect log
L3-M02-L05 MCP for testers: checking the tool list and tool calls of a small server 150' L04 host, client, server, the 2026-07-28 revision with no handshake, two kinds of error, list pinning Tests for the list and calls of a simulated MCP server

D. Module assignment

The module assignment (exercises.md): a tool validation test suite for ThuVien Mini v2 (tool contracts, tool selection cases, adversarial cases, a tool layer with an effect log, MCP list and calls, reproduction; acceptance criteria AC1-AC6 and a 100-point rubric) together with one independent challenge (LichHoc). The five independent per-lesson exercises are in code-labs/l3-m02/exercises/ and in §9 of each lesson: fix four broken tools (L01), predict and fix where a policy chooses wrongly (L02), predict the results of twelve calls and write adversarial cases (L03), configure the tool layer and predict a dry-run (L04), predict the answers of the simulated MCP server and write requests (L05).

E. Module assessment

The module quiz quiz.md (12 questions, 20 points, pass mark 70%), the module assignment with acceptance criteria and rubric (exercises.md); the solutions and the instructor guide are for instructors only. The practice for each lesson is practice-01.md to practice-05.md (with .en.md versions), public and with reference answers. The solution set and the instructor's tests are in code-labs/l3-m02/instructor/ and are for instructors only.

F. References

The shared list is in references.md (15 sources re-fetched on 2026-11-04, access dates, how much of each source was read). Each lesson also has its own references table in §12. The MCP specification has dated revisions; every lesson uses the 2026-07-28 revision and says so explicitly.

Objective ↔ assessment matrix

Objective Lesson Independent exercise (§9) Quiz question Module assignment criterion
MO1 L01 exercises/test_l01_contract.py Q1, Q6, Q12 AC1 (and AC6 for the whole module)
MO2 L02 exercises/test_l02_selection.py Q2, Q7, Q9 AC2 (and AC6 for the whole module)
MO3 L03 exercises/test_l03_args.py Q3, Q11 AC3 (and AC6 for the whole module)
MO4 L04 exercises/test_l04_toollayer.py Q4, Q8, Q10 AC4 (and AC6 for the whole module)
MO5 L05 exercises/test_l05_mcp.py Q5, Q12 AC5 (and AC6 for the whole module)