Skip to main content
CodeMode is an experimental transform that replaces a large tool surface with two meta-tools:
  • search: inspect tool metadata by writing Python code
  • execute: run tool calls from Python code via call_tool(...)
This is useful when you want an LLM to plan multi-step API/tool workflows with a small interface. For background and design context, see Cloudflare’s Code Mode post: https://blog.cloudflare.com/code-mode-mcp/.

Install Monty (Optional)

CodeMode defaults to MontySandboxProvider, which requires the optional Monty dependency.
pip install "fastmcp[monty]"
If you do not want Monty, pass a custom sandbox provider.

Basic Transform Usage

from fastmcp import FastMCP
from fastmcp.experimental.transforms import CodeMode

mcp = FastMCP("Server")

@mcp.tool
def add(x: int, y: int) -> int:
    return x + y

mcp.add_transform(CodeMode())
# Clients now see only: search, execute
CodeMode is a transform-first API. Attach it with mcp.add_transform(...). Backend tools are hidden from listings, while execute can still call them.

OpenAPI + Code Mode

You can place CodeMode on top of an OpenAPI-backed provider so clients only see search and execute while the backing tools come from your API spec.
import httpx
from fastmcp import FastMCP
from fastmcp.experimental.transforms import CodeMode
from fastmcp.server.providers.openapi import OpenAPIProvider

openapi_spec = httpx.get("https://api.example.com/openapi.json").json()
api_client = httpx.AsyncClient(base_url="https://api.example.com")

provider = OpenAPIProvider(
    openapi_spec=openapi_spec,
    client=api_client,
    validate_output=False,  # Optional: tolerate APIs that don't match their response schemas exactly
)

mcp = FastMCP("API Code Mode", providers=[provider])
mcp.add_transform(CodeMode())

if __name__ == "__main__":
    mcp.run()
Equivalent shortcut:
mcp = FastMCP.from_openapi(openapi_spec=openapi_spec, client=api_client, name="API Code Mode")
mcp.add_transform(CodeMode())

Default Prompt Behavior

  • search receives tools: list[dict] with metadata fields including name, key, parameters, and output_schema. Use key when tools have versions — it uniquely identifies a specific tool version.
  • execute exposes a single callable: call_tool(tool_name_or_key, params). Prefer passing key for versioned tools.
  • execute is intended for chaining multiple await call_tool(...) steps in one block and returning the final value.

Custom Tool Names and Descriptions

code_mode = CodeMode(
    search_tool_name="search_meta",
    execute_tool_name="execute_meta",
    search_description="Find available tools by metadata",
    execute_description="Run multi-step tool workflows",
)

Custom Sandbox Providers

Third-party sandbox providers (including remote providers) can integrate by implementing the SandboxProvider protocol.
from collections.abc import Callable
from typing import Any

from fastmcp.experimental.transforms import CodeMode, SandboxProvider

class RemoteSandboxProvider:
    async def run(
        self,
        code: str,
        *,
        inputs: dict[str, Any] | None = None,
        external_functions: dict[str, Callable[..., Any]] | None = None,
    ) -> Any:
        # Send code + inputs to your remote runtime.
        # Implement callback semantics for external_functions as needed.
        ...

mcp.add_transform(CodeMode(sandbox_provider=RemoteSandboxProvider()))
CodeMode treats the sandbox provider as a pluggable execution backend, so provider-specific security and isolation can evolve independently.