Skip to main content
codemode
Code Mode enables AI agents to interact with MCP tools through code execution instead of direct tool calls. Based on research from Anthropic and Cloudflare, this approach reduces context consumption by up to 98.7% for complex workflows.

Quick Start

Why Code Mode?

Traditional MCP clients load all tool definitions upfront and pass intermediate results through the model context. This creates two problems:
  1. Tool definitions overload context - 150+ tools = 150,000+ tokens before processing any request
  2. Intermediate results consume tokens - Each tool result flows through the model, even when just passing data between tools
Code Mode solves both by having agents write code that executes in a separate environment.

Key Advantages

1. Progressive Disclosure

Agents load only the tools they need, when they need them:
Benefit: 2,000 tokens instead of 150,000 tokens (98.7% reduction)

2. Context-Efficient Tool Results

Large datasets are processed in the execution environment before returning to the agent:
Benefit: Agent sees 5 issues instead of 10,000

3. More Powerful Control Flow

Loops, conditionals, and error handling use familiar code patterns instead of chaining individual tool calls:
Benefit: More efficient than alternating between tool calls and sleep commands through the agent loop

4. Privacy-Preserving Operations

Intermediate results stay in the execution environment by default:
Benefit: Sensitive data flows through the workflow without entering the model’s context

Real-World Example: File System Exploration

This example demonstrates how Code Mode dramatically simplifies complex workflows. An agent tasked with “list files and rename them” performs the entire operation efficiently.
Code Mode in Action

Discovery: Finding the Right Tools

The agent starts by searching for filesystem tools:
Result (16 filesystem tools found)
Code Mode Advantage: The agent discovered 16 tools using ~200 tokens, compared to ~8,000+ tokens if all filesystem tool definitions were pre-loaded into the system prompt.

Execution: Processing Files Efficiently

The agent writes a single Python script to complete the task:

Efficiency Comparison

By writing code instead of chaining tool calls, the agent processed the file list locally and only returned the summary. This avoided passing the raw directory listing (and each rename confirmation) through the LLM context.

API Reference

MCPClient

__init__(code_mode=True)

execute_code(code: str, timeout: float = 30.0)

Execute Python code with MCP tool access. Returns:

search_tools(query: str = "", detail_level: str = "full")

Search available tools across all servers. Returns: Dictionary with:
  • meta: Dictionary containing total_tools, namespaces, and result_count
  • results: List of tool information dictionaries matching the query
Detail levels: "names", "descriptions", "full"

What’s Available in Code

Functions

  • search_tools(query, detail_level) - Discover tools
  • server.tool_name(**kwargs) - Call any MCP tool
  • __tool_namespaces - List of server names
Note: Tool names are automatically sanitized to be valid Python identifiers. For example, a tool named list-files becomes list_files.

Builtins

Restricted: import, open, eval, file I/O

Performance

From Anthropic’s research:

Examples

Tool Chaining

Data Processing

Error Handling

Agent Integration

Agents only see 2 tools when code_mode=True:
  • execute_code - Execute Python code with tool access
  • search_tools - Discover available tools
All other MCP tools are accessible within code execution.

References

See Also