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
Plan restocks for everything below target.
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 })));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.
- 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.
- 02
Batch calls in parallel
Promise.alldispatches all ten plan_restock calls together inside one turn instead of burning extra model round trips. - 03
Every call recorded on the run
The
RunRecordlogs each tool call intoolCallswith its id, name, and ok status so your app can audit everything the script invoked.
codemoderunIdrun_7c91f2a4
| name | id | ok |
|---|---|---|
| read_inventory | tc_01 | true |
| plan_restock | tc_02 | true |
| plan_restock | tc_03 | true |
| plan_restock | tc_11 | true |
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, andbash
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
Read inventory and target stock from SQLite.
Set one SKU's local restock quantity. Does not place orders.
Find products in the supplier's catalog by name or SKU.
Price per case for one product, with volume breaks.
Days from order to delivery for one product.
Open delivery slots for the next two weeks.
Sales for one day, item by item.
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.
await searchTools("price per case for oat milk")
- 1
name: "supplier__get_price"
description: "Price per case for one product, with volume breaks."
input: "{ sku: string }"
- 2
name: "supplier__list_price_breaks"
description: "Case prices by quantity for one product."
input: "{ sku: string }"
- 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.
// 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"],
}),
});| sku | quantity | sku | quantity |
|---|---|---|---|
| BEAN-01 | 32 | MILK-05 | 27 |
| COCO-08 | 15 | OAT-02 | 24 |
| CROI-11 | 20 | SUG-09 | 40 |
| DECAF-12 | 9 | SYRP-06 | 10 |
| FILT-13 | 80 | TEA-07 | 15 |
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
codemodetobuiltinson 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
codemodestep can fetch records, filter out rows that do not matter, and dispatch parallel calls withPromise.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, anddescribeNamespace()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
mcpServersover HTTP—using configured headers or runtime-authenticated Ed25519 JWTs (auth: { type: "runtime" })—and their tools become callable insidecodemodealongside your webhook tools and built-ins. - q6How do approvals work with tools and code mode?
- Mark any custom tool with
requiresApprovalor list tool names and prefixes inapprovalRequired(for examplesupplier__*). When a gated tool runs, camelRun pauses ininput_requireduntil a person's approval arrives viaPOST /v2/agents/:key/inputs/:inputId—billing $0 while parked. - q7When should I use QuickJS codemode vs Linux bash containers?
- Use
codemodefor fast JavaScript orchestration, filtering JSON, and batching tool or MCP calls with no container boot. Addbashtobuiltinswhen an agent needs an isolated Linux container with Node 24, Python 3, git, and ripgrep mounted over its versionedcamelFSworkspace ($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
fshelpers (whenfilesis enabled) explicitly granted to that agent, while your private services stay behind signed webhook or MCP endpoints.
More on tools for AI agents
- OverviewcamelRunThe runtime overview: the agent loop, channels, scale and prices.
- camelRunMCP agentsConnect agents to remote HTTP MCP tools. Use the hosted MCP server at /mcp with OAuth 2.1 or connect to one agent at /v2/agents/:key/mcp.
- camelRunMulti-agent orchestrationUse delegate to hand off a task, or use spawn_agent and wait_agent to run child agents in parallel and collect their results. The parent can park while its children work.
- CompareCloudflare Agents alternativeCompare Cloudflare's Agents SDK with camelRun's hosted agent loop. camelRun stores keyed agent state in SQLite Durable Objects and workspace files in camelFS.
- camelRunHuman-in-the-loop AI agentsPause a run for an approval or answer and resume when the person responds. Waiting for input costs $0 in agent time.
- camelRunMulti-tenant AI agentsGive each user or thread a keyed agent with its own history and workspace. Runtime-authenticated MCP calls carry Ed25519-signed JWTs proving the agent and end-user.
- camelRunDurable AI agentsKeyed agents keep their history, versioned camelFS workspaces, and pending runs in camelRun. Fork any agent and its repo instantly at any step (atSeq).
- GuideAgent Harness vs Framework vs Runtime vs Sandbox, ExplainedWhere an agent's loop, its hosting and the code the model writes each belong.
- DocsTools in camelRunWebhook tools, remote HTTP MCP servers, built-ins, QuickJS codemode, and Linux containers.
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.
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.