π€ π¨βπ» Agent Codemode
Generate programmatic tools from MCP Servers and Skills.
What is Agent Codemode?β
Agent Codemode generates programmatic tools from two sources:
- MCP Servers - Connect to any MCP server and generate typed Python bindings for its tools
- Skills - Reusable code patterns that compose multiple tools into higher-level operations
These programmatic tools can be:
- Used directly by an agent - Import generated bindings and call tools from your agent's code
- Exposed as an MCP Server - Serve the generated tools via MCP protocol for any MCP-compatible client
Agent Codemode uses Code Sandboxes for safe, isolated code execution.


Package Scopeβ
Agent Codemode is the tool generation layer in the Datalayer AI stack:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β agent-runtimes β
β (Agent hosting, protocols, UI) β
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ€
β agent-skills β agent-codemode β βββ You are here
β (skills management) β (tool generation from MCP/Skills)β
ββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββ€
β code-sandboxes β
β (Safe code execution environment) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
What Agent Codemode Does:
- β Generate programmatic tools from MCP Servers (typed Python bindings)
- β Generate programmatic tools from Skills (reusable compositions)
- β Provide progressive tool discovery (search_tools, get_tool_details)
- β Execute Python code that composes tools (CodeModeExecutor)
- β Expose generated tools directly to agents or via MCP Server
Not Responsible For:
- β Raw code execution isolation (β code-sandboxes)
- β Skill lifecycle management (β agent-skills)
- β Agent protocols or UI components (β agent-runtimes)
Why Code Mode?β
Traditional AI agents call tools one at a time, with each call requiring an LLM inference round-trip. This approach has limitations:
- Inefficient: Multiple LLM calls for multi-step operations
- Error-prone: No robust error handling between tool calls
- Limited: Cannot leverage programming constructs like loops or conditionals
- Not reusable: Patterns must be rediscovered each time
Agent Codemode solves these by letting agents write code that composes tools:
# Instead of many individual tool calls...
# The agent writes code that orchestrates tools programmatically
from generated.servers.filesystem import read_file, write_file
import asyncio
# Read multiple files in parallel
files = ["config.json", "data.csv", "readme.md"]
contents = await asyncio.gather(*[
read_file({"path": f"/data/{f}"}) for f in files
])
# Process with try/except for robustness
for i, content in enumerate(contents):
try:
processed = content.upper()
await write_file({"path": f"/output/{files[i]}", "content": processed})
except Exception as e:
print(f"Failed to process {files[i]}: {e}")
Same task, same MCP server β Code Mode uses significantly fewer tokens by composing tools in code instead of multiple LLM round-trips.


Prompt: "Generate 2000 words of random text and write to a file"
Before vs After (Codemode pattern)β
Codemode follows the same βwrap tools, then let the model write codeβ pattern popularized by Cloudflareβs Code Mode. The flow looks like this:
Before (traditional tool calls): the model calls each tool directly.
After (Codemode): you give the model meta-tools that let it discover tools and execute Python code that composes them.
Example (Agent Codemode, Python):
from agent_codemode import ToolRegistry, CodeModeExecutor, MCPServerConfig
# Configure the registry with MCP servers
registry = ToolRegistry()
registry.add_server(MCPServerConfig(
name="filesystem",
transport="stdio",
command="npx",
args=["-y", "@anthropic/mcp-server-filesystem", "/tmp"],
))
await registry.discover_all()
# Execute code that composes tools
executor = CodeModeExecutor(registry)
await executor.setup()
result = await executor.execute("""
from generated.servers.filesystem import read_file, write_file
content = await read_file({"path": "/tmp/input.txt"})
await write_file({"path": "/tmp/output.txt", "content": content.upper()})
""")
In Agent Codemode, the same idea is exposed as meta-tools (list_tool_names, search_tools, execute_code, etc.). When Codemode is enabled in agent-runtimes, selected MCP servers are converted into Codemode tool bindings instead of being exposed directly as individual tools.
Key Featuresβ
π Generate from MCP Serversβ
Connect to any MCP server and generate typed Python bindings:
from agent_codemode import ToolRegistry, MCPServerConfig
registry = ToolRegistry()
registry.add_server(MCPServerConfig(
name="filesystem",
transport="stdio",
command="npx",
args=["-y", "@anthropic/mcp-server-filesystem", "/workspace"]
))
await registry.discover_all()
# Now use the generated bindings
from generated.servers.filesystem import read_file, write_file
content = await read_file({"path": "/workspace/data.txt"})
See MCP Tools for complete documentation.
π― Generate from Skillsβ
Use reusable code patterns that compose multiple tools:
# skills/batch_process.py
async def batch_process(input_dir: str, output_dir: str) -> dict:
from generated.servers.filesystem import list_directory, read_file, write_file
entries = await list_directory({"path": input_dir})
for entry in entries["entries"]:
content = await read_file({"path": f"{input_dir}/{entry}"})
await write_file({"path": f"{output_dir}/{entry}", "content": content.upper()})
return {"processed": len(entries["entries"])}
# Use the skill
from skills.batch_process import batch_process
result = await batch_process("/data/input", "/data/output")
See Skills for complete documentation.
π Progressive Tool Discoveryβ
Large tool catalogs (100+ tools) are overwhelming for LLMs. The Tool Search Tool enables progressive discovery:
# Find relevant tools without loading all definitions
result = await search_tools("file operations", limit=10)
for tool in result["tools"]:
print(f"{tool['name']}: {tool['description']}")
See Tool Discovery for complete documentation.
π Use Directly or Expose as MCP Serverβ
The generated programmatic tools can be:
Used directly by an agent:
from pydantic_ai import Agent
from agent_codemode import CodemodeToolset
agent = Agent(
"anthropic:claude-sonnet-4-20250514",
toolsets=[CodemodeToolset(registry)]
)
result = await agent.run("Process all files in /data")
See Integrations for complete documentation.
Exposed as an MCP Server:
from agent_codemode import codemode_server, configure_server
# Configure with your registry
configure_server(config=config, registry=registry)
codemode_server.run()
Or run directly:
python -m agent_codemode.server --workspace /workspace
See MCP Server for complete documentation.
Quick Startβ
pip install agent-codemode
from agent_codemode import ToolRegistry, CodeModeExecutor, MCPServerConfig
# Set up registry with MCP servers
registry = ToolRegistry()
registry.add_server(MCPServerConfig(
name="filesystem",
transport="stdio",
command="npx",
args=["-y", "@anthropic/mcp-server-filesystem", "/tmp"]
))
await registry.discover_all()
# Execute code that composes tools
executor = CodeModeExecutor(registry)
await executor.setup()
result = await executor.execute("""
from generated.servers.filesystem import read_file, write_file
content = await read_file({"path": "/tmp/input.txt"})
await write_file({"path": "/tmp/output.txt", "content": content.upper()})
""")
Documentationβ
ποΈ Agent Codemode
Generate programmatic tools from MCP Servers and Skills.
ποΈ Concepts
This page explains the core concepts behind Agent Codemode and why it's a powerful paradigm for AI agents.
ποΈ Getting Started
This guide will help you get Agent Codemode up and running quickly.
ποΈ MCP Tools
MCP Servers are one of the two sources Agent Codemode uses to generate programmatic tools (the other being Skills).
ποΈ Skills
Skills are one of the two sources Agent Codemode uses to generate programmatic tools (the other being MCP Tools).
ποΈ Programmatic Tools
After generating programmatic tools from MCP Servers and Skills, you can execute them using the CodeModeExecutor.
ποΈ Tool Discovery
Agent Codemode provides progressive tool discovery to efficiently find relevant tools from the programmatic tools generated from MCP Servers and Skills.
ποΈ Integrations
The programmatic tools generated by Agent Codemode (from MCP Servers and Skills) can be used in two ways:
ποΈ MCP Server
The programmatic tools generated by Agent Codemode (from MCP Servers and Skills) can be exposed as an MCP Server for any MCP-compatible client.
ποΈ Examples
Run the examples from the examples folders.
Inspirationβ
Agent Codemode is inspired by:
- Cloudflare Code Mode - The better way to use MCP
- Cloudflare's open-source Code Mode agent
- Anthropic Code execution with MCP - Building more efficient agents
- Anthropic Introducing advanced tool use - Claude Developer Platform