Typed tool contracts: validate inputs without assuming atomic execution
Separate schema validation, domain constraints, authorization, resource limits, output validation, and partial side-effect reporting.
On this page
The question for today
What you’ll understand
- Define layered validation before a tool is dispatched.
- Distinguish typed payload validity from permission and business correctness.
- Represent partial and unknown effects when output validation or transport fails.
A tool call can be valid JSON and still target the wrong resource, exceed a budget, or perform an unauthorized operation. It can also perform a write before returning an invalid result. A typed contract must therefore describe more than its argument shape. Separate pre-execution checks from effect semantics and post-execution evidence so validation failures do not trigger blind retries.
Schema checks establish a structural contract
Specify a schema dialect and version, required fields, types, bounds, and treatment of unknown properties. JSON Schema 2020-12 provides structural validation vocabularies; implementations must configure the relevant features. Its format vocabulary can be annotation rather than an automatically enforced semantic assertion. A string passing a URI or date-related check is not proof that the endpoint exists or the date is applicable. Runtime type checks and generated language types also need agreement on numeric range, nullability, and serialization.
Domain validation adds meaning: an integer quantity may be structurally valid but exceed the remaining stock, refer to another tenant, or use the wrong unit. Resolve authenticated identity and resource ownership from trusted state, not model-provided claims. Check approval and budget requirements separately. Model Context Protocol's versioned tools specification describes schemas and result structures, but those do not replace an application's authorization or effect protocol. Tool annotations or descriptions supplied by an untrusted party are not independent proof of behavior.
Errors must say what is known about the effect
A tool wrapper should distinguish rejection before execution, a known committed result, a partial effect, and an unknown outcome. A remote write may succeed while response serialization, output-schema validation, or network delivery fails. Do not map every exception to no effect. Preserve external operation identity, receipt, resource version, and completed suboperations when available. A multi-operation tool can expose partial completion explicitly or be decomposed into smaller journaled effects, with the resulting coordination overhead acknowledged.
Validate returned data before downstream reasoning trusts its structure, but retain evidence even if the expected result shape is broken. A syntactically valid success payload can still describe an incorrect or unauthorized result, requiring domain and permission checks appropriate to the consumer. MCP distinguishes protocol-level and tool-execution errors under its specified semantics; neither automatically grants transaction rollback. The runtime should reconcile an uncertain effect or retry under a valid idempotency contract, rather than infer safety from an error field.
Test each validation boundary and its failure timing
Use malformed arguments, structurally valid but invalid-domain requests, missing scope, expired approval, budget exhaustion, malformed success results, and a crash after the external commit. Verify which checks prevent dispatch and which occur afterward. Keep contract and tool implementation versions in the trace so a replay does not validate old results against an unrelated new schema silently. Test that rejected requests cause no external call under the wrapper's actual implementation, and that post-dispatch uncertainty remains visible to recovery.
The probability lab is only a hypothetical acceptance-count model. Reset, set p=0.80 and n=10, predict eight accepted examples on average, then Play or Step. Separately Reset, set p=0.50 and n=10, predict five, then Play or Step. Its independent trials do not validate schemas, enforce permissions, or model partial writes. Real validation stages can be correlated and deterministic for a given input. Use the expected counts only to practice distinguishing a pass rate from the concrete dispatch and effect outcomes in a labeled test suite.
Work it through
Count checks separately from effects
Five proposed calls enter a test wrapper. One fails schema validation, one of the remaining four fails domain validation, and one of the remaining three fails authorization. The two dispatched calls yield one complete effect and one documented partial effect.
Schema validation leaves four calls; domain validation leaves three. No rejected pre-dispatch call should reach the external tool under this tested wrapper.
Authorization leaves two dispatched calls, or 2/5=40% of the original proposals.
One call completes and one is partial. The partial result is not a no-effect validation failure and needs its declared recovery path; completed-effect rate alone is 1/5=20%.
Structural acceptance, dispatch, completion, and partial execution have different numerators and recovery implications.
The living notebook
Explore the model
Interactive lab · probability
A pass-rate model does not enforce a contract
Reset, set p=0.80 and n=10, predict mean 8, then Play or Step. Separately Reset, set p=0.50 and n=10, predict mean 5, then Play or Step. This exact iid binomial has no schema engine, authorization, tool execution, or partial-effect state.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forValidate the actual staged contract and effect timing rather than inferring safety from a rate.
Pause & reason
Check your understanding
Your turn
Try it yourself
Seven proposals have two schema rejections, one further domain rejection, and one further authorization rejection. How many dispatch, and does that count reveal their final effect outcomes?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Subtract each rejection from the remaining population, then separate dispatch from completion.
Compare with the solution
Three calls dispatch: 7−2−1−1=3, or 3/7≈42.86%. Their success, partial, or unknown outcomes require execution evidence. Passing all pre-dispatch checks does not make an external operation atomic or guarantee successful completion.
Keep the idea
Take it with you
- Schema, domain, authorization, approval, and budget checks are distinct.
- Post-execution validation cannot imply an absent side effect.
- Tool contracts need explicit partial and unknown outcome semantics.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.