Skip to content

execute/search return formatter errors for non-JSON sandbox results #208

Description

@keeltrace

Current behavior

execute and search can return JavaScript values that cross the Worker RPC boundary successfully but are not JSON-stringifiable. The shared response formatter then throws and turns a successful sandbox program into an MCP tool error.

Confirmed real-tool examples:

async () => {}
async () => 1n
async () => { const x = {}; x.self = x; return x }

Observed errors include:

Error: Cannot read properties of undefined (reading 'length')
Error: Do not know how to serialize a BigInt
Error: Converting circular structure to JSON

Reproduction / evidence

Reproduced through the real MCP tool path on the current branch state using Node 22.23.2. Each value fails through both execute and search.

The direct root cause is in src/truncate.ts: every non-string goes through JSON.stringify(content, null, 2), then the result is treated as a string. undefined produces no string; BigInt and cyclic objects make JSON serialization throw.

Existing truncation tests cover strings, objects, large values, empty string, null, and arrays, but not these structured-clone values.

Expected behavior

A sandbox program that completes successfully should produce a valid MCP text result (using a deliberate representation for values JSON cannot encode), rather than an internal response-formatting exception.

Likely scope

This appears narrowly fixable in response formatting plus regression coverage. It should not require auth/token, deployment, dependency, or OpenAPI pipeline changes.

Duplicate / ownership check

I searched open/closed issues and PRs for the exact errors, truncateResponse, undefined/no-return, BigInt, and circular-result behavior and did not find a matching report or active fix. Issue #68 is broader model-evaluation work and does not appear to cover this formatter defect.

If this is a useful fix, I can take it; assignment or maintainer direction would be appreciated before I implement it.

Activity

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions