A question I keep coming back to when I design agent platforms is how the agent reaches the systems the business already runs: the ticket, the customer record, the reply in the support tool. I see three ways to wire that today. I can write functions that wrap the company's REST APIs and hand them to the model as tools. I can put the same capabilities behind an MCP server. Or I can let the model write a short program against a typed API generated from those MCP tools and run it in a sandbox, which is what Cloudflare calls Code Mode.

The integration underneath can be identical in all three. The difference I've learned to look for is where the plan runs, because that settles most of the rest: how much context gets spent, who holds the credentials, and what a reviewer is actually approving.

Hand-written tools over your own APIs

Hand-written tools are the simplest of the three, and for a small surface they're still what I trust most. The host application defines a function per capability, describes it to the model as a tool, and runs it when the model asks. The model picks a tool, the host calls the API with its own credentials, the result goes back into the conversation, and the model picks the next step.

The contract is as tight as I make it. The schema exposes exactly what I chose, the credentials never leave the host, and every call is an event I can log. It starts to hurt when a second AI application needs the same capability and writes its own wrapper with its own small differences. I wrote up where that threshold sits in MCP versus direct APIs.

MCP tool calls

MCP takes the wrapper out of the application and puts it behind a protocol. The client discovers tools at runtime with tools/list, each tool carries a name, a description, an inputSchema and optionally an outputSchema, and every call goes through one method whatever sits behind it. I like that the specification (opens in a new tab) doesn't pretend this is safe. It says tools represent arbitrary code execution and that annotations are untrusted, and it asks clients to show inputs before a call and log tool use for audit.

The loop itself doesn't change. The model still plans one step per turn and every result still comes back through its context, which gets expensive as servers pile up. Most clients load tool definitions into the context before the conversation starts, so one connected to many servers spends a big part of its window describing tools it won't call. Intermediate results also pass through the model even when it only needs to hand them on. Anthropic's code execution with MCP (opens in a new tab) post uses a meeting transcript as the example: it goes into the context once when it's read and again when it's written to another system.

Code Mode

Cloudflare announced Code Mode (opens in a new tab) in September 2025. The agent framework reads an MCP server's schema and generates a TypeScript API from it: an interface for each tool's inputs and outputs, one async function per tool, and doc comments from the tool descriptions. The model gets one tool that runs code. It writes a program that calls those functions, does its loops, filters and joins in plain code, and prints only what it needs to see.

Each snippet runs in a fresh V8 isolate created through the Worker Loader API and thrown away afterwards. Inside it, fetch() and connect() throw, so the only way out is through bindings for the MCP servers it's connected to. A binding call goes back to the agent's supervisor, which holds the tokens and adds them, so the generated code never sees a key. Cloudflare's argument for why models do better this way is about training data, since models have read far more real code than tool-call transcripts. I find that plausible, though the original post doesn't publish benchmarks for it.

Cloudflare has pushed the idea further since. In February 2026 it exposed its whole API (opens in a new tab) through a Code Mode server with just two tools, one that searches the OpenAPI spec with code and one that runs code against the API. By its count the tool definitions came to about 1,000 tokens, where a server with a tool per endpoint would need about 1.17 million.

In March the isolates underneath reached open beta as Dynamic Workers (opens in a new tab). A globalOutbound setting either blocks outbound traffic or sends every request through a callback that can inspect it, rewrite it or add credentials. Anthropic offers the same pattern in the Claude API as programmatic tool calling (opens in a new tab): the model's code runs in Anthropic's container, each tool call pauses it, and your client runs the tool, so the credentials stay with you.

Side by side

Hand-written toolsMCP tool callsCode Mode
Where the plan runsThe model, one step per turnThe model, one step per turnA program the model writes, in a sandbox
Context costYour own tool definitions, plus every intermediate resultEvery connected tool's definition, plus every intermediate resultA generated API or a search tool, plus only what the code prints
Who holds credentialsThe hostThe MCP client or serverThe supervisor or outbound handler, outside the sandbox
Unit you approveOne callOne callA program, or the calls it makes if you pause on them
Typical failureEach app's wrapper driftsThe window fills with tool definitions and payloadsGenerated code is wrong, or injected text becomes code
Runtime you operateNone beyond the appMCP serversMCP servers, and a sandbox you run or rent

What changes when the plan becomes code

Approval is the first thing I'd worry about. With tool calls, a person or a policy approves one named action with its arguments in view. With Code Mode the natural unit is a fifteen-line program, and I don't trust anyone, myself included, to review programs as reliably as single calls. Cloudflare's @cloudflare/codemode (opens in a new tab) SDK lets a method require approval, so the run pauses at that call, and it keeps an audit log per run. That only works if the risky operations are marked, so sorting your tools by side effect turns into design work you can't put off.

A tool-call transcript is already an audit trail. A program's trail has to be built, so I'd log every binding call with its arguments and result at the supervisor, and keep the code with the run. Without both, an investigation can see what the agent decided and still not know what it touched.

Prompt injection also gets worse. Text in a retrieved document or a tool result can already steer a tool-calling agent, but with an execute tool it steers a program, which can loop and combine calls in ways one call can't. Anthropic's documentation for programmatic tool calling warns about code injection from tool results, and says allowed_callers, the setting that lets code call a tool, is not a security boundary. The controls I'd rely on sit outside the generated code: no network egress, credentials added at the boundary, and scopes checked per call by the binding or the gateway.

The tool definitions themselves matter more too. In Code Mode a tool's description and schema become the types and comments the model codes against, so if a server quietly changes a description after you approved it, the generated programs change with it. I'd record what was approved and block any tool whose definition has moved until someone looks at it again.

Then there's the sandbox. Cloudflare is candid about it in the Dynamic Workers post, saying V8 security bugs are more common than hypervisor bugs and that every sandbox has them, so it relies on layered defence. If you run Code Mode yourself, the sandbox becomes infrastructure you own, with a patch cadence and a threat model of its own.

Why I built codemcp

I wanted to find out whether the checks I'd want on every call could really live behind the sandbox, so I wrote codemcp (opens in a new tab). It's a single MCP server that sits in front of the ones a client already uses, and the model sees just three tools, search, describe and execute. The model looks up typed stubs generated from the upstream schemas and writes a short TypeScript program against them.

That program runs in QuickJS compiled to WebAssembly on a worker thread, with memory, stack and deadline limits set from outside. Only JSON crosses between the sandbox and the host. The upstream credentials stay in the proxy's configuration, and inside the sandbox there's no process, environment, file system or network that could reach them.

codemcp-core (opens in a new tab) runs every call the program makes through the same sequence. It checks the pinned definition, then policy and approval, validates the arguments against the schema, counts the call against the budget, and writes an audit record before anything leaves. Only then does it forward the call upstream, cap the size of the result and record the outcome.

Policy is decided on the qualified server.tool name and the person the run acts for, never on a name the model typed. MCP annotations come from the server, so I only let them make a decision stricter. A destructive hint turns a default of allow into approve, and a read-only hint changes nothing. When a call needs approval, the sandbox simply waits inside that await until someone decides, and a refusal comes back to the script with its reason, as an error it can catch and work around.

Tool definitions get the same suspicion as the code. Their text is cleaned of control and invisible characters before the model reads it, and each one is pinned by a hash over the name, description, schemas and annotations. If a server changes a tool later, that tool is blocked, and re-approving it shows a diff of exactly what changed. For servers I don't control I turn off automatic pinning of new tools, so nothing gets trusted just because it showed up first.

I also wanted numbers rather than a feeling, so the repo has a benchmark (opens in a new tab) anyone can rerun. Against seven public MCP servers with 71 tools between them, attaching every tool directly costs about 11,700 tokens of definitions on each request, while codemcp's three tools cost about 490. Across 13 tasks on four Claude models, total tokens came out 85% lower and the first request 94% lower, with the same pass rate either way. The trade is speed, since the median task took about a second longer through the sandbox.

It's still early, and I keep a status page (opens in a new tab) of what it can't do yet. It's single-tenant and runs over stdio. It holds upstream credentials per server rather than per user, and it can't yet reach hosted servers that need an interactive OAuth login. On isolation, the status page puts it below a V8 isolate in its own process and above running code in-process.

So I'd use it today for one developer's tools, and not yet as a shared gateway. Every Code Mode deployment ends up needing what's on its list: identity on every call, credentials per user and someone who owns the sandbox.

How I choose

I start from how many tools the agent needs and how much data moves between them. With a handful of tools and small results I stay with plain tool calls. They're simpler to run and easier to approve, and at that size the context cost is small. Anthropic's own guidance agrees, saying programmatic tool calling doesn't pay for single calls, small responses, or tasks where the model has to reason over every intermediate result.

I reach for Code Mode when the agent connects to many tools or a large API, when it chains calls whose intermediate data the model doesn't need to read, or when the task is really a loop: fetch a hundred records, filter them, write back the ten that matter. That's where turn-by-turn tool calling spends most of its context shuffling data around.

Whether a capability should be an MCP tool in the first place is a separate question, and my answer there hasn't changed. If more than one AI application will need it, it goes behind a server. Code Mode only changes how an agent talks to that server once it exists.

The gateway still checks every call

Code Mode moves the loop out of the model's context and into a sandbox. A gateway in front of the MCP servers, like the one in my gateway field notes, still has to check the user's identity, the tool's scope and the call's arguments, because the code making the call is no more trustworthy than the model that wrote it.

Sources

Key takeaways

  • I compare the three on where the plan runs. With tool calls the model plans one step per turn; with Code Mode it writes the whole plan as a program that runs in a sandbox.
  • Code Mode keeps MCP's discovery, schemas and server boundary, and stops intermediate results flowing back through the model.
  • Approval moves from one named call to a program, so I keep the per-call checks behind the sandbox, at the binding or the gateway.
  • Tool definitions become code the model reads. I pin what was approved and block a tool whose definition changes afterwards.

About this comparison

Format
Comparison
Level
Architect
Read
8 min read
Published
Oct 29, 2026

Capabilities and trade-offs, without a winner score.

Comparison · · 1 min read

LangGraph vs writing the agent loop yourself

Two ways to write the same supervisor, an explicit state graph or a loop your own code owns on the model API. Compared on control flow, tracing, testing, checkpointing and what each makes hard.