An AI agent that writes and executes Python code in an isolated Docker sandbox. Uses OpenRouter for the LLM and MCP to connect the agent to the sandbox tool.
agent.py → OpenRouter LLM → run_code tool call
↓
server.py (MCP server)
↓
Docker container (isolated)
↓
result → LLM → final answer
- uv — Python package manager
- Docker — must be running
- gVisor — userspace kernel for syscall isolation
- An OpenRouter API key
curl -fsSL https://gvisor.dev/archive.key | sudo gpg --dearmor -o /usr/share/keyrings/gvisor-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/gvisor-archive-keyring.gpg] https://storage.googleapis.com/gvisor/releases release main" | sudo tee /etc/apt/sources.list.d/gvisor.list
sudo apt-get update && sudo apt-get install -y runscRegister runsc as a Docker runtime in /etc/docker/daemon.json:
{
"runtimes": {
"runsc": {
"path": "/usr/bin/runsc",
"runtimeArgs": ["--platform=kvm"]
}
}
}Then restart Docker:
sudo systemctl restart dockerPull the sandbox image ahead of time to avoid delay on first run:
docker pull python:3.12-slim- Install dependencies:
uv sync- Add your OpenRouter API key to
.env:
OPENROUTER_API_KEY=sk-or-...uv run agent.pyThe agent will spawn the MCP server automatically. You should see:
User: Calculate the first 10 fibonacci numbers using Python code.
[Tool call] run_code({'code': '...'})
[Tool result]
[sandbox] hostname=abc123 mem_limit=128MB cpu_limit=0.5 CPUs
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
[sandbox] mem_used=0KB cpu_used=0.09ms