Summary
Code mode lets an AI agent run a short program that orchestrates many MCP tools in a single call, instead of one round trip per tool. Filtering and joining happen in a locked-down sandbox, so only the final answer returns — cutting token usage on data-heavy workflows by up to 90–99%.
Why multi-tool agents burn so many tokens
Every time an AI agent calls a tool, the result travels back through the model. Ask it to "list the open invoices for every customer who ordered last quarter", and a tool-at-a-time agent makes dozens of calls and pulls every intermediate list — customers, orders, invoices — into the context window. The model reads all of it, token by token, just to keep a handful of ids at the end. On a data-heavy workflow the intermediate data, not the answer, is what costs you.
Code mode removes the round trips. Instead of calling tools one at a time, the agent writes a short JavaScript program that calls several of your MCP tools, filters and joins their results, and returns only what matters — in a single step. Dozens of calls and megabytes of intermediate data become one call that returns a few lines. On multi-step, filter-heavy tasks this cuts token usage by up to 90–99%, and it is faster, because the loop runs next to the data instead of through the model.
Before and after
Without code mode — the model drives every step and reads every result:
search invoices → (model reads 400 rows)
list customers → (model reads 1,200 rows)
for each open invoice, get the customer → (hundreds of calls, all read back)
With code mode — the model writes one program; only the result returns:
// Discover, read and call your tools from inside the sandbox.
const invoices = await amcp.run('list_invoices', { status: 'open' });
const customers = await amcp.run('list_customers', { activeSince: '2026-01-01' });
const active = new Set(customers.map((c) => c.id));
return invoices
.filter((i) => active.has(i.customerId))
.map((i) => ({ id: i.id, amount: i.amount, customer: i.customerId }));
The agent gets back the short list it actually asked for. The 1,600 rows it filtered over never enter the context window.
Untrusted by design — so it stays safe
A program is written by a model, and a prompt-injected tool result could try to
steer it. So code mode never runs in the AnythingMCP backend. It runs in a
separate code-runner: one locked-down subprocess per run, with no network,
no filesystem, no environment and no secrets. The only thing a program can do is
call the MCP tools the caller is already allowed to use, through an amcp
bridge the backend answers under that caller's role and permissions. Reach and
read/write rules are exactly the same as a direct tool call, every run is
audited, and a run is bounded by a time limit, a memory ceiling and a cap on how
many tool calls it may make.
Code mode is off by default. A workspace turns it on per MCP server, and inline code is also gated by the caller's role — so nothing runs until you opt in.
Two ways to use it
- Saved code tools. A reusable tool whose body is a stored program, created in the dashboard like any other tool. It shows up in the tool list, obeys role access and is audited. Perfect for a workflow your team runs again and again.
- Inline code. A generic Run code tool the agent can call with a program it writes on the spot, always read-only and with the caller's own permissions. An agent can also propose a saved code tool for an admin to approve.
Turning it on
- Open an MCP server in the dashboard and set Code mode to Saved code tools or Saved code tools + inline code.
- Pick which connectors the server exposes — a program can only reach those.
- Connect the server to Claude, ChatGPT or any MCP client and ask it to run a multi-step task. On AnythingMCP Cloud the runner is managed for you; when you self-host, run the bundled code-runner service.
Code mode is available on AnythingMCP Cloud and self-hosted. Saved code tools are included from the Starter plan up; inline code and agent proposals are on Team and above; write-enabled code tools are an Enterprise feature. See the pricing page for what each plan includes, or start with a free trial.
Was this guide helpful?