In short
- When an AI agent calls tools one at a time, every intermediate result passes through the model, even when the answer needs five lines of it.
- Code mode lets the agent run one short JavaScript program that makes those calls, filters and joins the results, and returns only the answer.
- In AnythingMCP the program runs in a separate sandbox with no network, no files and no secrets. It can only call the tools the caller may already use.
The context window is the budget
Context engineering is the work of deciding what reaches the model. Anthropic defines it as "the set of strategies for curating and maintaining the optimal set of tokens" and adds that context "must be treated as a finite resource with diminishing marginal returns" (Effective context engineering for AI agents, September 2025).
Tools are where that budget goes first. MCP, the Model Context Protocol, is the open standard AI clients such as Claude, ChatGPT, Copilot and Cursor use to call tools. With plain MCP, the agent calls one tool, reads the whole result, decides, and calls the next.
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 40 open quotes, the agent lists the quotes, then looks up the invoices of each customer. That is 41 tool calls. Each one is a turn through the model, and each result stays in the context window.
The cost is not special to one vendor. Anthropic describes the same pattern: "Every intermediate result must pass through the model." In its example, a transcript of a two-hour meeting that is copied from one tool to another adds about 50,000 tokens (Code execution with MCP, November 2025).
What code mode is
Code mode lets an AI client run a short JavaScript program that calls several MCP tools in one step. The program filters, joins or loops over the results, and only its return value goes back to the model.
The reason it works is simple. Cloudflare put it this way in the post that carries the name: "LLMs are better at writing code to call MCP, than at calling MCP directly" (Code Mode: the better way to use MCP, September 2025).
AnythingMCP is an open-source MCP gateway. It puts one governed endpoint in front of the ERP, databases and APIs a company already runs. Inside a code-mode program you have exactly one capability, an object called amcp:
| Call | What it does |
|---|---|
amcp.search(query) |
Finds tools the caller may use. |
amcp.describe(tool) |
Returns a tool's input schema. |
amcp.run(tool, args) |
Calls the tool and returns its result as a value. |
The 41 calls from the example become this. The tool names are illustrative. In practice they are the tools of your own connectors.
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;
The 40 quote records and the 40 invoice lists never enter the context. The returned list does.
| One tool call per step | Code mode | |
|---|---|---|
| Turns through the model | One per call (41 in the example) | One |
| What enters the context | Every intermediate result | The program and its return value |
| Loops, filters, joins | Done by the model, by reading | Done by code, exactly |
| Best for | Single lookups, steps that need judgement | Repetitive, mechanical multi-step work |
The second chart is Anthropic's own example, about tool definitions that are read on demand instead of loaded up front.
Measured: 20 calls or one program
We ran one task both ways on a self-hosted AnythingMCP 0.55.2 with the public OpenPLZ connector: find the localities of 20 German postal codes and group them by federal state.
| One tool call per postal code | One saved code tool | |
|---|---|---|
| Tool calls the AI client makes | 20 | 1 |
| Characters of tool results that reach the model | 7,283 | 895 |
That is 88% less text in the context, and one turn through the model in place of twenty. The program is the one from the tutorial below. The saving depends on the task: it grows with the number of calls and with the size of what each call returns.
Three ways to use it
| Saved code tool | Inline code | Proposal | |
|---|---|---|---|
| What it is | A stored program that appears as an ordinary MCP tool | The tool code_execute, which runs a program the agent writes on the spot |
The tool code_propose_tool, which records a program for an admin to review |
| Who writes the program | An admin, in the dashboard | The agent | The agent |
| Permissions | Invoker: the caller's own. Definer: the author's, admin-only | Always the caller's own | Nothing runs |
| May change data | Only with Allow write tools, on Enterprise | Never | Never |
| Plans | Community 3 tools, Starter 5, Team 20, Enterprise unlimited | Team and Enterprise | Team and Enterprise |
A proposal does not run code and does not create a tool. An admin reads the program and clicks Create tool. The new tool starts read-only and runs with each caller's own permissions, so an agent can never raise its own authority.
Tutorial: your first code tool
Code mode is off by default everywhere. It needs two things: a running code-runner, and code mode turned on for one MCP server. An MCP server in AnythingMCP is one endpoint with its own set of connectors.
Step 1: start the code-runner
On AnythingMCP Cloud the runner is managed for you. Go to step 2.
On a self-hosted instance (v0.52.1 or later), add two lines to .env and start the service:
COMPOSE_PROFILES=code-runner
CODE_RUNNER_URL=ws://code-runner:8777
docker compose up -d
If you already use profiles, list them together, for example COMPOSE_PROFILES=files,code-runner. The runner holds no secrets and needs no inbound internet. Without it, every code run is refused. Code never falls back to running in the backend.
Step 2: turn code mode on for one MCP server
In the dashboard, open MCP Servers and then the server. In the Server settings card, set Code mode and click Save.
| Setting | API value | What it serves |
|---|---|---|
| Off (no code execution) | off |
Nothing. This is the default. |
| Saved code tools only | tools |
Saved code tools |
| Saved code tools + inline code | inline |
Also code_execute and code_propose_tool, for roles with the inline-code capability |
The same from the terminal, with the amcp command line tool and an admin key:
npm i -g @anythingmcp/cli
amcp login --browser --scope admin --url https://cloud.anythingmcp.com
amcp api PUT /api/mcp-servers/$SRV --yes --data '{"codeMode":"inline"}'
Reconnect your AI client afterwards so it sees the new tools.
Step 3: create a saved code tool
Open a connector, go to MCP Tools and click Add Tool. Set Tool Type to Code Tool.
| Field | What to enter |
|---|---|
| Tool Name | plz_localities_by_state |
| Description | What the tool returns. The AI reads it to decide when to call the tool. |
| Program | The body of an async JavaScript function. Inputs are in args. Whatever you return is what the AI receives. |
| Uses (connectors) | The connectors, and optionally the exact tools, the program may call. Only connectors of the same workspace. |
| Run as | Invoker (the caller's own permissions): every inner call is checked against the role of the person using the tool. |
| Allow write tools | Leave it off. The program may then call only read-only tools. |
| Input Parameters | One parameter, postalCodes, of type string. |
This is the program from the screenshot, and the one we measured above. It uses the OpenPLZ connector from the catalog, which needs no API key, so you can repeat it on your own instance.
const codes = String(args.postalCodes).split(",").map((c) => c.trim()).filter(Boolean);
const byState = {};
for (const postalCode of codes) {
const rows = await amcp.run("openplz_lookup_postalcode", { postalCode });
for (const row of rows) {
const state = row.federalState.name;
(byState[state] ||= []).push(postalCode + " " + row.name);
}
}
return { codes: codes.length, byState };
Uses (connectors) is a JSON list. A connector's id is the last part of its address, /connectors/<id>. Add a tools list to an entry to allow only some of the connector's tools.
[{ "connectorId": "<OpenPLZ connector id>" }]
A call to a tool outside this list fails, even if the user could call that tool directly.
Step 4: test it, then ask
Click the tool's Test button first. It runs the program through the code-runner under the same rules as a real call.
Then refresh the tools in your AI client and ask a question the tool answers. The assistant calls it once.
Step 5: run a program from the terminal
With code mode set to inline, the server offers code_execute. Log in with an MCP key for that server, save a program to a file and send it:
amcp login --url https://cloud.anythingmcp.com --server SERVER_ID
jq -Rs '{code: .}' program.js | amcp tools call code_execute --args -
Inline code is always read-only and runs with your own permissions.
Step 6: let the agent propose a tool
When an agent keeps writing the same program, it can call code_propose_tool. The proposal appears for admins under AI Skills, in the Suggested tab.
Read the program as you would a small pull request: which tools it calls, what it returns, whether its loops end. Then click Create tool or Dismiss.
Why it is safe to run code a model wrote
Code written by a model is untrusted by construction. A prompt-injected tool result can steer what the model writes. So the program never runs in the AnythingMCP backend.
Each run gets its own subprocess, started fresh and stopped at the time limit. The backend answers every amcp call by running the real tool under the caller's role, exactly as it would for a direct call.
The repository ships a containment test that runs real programs through the runner. It checks that fetch, file reads, environment reads, subprocesses and imports all fail, and that a while(true) loop is stopped by the deadline.
Every run is recorded, and each tool call a program makes is recorded like any other tool call. Code tools are never served on the shared /mcp endpoint. They run only on a workspace's own MCP servers.
Limits and plans
A run stops after 10 seconds by default, 500 tool calls (16 in flight at once) or a returned value above 1 MB. A program over a limit fails with a clear error and affects nothing else.
On AnythingMCP Cloud the runner is shared between workspaces, so two quotas apply on top:
| Cloud plan | Runs per month | Runs at the same time |
|---|---|---|
| Trial without a card | 500 | 1 |
| Starter | 500 | 1 |
| Team, and a trial with a card | 5,000 | 3 |
| Enterprise | Unlimited | 10 |
Every Cloud trial has the Team code-mode features: inline code and up to 20 saved code tools. A self-hosted instance is not metered by runs.
Where code mode fits
Code mode pays off when the work is mechanical: the same lookup for 100 articles, a sum across two systems, two long lists matched against each other.
A single lookup, or a step that needs judgement such as "read this contract and decide what to check next", stays a normal tool call. And a program the agent writes on the spot never changes data: inline code is read-only by design.
Questions people ask
What is code mode in MCP?
A way for an AI agent to call MCP tools from inside a short program instead of one at a time. The program runs next to the tools and returns only the result. It does not replace MCP: it calls the same tools through the same gateway and permissions.
How does code mode reduce token usage?
Intermediate results stay in the sandbox. Only the program and its return value pass through the model. A single lookup saves nothing. The saving grows with the number of calls and the size of the data in between.
Which language do I write?
JavaScript, as the body of an async function. There are no imports and no packages. The program has amcp and the language itself.
Which AI clients can use it?
Any client that supports remote MCP servers, including Claude, ChatGPT, Copilot and Cursor. A saved code tool is a normal MCP tool, and code_execute is a normal tool that takes a program as its argument.
Can a program call an external API directly?
No. It has no network access. To reach another API, add it as a connector first, then call its tools with amcp.run.
Where to go next
- Documentation: Code mode and the code-runner containment model
- Command line tool: @anythingmcp/cli
- Source code: github.com/HelpCode-ai/anythingmcp
- Cloud trial and self-hosted setup: anythingmcp.com/code-mode
Start with the one question your team asks every week that takes the assistant more than five tool calls. Save it as a code tool.








Top comments (0)