Pi 1.0 Connects MCP via Codemode: The Agent Writes JS Code Instead of Making Separate Calls
Pi 1.0, the "harness" program that gives an AI agent its tools, now connects MCP servers (external services such as Sentry) through Codemode. The agent writes a short JavaScript program that combines calls inside an isolated QuickJS sandbox with no network or file access. Ordinary bash only shows the model the last 2000 lines of output, while Codemode returns the full result. That is why MCP servers should return clean, structured JSON.
AI-processed from Хабр: Машинное обучение; edited by Hamidun News
Imagine you need to go through a hundred open tasks in a repository: read each one, assess the author's tone, and work out whether it is a bug or a feature request. If an AI agent calls tools one at a time and pulls every response into the model's memory, the context fills up within a few steps. This is exactly the problem Codemode in Pi 1.
0 is meant to solve. Pi is an agent "harness": a program that gives a language model tools and runs them. MCP is the standard through which an agent connects to external services such as Sentry.
Now MCP support in Pi runs through Codemode: instead of a chain of separate calls, the agent writes a short JavaScript program that combines the tools itself.
More than a year ago, the author advised against loading user tools, including MCP servers, into the model's context and, where possible, relying on scripts instead. The Pi 1.0 release looks like a reversal, but in fact it is the next step in the same idea: let the agent work through code.
Why an agent needs code when it has bash
Bash is convenient because calls are easy to combine, and models understand the file system well after training. But it has a limit: it can only chain executable programs. Some tools are not programs at all; they are built into the interface used to talk to the LLM. For example, for a multimodal model to see an image, the agent's harness has to pass the image data directly through the protocol. A simple console command cannot do that.
There is a second complication: where things run. The system has two parts. The agent harness, the "brain," runs on a separate machine and is treated as trusted. The target environment, where bash and the tools run, may sit in a sandbox such as Gondolin. These parts have different file systems and different trust levels, and a clear boundary runs between them.
Codemode runs on the harness side, in its own sandbox. For it, Pi uses QuickJS in a WASM environment with deliberate restrictions: no network, no file access, limited memory. The only way to affect the outside world is to call other tools. The term was coined at Cloudflare.
What this delivers in practice
The context savings are easy to see in a simple example. When an LLM calls bash as an ordinary tool, only the last 2000 lines of output reach the context, and the agent has to read the rest from a file. Through Codemode, the same call returns bulky results in structured form. In addition, JavaScript lets the agent describe parallel operations and simple workflows. A typical pattern: the agent first looks at 5–10 items of a response to understand the structure, then writes a script to process the rest.
Another capability is state within a session history. One Codemode call saves data, and the next call in the same session loads it. All of this happens on the harness host, not in the sandbox.
The examples from real Pi sessions are telling, with the agent writing the code itself. It generates images through model APIs. It fetches up to 100 open GitHub Issues via gh, runs each one through the Jev classifier model, and rates the tone, the author's level of irritation, and the type of request. Parallel execution is safe here: Pi itself caps the number of simultaneous tool calls at four, and the rest wait in a queue. In another example, Jev picks the next action for a tank in a game while debugging. As separate tools, all of this would only take up space in the context.
With MCP, the agent first searches for the tools it needs through the API inside Codemode. Sometimes it contacts the server directly: for example, the Sentry MCP, whose existence it knows from the system prompt.
What still isn't right with MCP
The author admits that MCP does not work perfectly yet. Servers are mostly designed for harnesses without Codemode. Cloudflare's interim solution, running Codemode inside the MCP server itself, produces Codemode inside Codemode. This leads to double-escaped JSON, which confuses small models, and the inner code cannot call the outer tools.
Four problems stand out:
- Structured data. Servers must return clean JSON, and many do not.
- Consistent results. If a server changes its format depending on the number of items, say to save tokens, a five-item test request passes while the maximum batch breaks processing.
- Large binary data. MCP does not support it yet, so developers have to work around it with pre-signed URLs.
- Parallel tool search. There is no convenient mechanism for distributing it across several servers. Right now it depends on models' spontaneous behavior, which does not scale well.
Other questions remain open. Recovery after failures is harder with Codemode: perhaps the team should borrow ideas from reliable workflow execution engines and save snapshots of call state. Instead of JavaScript, Starlark may be a better fit thanks to its determinism. Working with images and binary data is also unresolved, as is the inability of small models to use this approach effectively.
What a developer should do
If you are writing an MCP server, return structured JSON and keep the response format identical regardless of the size of the output. If you are building your own agent harness, consider an isolated environment for model-generated code that has no network or file access but can call tools. In Pi, Codemode is enabled by default together with MCP, and it can also be activated separately via "defaultTools": ["+codemode"]. The author is confident the approach will be adopted more widely, although it is still far from ideal.
- Nov 21, 2026VibeCBS collects project nicknames in advance: the agent finds 2.8 times more documents
- Nov 21, 2026FDE job postings grew 4.6x in a year: what a forward deployed engineer actually does at the client's site
- Nov 21, 2026Sakana AI Opens Applied Research Lab for Market and Economic Modeling
- Nov 21, 2026Meta Is Reportedly Preparing the $300 Muse Charm AI Tamagotchi With a Year of Verizon 5G
Key Questions Answered
Why an agent needs code when it has bash?
Bash is convenient because calls are easy to combine, and models understand the file system well after training. But it has a limit: it can only chain executable programs. Some tools are not programs at all; they are built into the interface used to talk to the LLM. For example, for a multimodal model to see an image, the agent's harness has to pass the image data directly through the protocol. A simple console command cannot do that. There is a second complication: where things run. The system has two parts.
What this delivers in practice?
The context savings are easy to see in a simple example. When an LLM calls bash as an ordinary tool, only the last 2000 lines of output reach the context, and the agent has to read the rest from a file. Through Codemode, the same call returns bulky results in structured form. In addition, JavaScript lets the agent describe parallel operations and simple workflows. A typical pattern: the agent first looks at 5–10 items of a response to understand the structure, then writes a script to process the rest.
What still isn't right with MCP?
The author admits that MCP does not work perfectly yet. Servers are mostly designed for harnesses without Codemode. Cloudflare's interim solution, running Codemode inside the MCP server itself, produces Codemode inside Codemode. This leads to double-escaped JSON, which confuses small models, and the inner code cannot call the outer tools. Four problems stand out: Structured data. Servers must return clean JSON, and many do not. Consistent results.
Build & Ship Production AI Agents Without Boilerplate Code
Start your free 72-hour trial at Hamidun Academy (academy.hamidun.com) — interactive lessons on Claude Code & Cursor, live Tech Radar, and production agent templates.
The AI world, distilled — once a week
Seven stories that actually mattered, hand-picked. No noise, no reposts, no press releases.
Done! Check your inbox for a confirmation.