Developer docs / AI Fabric tutorials

Stateless MCP server with FastMCP

  • Sandbox
  • Web endpoint
  • Image DSL

Take it for a spin

The whole program in one file, ready to run.

sandbox_mcp.py
import bladekit

app = bladekit.app("sandbox-mcp")

server_image = (
    bladekit.Image.debian_slim(python_version="3.12")
    .pip_install("fastmcp", "httpx")
)

sandbox_image = (
    bladekit.Image.debian_slim(python_version="3.12")
    .pip_install("numpy", "scipy", "pandas")
)

@app.web_endpoint(methods=["POST"], min_replicas=1)
@app.function(image=server_image, timeout=120)
def mcp_handler(item: dict) -> dict:
    """Handle MCP tool-call requests. Each code execution runs in a fresh sandbox."""
    tool = item.get("tool")
    args = item.get("arguments", {})

    if tool == "run_python":
        code = args.get("code", "")
        with bladekit.Sandbox.create(image=sandbox_image, timeout=30) as sb:
            sb.write_file("/work/code.py", code.encode())
            proc = sb.exec("python", "/work/code.py")
            proc.wait()
            return {
                "content": [{"type": "text", "text": proc.stdout.read().decode()}],
                "isError": proc.returncode != 0,
            }

    return {"content": [{"type": "text", "text": f"Unknown tool: {tool}"}], "isError": True}

Run it

  1. Deploy: blade deploy sandbox_mcp.py.
  2. Register the endpoint URL with your MCP client (e.g. Claude Desktop).
  3. Each run_python tool call executes code in a fresh, isolated sandbox, so no state leaks between calls.
  4. min_replicas=1 keeps the MCP server warm so tool calls respond quickly.

Concepts used: Endpoint, Replica, Cold start

Contact the team

Loading the form…