You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(server): pre-validation request hook for spec-default injection (#614) (#629)
* feat(server): pre-validation request hook for spec-default injection
Adds `pre_validation_hooks` to `serve()`, `create_mcp_server()`,
`create_a2a_server()`, and `create_tool_caller()`. The per-tool hook
runs on the raw wire dict before `validate_request` (schema) and
`model_validate` (Pydantic), so it can supply spec-mandated defaults
that absent pre-v3 buyers omit. Hook exceptions surface as
INVALID_REQUEST. Includes 7 new pytest tests.
Closes#614https://claude.ai/code/session_015hHQqXRbn2jX9WTu564gjZ
* fix(server): snapshot raw_params before hook to preserve context-echo contract
Both code-reviewer and dx-expert expert review flagged that assigning
raw_params after the hook would echo server-injected defaults back to the
buyer as if they were sent on the wire, violating the AdCP context-echo
contract. Move the snapshot to before the hook call (raw_params = params),
so inject_context always uses the original wire dict regardless of what
the hook returns or mutates.
Also strengthens test_hook_does_not_pollute_context_echo: the previous test
passed a copy of the outer dict so the assertion was trivially true. The
new test sends a wire payload with a context field, uses a stripping hook
that returns a completely new dict (no context key), and asserts the
response context echo still carries the original wire context — only possible
if raw_params was captured before the hook ran.
https://claude.ai/code/session_015hHQqXRbn2jX9WTu564gjZ
* fix(server): defensive copy before hook call eliminates in-place mutation footgun
Pass dict(params) to pre_validation_hook so hooks that mutate their
argument in-place still leave raw_params (the context-echo snapshot)
untouched. The "must return a new dict" restriction is removed from
docstrings; either mutation style is now safe.
Adds test_in_place_mutation_is_safe_for_context_echo to prove the
invariant. Adds a docstring cross-reference to #623 for the account-omission
case per adopter feedback on #629.
Co-authored-by: bokelley <bokelley@users.noreply.github.com>
https://claude.ai/code/session_0115Pruuy4MbdaxhPvgTBFoo
---------
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments