Skip to content

Fix McpServer toolkit results whose encoded value is not JSON - #10089

Merged
tim-smart merged 5 commits into
Effect-TS:mainfrom
creatifcoding:fix/mcp-toolkit-json-structured-content
Oct 29, 2026
Merged

tim-smart merged 5 commits into
Effect-TS:mainfrom
creatifcoding:fix/mcp-toolkit-json-structured-content

Conversation

@creatifcoding

@creatifcoding creatifcoding commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

McpServer.registerToolkit put the schema-encoded success value of a toolkit tool (Schema.encodeUnknownEffect(successSchema)) into structuredContent. The RPC layer then serializes the tools/call result with Schema.toCodecJson, which rejects values that are not JSON. A result with a Schema.Void field, a Schema.Date field or a Schema.optional field set to undefined therefore failed with -32603 Internal error, on 2026-07-28 and on 2025-11-25. Repro: #10088

The advertised outputSchema was already built from the JSON codec (toolJsonSchema), so results that did serialize could still contradict it. For example, NaN was sent as a number, but the schema lists it as the string "NaN".

registerToolkit now converts the Toolkit's encoded success value with Schema.toCodecJson(Schema.toEncoded(successSchema)), so the success schema's own transformations still run only once. The encoder is built once per tool. Both structuredContent and its text mirror use the JSON value:

Success schema Handler result structuredContent
Struct({ reply: Void, version: Number }) { reply: undefined, version: 1 } {"reply":null,"version":1}
Struct({ at: Date }) { at: new Date(0) } {"at":"1970-01-01T00:00:00.000Z"}
Struct({ note: optional(String) }) { note: undefined } {"note":null}

Each value validates against the tool's listed outputSchema.

  • A result that is absent, such as a root Schema.Void, still returns content: [] without structuredContent (Fix void MCP tools are reported as internal failures #7072).
  • null and array results keep the behavior from fix(ai): keep non-object tool results out of structuredContent #7495.
  • Declared failures are unchanged.
  • If JSON encoding fails, the client gets the generic tool error. The failure is logged and reported as an AiError with reason ToolResultEncodingError, as the Toolkit already does for its own result-encoding failures. Before this change, a bigint under Schema.Unknown was reported as a TypeError defect from JSON.stringify. The existing test is updated for the new cause.
  • Visible change: a value whose schema encoding differs from its JSON encoding now arrives in JSON form. A Date becomes an ISO string, NaN becomes "NaN", and a Void field becomes null. MCP.md mentions this under "Tools and Toolkit".

There is no public API change.

Tests

  • test/ai/McpServer/McpConformance/ToolsTest.ts, "Structured content": new toolkit fixtures VoidFieldTool, DateFieldTool, OptionalFieldTool and NaNFieldTool. For each, the test checks the structuredContent on the wire, that content[0].text is its JSON.stringify, and that it validates against the listed outputSchema (Ajv 2020). The suite runs for 2025-06-18, 2025-11-25 and 2026-07-28. The cases for all three revisions fail without the source change and pass with it.
  • test/ai/McpServer/McpServer.test.ts: the serialization-failure test now expects a reported ToolResultEncodingError.
  • pnpm test --run packages/effect/test/ai/: 1662 passed, 48 skipped.
  • pnpm check, pnpm test-types packages/effect/typetest/ai/McpServer.tst.ts packages/effect/typetest/ai/Toolkit.tst.ts, pnpm lint, pnpm jsdocs --check, pnpm circular, deno check .: clean. pnpm ai-docgen leaves no diff.

Closes #10088
Closes EFF-3101

@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7b71d22

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

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

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.35 KB 39.35 KB 0.00 KB (0.00%)
basic.ts 7.18 KB 7.18 KB 0.00 KB (0.00%)
batching.ts 11.06 KB 11.06 KB 0.00 KB (0.00%)
brand.ts 6.84 KB 6.84 KB 0.00 KB (0.00%)
cache.ts 11.49 KB 11.49 KB 0.00 KB (0.00%)
config.ts 22.77 KB 22.77 KB 0.00 KB (0.00%)
differ.ts 21.92 KB 21.92 KB 0.00 KB (0.00%)
http-client.ts 26.86 KB 26.86 KB 0.00 KB (0.00%)
http-router.ts 36.85 KB 36.85 KB 0.00 KB (0.00%)
logger.ts 11.96 KB 11.96 KB 0.00 KB (0.00%)
metric.ts 9.42 KB 9.42 KB 0.00 KB (0.00%)
optic.ts 7.05 KB 7.05 KB 0.00 KB (0.00%)
pubsub.ts 15.97 KB 15.97 KB 0.00 KB (0.00%)
queue.ts 12.77 KB 12.77 KB 0.00 KB (0.00%)
schedule.ts 11.74 KB 11.74 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.02 KB 14.02 KB 0.00 KB (0.00%)
schema-binary.ts 41.06 KB 41.06 KB 0.00 KB (0.00%)
schema-class.ts 21.65 KB 21.65 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.51 KB 29.51 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.91 KB 27.91 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.27 KB 15.27 KB 0.00 KB (0.00%)
schema-string.ts 12.26 KB 12.26 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.19 KB 16.19 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.87 KB 38.87 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.10 KB 26.10 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.83 KB 20.83 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.97 KB 20.97 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.07 KB 21.07 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.11 KB 26.11 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.16 KB 21.16 KB 0.00 KB (0.00%)
schema.ts 20.75 KB 20.75 KB 0.00 KB (0.00%)
stm.ts 14.15 KB 14.15 KB 0.00 KB (0.00%)
stream.ts 10.33 KB 10.33 KB 0.00 KB (0.00%)

@tim-smart
tim-smart merged commit 2863a22 into Effect-TS:main Oct 29, 2026
10 of 11 checks passed
@effect-bot effect-bot mentioned this pull request Oct 29, 2026

This branch is waiting to be deployed

1 waiting deployment
fork — 7b71d227 Waiting Oct 29, 2026 by effect-bot via approval-gate #23952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

McpServer: toolkit results with a Schema.Void, Schema.Date or optional field fail tools/call with -32603

3 participants