MCP code mode
Last updated: 2026-11-21T10:44:38-03:00 (UTC-3)
Code mode lets an agent use many MCP tools without putting every tool definition in the model’s context. Instead of a full listing, the client gets three bounded operations: search for tools by name and description, read the schema of the one it needs, and execute a short JavaScript program that calls tools with loops and conditions inside the gateway’s extension sandbox. Only the program’s result goes back to the model.
| Flag | Default | What it turns on |
|---|---|---|
mcp_code_mode |
false |
POST /v1/mcp/code/search, /schema and /execute; requires extension_javascript, extensions, mcp_execution and mcp_registry. A plan right under entitlements, next to mcp_execution |
A key needs mcp:read and mcp:execute, like a direct MCP call.
Operations
Section titled “Operations”Search - {"query": "open issues", "limit": 5} returns at most code_mode.search_limit tools the key may run, as server_id, tool and a description cut at 300 bytes, with authorized_tools (how many the key may run). The candidates are built from the same grants as a call before anything is ranked, as in tool search; a tool the key cannot run never appears or counts.
Schema - {"server_id": "tracker", "tool": "count_issues"} returns that tool’s live description and input_schema, read through the same route as the tool listing, with every grant, scope, quarantine and approved-schema check.
Execute - {"program": "..."} runs the program, the body of a function that returns its result:
const [tool] = tools.search("open issues", 1);const counts = {};for (const label of ["bug", "security", "docs"]) { try { counts[label] = JSON.parse(tools.call(tool.server_id, tool.tool, { label }).content[0].text).count; } catch (e) { counts[label] = "unavailable: " + e.code; // not_found, step_limit, key_refused... }}return counts;tools.search(query, limit), tools.schema(server, tool) and tools.call(server, tool, args) are the only host operations. The full example is extensions/examples/javascript-code-mode/program.js.
What every step goes through
Section titled “What every step goes through”Each operation of a program is one step through the same route as a direct call:
- the live configuration is read and the key is authenticated again, with its
mcp:readscope and themcp_executionandmcp_code_modeplan rights, so a key revoked or narrowed, or the flag turned off, during a run stops it at the next step (key_refused); a temporary verification failure fails only that step; - registry scopes, tool grants, access-group toolsets, quarantine and approved schemas are checked live; searches share the bounded MCP operation slots;
- arguments are validated against the tool schema; the server credential is resolved inside the gateway and never reaches the program;
- the call is recorded in the MCP execution trail with surface
code_modeand the run ID as its session, and the step trail carries its execution ID; - a refused or failed call is an exception with a code that the program may catch and recover from; calls are never retried.
Bounds
Section titled “Bounds”The program runs in QuickJS compiled to WebAssembly, a fresh instance per run, with no file system, environment, network, clock or credential in reach (extensions.code_mode, restart to apply):
| Setting | Default | Bound |
|---|---|---|
extensions.code_mode.max_program_bytes |
16384 | Larger programs answer 413 |
extensions.code_mode.max_steps |
25 | Search, schema and call operations per run; then step_limit |
extensions.code_mode.timeout_ms |
15000 | The whole run; the program is interrupted, even in an infinite loop |
extensions.code_mode.step_timeout_ms |
10000 | One tool call |
extensions.code_mode.max_step_bytes |
262144 | One tool answer or definition handed to the program |
extensions.code_mode.max_result_bytes |
65536 | The program’s JSON result |
extensions.code_mode.memory_pages, extensions.code_mode.heap_bytes |
1024, 32 MiB | WebAssembly memory and QuickJS heap |
extensions.code_mode.search_limit |
10 | Tools per search |
extensions.code_mode.team_concurrency |
2 | Runs per team per replica; more answer 429 code_mode_busy. Runs per replica are also capped by extensions.max_concurrency |
A run is cancelled when the client disconnects.
Answer and audit
Section titled “Answer and audit”Any run that started answers 200 with run_id, status (ok, program_error, timeout, memory_limit, output_limit, canceled, key_refused), result (when ok), a bounded error message, the steps (operation, server, tool, HTTP status, code, execution ID, duration, answer size) and metrics (steps, tool calls, duration, result and tool answer bytes). The step trail stores only fixed operation names and validated server and tool identifiers, so a program cannot write content into its record. Each run writes one extension execution record (hook code_mode, GET /api/v1/admin/extensions/executions) with identifiers, outcome and the step trail - never the program, arguments or results.
Measured effects
Section titled “Measured effects”Measured in the PostgreSQL test with a fake MCP server of 44 granted tools, not a promise of savings for any workload:
- Context: the full tool listing for the key is 18,889 bytes; a search of 3 results plus one schema is 228 bytes.
- Latency: a program making five sequential tool calls ran in 76 ms; the same five calls made one by one over REST took 74 ms in the same process. Code mode saves the model round trips between calls, not gateway time.
- Runtime: a program with three host calls costs about 1.3 ms of sandbox time (
BenchmarkJavaScriptProgram).
What a real agent saves depends on how many tools it would otherwise list and how many model turns the program replaces; measure it on your own traffic.