Code Execution with MCP: Building More Efficient AI Agents

Anthropic has proposed a shift in how AI agents interact with the Model Context Protocol (MCP), moving from direct tool calling to code execution. By presenting MCP servers as code APIs, agents can load only the necessary tool definitions and process data within an execution environment, significantly reducing token overhead and latency.

Reducing Token Consumption in MCP Agents

As AI agents scale to use hundreds or thousands of tools across multiple MCP servers, two primary inefficiencies emerge regarding token usage:

1. Tool Definition Overload

Standard MCP clients typically load all tool definitions into the model's context window upfront. When an agent is connected to thousands of tools, the model must process hundreds of thousands of tokens before it can even begin addressing a user request, which increases both cost and response time.

2. Intermediate Result Bloat

In traditional tool-calling loops, every intermediate result must pass through the model's context. For example, if an agent downloads a large meeting transcript from Google Drive to upload it to Salesforce, the full text of that transcript is loaded into the context window during the first call and then written back into the context for the second call. For large documents, this can consume tens of thousands of tokens and may exceed context window limits, leading to workflow failures or data copying errors.

Implementing Code Execution with MCP

Instead of direct tool calls, agents can interact with MCP servers by writing code. One implementation involves generating a filesystem tree where each MCP server and its associated tools are represented as files (e.g., ./servers/google-drive/getDocument.ts).

In this architecture, the agent discovers tools by exploring the filesystem—listing directories to find servers and reading specific files to understand tool interfaces. This "progressive disclosure" approach allows the agent to load only the definitions it needs for a specific task. Anthropic notes that this can reduce token usage from 150,000 tokens to 2,000 tokens, representing a 98.7% saving in time and cost.

Key Benefits of the Code Execution Approach

Context-Efficient Data Handling

Code execution allows agents to filter, transform, and aggregate data within the execution environment before returning the result to the model. For instance, instead of loading a 10,000-row spreadsheet into the context window to filter it manually, an agent can write a script to filter for "pending" orders and only log the first five rows for review, drastically reducing the number of tokens processed.

Advanced Control Flow

By using standard programming constructs like loops, conditionals, and error handling, agents can execute complex logic in a single step. This is more efficient than chaining individual tool calls through the agent loop. Furthermore, executing conditional trees in the environment reduces "time to first token" latency because the model does not need to evaluate every if-statement sequentially.

Privacy and Security

Intermediate results remain in the execution environment by default, meaning sensitive data that does not need to be seen by the model never enters the context window. Additionally, the MCP client can be configured to automatically tokenize PII (Personally Identifiable Information) before it reaches the model, untokenizing it only when the data is passed to another MCP tool call. This ensures that sensitive data flows from source to destination without the model ever processing the raw PII.

State Persistence and Reusable Skills

Filesystem access enables agents to maintain state across operations by writing intermediate results to files. Agents can also persist their own successful implementations as reusable functions in a "skills" folder. When paired with a SKILL.md file, these functions become structured skills that the model can reference for specialized tasks, allowing the agent to evolve its own toolbox of higher-level capabilities over time.

Operational Considerations

While code execution offers significant efficiency gains, it introduces infrastructure complexity. Running agent-generated code requires a secure execution environment featuring sandboxing, resource limits, and monitoring to mitigate security risks. These operational overheads must be weighed against the benefits of reduced token costs and lower latency.

Sources

Related

  • Project
  • Project
  • Project
  • Project
  • Dispatch