Skip to content

Your model writes one script instead of making twenty tool calls

codemode-mcp sits between your AI app and your MCP servers. The model sees one small tool, run_code, writes a short Python script that calls your tools, and gets back only the answer. The script runs in a locked-down sandbox on your machine.

Start in five minutes See what it saves, and what it doesn't

What the model sends: one call
# run_code
spend = {}
for customer in await shop.list_customers(country="IN"):
    orders = await shop.get_orders(customer["id"])
    spend[customer["name"]] = sum(o["total"] for o in orders)

sorted(spend.items(), key=lambda kv: -kv[1])[:3]
What comes back into the model's context
[["Customer 8", 12086], ["Customer 32", 9990], ["Customer 26", 7694]]
What the model sends: one call at a time
→ list_customers(country="IN")
← 13 customers, full JSON               (into the context)
→ get_orders(customer_id=3)
← every order, every line item          (into the context)
→ get_orders(customer_id=8)
← every order, every line item          (into the context)
  … 11 more calls …
→ the answer, after carrying all of that data

Install

pip install codemode-mcp
codemode-mcp setup
uv tool install codemode-mcp
codemode-mcp setup
pip install codemode-mcp
codemode-mcp setup
codemode-mcp init --from claude --install   # or: cursor | vscode | windsurf

setup downloads the sandbox runtime once (about 10 MB). You need Python 3.11 or newer; nothing else has to be installed separately.

Where to start

What it saves, and what it doesn't

Code Mode is not a discount on everything. It has a fixed cost of about 450 tokens for the run_code instructions, so the saving depends on the work:

Your situation What to expect
Many tools connected (dozens or more) The tool definitions sent with every request shrink to roughly 1–2k tokens, however many tools you have
Tasks that loop, filter or join across many calls Intermediate results stay in the sandbox, so the model reads only the final answer, provided the model writes a script that calls the tools
A provider with a large discount for cached input A smaller saving in money than in tokens, and possibly none. Classic's fixed block of definitions is what gets discounted
One or two small tools, one call per task No saving. The run_code instructions cost more than the schemas they replace
A client that already loads tool schemas on demand The definitions saving is smaller; the saving on intermediate results still applies

Measured: 20 runs of six tasks on two Gemini models

Code Mode gave the right answer in 238 of 240 runs, classic tool calling in 219 of 240. It used 62% fewer tokens on gemini-3.5-flash-lite and 91% fewer on gemini-3.8-flash. In dollars it cost 78% less on the larger model and 11% more on the smaller one, where prompt-cache discounts favour classic. Through the MCP proxy the result is the same (357 of 360 runs right), and it holds on four further tasks the fixes were not tuned on (238 of 240 right, against 108 of 120 for classic). These numbers are for version 0.3.0: 0.2.0 failed with small models, so upgrade if you have it installed.

The benchmarks page has the full tables, what was wrong in 0.2.0, the task where Code Mode still loses, and the commands to run the suite on your own servers.

How it stays safe

The model's script runs in Python compiled to WebAssembly, inside a process that has no network, no environment variables, no ability to start programs and no write access. The only thing a script can do is call the tools you configured, and you can require a human "yes" before any of them runs. Credentials stay outside the sandbox. Read the threat model for what this does and does not protect against.

Credits

Inspired by Cloudflare's Code Mode and Anthropic's work on code execution with MCP. codemode-mcp is an independent, MIT-licensed implementation for Python.