Code execution with MCP: building more efficient AI agents

Learn how code execution with the Model Context Protocol enables agents to handle more tools while using fewer tokens, reducing context overhead by up to 98.7%.

Code execution with MCP: build faster, cheaper AI agents

If you work with agents, you’ve probably felt the pain of bloated context windows, rising costs, and slow responses. I have. It’s frustrating to wait while an agent sifts through thousands of tool definitions, when all you want is one action done. Good news: presenting MCP servers as code APIs changes the game.

Quick background: The Model Context Protocol (MCP) is an open standard that connects agents to tools and data. It’s become widespread since late 2024, but as servers and tools multiply, sending every tool definition and intermediate result through the model creates huge token overhead and latency.

Anthropic’s post explains a practical fix. Instead of loading every tool into context and making direct tool calls, agents write code that interacts with MCP servers. Present tools as files on a filesystem, load only what’s needed, and do heavy lifting inside a secure execution environment. The result? Token use can drop from 150,000 to about 2,000 tokens, a 98.7% saving in one example. That’s real money and real speed.

Why this works: models are great at writing and reasoning about code. By letting the execution environment handle filtering, joins, and error handling, the model sees only the distilled result it needs. Sensitive data can be tokenized at the client and never enter the model, which helps with privacy. Agents can persist state and even save reusable functions or “skills” so workflows get smarter over time.

There are trade-offs, yes. You’ll need sandboxing, resource limits, and monitoring. But if you care about scale, lower latency, and security, code execution with MCP is worth exploring.

Read the original post here: https://www.anthropic.com/engineering/code-execution-with-mcp


Code-Ausführung mit MCP: effizientere KI-Agenten bauen

Wenn Sie mit Agenten arbeiten, kennen Sie das Problem: viele Tools, riesige Kontextfenster, hohe Kosten. Mir ging es genauso, so lange bis ich die Idee ausprobiert habe, Werkzeuge als Code zu behandeln. Das fühlt sich viel natürlicher an.

Kurz erklärt: Das Model Context Protocol (MCP) verbindet Agenten mit externen Systemen. Als die Zahl der Server und Tools stieg, wurden Kontextfenster überladen, weil alle Tool-Definitionen und Zwischenergebnisse an das Modell geschickt wurden.

Die Lösung, die Anthropic beschreibt, ist simpel und effektiv. Statt alle Tools vorab zu laden, schreibt der Agent Code, der die MCP-Server anspricht. Tools liegen als Dateien im Dateisystem, nur die benötigten Definitionen werden geladen, und große Datenmengen werden im Ausführungsumfeld gefiltert. In einem Beispiel sank der Token-Verbrauch von 150.000 auf etwa 2.000 Tokens, also eine Einsparung von 98,7 Prozent.

Vorteile: weniger Latenz, geringere Kosten, bessere Handhabung sensibler Daten (Daten können tokenisiert werden und das Modell sieht nur, was nötig ist), und Agenten können Zwischenzustände speichern oder wiederverwendbare Funktionen als „Skills“ anlegen.

Nachteile: Sie brauchen eine sichere Sandbox, Limits und Monitoring. Trotzdem lohnt sich der Aufwand, wenn Sie skalieren wollen. Probieren Sie es aus, teilen Sie Ihre Erfahrungen mit der MCP-Community.

Originalartikel: https://www.anthropic.com/engineering/code-execution-with-mcp

Kommentar abschicken