Skip to content

fix(server): symmetric MCP/A2A request validation (#909) - #915

Merged
bokelley merged 2 commits into
mainfrom
bokelley/909-validation-symmetry
Apr 24, 2026
Merged

bokelley merged 2 commits into
mainfrom
bokelley/909-validation-symmetry

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes #909. Before this change, the MCP SDK's Zod validator ran only on the MCP transport; A2A bypassed it via AdcpServer.invoke(). Same malformed request → rejected on MCP, silently accepted on A2A. After this change the framework AJV validator is the sole enforcement path on both transports, emitting the same structured `adcp_error` envelope with pointer-level `issues[]`.

Changes

  • src/lib/server/create-adcp-server.ts — framework-registered tools pass `z.object({}).passthrough()` as inputSchema instead of per-tool Zod shapes. Key subtlety: the SDK's `validateToolInput` returns `undefined` when no inputSchema is registered, which would destroy args on MCP. Passthrough keeps every key intact while declaring no properties, so AJV is the sole enforcer on both paths.
  • Request validation default flipped to `'strict'` outside production (matches the existing `responses: 'strict'` default). Necessary because dropping the SDK's Zod means malformed A2A payloads would otherwise reach handlers unchecked.
  • src/lib/core/SingleAgentClient.ts — `adaptRequestForServerVersion` treats empty-properties schemas as "fail open" per JSON Schema semantics. MCP `tools/list` post-fix publishes `{ type: 'object', properties: {} }` per tool; the old code would have interpreted that as "strip every buyer-supplied field."

Wire format changes (intentional)

  • MCP clients receive structured `adcp_error` envelopes on malformed requests instead of raw `-32602 Input validation error: `. Same shape A2A clients always got.
  • `tools/list` over MCP no longer publishes per-tool parameter schemas. AdCP-native discovery (`get_adcp_capabilities`) stays; upstream adcontextprotocol/adcp#3057 proposes a `get_schema` capability tool.
  • Test seller fixtures using sparse payloads now need `validation: { requests: 'off' }` alongside `responses: 'off'`. Seven in-tree helpers updated.

Test plan

  • New test: `test/server-validation-symmetry.test.js` — sends identical malformed `create_media_buy` to one `AdcpServer` over both transports; asserts `adcp_error.code`, `recovery`, and sorted JSON-pointer issue lists match. Also asserts the handler does NOT run on either transport for malformed input.
  • 189 tests pass across touched areas (server dispatcher, A2A adapter, idempotency, state store extensions, uniform-error invariants on MCP + A2A, conformance seeder)
  • tsc clean, build clean, prettier clean
  • Stood up end-to-end; MCP + A2A produce identical `VALIDATION_ERROR` envelopes with matching pointer lists (`["/brand/domain", "/packages/0/budget", "/packages/0/pricing_option_id"]`)
  • CI green

Related

🤖 Generated with Claude Code

Before: MCP SDK's Zod validator ran only on the MCP transport; A2A
bypassed it via AdcpServer.invoke(). Same malformed request got
rejected on MCP (with raw -32602 JSON-RPC text) but silently reached
the handler on A2A — handlers could produce "successful" responses
for inputs that never matched the AdCP request schema.

After: framework AJV validator (loaded from schemas/cache/<version>/)
is authoritative on both transports, emitting a structured
adcp_error envelope with pointer-level issues[] regardless of which
transport carried the request.

Changes:

- Framework-registered tools pass `z.object({}).passthrough()` as
  inputSchema instead of per-tool Zod shapes. The SDK's validateToolInput
  returns undefined when no schema is registered, which would destroy
  args on MCP; passthrough keeps every key intact while declaring no
  properties, so the framework AJV is the sole enforcer.
- Request validation default flipped from 'warn' to 'strict' outside
  production (matches responses default). Malformed A2A payloads now
  reject before the handler instead of logging + dispatching.
- Client-side field-stripping in SingleAgentClient.adaptRequestForServerVersion
  treats empty-properties schemas as "fail open" (JSON Schema
  semantics). Required because MCP tools/list now publishes
  { type: 'object', properties: {} } per tool; the previous code would
  have stripped every buyer-supplied field.

Wire format change: MCP clients receive structured adcp_error instead
of raw -32602 text. tools/list loses per-tool parameter schemas;
AdCP-native discovery via get_adcp_capabilities stays; upstream
adcp#3057 proposes a get_schema capability tool.

Seven in-tree test helpers updated to opt out of strict request
validation (sparse fixtures by design).

New: test/server-validation-symmetry.test.js — sends the same
malformed request to one AdcpServer over MCP and A2A, asserts
adcp_error.code + recovery + sorted issue-pointer lists match. Locks
#909 against regression.

Closes #909.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
- Tighten second symmetry test to assert VALIDATION_ERROR code
  alongside handlerRan=false, so a route regression (404) can't
  masquerade as a validation rejection.
- BUILD-AN-AGENT: call out that @adcp/client MCP tools/list now
  publishes an untyped shape per tool. Points at upstream adcp#3057
  (get_schema) as the forward path.

Related upstream filed while reviewing:
- adcp#3059 — standardize adcp_error.issues[] as a spec field (today
  an @adcp/client extension; spec only names field singular).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
bokelley added a commit that referenced this pull request Apr 24, 2026
* feat(validation): enrich oneOf/anyOf errors with variant metadata

When AJV rejects a request because a discriminated-union field matched
none of its variants, the emitted ValidationIssue now carries a
variants[] array with each variant's required[] and properties[]. Naive
LLM clients that previously stalled on 'must match exactly one schema
in oneOf' can now read the variants list and pick one — directly
addressing the #1 stuck point in the three-way LLM probe
(discriminated /account on create_media_buy, destinations[] on
activate_signal, etc.).

Before:
  { pointer: '/account', keyword: 'oneOf', message: 'must match...' }

After:
  { pointer: '/account', keyword: 'oneOf', message: '...',
    variants: [
      { index: 0, required: ['account_id'], properties: [...] },
      { index: 1, required: ['brand','operator'], properties: [...] }
    ] }

Scope: both validateRequest and validateResponse. Variants resolved via
AJV's schemaPath against the compiled validator's root schema.
Non-union keywords unchanged.

Flows through to adcp_error.issues[] and adcp_error.details.issues[] on
the wire envelope automatically — no envelope change needed since the
issue type already carried ValidationIssue[].

Test: test/lib/schema-validation.test.js exercises the real
create_media_buy /account oneOf — 2 variants, correct required lists.

Pairs with PR #918 (call-adcp-agent skill) and PR #915 (validation
symmetry) to give naive LLM clients two recovery paths: skill-level
priors + runtime variant metadata on errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* test: add anyOf variant-enrichment coverage per code review

Code review noted that 49 AdCP 3.0 bundled schemas use anyOf and the
new test only covered oneOf. Added a sibling test against
create_content_standards (which has a top-level anyOf: policies OR
registry_policy_ids) to lock both keywords' enrichment paths.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* fix(validation): gate oneOf variants[] behind exposeSchemaPath

Security review flagged that variants[] carries more structural schema
info than schemaPath does — schemaPath reveals one branch index;
variants[] reveals every variant's required/properties. Existing
policy strips schemaPath in production via exposeSchemaPath; apply
the same gating to variants[] for symmetry.

- schema-errors.ts: extend the emittedIssues map to also drop variants
  when exposeSchemaPath is false.
- schema-validator.ts: docstring on ValidationIssue.variants explains
  the gating and points at ValidationIssue.schemaPath for symmetry.
- schema-validation.test.js: new test asserts variants is stripped in
  default mode and preserved when exposeSchemaPath: true.

Today AdCP validators only load canonical public spec schemas so the
leak is theoretical — but consistent policy is cheaper to audit than
per-field exceptions, and a future per-seller schema extension would
need gating anyway.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* fix(validation): do not gate variants[] behind exposeSchemaPath

Revert the gating added in c385d9a. Different sensitivity classes
justify different defaults:

- `schemaPath` (e.g. `#/properties/account/oneOf/2`) encodes which
  branch of the validator rejected first — an implementation detail
  that can fingerprint the seller's handler ordering. Stays gated.
- `variants[]` summarizes the PUBLIC spec's union shape (each
  variant's `required` / `properties`). The bundled AdCP schemas
  are already npm-shipped with `@adcp/client` and published at
  adcontextprotocol.org — a motivated buyer has them offline anyway.
  Gating would only hurt naive LLM clients in production, which is
  exactly the audience `variants[]` was built for (#919).

The original changeset already describes this policy ("variants is
not gated because the information is already public in the canonical
schemas"). This brings the code back in line with that promise.

Per dx-expert review: resolving the #919 changeset/code drift by
unpinning the gate rather than rewriting the changeset.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions github-actions Bot mentioned this pull request Apr 24, 2026
bokelley added a commit that referenced this pull request Apr 24, 2026
* docs: add call-adcp-agent skill for buyer-side LLM clients

New skill at skills/call-adcp-agent/SKILL.md teaches LLM buyers how to
call AdCP agents: wire contract, account oneOf variants, idempotency
invariants, async flow (status:'submitted' + task_id), error recovery
from adcp_error.issues[], minimal payload examples for get_products,
create_media_buy, sync_creatives, get_signals, activate_signal.

Motivation: #915 made MCP tools/list schema-free (validation-symmetry
trade-off). Empirical three-way probe: naive LLM stuck on 5/5 tools;
skill-aware Claude lands first successful call in 1 hop on all 5.
Schema-aware via a mock get_schema lands 2/5 at 2-3 hops.

Upstream adcp#3057 (get_schema) remains the spec path for programmatic
discovery; this skill is the no-spec-dependency answer for LLM buyers
today.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* docs: fix package-required list and destinations example per code review

- Required per package is {product_id, budget, pricing_option_id};
  buyer_ref is optional (correlation id). Prior wording implied
  buyer_ref was required.
- Destinations platform example aligned with spec exemplars
  (the-trade-desk, amazon-dsp) rather than an invented name.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* docs: address docs-expert review

- Frontmatter description: drop oneOf narrowing clause so vector-recall
  matches the broader 'calling an AdCP agent' invocation.
- Add an 'Overview' section before 'When to Use' (matches sibling
  skills' opening cadence).
- Pin schema-cache paths to 3.0.0 and cite 'npm run sync-schemas' so
  readers can resolve paths without external context.
- Add gotcha #6: format_id is always an object {agent_url, id}, not a
  string (common LLM mistake when the tool-call prompt only mentions
  the format id string).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* docs: teach variants[] in skill + README cross-link per dx-expert review

- Error-recovery section now shows variants[] on the oneOf example
  payload and teaches 'pick ONE variant, send only its required
  fields'. Pairs with adcp-client#919 which lands variants[] on the
  wire.
- README 'For AI Agents' section gets a direct pointer to the skill
  so buyer-side LLMs find it from the standard entry point (not just
  from CLAUDE.md which only exists in the repo, not the npm package).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit 0a0b802 into main Apr 24, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP vs A2A validation asymmetry — SDK Zod fires only on MCP path

1 participant