Repository navigation
fix(ai): keep non-object tool results out of structuredContent - #7495
Merged
Merged
Conversation
The MCP toolkit handler classified a result as structured with `typeof result.encodedResult === "object"`, which also holds for `null` and for arrays. A tool whose success schema encoded to either shape therefore put a non-object into `structuredContent`, where MCP permits only a JSON object. Older clients that validate `CallToolResult` rejected the whole reply. On current protocol revisions the server rejects it first, failing the call with `non-object structured tool content is not supported`, so a valid tool call becomes a protocol error. Test the value instead of its `typeof`, so `null` and arrays fall through to `undefined` and travel as text content only. That matches the advertised capability: `outputSchema` is set only when the JSON schema type is `object`, so these tools never claimed structured output to begin with. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01MjDyCJcEoLsS2LvMCEfedh
🦋 Changeset detectedLatest commit: fb992b8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The added tests asserted only that null and array results come back without structured content. Replacing the fix with one that never emits `structuredContent` at all kept the whole file green, so the tests admitted a fix that breaks every structured tool. Add a positive control over StructuredResultTool and assert the field directly rather than deep-comparing whole results, so emitting nothing and emitting a non-object both fail. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01MjDyCJcEoLsS2LvMCEfedh
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01MjDyCJcEoLsS2LvMCEfedh
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
3 tasks done
This was referenced Oct 29, 2026
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.
Closes #7494.
The defect
packages/effect/src/unstable/ai/McpServer.tsdecided whether a toolkit result was structured with:typeofalso reports"object"fornulland for arrays. A tool whose success schema encoded to either shape put a non-object intostructuredContent, which MCP requires to be a JSON object.Symptom, with one correction to the issue
The issue reports clients rejecting
structuredContent: nullwhile validatingCallToolResult. That is what happens on4.0.0-beta.103.On
mainthe failure is different. I reproduced both cases against a live server, aSchema.NullOr(...)tool returningnulland aSchema.Array(Schema.String)tool, and the bad value never reaches the client. The protocol adapter ininternal/mcpProtocol/v2025_06_18.ts:106already screens structured content correctly and fails the call first:Same root cause, but a valid tool call becomes a protocol error instead of a malformed reply.
The
voidcase in the issue does not bite.Schema.Voidencodes toundefined, notnull, and an existing test already covers it. The shapes that actually break are nullable and array success schemas.The fix
Test the value instead of its
typeof, sonulland arrays fall through toundefined:Those results now go out as text content only, which matches what the server advertises.
outputSchemais set only when the JSON schema type isobject, so these tools never claimed structured output in the first place.tools/listconfirms it:The data is still there. An array comes back as JSON in
content[0].text, so a client reads the same bytes and parses them itself.Tests
McpServer.test.tshad no coverage ofstructuredContentat all, so the new tests pin both directions.carries object tool results as structured contentchecks thatStructuredResultToolstill emits the field. Without it, a fix that simply never emitsstructuredContentwould pass the whole file.omits structured content for null and array tool resultschecks thatNullableResultToolandArrayResultToolcome back with the field absent.I ran three variants of the source against them. The fix passes. The original
typeofcheck fails the second test. A stub that always returnsundefinedfails the first.packages/effecttypechecks clean, and all 24unstable/aitest files pass with 1054 tests and 44 skipped.One thing this does not resolve
The check for emitting
structuredContentreads the runtime value, while the check for advertisingoutputSchemareads the schema, so the two can disagree.NullTooladvertises nooutputSchema, but when its handler returns an object rather thannullthat value does go out asstructuredContent. The same tool varies its reply shape by runtime value. MCP permits unschema'dstructuredContent, so nothing breaks, but deriving the flag fromoutputSchema !== undefinedwould make the two agree. I kept this change minimal and value-based to match the issue. Happy to align them if reviewers prefer.🤖 Generated with Claude Code
https://claude.ai/code/session_01MjDyCJcEoLsS2LvMCEfedh