Skip to content

fix(mcp): serialize non-object tool outputs as JSON instead of throwing in mcpToModelOutput - #23830

Closed
princejain756 wants to merge 1 commit into
vercel:mainfrom
princejain756:fix-mcp-scalar-model-output
Closed

princejain756 wants to merge 1 commit into
vercel:mainfrom
princejain756:fix-mcp-scalar-model-output

Conversation

@princejain756

@princejain756 princejain756 commented Oct 22, 2026 •

Copy link
Copy Markdown

Background

#23825 reports that mcpToModelOutput crashes on any tool output that is not an object. The guard meant to fall back to JSON serialization runs 'content' in result first, and the in operator throws a TypeError on scalars (string, number, boolean, null) before the fallback can run. Same defect as #22630, reached through a different producer: a subagent tool result rather than a schema transform.

Opening with the reproduction and regression test the report points at, plus the minimal guard.

Summary

  • mcpToModelOutput now checks typeof result !== 'object' || result === null before the in check, so scalar outputs take the JSON fallback ({ type: 'json', value }) instead of throwing.
  • Regression test covering 7, 'plain', null, and true outputs.
  • Reproduction examples/ai-functions/src/reproduction/issue-23825-mcp-scalar-tool-output.ts: an MCP tool whose outputSchema transforms its structured payload into the scalar 7. Before the fix it crashes with TypeError: Cannot use 'in' operator to search for 'content' in 7; after the fix it returns {"type":"json","value":7}.

Contributor Credit

End-to-End Verification

  • Reproduction script crashes on main and prints {"type":"json","value":7} with the fix: pnpm tsx src/reproduction/issue-23825-mcp-scalar-tool-output.ts from examples/ai-functions
  • pnpm test:node in packages/mcp: 495 tests pass, including the new regression test

Checklist

  • All commits are signed (PRs with unsigned commits cannot be merged)
  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Fixes #23825

@aayush-kapoor aayush-kapoor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needed signed commits so added you as co-author in #23935

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcpToModelOutput throws on any non-object output — second independent trigger, from subagent tool results

2 participants