← back

Code Execution with MCP: Anthropic's Guide to Building More Efficient Agents

Dec 6, 2025

mcpanthropicagentscode-executionefficiency

Since I started using MCP yesterday, I’m completely blown away by the possibilities. It’s one of those “how did I ever work without this?” moments. And now Anthropic has published a guide that takes MCP to the next level.


Source Article

Code execution with MCP: Building more efficient agents

By Adam Jones and Conor Kelly — Anthropic Engineering Blog

This article is essential reading for anyone building with MCP. The full source is available at the link above.


The Problem

As you connect more MCP servers, tool definitions and results consume excessive tokens. Anthropic has seen tool definitions alone consume 134K tokens before optimization. That’s expensive and slow.

The Solution: Code-First MCP

Instead of having the model call tools directly, the MCP client exposes each server as code modules on a filesystem. The model writes TypeScript code that imports and composes those modules, running in a sandboxed environment.

servers/
├── google-drive/
│   ├── getDocument.ts
│   └── listFiles.ts
├── salesforce/
│   ├── createContact.ts
│   └── updateRecord.ts
└── slack/
    └── sendMessage.ts

The agent explores this filesystem on demand, reading only the tool definitions it needs.

The Result

Token usage drops from 150,000 tokens to 2,000 tokens — a 98.7% reduction in cost and latency.

Key Benefits

Progressive tool discovery: The agent doesn’t need all tool definitions in context. It explores the generated filesystem and reads specific tool modules only when needed.

Privacy-preserving data flows: Intermediate results stay in the execution environment. Sensitive data can flow between tools without ever entering the model’s context.

Persistent skills: Save working code as reusable functions in a ./skills/ directory. Over time, your agent builds a growing toolbox of higher-level capabilities.

The Bigger Picture

This is the evolution of MCP from “here are some tools” to “here’s a programmable environment.” The model isn’t just calling APIs — it’s writing code that orchestrates complex workflows.

Cloudflare calls this pattern “Code Mode” and has published similar findings. The core insight is the same: LLMs are adept at writing code, so leverage that strength for tool interaction instead of fighting against token limits.


The original article at anthropic.com/engineering/code-execution-with-mcp includes detailed implementation examples and security considerations for sandboxed execution.