Skip to main content
In Code Mode, an agent writes one script that calls many tools, instead of asking for one tool call per model turn. The script runs in the agent’s own sandbox, and each tool call inside it is an HTTPS request to MCP Gateway. The gateway supplies the typed tool catalog and the call path. It doesn’t run the script. A Code Mode harness does three things with the gateway:
  1. Calls tools/list on https://mcp.ngrok.ai to get every tool the access key may call, with its inputSchema and outputSchema.
  2. Generates TypeScript declarations from those schemas, so the model writes typed calls and the harness can typecheck the script before it runs.
  3. Gives the script one function, callTool(name, args), that sends one stateless tools/call request per call.
Upstream credentials stay in the gateway, so the sandbox holds only the access key.

What you’ll need

Send stateless calls

MCP protocol version 2026-07-28 needs no initialize request and no session. Each request is one POST https://mcp.ngrok.ai/ that carries everything the gateway needs: The headers must match the body. A mismatch returns HTTP 400 with JSON-RPC error -32020, such as Mcp-Name must match params.name. The Accept header above returns JSON for every method. When application/json and text/event-stream have equal weight, the gateway streams tools/call results as server-sent events, which a script then has to parse.
The result holds every tool the key may call, each named <label>__<tool>, with the upstream descriptor unchanged. That includes inputSchema, outputSchema when the upstream declares one, and annotations. tools/list is free and returns the full catalog in one response, with no pagination.

Example harness

This TypeScript file lists the tools, writes their declarations to mcp-tools.d.ts, and runs a script with parallel calls. It uses json-schema-to-typescript for the type generation.
Save the file as codemode.ts. The script at the end calls docs__search from the setup example; change it to a tool your key can call.
codemode.ts
Run it:
The first line of output reads Wrote types for <n> tools to mcp-tools.d.ts. Each tool becomes a declaration like this one, which the harness shows to the model and typechecks scripts against:
A script that passes { q: "oauth" } fails the typecheck with Property 'query' is missing, before any call reaches the gateway. A tool without an outputSchema returns unknown, so the script has to inspect its content blocks.

Run calls in parallel

Each callTool is an independent HTTPS request, so Promise.all sends them at the same time. The script’s wall time is close to its slowest call, not the sum of all of them. Don’t use JSON-RPC batches for Code Mode. The gateway accepts batches only on protocol version 2025-03-26, runs the items one after another, and limits a batch to 16 items.

Handle errors and cost

  • A tool that fails returns a result with isError: true, which the example turns into a thrown error. Promise.all rejects on the first one; use Promise.allSettled when the script should keep the other results.
  • A denied tool returns JSON-RPC error -32003, and an account with no credits returns HTTP 402. See Control tool access and Pricing.
  • Every completed tools/call is one billable operation, including an isError result. A script that loops over a tool bills each iteration.
  • Each call can run up to the server’s call timeout. See Limits.
  • Each call appears as its own request in Activity under MCP calls.
Give the sandbox a key whose configuration allows only the servers and tools the script needs, and set an expiry on the key when the script is short-lived.

Use Claude Code’s Code Mode

Claude Code’s Code Mode is experimental and off by default. With it on, Claude writes JavaScript that calls MCP tools by name, so the tools of an MCP Gateway you added with claude mcp add work in that code like any other MCP server’s tools. Claude Code does the type generation and the calls itself; you don’t need the harness on this page.
Claude Code’s Code Mode doesn’t start when Claude Code sends model requests through an LLM gateway, which includes the AI Gateway at gateway.ngrok.ai, or when it authenticates with ANTHROPIC_AUTH_TOKEN or an API key helper. To use Code Mode, connect Claude Code to Anthropic directly. MCP Gateway is a separate MCP server, so it works in Code Mode either way.