Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

datalayer/mcp-codemode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Datalayer

Become a Sponsor

πŸ”§ MCP Codemode

This is now developed in https://github.com/datalayer/agent-codemode

PyPI - Version

Code Mode for MCP Tools: Programmatically call and compose MCP tools through code execution instead of individual LLM tool calls.

Overview

MCP Codemode enables a "Code Mode" pattern where AI agents write Python code that orchestrates multiple MCP tool calls, rather than making individual tool calls through LLM inference. This approach is:

  • More efficient: Reduce LLM calls for multi-step operations
  • More reliable: Use try/except for robust error handling
  • More powerful: Parallel execution with asyncio, loops, conditionals
  • More composable: Save reusable patterns as skills

Configuration Highlights

Option Description
allow_direct_tool_calls When False (default), call_tool is hidden; all execution flows through execute_code
max_tool_calls Safety cap limiting tool invocations per execute_code run
sandbox_variant Sandbox type for code execution (default: "local-eval")
workspace_path Working directory for sandbox execution
generated_path Path where tool bindings are generated
skills_path Path for saved skills

Tool Discovery Options

  • list_tool_names: Accepts server, keywords, limit, and include_deferred for fast filtering
  • search_tools: Natural language search with include_deferred=True by default
  • list_tools: Returns full ToolDefinition objects with include_deferred=False by default

Tool Metadata

Tools include output_schema and input_examples to improve parameter accuracy. Tools marked with defer_loading=True are excluded from default listings but included in search results.

Installation

pip install mcp-codemode

Quick Start

from mcp_codemode import ToolRegistry, CodeModeExecutor, MCPServerConfig

# Set up registry with MCP servers
registry = ToolRegistry()

# Add an MCP server (stdio transport - uses command/args)
registry.add_server(MCPServerConfig(
    name="filesystem",
    command="npx",
    args=["-y", "@anthropic/mcp-server-filesystem", "/tmp"]
))

# Or add an HTTP-based server
# registry.add_server(MCPServerConfig(name="web", url="http://localhost:8001"))

await registry.discover_all()

# Execute code that composes tools
async with CodeModeExecutor(registry) as executor:
    result = await executor.execute("""
        from generated.servers.filesystem import read_file, write_file
        
        # Read multiple files
        content1 = await read_file({"path": "/tmp/file1.txt"})
        content2 = await read_file({"path": "/tmp/file2.txt"})
        
        # Process and combine
        combined = content1 + "\\n---\\n" + content2
        
        # Write result
        await write_file({"path": "/tmp/combined.txt", "content": combined})
    """)

Features

Progressive Tool Discovery

Use the Tool Search Tool to discover relevant tools without loading all definitions upfront:

# Search for tools matching a description (includes deferred tools by default)
result = await registry.search_tools("file operations", limit=10)

for tool in result.tools:
    print(f"{tool.name}: {tool.description}")

# Fast listing (deferred tools excluded by default)
names = registry.list_tool_names(limit=50)

# Include deferred tools explicitly
names_all = registry.list_tool_names(limit=50, include_deferred=True)

Code-Based Tool Composition

Execute Python code in an isolated sandbox with auto-generated tool bindings:

async with CodeModeExecutor(registry) as executor:
    execution = await executor.execute("""
        import asyncio
        from generated.servers.filesystem import ls, read_file
        
        # List all files
        files = await ls({"path": "/data"})
        
        # Read all files in parallel
        contents = await asyncio.gather(*[
            read_file({"path": f}) for f in files
        ])
    """, timeout=30.0)

# Outputs are available on the execution object
print(execution.stdout)
print(execution.stderr)
print(execution.text)
print(execution.success)

Skills (Reusable Compositions)

Skills are Python files that compose tools into reusable operations. This allows agents to evolve their own toolbox by saving useful code patterns. Skills functionality is provided by the agent-skills package.

Creating Skills as Code Files

The primary pattern is skills as Python files in a skills/ directory:

# skills/batch_process.py
"""Process all files in a directory."""

async def batch_process(input_dir: str, output_dir: str) -> dict:
    """Process all files in a directory.
    
    Args:
        input_dir: Input directory path.
        output_dir: Output directory path.
    
    Returns:
        Processing statistics.
    """
    from generated.servers.filesystem import list_directory, read_file, write_file
    
    entries = await list_directory({"path": input_dir})
    processed = 0
    
    for entry in entries.get("entries", []):
        content = await read_file({"path": f"{input_dir}/{entry}"})
        # Process content...
        await write_file({"path": f"{output_dir}/{entry}", "content": content.upper()})
        processed += 1
    
    return {"processed": processed}

Using Skills in Executed Code

Skills are imported and called like any Python module:

# In executed code
from skills.batch_process import batch_process

result = await batch_process("/data/input", "/data/output")
print(f"Processed {result['processed']} files")

Managing Skills with SimpleSkillsManager

For programmatic skill management, use the SimpleSkillsManager:

from agent_skills import SimpleSkillsManager, SimpleSkill

# Create a skills manager
manager = SimpleSkillsManager("./skills")

# Save a skill
skill = SimpleSkill(
    name="batch_process",
    description="Process files in a directory",
    code='''
async def batch_process(input_dir, output_dir):
    entries = await list_directory({"path": input_dir})
    for entry in entries.get("entries", []):
        content = await read_file({"path": f"{input_dir}/{entry}"})
        await write_file({"path": f"{output_dir}/{entry}", "content": content.upper()})
''',
    tags=["file", "batch"],
)
manager.save_skill(skill)

# Load and use a skill
loaded = manager.load_skill("batch_process")
print(loaded.code)

Examples

See the runnable examples in examples/.

Simple Examples

python examples/simple/codemode_example.py
python examples/simple/codemode_patterns_example.py

Agent CLI

Interactive CLI agent with MCP codemode support:

# Standard mode
python examples/agent/agent_cli.py

# Codemode variant (code-first tool composition)
python examples/agent/agent_cli.py --codemode

Pydantic AI Integration

Use the CodemodeToolset for direct integration with Pydantic AI agents:

from pydantic_ai import Agent
from mcp_codemode import CodemodeToolset, ToolRegistry, MCPServerConfig

# Set up registry
registry = ToolRegistry()
registry.add_server(MCPServerConfig(
    name="filesystem",
    command="npx",
    args=["-y", "@anthropic/mcp-server-filesystem", "/tmp"]
))
await registry.discover_all()

# Create toolset
toolset = CodemodeToolset(registry=registry)

# Use with Pydantic AI agent
agent = Agent(
    model='anthropic:claude-sonnet-4-5',
    toolsets=[toolset],
)

MCP Server

Expose Code Mode capabilities as an MCP server:

from mcp_codemode import codemode_server, configure_server
from mcp_codemode import ToolRegistry, MCPServerConfig, CodeModeConfig

# Create and configure registry
registry = ToolRegistry()
registry.add_server(MCPServerConfig(
    name="filesystem",
    command="npx",
    args=["-y", "@anthropic/mcp-server-filesystem", "/tmp"]
))

# Configure with custom settings
config = CodeModeConfig(
    workspace_path="./workspace",
    skills_path="./skills",
    generated_path="./generated",
)

configure_server(config=config, registry=registry)
codemode_server.run()

Or start with default configuration:

from mcp_codemode import codemode_server, configure_server

configure_server()
codemode_server.run()

Tools exposed:

  • search_tools - Progressive tool discovery
  • list_servers - List connected MCP servers
  • get_tool_details - Get full tool schema
  • execute_code - Run code that composes tools
  • call_tool - Direct tool invocation (when allow_direct_tool_calls=True)
  • save_skill / run_skill - Skill management

Key Concepts

Tool Discovery

Instead of loading all tool definitions upfront (which can overwhelm context), use the Tool Search Tool pattern for progressive discovery based on the task at hand.

Tool Composition

Compose tools through code instead of reading all data into LLM context. This is faster, more reliable (no text reproduction errors), and more efficient.

Control Flow

Code allows models to implement complex control flow: loops, conditionals, waiting patterns, and parallel execution without burning through context with repeated tool calls.

State Persistence

When running in a sandbox, state can persist between execute_code calls within the same session. Variables, functions, and imported modules remain available for subsequent code executions. Skills can also be saved to disk and loaded later for reuse across sessions.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         MCP Codemode                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Tool Registry  β”‚  β”‚ Code Executor  β”‚  β”‚   CodemodeToolset     β”‚ β”‚
β”‚  β”‚  - Discovery    β”‚  β”‚  - Sandbox     β”‚  β”‚   (Pydantic AI)       β”‚ β”‚
β”‚  β”‚  - Search       β”‚  β”‚  - Bindings    β”‚  β”‚   - search_tools      β”‚ β”‚
β”‚  β”‚  - Cache        β”‚  β”‚  - Execute     β”‚  β”‚   - execute_code      β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                              β”‚                                      β”‚
β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚              β”‚        Generated Bindings      β”‚                      β”‚
β”‚              β”‚   generated/servers/<name>.py  β”‚                      β”‚
β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                         MCP Servers                                 β”‚
β”‚    (filesystem, bash, web, etc. - connected via MCP protocol)       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                      Agent Skills (agent_skills)                    β”‚
β”‚    (SimpleSkillsManager, SkillDirectory, DatalayerSkillsToolset)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

References

License

BSD 3-Clause License

About

πŸ”§ Code Mode for MCP (Model Context Protocol).

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project