Blog radlak.com

…what’s there in the world

Rethinking AI Agents: Why Direct Code Execution is Surpassing MCPs

A recent post from Anthropic suggests that the Model Context Protocol (MCP), the standard for connecting AI agents to tools, may be an inefficient abstraction. An alternative approach, direct code execution, has shown to produce significantly better results, increase agent autonomy, and reduce token consumption by as much as 98%.

The Core Problems with MCPs

MCPs are essentially APIs designed for AI agents, creating a universal protocol that allows developers to easily share and integrate tools. However, this approach has two major drawbacks that lead to excessive token consumption:

  • Tool Definition Overload: When an agent connects to multiple MCP servers, the definitions for every single available tool (descriptions, parameters, etc.) are loaded into the context window. This happens even if the agent only needs to use one tool, leading to higher costs, increased latency, and a greater risk of hallucinations.
  • Intermediate Tool Results: When a tool returns a large amount of data, such as a long document transcript, the entire output is passed back into the context window. The agent may only need a small piece of this information, but it is forced to process the entire, token-heavy result.

The Alternative: Direct Code Execution

The proposed solution is to allow the agent to generate and execute code to call tools on an as-needed basis. Instead of pre-loading all tool definitions, the agent can simply import and call a specific tool function (e.g., from a TypeScript file) when required. This resolves the core issues with MCPs by only loading what is necessary and allowing the agent to handle large data outputs locally (e.g., saving a file and only extracting relevant details) without congesting the context window.

Key Benefits and Conclusion

This code-centric approach offers several powerful advantages:

  • Progressive Disclosure: Agents are no longer limited by the context window and can access thousands of tools, using a search function to discover the one they need at the moment.
  • Enhanced Privacy: Code can be designed to automatically anonymize sensitive data *before* it is passed to a third-party model provider.
  • State Persistence & Skills: The agent can create and save new functions for itself, essentially learning new skills that it can reuse in the future, allowing it to evolve.

While this method is less reliable (code generation can have errors) and requires more infrastructure (a secure sandbox environment), it fundamentally increases an agent’s autonomy. The conclusion is that for complex tasks, direct code execution is a more powerful paradigm. MCPs will still be useful for simple, well-defined use cases (like sending a support ticket), but the future of advanced agents lies in giving them the freedom to write and execute code directly.

Mentoring question

Considering the trade-offs between the reliability of MCPs and the autonomy of direct code execution, which approach would you choose for your next AI agent project, and what factors would influence your decision?

Source: https://youtube.com/watch?v=D4ImbDGFgIM&si=3nJzu9rW3fpSRch7

Leave a Reply


Posted

in

by

Tags: