Skip to main content

Overview

Code Mode is an opt-in Smart Routing capability ($smart and $smart/<group>) that lets an LLM write a short async JavaScript snippet to search for tools, call multiple upstream tools in loops, filter or aggregate intermediate payloads, and return only the final answer. By keeping intermediate payloads inside the sandbox instead of bouncing every raw upstream response back through the model context window, Code Mode reduces multi-step round trips and token consumption while reusing MCPHub’s exact caller-scoped authorization pipeline. Code Mode is off by default and must be explicitly enabled by an administrator.

Exposed Tools

When enabled under Settings → Smart Routing → Code Mode, $smart and $smart/<group> routes expose:
  • execute_code({ code }): Runs an async JavaScript function body in an isolated QuickJS-NG WebAssembly worker and returns { value, logs, toolCalls }.
  • execute_code_read({ code }) (enabled by default when Code Mode is on; can be toggled off separately): Runs the same sandbox, but restricts tools.call(...) to tools whose descriptors declare readOnlyHint: true and do not set destructiveHint: true.
Existing Smart Routing meta-tools (search_tools, describe_tool, call_tool) remain available. describe_tool also returns a generated typescriptSignature derived from each tool’s inputSchema to help models write accurate tools.call(...) invocations.
MCP readOnlyHint and destructiveHint annotations are advisory hints self-declared by upstream servers. For hard security boundaries, configure MCPHub Tool Policies in addition to execute_code_read.

Sandbox API

Scripts run as the body of an async () => { ... } function. Top-level await and return are supported:

Globals Available Inside the Sandbox

  • tools.search(query, { limit? }): Runs caller-scoped semantic tool search (search_tools).
  • tools.call(toolName, args?): Invokes an authorized tool through MCPHub’s $smart call_tool path.
    • If the upstream tool returns isError: true, tools.call throws a JavaScript Error.
    • If structuredContent is present, it is returned directly.
    • Otherwise, a single JSON text block is parsed and returned as a JS value; plain text is returned as a string; mixed/binary results return the raw CallToolResult.
  • console.log / info / warn / error: Captured up to maxLogBytes and returned in logs (with truncatedLogs: true if capped).

Security Model & Isolation

  • QuickJS-NG WebAssembly in worker_threads: Guest code executes inside QuickJS-NG compiled to WebAssembly (quickjs-emscripten), isolated from Node.js and running off the main event loop.
  • No ambient capabilities: The sandbox has no access to fetch, network sockets, filesystem (fs), process, environment variables, require/import, or timers. Credentials never enter the sandbox.
  • Zero new permission surface: Every tools.call() bridges back to the main thread inside the caller’s live request and user context, enforcing group visibility, bearer-key exclusions and personal-key scopes, user-scoped runtimes, Tool Policies, TOOL_CALL_GUARD_ENABLED, hidden parameters, file staging, activity logging, and refusal of hub built-ins (mcphub_file_*) and recursive meta-tool calls.
  • Intermediate vs. final compression: When Tool Result Compression is enabled, intermediate tools.call() payloads stay uncompressed (bounded by maxToolResultBytes) so scripts can reliably parse JSON, while the final execute_code output is compressed once before returning to the model.
  • Elicitation & Sampling: Upstream elicitation requests are rejected with elicitation_unsupported while a script is running.

Limits & Configuration

Configure Code Mode in Settings → Smart Routing or via environment variables:

Error Kinds

When execution fails, execute_code returns isError: true with a structured error payload { error: { kind, message }, toolCalls }:
  • disabled — Code Mode or execute_code_read is turned off, or the route is not $smart.
  • syntax_error / runtime_error — Invalid JavaScript or uncaught exception.
  • timeout / cpu_timeout — Wall-clock timeout or JS CPU budget exceeded (worker terminated).
  • memory_limit — QuickJS heap limit exceeded.
  • call_budget_exceeded — Script exceeded maxToolCalls.
  • source_too_large / tool_result_too_large / output_too_large — Payload exceeded configured byte limits.
  • concurrency_limit — Too many concurrent sandbox runs active.
  • unauthorized / readonly_violation — Caller lacks access, attempted to call a hub built-in, or called a non-read-only tool from execute_code_read.
  • elicitation_unsupported — An upstream tool requested elicitation during script execution.