Code mode · MCP · Tool search

Code mode for AI agents

camelRun by camelAI runs QuickJS codemode so agents batch tool and MCP calls in one step with fewer round trips.camelRun by camelAI lets AI agents run JavaScript in an isolated QuickJS codemode sandbox. Batch tool and MCP calls in one step, filter intermediate data out of model context, and cut round trips.

camelRun runs: model turns and codemodeYour tools

Agentinventory
completed
You

Plan restocks for everything below target.

codemodeQuickJS sandbox
const rows = await tools.read_inventory({});
const low = rows.filter(row => row.stock < row.target);
return await Promise.all(low.map(row => tools.plan_restock({ sku: row.sku, quantity: row.target - row.stock })));
Code mode
model
codemode
read_inventory
plan_restock
BEAN-01→ 32
COCO-08→ 15
CROI-11→ 20
DECAF-12→ 9
FILT-13→ 80
MILK-05→ 27
OAT-02→ 24
SUG-09→ 40
SYRP-06→ 10
TEA-07→ 15
model
model reads
Direct tool calls
model
your tools
model reads

camelRun runs: model turns and codemodeYour tools

How it works

Fewer model turns, cleaner context

Enable codemode in builtins on any agent and the model writes JavaScript that calls your webhook tools, local tools, and remote MCP servers in one pass. Loops and filters run inside QuickJS before anything enters model context.

  1. 01

    Filter data before it hits context

    The script reads all fourteen inventory rows and filters down to the ten below target inside QuickJS. The model reads only the compact return value, not every raw row.

  2. 02

    Batch calls in parallel

    Promise.all dispatches all ten plan_restock calls together inside one turn instead of burning extra model round trips.

  3. 03

    Every call recorded on the run

    The RunRecord logs each tool call in toolCalls with its id, name, and ok status so your app can audit everything the script invoked.

RunRecord · toolCalls
11 calls

codemoderunIdrun_7c91f2a4

nameidok
read_inventorytc_01true
plan_restocktc_02true
plan_restocktc_03true
plan_restocktc_11true
upsert-agent.ts
GitHub
const headers = { Authorization: `Bearer ${process.env.CAMELRUN_API_KEY}`, "Content-Type": "application/json" };
await fetch("https://run.camelai.com/v2/agents/inventory", {
  method: "PUT",
  headers,
  body: JSON.stringify({
    instructions: "You manage cafe stock. Plan restocks without placing orders.",
    builtins: ["files", "codemode"],
    tools: [
      {
        name: "read_inventory",
        description: "Read inventory and target stock from SQLite.",
        parameters: { type: "object", properties: {} },
        url: "https://cafe.example.com/tools/read-inventory",
      },
      {
        name: "plan_restock",
        description: "Set one SKU's local restock quantity.",
        parameters: { type: "object", properties: { sku: { type: "string" }, quantity: { type: "integer" } }, required: ["sku", "quantity"] },
        url: "https://cafe.example.com/tools/plan-restock",
      },
    ],
    mcpServers: [{ name: "supplier", url: "https://mcp.example.com/supplier", auth: { type: "runtime" } }],
  }),
});

Code mode MCP

QuickJS codemode for MCP servers

Attach remote HTTP MCP servers in mcpServers alongside your HTTPS webhook or local tools. Inside codemode, every tool is callable by name—while bounded inline declarations keep big catalogs from crowding the prompt.

Configure webhook tools, remote MCP servers, and approvalRequired patterns per agent or definition, and inspect them in the console.

webhook
HTTPS tools in your app, signed with runtime Ed25519 JWTs
mcp
remote HTTP MCP servers with headers or runtime auth
builtin
opt-in platform tools like codemode, files, and bash
Console/Agents/inventory

Tools the model gets (117)

Opt into codemode in builtins so the model can batch your webhook and mcp tools in QuickJS. Gate sensitive actions with approvalRequired. How

Webhook toolsconnected
read_inventorywebhook

Read inventory and target stock from SQLite.

plan_restockwebhook

Set one SKU's local restock quantity. Does not place orders.

MCP serversupplierhttps://mcp.example.com/supplierruntime auth
supplier__search_productsmcp

Find products in the supplier's catalog by name or SKU.

supplier__get_pricemcp

Price per case for one product, with volume breaks.

supplier__get_lead_timemcp

Days from order to delivery for one product.

supplier__list_delivery_slotsmcp

Open delivery slots for the next two weeks.

MCP serverposhttps://mcp.example.com/posruntime auth
pos__list_salesmcp

Sales for one day, item by item.

pos__top_itemsmcp

Best sellers between two dates.

Tool search

Discover tools inside QuickJS with BM25 search

When an agent connects multiple MCP servers, codemode keeps inline declarations within a bounded budget and exposes searchTools(), describeTool(), and describeNamespace() so scripts find and inspect tools on demand.

Your app 2
pos 38
rota 24
supplier 46
supplier 46
codemode (QuickJS)
BM25 matches

await searchTools("price per case for oat milk")

  1. 1

    name: "supplier__get_price"

    description: "Price per case for one product, with volume breaks."

    input: "{ sku: string }"

  2. 2

    name: "supplier__list_price_breaks"

    description: "Case prices by quantity for one product."

    input: "{ sku: string }"

  3. 3

    name: "supplier__get_case_size"

    description: "Units in one case of a product."

    input: "{ sku: string }"

Bounded inline declarations
Tool declarations stay within a bounded prompt token budget, keeping large MCP catalogs from crowding every model turn.
BM25 keyword ranking
searchTools() ranks tools with BM25 across names and descriptions, returning top matches inside the same script.
Inspect schemas on demand
Call describeTool() for an exact parameter schema or describeNamespace() to list a connected server's tools before invoking them.

QuickJS vs Linux container

Pair QuickJS codemode with isolated Linux containers

Use codemode for fast JavaScript batching across tools and MCP servers with zero container boot. Add bash to builtins when an agent needs a full Linux container with Node 24, Python 3, git, and ripgrep over its versioned camelFS workspace.

codemode-vs-bash.ts
// QuickJS codemode: fast JavaScript tool orchestration with zero container boot.
// Add "bash" when the agent needs an isolated Linux container (Node 24, Python 3, git, ripgrep).
await fetch("https://run.camelai.com/v2/agents/inventory", {
  method: "PUT",
  headers,
  body: JSON.stringify({
    instructions: "Batch stock checks in codemode; run Python or git in bash when needed.",
    builtins: ["files", "codemode", "bash"],
    approvalRequired: ["supplier__place_order"],
  }),
});
Your databaseSELECT sku, quantity FROM restock ORDER BY sku
The restock table after the code ran
skuquantityskuquantity
BEAN-0132MILK-0527
COCO-0815OAT-0224
CROI-1120SUG-0940
DECAF-129SYRP-0610
FILT-1380TEA-0715
FAQ

Code mode and tool search questions

q1What is code mode for MCP?
Code mode lets an AI agent write JavaScript that calls its tools and MCP servers as functions instead of issuing one tool call per turn. In camelRun v2, opt in by adding codemode to builtins on the agent or definition; scripts run in an isolated QuickJS sandbox and return a compact result to the model.
q2Is code mode the same as programmatic tool calling?
Yes. Instead of round-tripping every raw tool output through the model, the model writes a short JavaScript script that loops, filters, and invokes tools via tools.<name>(), then reads back only the script's return value.
q3Why let the model write code to call tools?
Fewer model round trips and cleaner context. A single codemode step can fetch records, filter out rows that do not matter, and dispatch parallel calls with Promise.all—keeping bulky intermediate payloads out of the model's context window.
q4How does MCP tool search work in code mode?
Inline tool declarations stay within a bounded prompt token budget. Inside QuickJS scripts, the model calls searchTools() for BM25 keyword search, describeTool() to inspect a tool's schema, and describeNamespace() to explore a connected server's tools in the same script.
q5Does code mode work with remote MCP servers?
Yes. Connect your MCP servers via mcpServers over HTTP—using configured headers or runtime-authenticated Ed25519 JWTs (auth: { type: "runtime" })—and their tools become callable inside codemode alongside your webhook tools and built-ins.
q6How do approvals work with tools and code mode?
Mark any custom tool with requiresApproval or list tool names and prefixes in approvalRequired (for example supplier__*). When a gated tool runs, camelRun pauses in input_required until a person's approval arrives via POST /v2/agents/:key/inputs/:inputId—billing $0 while parked.
q7When should I use QuickJS codemode vs Linux bash containers?
Use codemode for fast JavaScript orchestration, filtering JSON, and batching tool or MCP calls with no container boot. Add bash to builtins when an agent needs an isolated Linux container with Node 24, Python 3, git, and ripgrep mounted over its versioned camelFS workspace ($0.00005/sec or $0.18/hr active container time, with free pre-warming).
q8Is it safe to let the model run JavaScript in code mode?
Yes. Scripts execute in a dedicated QuickJS sandbox Worker isolated from the host runtime, with bounded memory and execution time. A script can only call tools and repo fs helpers (when files is enabled) explicitly granted to that agent, while your private services stay behind signed webhook or MCP endpoints.

Build an agent with code mode

Add codemode to an agent's builtins to batch tools and remote MCP servers in QuickJS—or add bash when you need a full Linux container.

$0.05 per active agent-hour ($0 while idle or parked). Active Linux containers cost $0.00005/sec ($0.18/hr) with free pre-warming. Storage is currently unmetered; BYOK model tokens are $0 from camelRun, and direct provider tokens pass through with no markup.

Connect your coding agent (no key)

claude mcp add --transport http camelrun https://run.camelai.com/mcp

Adds camelRun's hosted MCP server to Claude Code (also works with Codex and ChatGPT). Signing in creates your account automatically.