Getting started6-minute read7 languages

What Is MCP Code Mode? Fewer Tokens and Round Trips for AI Agents

Code mode lets an AI agent run one short program that calls several MCP tools, instead of calling them one at a time through the model. Why that saves tokens and round trips, when it helps and when it does not, and how AnythingMCP runs it: saved code tools, inline code and agent proposals, in an isolated runner.

HCBy HelpCode teamUpdated 6 min read Open-source on GitHub

No credit card · 7-day trial · Self-host alternative available

  • 7-day free trial
    No credit card required
  • GDPR & SOC 2 ready
    EU data residency, audit logs
  • Open-source on GitHub
    Open source · AGPL-3.0
  • Works with ChatGPT, Claude, Gemini
    Any MCP-compatible client

Summary

Code mode lets an AI agent run one short program that calls several MCP tools, instead of calling them one at a time through the model. Why that saves tokens and round trips, when it helps and when it does not, and how AnythingMCP runs it: saved code tools, inline code and agent proposals, in an isolated runner.

TL;DR: With plain MCP, an AI agent calls one tool, reads the whole result, decides, calls the next tool, and so on. Every step is a round trip through the model, and every intermediate result passes through its context window. Code mode lets the agent (or an administrator, ahead of time) write a short JavaScript program that makes those calls, filters and joins the results, and returns only the answer. In AnythingMCP the program runs in an isolated runner that can reach nothing but the MCP tools the caller is already allowed to use. Code mode is available in self-hosted AnythingMCP from v0.52.0 and coming soon to AnythingMCP Cloud; it is off until you turn it on.

The problem: tool calls go through the model

Take a question a sales lead might ask an assistant connected to the CRM and the ERP:

"Which of our open quotes belong to customers who still have an overdue invoice?"

With one tool call per step, the agent:

  1. calls the CRM to list the open quotes (say 40 of them) and reads all 40 into its context;
  2. calls the ERP once per customer to look up open invoices: 40 more calls, 40 more results in the context;
  3. compares them in its head and writes the answer.

Each call is a turn: the model writes the call, the client runs it, the result goes back into the model, the model reads it and writes the next call. The full results stay in the context, although the answer needs perhaps five lines. That costs tokens (everything the model reads is billed and counts toward its context window), time (dozens of turns), and accuracy (comparing 40 lists by reading them is where models slip).

The idea: let the model write a program instead

Language models are good at writing short programs. So instead of making the calls itself, the agent writes one:

const quotes = await amcp.run('crm_list_open_quotes', {});
const overdue = [];
for (const q of quotes) {
  const invoices = await amcp.run('erp_open_invoices', { customer: q.customerId });
  const late = invoices.filter((i) => i.dueDate < new Date().toISOString().slice(0, 10));
  if (late.length) overdue.push({ quote: q.number, customer: q.customerName, overdueInvoices: late.length });
}
return overdue;

Tool names are illustrative; in practice they are the tools of your connectors.

The program runs next to the tools. The 40 quote records and 40 invoice lists never enter the model's context; only the returned list does. One turn instead of forty-one. The idea was popularised in 2025 under names like "code mode" and "code execution with MCP"; AnythingMCP implements it as a governed feature of the gateway.

One tool call per stepCode mode
Turns through the modelOne per call (41 in the example)One
What enters the contextEvery intermediate resultThe program and its return value
Loops, filters, joinsDone by the model, by readingDone by code, exactly
Best forSingle lookups, steps that need judgementRepetitive, mechanical multi-step work

Three forms in AnythingMCP

Saved code tools. An administrator writes a program once and saves it as a tool, for example customer_overview(customer), which reads the ERP and the CRM and returns one summary. To the AI client it is an ordinary MCP tool: listed, permissioned per role, audited. The agent just calls it. Build your first code tool.

Inline code (code_execute). On servers where it is enabled, the agent can write and run a program on the spot, over the tools the user may use. Always read-only, always with the caller's own permissions.

Proposals (code_propose_tool). When the agent notices a multi-step pattern worth keeping, it can propose it as a saved code tool. The proposal runs nothing and creates nothing; an administrator reviews the program and clicks Create tool. How proposals are approved.

What keeps it safe

Code written by a model is untrusted by construction: a prompt-injected tool result can steer what the model writes. So AnythingMCP never runs it in its own process. Every run goes to a separate code-runner service, one locked-down subprocess per run, with no network, no filesystem, no environment variables and no secrets. The only way out is amcp.run, which the gateway answers under the caller's role, with the same read/write rules as a direct tool call. Every run and every tool call inside it is in the audit log. The full model: Secure code execution for AI agents.

When code mode helps, and when it does not

It helps when the work is mechanical and repetitive: the same lookup for 100 articles, a sum across two systems, matching invoices to orders, a report that always takes the same five calls. See three recipes.

It does not help when there is one call to make (a program around a single call only adds a step), or when every step needs the model's judgement on what it just read ("read this contract and decide what to check next"). Writes are deliberately restricted: inline code never writes, and a saved code tool may call write tools only if an administrator allows it, on Enterprise.

Availability and limits

Self-hosted (v0.52.0+)AnythingMCP Cloud
Code modeAvailable, off by defaultComing soon
RequirementsThe code-runner service, and code mode turned on per MCP server–

Per edition, as announced with v0.52:

CommunityStarterTeamEnterprise
Saved code tools3520Unlimited
Inline code and agent proposals––✓✓
Code tools that may write, "run as definer"–––✓

A single run is limited to 10 seconds, 500 tool calls (16 at a time), and 1 MB of returned data. Code tools are served only on a workspace's own MCP servers, never on the shared /mcp endpoint used by directory AI clients. Compare plans.

FAQ

Is code mode a replacement for MCP? No. It is a way of using MCP tools. The program calls the same tools the agent could call directly, through the same gateway and permissions.

Which AI clients support it? All of them. A saved code tool is a normal MCP tool, and code_execute is a normal tool that takes a program as its argument. Claude, ChatGPT, Copilot, Gemini and Cursor can call both.

Which language? JavaScript, as the body of an async function. No imports, no packages: the program has amcp and the language itself.

How much does it save? It depends entirely on the task. A single lookup saves nothing. A task that would read dozens of intermediate results into the context saves most of those tokens and turns, because only the return value goes back to the model.

Was this guide helpful?

Ship MCP to your stack in 60 seconds.

Spin up AnythingMCP on managed Cloud or self-host it on your infrastructure. Free for 7 days, no credit card.

Related guides

GS
Getting started

Code Mode: Cut AI Agent Token Usage by Running Tools in One Step

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%.

GS
Getting started

MCP Code Mode Recipes: Monthly Revenue, Invoice Reconciliation and 100 Price Lookups in One Call

Three practical code mode programs for AnythingMCP: monthly revenue across the online shop (Shopify) and accounting (Lexware Office), orders reconciled against invoices, and 100 customer price lookups in one tool call instead of 100. With the code, the limits that matter, and when to save a program as a tool.

GS
Getting started

Build Your First MCP Code Tool: ERP Orders and CRM Account in One Call

A step-by-step tutorial for AnythingMCP code mode: start the code runner, turn code mode on, and save a code tool that reads the last orders from the ERP (weclapp) and the account and open deals from the CRM (HubSpot) in one tool call. Read-only, with the caller's permissions, audited.

GS
Getting started

Connect AnythingMCP to Claude: One URL for All Your Business APIs

Add AnythingMCP as a Claude connector in five minutes. Sign in, pick the MCP servers Claude may reach, and query weclapp, DATEV, Etsy, Deutsche Bahn or your own API in plain language.

GS
Getting started

The MCP Knowledge Graph: a connector that learns how your systems connect

AnythingMCP isn't just an MCP proxy — it builds a knowledge graph of your tools so AI agents know how to chain a customer in your CRM to their orders in your ERP. Here's how the smart connector works.

GS
Getting started

What is the Model Context Protocol (MCP)?

A comprehensive guide to understanding the Model Context Protocol — how it works, why it matters, and how to get started.