What is Code Mode?¶
This page explains the idea in plain language. There is nothing to install yet.
First, what is MCP?¶
The Model Context Protocol (MCP) is a standard way to give an AI model tools: things it can do besides write text, such as read a file, query a database or create a GitHub issue. A program that offers tools is called an MCP server. An app that uses them, such as Claude Desktop, Cursor or VS Code, is an MCP client.
The problem with many tools¶
When a client connects to a server, two things cost you tokens, and tokens are what you pay for and what fills up the model's limited context window:
- Tool definitions. The name, description and argument schema of every tool is sent to the model with every request, whether or not the tool is used. A handful of servers can easily mean dozens of tools.
- Intermediate results. Each tool call is a round trip: the model asks, the result comes back into the conversation, the model reads it and asks for the next thing. If a task needs twenty calls, the model reads twenty raw results, even when it only needed one number from each.
What Code Mode changes¶
Instead of handing the model every tool, codemode-mcp hands it one tool called
run_code, plus a way to look up tool signatures when there are many. The model writes a
short Python script; the script calls your tools as ordinary functions; and only what the
script returns goes back to the model.
# The model writes this once. The 13 get_orders results never enter its context.
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]
Your MCP servers do not change. codemode-mcp connects to them for you and sits in the middle:
your AI app ──► codemode-mcp ──► your MCP servers
(sees run_code) (runs the script (github, database,
in a sandbox) files, …)
Is it safe to run code a model wrote?¶
That is the right question to ask. The script does not run on your computer the way a normal program does. It runs inside a sandbox with no network, no access to your files or environment variables, and no way to start other programs. The one thing it can do is call the tools you configured, and you can require your approval before specific tools run. The security page describes exactly what is and is not protected.
When it helps, and when it doesn't¶
Good fit
- You have many tools connected and most requests use only a few of them.
- Tasks involve loops, filtering or combining data from several calls.
- Tool results are large and the model only needs a small part of them.
Poor fit
- You have one or two small tools and each task is a single call. The
run_codeinstructions cost about 450 tokens, which is more than the schemas they replace. Your first run shows this with real numbers. - A tool already does the heavy lifting for you, such as one SQL query that returns five rows. There is little left to keep out of the context.
- You are on version 0.2.0 with a small, cheap model. In our
measured runs that combination failed. Version 0.3.0 fixes it:
pip install -U codemode-mcp.
For the tools you call often and simply, you can keep them as normal tools next to
run_code. That is called hybrid mode.
Words you will see in these docs¶
| Term | Meaning |
|---|---|
| MCP server | A program that offers tools, for example a GitHub or database server |
| MCP client | The AI app that uses the tools: Claude Desktop, Cursor, VS Code and others |
| Upstream server | One of your MCP servers, as seen from codemode-mcp |
mcp.json |
The file that lists your MCP servers, in the same format your client already uses |
run_code |
The single tool the model sees. It takes a Python script |
search_tools |
A second tool that appears when you have many tools, to look up their signatures |
| Sandbox | The isolated environment where the model's script runs |
| Token | The unit models read, write and are billed in. Roughly three quarters of a word |
Next: Your first run.