We’re interested in code execution with MCP because so much agent work is ordinary data processing. Fetch records, filter them, group them, return the answer. Making a model read and relay every intermediate result can be an expensive way to do that work.
With code execution, the agent writes a program that calls connected tools and sends a smaller result back to the model. The efficiency case makes sense. Before giving that program access to real systems, though, we want to know exactly what it can read, change, and send elsewhere.
What changes when an agent calls tools through code?
The Model Context Protocol, or MCP, provides a common way for applications to expose tools and resources to AI clients. In a conventional tool loop, the model requests a tool, receives its output, and decides what to do next. A workflow that retrieves, filters, and joins several datasets can take many such turns.
With code execution, the model can express those steps as a program. The execution environment handles intermediate data and returns the selected result. Anthropic describes this pattern in its engineering article on code execution with MCP. Cloudflare describes a related approach as Code Mode.
These are implementation patterns around tool use. Connecting an MCP server does not, by itself, create an isolated execution environment or give every client the same permissions.
Take a hypothetical inventory task: find services with an outdated runtime, group them by owner, and return a count. A program could do the grouping and return a short table without passing every record through the model. We haven’t measured this example in our project; it’s a way to see where the approach could help.
Where the efficiency comes from
The potential savings come from loading relevant tool definitions when needed and keeping intermediate results out of the model’s context. A model may need the shape of an inventory record and the final counts without needing the complete dataset. Anthropic’s explanation describes both mechanisms.
Savings will vary. For a one-step lookup, writing and running a program may add work. For a large transformation, it may remove several model round trips. Code generation errors, retries, tool latency, and the cost of operating the runtime all belong in the comparison.
We’d compare total cost per successfully completed task. A smaller token bill isn’t much help if the workflow gets there by dropping records.
Less model context does not mean less sensitive data
Filtering data before returning it to the model can reduce what the model receives. The execution environment still handles the full response. So may the MCP server, its upstream service, and your logging system.
For the inventory example, draw the path from the source database to the final answer. At each step, record who can read the data, what is retained, and which credentials are present. Check error logs as well as successful responses: a stack trace can reveal values that the normal output omits.
That’s the data review we’d want before using this with confidential records. A shorter prompt tells us little about what the rest of the workflow retains.
Keep permission checks at the action boundary
A program that combines ten calls still needs ten appropriate authorization decisions. Approving the script’s broad purpose should not silently approve every destination or write it constructs later.
For a first implementation, we’d start with:
- A runtime with explicit filesystem and network limits, tested with denied operations.
- Tool credentials restricted to the relevant account, resource, and operation.
- A policy check immediately before each external action, outside the model’s control.
- Hard limits on execution time, response size, retries, and spend.
- Action records that identify the tool, target, authorization decision, and outcome without recording secrets.
The MCP security guidance documents risks including confused-deputy behavior and token passthrough. The protocol is a useful integration standard; your implementation still has to enforce the access boundary.
Treat tool results as data with a source
Suppose an inventory record contains a note telling the agent to upload its configuration elsewhere. That note is content from a connected system. It does not grant permission to contact a new destination.
Generated code does not eliminate this problem. It changes where untrusted content is processed. Keep data out of executable code construction, validate structured fields, and make destination checks independent of instructions found in tool output. OWASP identifies instructions arriving through external content as indirect prompt injection.
A useful first evaluation
Run the same read-only inventory task through a conventional tool loop and a code execution path. Use synthetic records with known answers, including missing owners, duplicate services, malformed rows, and a hostile note.
Compare correctness, total cost, elapsed time, data sent to the model, and whether denied actions actually stay denied. Reset the environment between runs and repeat the tasks so one lucky attempt does not decide the result.
Move to write access only when the workflow needs it and you have evidence for that narrower capability. The hidden instructions behind an AI response are another reason to evaluate the complete application, including the instructions and tools surrounding the model.
For a workflow dominated by data processing, we’d try this with read-only access first. Keep the simpler tool loop as a comparison, measure the difference, and expand permissions only when the next task needs them and its controls hold up in testing.
