Skip to content

Repository files navigation

MCP Code Sandbox Agent

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.

Architecture

agent.py → OpenRouter LLM → run_code tool call
                                    ↓
                             server.py (MCP server)
                                    ↓
                             Docker container (isolated)
                                    ↓
                             result → LLM → final answer

Prerequisites

  • uv — Python package manager
  • Docker — must be running
  • gVisor — userspace kernel for syscall isolation
  • An OpenRouter API key

Install gVisor

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 runsc

Register 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 docker

Pull the sandbox image ahead of time to avoid delay on first run:

docker pull python:3.12-slim

Setup

  1. Install dependencies:
uv sync
  1. Add your OpenRouter API key to .env:
OPENROUTER_API_KEY=sk-or-...

Running

uv run agent.py

The 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

About

Demo of agentic code execution "programmatic tool calling" using Docker + gVisor

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages