Skip to content

fix(ai): decode JSON-encoded tool arguments for Records, typeless unions and protocol-validated schemas - #6503

Open
surlymochan wants to merge 2 commits into
code-yeongyu:mainfrom
surlymochan:fix/6271-tool-arg-decode-gaps
Open

surlymochan wants to merge 2 commits into
code-yeongyu:mainfrom
surlymochan:fix/6271-tool-arg-decode-gaps

Conversation

@surlymochan

@surlymochan surlymochan commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6271.

What changes

packages/ai/src/utils/validation.ts: decodeJsonEncodedStructures() now covers the three shapes the #6266 pass missed:

  • Record values: the object branch resolves a key's schema through patternProperties as well as properties/additionalProperties, so {k: '["a"]'} against Record(String, Array(String)) decodes to {k: ["a"]} instead of validating as ['["a"]'].
  • Typeless unions: when a schema carries no top-level type, decoding recurses into the anyOf/oneOf members that accept the value's kind, so JSON text nested inside e.g. Union([Array(Array(String)), Null]) is decoded.
  • Protocol-validated (modern MCP) schemas: validateToolArguments() now runs the decode before the protocol-validator early return, so those tools no longer keep the pre-fix(ai): decode JSON-encoded array/object tool arguments before conversion #6266 behaviour for JSON-encoded arrays/objects.

packages/ai/test/validation.test.ts: table-driven regression tests for all three shapes (senpi#6271). All three failed before the fix for the intended reason (see below) and pass after it.

Test evidence

RED (before fix, test/validation.test.ts -t "6271"): 3 failed / 18 skipped — Record got { values: { k: ['["a"]'] } }, the union kept the inner '["a"]' string, the protocol-validated call threw Validation failed.

GREEN (after fix, full file): 21 passed / 0 failed.

Static checks: oxlint --deny-warnings 0 warnings, bun fmt --check clean, bun check --no-script no type errors on the two touched files, and the repo pre-commit hook (fmt + oxlint --fix + npm run check incl. browser-smoke) passed on commit.

Change fragment

changes/unreleased/<this PR number>.md follows in a second push (named for the PR number per changes/unreleased/README.md): tracker block for packages/ai/src/utils/changes.md correcting the #6266 MCP coverage sentence — the core fix covers MCP tools whose schemas no extension owns only for legacy MCP schemas; protocol-validated modern MCP schemas now get the decode too — plus a Fixed changelog bullet for @earendil-works/pi-ai.


Summary by cubic

Fixes #6271 by decoding JSON-encoded tool arguments in the three shapes the #6266 fix missed: Record values under patternProperties, JSON text nested in typeless unions, and protocol-validated modern MCP schemas.

  • Record value schemas are now resolved through patternProperties as well as properties/additionalProperties.
  • Schemas without a top-level type now recurse into anyOf/oneOf members that accept the value's kind.
  • The decode now runs before the protocol-validator early return, so protocol-validated MCP tools unescape JSON-encoded arrays and objects too.
  • Adds regression tests covering all three shapes and a changes/unreleased/6503.md fragment logging the fix.

Written for commit 9e22b53. Summary will update on new commits.

View guided diff

@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks @surlymochan for closing the three shapes that #6266's pass left out (#6271): Record values resolved through patternProperties, anyOf/oneOf members picked by the value's kind when a schema has no top-level type, and the protocol-validated schemas that modern MCP servers send. Each one is a case where a JSON-encoded argument would otherwise reach the tool as a string.

The checks that ran are green. I've passed it on for maintainer review, including the full-CI run that a maintainer has to start on senpi.

@code-yeongyu code-yeongyu added the run-full-ci Run the full macOS/Windows CI set on this PR label Nov 13, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-full-ci Run the full macOS/Windows CI set on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool-argument decode misses Record values, typeless unions and protocol-validated MCP schemas (follow-up to #6265)

2 participants