Repository navigation
fix(mcp): serialize non-object tool outputs as JSON instead of throwing in mcpToModelOutput - #23830
Closed
princejain756 wants to merge 1 commit into
Closed
princejain756 wants to merge 1 commit into
princejain756 wants to merge 1 commit into
Conversation
…ng in mcpToModelOutput
aayush-kapoor
left a comment
Collaborator
There was a problem hiding this comment.
this needed signed commits so added you as co-author in #23935
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
#23825 reports that
mcpToModelOutputcrashes on any tool output that is not an object. The guard meant to fall back to JSON serialization runs'content' in resultfirst, and theinoperator throws aTypeErroron 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
mcpToModelOutputnow checkstypeof result !== 'object' || result === nullbefore theincheck, so scalar outputs take the JSON fallback ({ type: 'json', value }) instead of throwing.7,'plain',null, andtrueoutputs.examples/ai-functions/src/reproduction/issue-23825-mcp-scalar-tool-output.ts: an MCP tool whoseoutputSchematransforms its structured payload into the scalar7. Before the fix it crashes withTypeError: 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
mainand prints{"type":"json","value":7}with the fix:pnpm tsx src/reproduction/issue-23825-mcp-scalar-tool-output.tsfromexamples/ai-functionspnpm test:nodeinpackages/mcp: 495 tests pass, including the new regression testChecklist
pnpm changesetin the project root)Fixes #23825