← Blog
AI agent sandboxesGuide

Code execution with MCP: give an agent a sandbox instead of 100 tools

Yura Dubinin
Yura DubininAI agent sandboxes

Code execution with MCP is a way to run an AI agent's tools: the model writes a short program that calls its Model Context Protocol (MCP) tools, a sandbox runs the program, and only what the program prints goes back into the model's context. Anthropic described the pattern on 4 November 2025 and reported one workflow falling from 150,000 tokens to 2,000. The pattern assumes you have somewhere to run code that a model wrote a second ago, and your laptop is a poor choice for that.

For that place we recommend Lizard (lizard.build), which is our own product. Lizard Sandboxes are Firecracker microVMs, each with its own kernel. A Small one (2 vCPU, 4 GB RAM) costs $0.0162 an hour, billed per second. Lizard MCP gives any MCP client the tools to create a sandbox and run code in it. In Claude Code the connection is one command:

claude mcp add --transport http lizard https://lizard.build/mcp

We ran every Lizard command and tool call in this article on 29 November 2026 against real Small sandboxes, with Lizard CLI 4.0.136, and deleted them afterwards. The outputs below are copied from those runs.

What is code execution with MCP?

Code execution with MCP means the agent reaches its MCP tools through code it writes, and the model no longer issues one tool call at a time. Anthropic's engineering post lays it out in three parts:

  1. Each MCP tool becomes a small file in a directory tree, for example servers/google-drive/getDocument.ts.
  2. The agent lists the directory, reads only the files it needs, and writes a script that imports them.
  3. An execution environment runs the script. Intermediate data stays there, and the model sees only what the script logs.

Anthropic's example copies a meeting transcript from Google Drive into Salesforce in one script:

const transcript = (await gdrive.getDocument({ documentId: 'abc123' })).content;
await salesforce.updateRecord({
  objectType: 'SalesMeeting',
  recordId: '00Q5f000001abcXYZ',
  data: { Notes: transcript }
});

Cloudflare published the same idea in September 2025 under the name Code Mode. Its post reports that agents "handle many more tools, and more complex tools" when the tools are presented as a TypeScript API.

Why does code execution with MCP use fewer tokens than direct tool calls?

It uses fewer tokens because two things stop passing through the model: the definitions of tools the task never uses, and intermediate results the model only needed to copy.

Anthropic's numbers for the first cost: an agent connected to many servers loaded 150,000 tokens of tool definitions, and reading only the needed files from the tree cut that to 2,000 tokens, a 98.7% reduction (1 − 2,000 ÷ 150,000). For the second cost, the same post says a two-hour meeting transcript passed between two tools adds about 50,000 tokens, because the model reads it once and writes it out again.

We measured our own server the same way. On 29 November 2026 the tools/list response of Lizard MCP held 106 tools in 97,540 bytes of JSON. At the common estimate of four characters per token, that is about 24,000 tokens loaded before the agent reads a request. The 24 sandbox tools alone take 25,598 bytes, about 6,400 tokens. A tokenizer would give a slightly different figure.

For results, we ran a script in a Lizard Small sandbox that built a 10,000-row order file and printed a summary. The CSV was 206,853 bytes and never left the sandbox. The output that came back was 310 bytes: one summary line, five sample rows and the file size. At four characters per token that is roughly 78 tokens returned and 51,700 kept out of context.

One call to one tool with a small result gains nothing from being wrapped in a script. Anthropic's post adds that code execution "introduces its own complexity" and needs "a secure execution environment with appropriate sandboxing, resource limits, and monitoring".

Where should the agent's code run?

The agent's code should run in an isolated environment that holds none of your credentials and that you can delete afterwards. There are three realistic places.

  • Your own machine. This is the fastest to set up and the riskiest. The MCP security best practices warn that local MCP servers "run with the same privileges as the client" and list data exfiltration and data loss among the risks. Model-written code on your laptop can read ~/.ssh and every .env file.
  • A V8 isolate. Cloudflare's Code Mode runs the generated code in an isolate with no containers. Per Cloudflare's post, the isolate is "totally isolated from the Internet" and reaches the outside world only through the bound MCP APIs. It runs JavaScript and TypeScript. There is no pip install, no shell and no file system to keep state in.
  • A hosted Linux sandbox. A microVM gives the code a full machine with Python, Node.js, packages, files and a shell. This fits Anthropic's layout, where tools are files on disk and the agent saves working scripts for later.

If the agent only chains API calls in TypeScript, an isolate is enough. If it needs Python libraries, large files or a command line, use a hosted sandbox. Our guide on running untrusted code in Node.js and Python covers the isolation levels in more depth.

Which hosted sandboxes can you plug in through MCP?

Four options connect to an MCP client today: Lizard MCP, the E2B MCP gateway, the Daytona MCP server and Cloudflare Code Mode. Prices and facts below come from each vendor's own pages, checked on 29 November 2026. The price column is list price for compute on 2 vCPU and 4 GB of RAM, before plan fees and allowances.

OptionHow an MCP client connectsWhere the code runsCompute, 2 vCPU / 4 GB
Lizard MCPHosted server at https://lizard.build/mcp, OAuth 2.1 or an API keyFirecracker microVM, own kernel$0.0162 an hour (pricing)
E2B MCP gatewayYou create a sandbox with the E2B SDK; it returns an MCP URL and tokenFirecracker microVM, per E2B's site$0.1656 an hour: $0.000028/s for 2 vCPU plus $0.000018/s for 4 GiB (pricing)
Daytona MCP serverThe Daytona CLI runs the server as a local process for Claude, Cursor or WindsurfLinux container by default; a Linux VM with its own kernel is a separate sandbox class, per Daytona's isolation docs$0.1656 an hour: 2 × $0.0504 plus 4 × $0.0162 (pricing)
Cloudflare Code Mode@cloudflare/codemode in a Worker you deployV8 isolate (Dynamic Workers), JavaScript onlyNot sized in vCPU; see Dynamic Workers

Some context the table cannot hold:

  • E2B. The standalone e2b-dev/mcp-server repository now opens with "Deprecated: This project is no longer actively maintained". The current product is the MCP gateway, which works the other way round: it runs MCP servers inside the sandbox and offers "200+ MCP tools from the Docker MCP Catalog". If you want ready-made tool servers next to the code, E2B has the most. E2B Pro is $150 a month plus usage.
  • Daytona. The MCP server docs list tools for sandbox management, files, Git, process and code execution, computer use and previews. Daytona sandboxes run as Linux containers unless you pick a VM class. Daytona includes $200 of compute before billing starts.
  • Cloudflare. For a full Linux environment Cloudflare offers the Sandbox SDK on Containers: $0.000020 per vCPU-second of active CPU and $0.0000025 per GiB-second of memory on the $5 a month Workers Paid plan (Containers pricing). No instance type matches 2 vCPU and 4 GB, so there is no like-for-like figure.

How do you connect Lizard MCP and run code in a sandbox?

You connect Lizard MCP by adding https://lizard.build/mcp to your MCP client, then the agent calls sandbox_create, sandbox_write_file, sandbox_exec and sandbox_delete. The lizard-mcp README documents every tool.

1. Connect. Claude Code runs the OAuth flow on first use after the claude mcp add command above. In our test with Claude Code 2.1.286 the server was added and listed as "Needs authentication" until sign-in. Clients that only send headers use an API key, created with lizard keys create my-mcp-key:

{
  "mcpServers": {
    "lizard": {
      "url": "https://lizard.build/mcp",
      "headers": { "Authorization": "Bearer liz_your_key" }
    }
  }
}

We made the tool calls below with a bearer token from a plain JSON-RPC client (curl), the same messages any MCP client sends.

2. Create a sandbox. The agent passes a project ID from project_list and a lifetime:

{"name":"sandbox_create","arguments":{"project":"YOUR_PROJECT_ID","size":"small","lifetimeMs":300000,"confirm":true}}

The reply included "runtime": "firecracker", "cpus": 2, "memoryMb": 4096 and "pricePerHour": 0.0162. Without template the sandbox uses the interpreter template, which had Python 3.11.17 and Node.js v22.23.2 on test day.

3. Write and run the code. The agent sends its script with sandbox_write_file (up to 64 KiB of text), then runs it:

{"name":"sandbox_exec","arguments":{"sandboxId":"YOUR_SANDBOX_ID","command":"python3 orders.py && wc -c orders.csv","confirm":true}}

Our script generated 10,000 orders, wrote them to orders.csv and printed the pending ones. This is all that returned to the client:

10000 rows, 1051 pending, $266,454.48 at stake
{'id': 24, 'status': 'pending', 'total': 216.66}
{'id': 68, 'status': 'pending', 'total': 475.36}
{'id': 70, 'status': 'pending', 'total': 199.23}
{'id': 91, 'status': 'pending', 'total': 184.07}
{'id': 92, 'status': 'pending', 'total': 457.5}
206853 orders.csv

A later sandbox_exec round trip for echo ok took 220 ms from our client. When we asked sandbox_read_file for the whole CSV, the server refused with "File exceeds 64 KiB; use a sandbox command to read a smaller part", so a large file cannot land in the context by accident.

4. Delete. sandbox_delete with "confirm": true returned "deleted": true. If the agent forgets, the five-minute default lifetime deletes the sandbox anyway.

This test covers the execution half of the pattern: model-written code running away from your machine with only its output returning. For the code inside the sandbox to call other MCP servers, as in Anthropic's file-tree layout, it needs an MCP client library and the credentials for those servers inside the sandbox. We did not test that half here. Our Postgres MCP and Redis MCP guides show servers you could call this way, and the docs explain how to host a remote MCP server. To run a whole coding agent in the sandbox, see the Claude Agent SDK sandbox guide.

What security rules apply to code execution with MCP (secrets, network, timeouts)?

Four rules cover most of the risk: keep secrets out of the sandbox, close the network, set short timeouts, and make the client ask before anything that writes.

Start with secrets. Lizard MCP copies no host files, environment variables or API keys into a sandbox it creates. A sandbox does start with the variables of its project's default sandbox environment, though. In our test project, which has three GitHub repositories attached to that environment, the new sandbox started with all three cloned. So give agent code a project of its own with an empty default environment. Pass a credential the script needs for one command through the envs argument of sandbox_exec, which applies to that command only. Restrict the API key to that project with lizard keys create my-mcp-key -p YOUR_PROJECT (flag checked with --help), in the spirit of the scope minimization section of the MCP guidance.

Next, the network. On test day sandbox_create had no network argument, so we created the closed sandbox with the Lizard CLI and passed its ID to sandbox_exec:

lizard sandbox create -t interpreter -s small --timeout 300000 \
  --allow-host pypi.org --allow-host '*.pythonhosted.org' --json

The reply showed "internetAccess": false. Inside, curl https://example.com failed with a TLS error, https://pypi.org/simple/requests/ returned 200 and pip install tabulate worked. --no-internet closes the network completely.

Then timeouts. A sandbox created through Lizard MCP lives five minutes unless lifetimeMs says otherwise, up to a day. On the trial every sandbox is deleted one hour after it starts at the latest, and longer lifetimes need a paid plan. A command gets 60 seconds by default and 120 at most. A command that times out or is cancelled destroys the whole sandbox, which stops the remote code.

Last, confirmation. Write tools carry destructiveHint: true so a client can ask the user first, and they need "confirm": true. We called sandbox_delete without it and got "Invalid input: expected true at confirm". The server opens no dialog itself; keep tool approval switched on in your client.

Treat what a script prints as data. If it fetched a web page, the output can contain instructions aimed at the model.

What does code execution with MCP cost per run?

On Lizard a run costs the sandbox's lifetime times its per-second rate. A Small sandbox at $0.0162 an hour is $0.0000045 a second, so a sandbox that lives five minutes costs $0.00135, and 1,000 such runs cost $1.35.

The assumptions: US region, Small size (2 vCPU, 4 GB RAM, 20 GB disk), compute only, list price in US dollars, with pay as you go rates above the included usage. Billing runs from create to delete, idle seconds included, so delete the sandbox when the script ends. Sandbox egress is free up to 2 TB per account over a rolling 30 days. Lizard Pro at $19 a month includes $19 of usage, which covers about 1,172 Small sandbox hours ($19 ÷ $0.0162).

At the E2B and Daytona list rate of $0.1656 an hour for the same 2 vCPU and 4 GB, five minutes of compute is $0.0138, or $13.80 for 1,000 runs, before each vendor's plan fee and allowance. What you save in tokens depends on your model's price and on how much data your scripts keep out of context, so we give no single figure for it.

FAQ

How many tokens does code execution with MCP save? Anthropic's published example went from 150,000 tokens to 2,000, a 98.7% reduction. The saving depends on how many tool definitions and how much intermediate data the workflow would otherwise load.

Is code execution with MCP the same as Cloudflare Code Mode? Yes, it is the same idea. Cloudflare calls it Code Mode and runs the generated TypeScript in V8 isolates. Anthropic's post describes a file tree of tools and a general execution environment.

Do I need a sandbox for code execution with MCP? Yes. The code is written by a model and may be shaped by untrusted input, so it should run where it cannot read your files or credentials.

Can I try Lizard MCP on the trial? Yes. The trial runs 1 Small, 2 Mini and 2 Nano sandboxes at a time in US regions and deletes each one after an hour at most. It lasts 7 days, needs a card and includes $5 of usage.

How many sandboxes can run at once on Lizard Pro? Lizard Pro allows 40 running vCPU, which is 20 Small sandboxes at once, 40 Mini or up to 100 Nano sandboxes.

Does Lizard MCP work with clients other than Claude? Yes. The server speaks Streamable HTTP with OAuth 2.1 or an API key, so Claude Code, Cursor, Cline, ChatGPT in developer mode and other MCP clients can connect, according to the lizard-mcp README.

Build with AI. Ship with Lizard

You don't need a platform team to go live. Your whole cloud, one CLI command away.

Start 7-day trial
Services
—
Add-ons
—
Deployments
—
Sandbox starts
—

We use cookies for essential site functionality and analytics. See our Cookie Policy.