Skip to content

Encode tool results with their JSON codec - #10362

Merged
tim-smart merged 3 commits into
mainfrom
agent/nelson/32c5dbe5d731
Nov 4, 2026
Merged

tim-smart merged 3 commits into
mainfrom
agent/nelson/32c5dbe5d731

Conversation

@tim-smart

@tim-smart tim-smart commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #10332, which decodes tool call parameters with their JSON codec. This PR does the same for tool results.

Problem

Toolkit encoded handler results with the raw successSchema and Tool.failureResultSchema(tool). For a schema whose Encoded side is not JSON (Schema.Date, Schema.BigInt inside a struct), encodedResult was not JSON. Providers got it through JSON.stringify, and Chat.exportJson threw on a BigInt. McpServer ran a second JSON conversion of its own to make structuredContent match the advertised outputSchema.

Changes

  • Toolkit: encode successes and failures with Schema.toCodecJson(...).
  • McpServer: drop the second conversion, since encodedResult is now JSON. Failures raised in "error" mode skip the Toolkit's encoding, so they still go through the JSON codec in McpServer. A void result still produces content: []; this is now checked on the decoded result, because Schema.Void encodes to null.
  • Response: per-tool ToolResultPart schemas use the JSON codec for locally handled tools. Provider-executed tools (providerDefined without requiresHandler) keep the raw schema, which is the same split Decode McpServer tool parameters with their JSON codec #10332 made for params.
  • Prompt: tool-result result is Schema.Unknown and is filled from encodedResult, so only its docs changed.

Provider-defined tools

The handler-required provider tools (Anthropic bash/computer/memory/text editor, OpenAI apply_patch/local_shell/shell) have JSON-native result schemas (strings, literals, optionalKey), so the JSON codec leaves their payloads unchanged. Provider-executed tools keep decoding the provider's payload with the raw schema.

Behavior changes

The changeset is patch. Both changes affect unstable APIs, and the changeset gives migration steps.

  • Schema.Void results now encode to null instead of undefined. Providers previously sent JSON.stringify(undefined), which is undefined, so null is the safer value.
  • The JSON codec of Schema.Unknown rejects non-JSON values. A Tool.dynamic tool (which defaults to Unknown) that returns undefined, a Date, or an object with an undefined field now fails with ToolResultEncodingError, even when toModelOutput replaces the model payload. McpServer already rejected these values. Migration: declare a success schema (Schema.Date, or Schema.Void for undefined) or return a JSON value.

Tests

  • Tool.test.ts: Toolkit.handle JSON-encodes Date/BigInt successes and "return"-mode failures, encodes a void result to null, and rejects a Date from a dynamic tool without a success schema.
  • Response.test.ts: a Toolkit result with a Date round-trips through AllParts JSON, and provider-executed results keep the raw encoding.
  • Chat.test.ts: a BigInt tool result survives exportJson/fromJson. This test fails without the fix.

Closes EFF-3334

@changeset-bot

changeset-bot Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1094f7c

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

@effect-bot
effect-bot changed the base branch from main to v4/next-minor November 4, 2026 00:09
@github-actions

github-actions Bot commented Nov 4, 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.41 KB 39.41 KB 0.00 KB (0.00%)
basic.ts 7.23 KB 7.23 KB 0.00 KB (0.00%)
batching.ts 11.08 KB 11.08 KB 0.00 KB (0.00%)
brand.ts 6.87 KB 6.87 KB 0.00 KB (0.00%)
cache.ts 11.54 KB 11.54 KB 0.00 KB (0.00%)
config.ts 22.84 KB 22.84 KB 0.00 KB (0.00%)
differ.ts 22.00 KB 22.00 KB 0.00 KB (0.00%)
http-client.ts 27.08 KB 27.12 KB -0.03 KB (-0.12%)
http-router.ts 37.00 KB 37.04 KB -0.04 KB (-0.10%)
logger.ts 12.01 KB 12.01 KB 0.00 KB (0.00%)
metric.ts 9.47 KB 9.47 KB 0.00 KB (0.00%)
optic.ts 7.10 KB 7.10 KB 0.00 KB (0.00%)
pubsub.ts 16.02 KB 16.02 KB 0.00 KB (0.00%)
queue.ts 12.89 KB 12.89 KB 0.00 KB (0.00%)
schedule.ts 11.80 KB 11.80 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.14 KB 14.14 KB 0.00 KB (0.00%)
schema-binary.ts 41.99 KB 41.99 KB 0.00 KB (0.00%)
schema-class.ts 21.73 KB 21.73 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.60 KB 29.60 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.99 KB 27.99 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.35 KB 15.35 KB 0.00 KB (0.00%)
schema-string.ts 12.34 KB 12.34 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.27 KB 16.27 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.95 KB 38.95 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.15 KB 26.15 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.90 KB 20.90 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 21.05 KB 21.05 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.14 KB 21.14 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.17 KB 26.17 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.23 KB 21.23 KB 0.00 KB (0.00%)
schema.ts 20.82 KB 20.82 KB 0.00 KB (0.00%)
stm.ts 14.20 KB 14.20 KB 0.00 KB (0.00%)
stream.ts 10.38 KB 10.38 KB 0.00 KB (0.00%)

@tim-smart

Copy link
Copy Markdown
Contributor Author

/rebase

@tim-smart
tim-smart force-pushed the agent/nelson/32c5dbe5d731 branch from e6823f3 to a024192 Compare November 4, 2026 01:00
@effect-bot
effect-bot force-pushed the agent/nelson/32c5dbe5d731 branch from 1d42845 to 7b6b327 Compare November 4, 2026 01:45
@tim-smart
tim-smart changed the base branch from v4/next-minor to main November 4, 2026 02:02
@tim-smart
tim-smart force-pushed the agent/nelson/32c5dbe5d731 branch from 7b6b327 to 1094f7c Compare November 4, 2026 02:02
@effect-janitor effect-janitor Bot added the 4.0 label Nov 4, 2026
@tim-smart
tim-smart merged commit 781f31e into main Nov 4, 2026
18 of 31 checks passed
@tim-smart
tim-smart deleted the agent/nelson/32c5dbe5d731 branch November 4, 2026 02:30
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.

2 participants