[PR #3525] fix(mcp): recover structured tool arguments clients serialize as JSON text #5802

Open
opened 2026-10-31 01:09:33 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/3525
Author: @hemv-857
Created: 10/14/2026
Status: 🔄 Open

Base: main ← Head: fix-3408-stringified-structured-arguments


📝 Commits (6)

  • 23692aa fix(mcp): recover structured tool arguments clients serialize as JSON text
  • 3944f0e fix(mcp): skip argument recovery on application-validated registrations
  • c37f4a2 fix(mcp): recover structured arguments on application-validated registrations
  • 37f406b Merge origin/main into fix-3408-stringified-structured-arguments
  • b790b05 fix(mcp): keep schema validation synchronous for native arguments
  • 11faa18 Merge remote-tracking branch 'origin/main' into pr3525

📊 Changes

3 files changed (+314 additions, -8 deletions)

View changed files

📝 src/server/EvidenceMcpServerCore.ts (+14 -6)
📝 src/server/toolRegistrationOptions.ts (+87 -2)
➕ tests/boundary/mcp/stringifiedStructuredArguments.test.ts (+213 -0)

📄 Description

Summary

Fixes #3408.

Claude Desktop delivers array and object tool arguments as JSON text, so every structured parameter of every tool failed SDK input validation before reaching its handler. Recover JSON text at the registered input schema's validation step in EvidenceMcpServerCore.registerTool, then let the canonical parser decide the result unchanged.

Problem and expected behavior

Smallest trigger: tools/call for inspect_web_page with allowed_origins supplied as '["https://example.com"]', or capture_browser_scenario with browser, start_url, actions, and capture supplied as JSON text. Violated invariant: a structured parameter whose supplied value is valid JSON text for that parameter's schema should be validated as that structure instead of being rejected as a string.

Reported before this change (REA 6.4.0, Claude Desktop 2.37337.1 on Windows 11):

  • Input validation error: Invalid arguments for tool inspect_web_page: Invalid input
  • browser: Invalid input: expected object, received string, start_url: Invalid input: expected object, received string, actions: Invalid input: expected array, received string, capture: Invalid input: expected object, received string

String-only parameters keep working, so the failure is partial and reads as missing data: inspect_web_page without allowed_origins succeeds and reports third-party resources as disallowed_origin.

Change and scope

EvidenceMcpServerCore.registerTool, where every MCP tool registration passes, wraps each registered input schema's Standard Schema validate with acceptStringifiedStructuredArguments (src/server/toolRegistrationOptions.ts). For each top-level string argument that starts with { or [ and parses as a JSON object or array, the parsed bag is validated first. If that fails but the caller's original bag validates (JSON text a string parameter accepts), the original is kept. If both fail, the parsed form's content-level issues are reported. Native JSON values never take this path and pass straight to the canonical validator without an extra async hop, so deferred-call ordering and request metadata stay unchanged; the advertised JSON Schema projection is spread through unchanged. Application-owned validators (vendor: "rea-application" from applicationToolRegistrationOptions) recover through the same shared bag-merge helper inside their own wrapper: the canonical parser chooses between the recovered bag and the caller's original, the wrapper returns only client-supplied bags (never the canonical parser's parsed output, so application-owned parsing stays in the handler), and when neither bag satisfies the canonical parser the handler receives the caller's original arguments — a capture path named [] keeps its string, while record_ordinals: "[0]" recovers to the array.

Intentionally not included:

  • Rewriting the advertised schemas. The issue proposes inlining $ref/$defs. capture_browser_scenario's served schema contains no $ref at all (browser is oneOf, start_url and capture are object, actions is array) and still received strings, so inlining would not fix the reported tools, and changing the advertised projection would move tools_sha256.
  • Per-key recovery. Recovery applies to the argument bag as a whole, so a call that mixes recovered structured arguments with JSON text that a string parameter accepts keeps its pre-change rejection.
  • Number-typed parameters delivered as numeric text; the report covers arrays and objects only.

Contribution scope: ordinary fix within the existing MCP boundary. No target execution, injection, mutation, or protection recovery, so no maintainer scope decision is required.

Contract and boundary impact

  • Semantic owner and earliest changed stage: the MCP adapter owns argument recovery at the input schema's validate step; the canonical contract parser stays the semantic owner of validation.
  • Contribution scope fit and linked maintainer decision: not applicable; ordinary fix within existing boundaries.
  • CLI and MCP/tool-catalog contract: tools/list and catalog digests unchanged; tools/call accepts JSON text where the contract expects an object or array and still rejects everything the contract rejected before.
  • Provider, bridge, target-format, or platform compatibility: client-side serialization quirk reported on Claude Desktop/Windows; no provider or platform code changes.
  • Evidence, artifact, provenance, or reconstruction contract: unchanged. Recovered arguments pass the same canonical parser as native JSON values, so downstream handlers and Evidence see the same shapes.
  • Target selection, authority, upstream behavior, and cleanup details: not applicable.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none. No contract or example edits, so regenerated catalogs are unchanged.

Expected effects:

No listed target effect is introduced: this change normalizes MCP argument delivery only. Unchecked items do not establish absence.

Evidence and regression coverage

  • Tests added or updated: tests/boundary/mcp/stringifiedStructuredArguments.test.ts (7 tests) covering recovery of a fully stringified capture_browser_scenario example, recovery of inspect_web_page.allowed_origins, preservation of JSON text in a string parameter, preservation of a JSON-looking string (capture_path: "[]") on an application-validated registration, recovery of structured text on an application-validated registration (record_ordinals: "[0]"), preservation of the caller's original arguments when neither bag satisfies the canonical parser, and continued rejection of malformed structured text.

  • Base reproduction: the pre-port head c37f4a2 had the same split, with the three recovery tests failing on unmodified origin/main (the handler never ran on the contract tools; application-owned registrations never recovered) and the four guard tests passing. This merge only moves the recovery boundary to main's new core registration and application helper; with the change, all seven pass.

  • User-visible CLI/MCP output: the reported Input validation error: Invalid arguments for tool ... results become ordinary tool results of the recovered call. Real browser output was not captured here; the recovery boundary is covered by the in-memory MCP client tests above.

  • Remaining proof gaps: Claude Desktop itself was not run (Windows/Electron unavailable in this environment). Recovery is verified through an in-memory MCP client sending exactly the reported wire values. Upstream stringification cases that do not arrive as JSON text (anthropics/claude-code#58747 type stripping) are not recoverable server-side.

  • Observed, derived, and inferred claims remain distinguishable.

  • Artifact identity, source provenance, and failed attempts remain preserved.

  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npx vitest run tests/boundary/mcp/stringifiedStructuredArguments.test.ts — 7 passed.
  • src/server/DeferredEvidenceMcpServer.test.ts plus the stringified suite — 19 passed, covering both recovery and request-metadata ordering during deferred registration.
  • npm run test:focused over stringifiedStructuredArguments, EvidenceMcpServer, toolInputAdmission, toolSchemaValidity, compactInputSchemaMcp, browserComparisonInput, outputValidatorReuse — 43 passed, 3 failed; all three are the same catalog tests exceeding the 30 s test timeout under load (59 s, 47 s, 40 s). toolSchemaValidity passes 14/16 and compactInputSchemaMcp 12/13 in isolation, with the same timeout; the three tests are environmental on this machine.
  • npm run check:fast — passed (typecheck and lint).
  • npx oxfmt --check <changed files> — passed.
  • Full npm run test:mcp was not rerun after the merge; the focused MCP set above is the post-merge evidence.
  • Real Hopper/Ghidra, browser, or OS provider verification: not applicable to this boundary; the browser tools are exercised through their contract validation, not a live CDP session.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. Calls that failed validation before now succeed only when the supplied JSON text satisfies the same contract.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; no target interaction introduced.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: JSON.parse runs only on top-level string arguments that begin with { or [, inside the existing MCP request size bound, at most twice per call. The recovered bag is built with object spread and Object.fromEntries (own data properties, no [[Set]]), so __proto__ argument keys cannot pollute prototypes. Recovered values pass the same canonical parser as before, and the change adds no file, process, network, or authority access.

Author readiness

Human owner: @hemv-857

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed — none required; no contract, docs, or generated metadata changes.
  • User-visible CLI/MCP changes include representative output — the reported rejection messages are quoted above; recovered calls return ordinary tool results.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/3525 **Author:** [@hemv-857](https://github.com/hemv-857) **Created:** 10/14/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix-3408-stringified-structured-arguments` --- ### 📝 Commits (6) - [`23692aa`](https://github.com/morluto/rea/commit/23692aab8b8765d7c7a61046b584b7b59d4b0379) fix(mcp): recover structured tool arguments clients serialize as JSON text - [`3944f0e`](https://github.com/morluto/rea/commit/3944f0e538cd99a9fe664d533371f94aab546d07) fix(mcp): skip argument recovery on application-validated registrations - [`c37f4a2`](https://github.com/morluto/rea/commit/c37f4a2f8dd6013800ba3ef5e42abc1e125c294b) fix(mcp): recover structured arguments on application-validated registrations - [`37f406b`](https://github.com/morluto/rea/commit/37f406bcae2b644828900fd7e178cfbb139b9ef4) Merge origin/main into fix-3408-stringified-structured-arguments - [`b790b05`](https://github.com/morluto/rea/commit/b790b053c90c3dd52bdc5ae7253cb2e3749ad0d0) fix(mcp): keep schema validation synchronous for native arguments - [`11faa18`](https://github.com/morluto/rea/commit/11faa18012a7c84d98da6dd3974e7a96a899504d) Merge remote-tracking branch 'origin/main' into pr3525 ### 📊 Changes **3 files changed** (+314 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `src/server/EvidenceMcpServerCore.ts` (+14 -6) 📝 `src/server/toolRegistrationOptions.ts` (+87 -2) ➕ `tests/boundary/mcp/stringifiedStructuredArguments.test.ts` (+213 -0) </details> ### 📄 Description ## Summary Fixes #3408. Claude Desktop delivers array and object tool arguments as JSON text, so every structured parameter of every tool failed SDK input validation before reaching its handler. Recover JSON text at the registered input schema's validation step in `EvidenceMcpServerCore.registerTool`, then let the canonical parser decide the result unchanged. ## Problem and expected behavior Smallest trigger: `tools/call` for `inspect_web_page` with `allowed_origins` supplied as `'["https://example.com"]'`, or `capture_browser_scenario` with `browser`, `start_url`, `actions`, and `capture` supplied as JSON text. Violated invariant: a structured parameter whose supplied value is valid JSON text for that parameter's schema should be validated as that structure instead of being rejected as a string. Reported before this change (REA 6.4.0, Claude Desktop 2.37337.1 on Windows 11): - `Input validation error: Invalid arguments for tool inspect_web_page: Invalid input` - `browser: Invalid input: expected object, received string, start_url: Invalid input: expected object, received string, actions: Invalid input: expected array, received string, capture: Invalid input: expected object, received string` String-only parameters keep working, so the failure is partial and reads as missing data: `inspect_web_page` without `allowed_origins` succeeds and reports third-party resources as `disallowed_origin`. ## Change and scope `EvidenceMcpServerCore.registerTool`, where every MCP tool registration passes, wraps each registered input schema's Standard Schema `validate` with `acceptStringifiedStructuredArguments` (`src/server/toolRegistrationOptions.ts`). For each top-level string argument that starts with `{` or `[` and parses as a JSON object or array, the parsed bag is validated first. If that fails but the caller's original bag validates (JSON text a string parameter accepts), the original is kept. If both fail, the parsed form's content-level issues are reported. Native JSON values never take this path and pass straight to the canonical validator without an extra async hop, so deferred-call ordering and request metadata stay unchanged; the advertised JSON Schema projection is spread through unchanged. Application-owned validators (`vendor: "rea-application"` from `applicationToolRegistrationOptions`) recover through the same shared bag-merge helper inside their own wrapper: the canonical parser chooses between the recovered bag and the caller's original, the wrapper returns only client-supplied bags (never the canonical parser's parsed output, so application-owned parsing stays in the handler), and when neither bag satisfies the canonical parser the handler receives the caller's original arguments — a capture path named `[]` keeps its string, while `record_ordinals: "[0]"` recovers to the array. Intentionally not included: - Rewriting the advertised schemas. The issue proposes inlining `$ref`/`$defs`. `capture_browser_scenario`'s served schema contains no `$ref` at all (`browser` is `oneOf`, `start_url` and `capture` are `object`, `actions` is `array`) and still received strings, so inlining would not fix the reported tools, and changing the advertised projection would move `tools_sha256`. - Per-key recovery. Recovery applies to the argument bag as a whole, so a call that mixes recovered structured arguments with JSON text that a string parameter accepts keeps its pre-change rejection. - Number-typed parameters delivered as numeric text; the report covers arrays and objects only. Contribution scope: ordinary fix within the existing MCP boundary. No target execution, injection, mutation, or protection recovery, so no maintainer scope decision is required. ## Contract and boundary impact - Semantic owner and earliest changed stage: the MCP adapter owns argument recovery at the input schema's `validate` step; the canonical contract parser stays the semantic owner of validation. - Contribution scope fit and linked maintainer decision: not applicable; ordinary fix within existing boundaries. - CLI and MCP/tool-catalog contract: `tools/list` and catalog digests unchanged; `tools/call` accepts JSON text where the contract expects an object or array and still rejects everything the contract rejected before. - Provider, bridge, target-format, or platform compatibility: client-side serialization quirk reported on Claude Desktop/Windows; no provider or platform code changes. - Evidence, artifact, provenance, or reconstruction contract: unchanged. Recovered arguments pass the same canonical parser as native JSON values, so downstream handlers and Evidence see the same shapes. - Target selection, authority, upstream behavior, and cleanup details: not applicable. - Generated metadata (`docs/public/product-catalog.json`), package, or installation impact: none. No contract or example edits, so regenerated catalogs are unchanged. Expected effects: No listed target effect is introduced: this change normalizes MCP argument delivery only. Unchecked items do not establish absence. ## Evidence and regression coverage - Tests added or updated: `tests/boundary/mcp/stringifiedStructuredArguments.test.ts` (7 tests) covering recovery of a fully stringified `capture_browser_scenario` example, recovery of `inspect_web_page.allowed_origins`, preservation of JSON text in a string parameter, preservation of a JSON-looking string (`capture_path: "[]"`) on an application-validated registration, recovery of structured text on an application-validated registration (`record_ordinals: "[0]"`), preservation of the caller's original arguments when neither bag satisfies the canonical parser, and continued rejection of malformed structured text. - Base reproduction: the pre-port head `c37f4a2` had the same split, with the three recovery tests failing on unmodified `origin/main` (the handler never ran on the contract tools; application-owned registrations never recovered) and the four guard tests passing. This merge only moves the recovery boundary to main's new core registration and application helper; with the change, all seven pass. - User-visible CLI/MCP output: the reported `Input validation error: Invalid arguments for tool ...` results become ordinary tool results of the recovered call. Real browser output was not captured here; the recovery boundary is covered by the in-memory MCP client tests above. - Remaining proof gaps: Claude Desktop itself was not run (Windows/Electron unavailable in this environment). Recovery is verified through an in-memory MCP client sending exactly the reported wire values. Upstream stringification cases that do not arrive as JSON text (anthropics/claude-code#58747 type stripping) are not recoverable server-side. - [x] Observed, derived, and inferred claims remain distinguishable. - [x] Artifact identity, source provenance, and failed attempts remain preserved. - [x] Unsupported, incomplete, unavailable, or uncertain outcomes remain visible. ## Validation performed - `npx vitest run tests/boundary/mcp/stringifiedStructuredArguments.test.ts` — 7 passed. - `src/server/DeferredEvidenceMcpServer.test.ts` plus the stringified suite — 19 passed, covering both recovery and request-metadata ordering during deferred registration. - `npm run test:focused` over stringifiedStructuredArguments, EvidenceMcpServer, toolInputAdmission, toolSchemaValidity, compactInputSchemaMcp, browserComparisonInput, outputValidatorReuse — 43 passed, 3 failed; all three are the same catalog tests exceeding the 30 s test timeout under load (59 s, 47 s, 40 s). `toolSchemaValidity` passes 14/16 and `compactInputSchemaMcp` 12/13 in isolation, with the same timeout; the three tests are environmental on this machine. - `npm run check:fast` — passed (typecheck and lint). - `npx oxfmt --check <changed files>` — passed. - Full `npm run test:mcp` was not rerun after the merge; the focused MCP set above is the post-merge evidence. - Real Hopper/Ghidra, browser, or OS provider verification: not applicable to this boundary; the browser tools are exercised through their contract validation, not a live CDP session. ## Compatibility, safety, and release - Breaking changes or migration steps: none. Calls that failed validation before now succeed only when the supplied JSON text satisfies the same contract. - Real Hopper/Ghidra, browser, or OS coverage: not applicable; no target interaction introduced. - Package or release metadata impact: none. - Security, privacy, process, or containment review: `JSON.parse` runs only on top-level string arguments that begin with `{` or `[`, inside the existing MCP request size bound, at most twice per call. The recovered bag is built with object spread and `Object.fromEntries` (own data properties, no `[[Set]]`), so `__proto__` argument keys cannot pollute prototypes. Recovered values pass the same canonical parser as before, and the change adds no file, process, network, or authority access. ## Author readiness Human owner: @hemv-857 - [x] I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work. - [x] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps. - [x] I declare this PR ready for initial maintainer review. ## Before merge - [x] This PR addresses a concrete problem or an agreed enhancement. - [x] Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. - [x] The PR has one focused outcome and the title follows `type(scope): outcome`. - [x] Related issue is linked, or the reason for not linking one is stated above. - [x] Tests cover changed observable behavior and meaningful failure paths. - [x] Owning docs, contracts, and generated metadata are updated where needed — none required; no contract, docs, or generated metadata changes. - [x] User-visible CLI/MCP changes include representative output — the reported rejection messages are quoted above; recovered calls return ordinary tool results. - [x] I checked the final diff for secrets, unrelated cleanup, and unsupported claims. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#5802
No description provided.