Skip to main content
Glama
README.md
# Boxwell

**OpenRouter for sandboxes.** One API — and one MCP server — in front of many code-execution sandbox providers,
with capability-aware routing, automatic fallback, usage events and built-in benchmarks.

```ts
import { Boxwell, DockerProvider, E2BProvider } from "boxwell";

const bw = new Boxwell({ providers: [new E2BProvider(), new DockerProvider()] });

const box = await bw.create({ timeoutSec: 600 });          // E2B, or Docker if E2B is down
await box.writeFile("/work/main.py", "print(sum(range(10)))");
const r = await box.exec("python3 main.py", { cwd: "/work" });
console.log(r.stdout, r.exitCode, box.provider);           // "45\n" 0 "e2b"
await box.destroy();
```

> **Status: 0.1.0, early.** The core, the Docker adapter and the MCP server are tested end-to-end.
> The E2B and Daytona adapters are built against their SDK types and tested with fakes, but **not yet verified against the live APIs** — see [Providers](#providers).

---

## Why

AI agents need somewhere safe to run the code they write. Every sandbox vendor has its own SDK, limits, pricing and outages.
Boxwell gives you **one interface**, lets you **switch or mix providers with one line**, and keeps your agent running when a provider fails.

| Without Boxwell | With Boxwell |
|---|---|
| One SDK per vendor | One API (TypeScript SDK or MCP) |
| Locked in to one vendor | Change provider by name, or let the router choose |
| Provider down → agent down | Automatic fallback on retryable errors |
| Guessing which is fastest/cheapest | `fastest` / `cheapest` strategies + a benchmark harness |

## Install

```bash
npm install boxwell
# cloud providers are optional peer dependencies — install the ones you use:
npm install e2b              # for E2BProvider
npm install @daytonaio/sdk   # for DaytonaProvider
```

Requires Node.js ≥ 22. The Docker provider needs a running Docker daemon.

## Providers

| Provider | Class | Isolation | Persist | Ports | Status |
|---|---|---|---|---|---|
| Docker (local) | `DockerProvider` | Container (shared kernel) | – | ✅ | ✅ Tested end-to-end |
| [E2B](https://e2b.dev) | `E2BProvider` | Firecracker microVM | ✅ | ✅ | ⚠️ Unverified against live API |
| [Daytona](https://daytona.io) | `DaytonaProvider` | Provider-managed sandbox | ✅ | ✅ | ⚠️ Unverified against live API |
| Mock (in-memory) | `MockProvider` | – | – | optional | ✅ For tests & demos |

Credentials are read from `E2B_API_KEY` and `DAYTONA_API_KEY` (or passed to the constructor).
Adding a provider = one class implementing the `SandboxProvider` interface (`src/types.ts`).

> **Security note:** Docker containers share the host kernel. Boxwell applies strict defaults (no network, all capabilities dropped,
> `no-new-privileges`, PID limit), but **don't use the Docker provider for hostile code in production** — use a microVM provider.

## Routing

```ts
const bw = new Boxwell({
  providers: [e2b, daytona, docker],
  strategy: "cheapest",          // "priority" (default) | "fastest" | "cheapest" | "round-robin"
  fallback: true,                // default
  cooldownMs: 30_000,            // skip a failing provider for a while (still used as last resort)
  onEvent: (e) => console.log(e) // create / create_failed / exec / destroy (+ lifetime & estimated cost)
});

await bw.create({ requires: { persist: true } }); // only providers with that capability
await bw.create({ ports: [3000] });               // implies requires.ports
await bw.create({ provider: "docker" });          // force one
```

- **Fallback happens only at create time and only on retryable errors** — outages, rate limits, timeouts, network and auth errors.
  Invalid requests (bad image, bad arguments) fail immediately because they would fail everywhere.
  A running sandbox is never moved silently: it has state.
- If nothing works you get a `RoutingError` listing every attempt.
- `bw.getStats()`, `bw.health()`, `bw.list()`, `bw.connect(provider, id)`, `bw.destroyAll()`.

## Sandbox API

```ts
const box = await bw.create({ image: "python:3.12-alpine", ports: [8000], internet: true, env: { DEBUG: "1" } });

await box.exec("pip --version", { cwd: "/", env: {}, timeoutSec: 30, onStdout: (c) => process.stdout.write(c) });
await box.writeFile("/site/index.html", "<h1>hi</h1>");   // string or Uint8Array; parent dirs created
await box.readText("/site/index.html");
await box.listFiles("/site");                            // [{ name, path, type, size }]

const p = await box.startProcess("python3 -m http.server 8000", { cwd: "/site" });
await box.waitForPort(8000);
const { url, headers } = await box.getPortUrl(8000);     // reachable from your machine
await box.processLogs(p.id);  await box.listProcesses();  await box.killProcess(p.id);

await box.destroy();
```

Defaults are secure: **no network** unless `internet: true`, and every sandbox you give `timeoutSec` destroys itself even if your process crashes.
Background processes, file listing and port waiting are implemented once on top of `exec`, so they behave identically on every provider.

## MCP server

`boxwell-mcp` exposes the router as 13 MCP tools (`sandbox_create`, `sandbox_exec`, `sandbox_write_file`, `sandbox_read_file`,
`sandbox_list_files`, `sandbox_start_process`, `sandbox_list_processes`, `sandbox_process_logs`, `sandbox_kill_process`,
`sandbox_get_port_url`, `sandbox_list`, `sandbox_destroy`, `providers_status`), with read-only / destructive annotations.

```bash
# Claude Code
claude mcp add boxwell -- npx -y -p boxwell boxwell-mcp
```

```json
{
  "mcpServers": {
    "boxwell": {
      "command": "npx",
      "args": ["-y", "-p", "boxwell", "boxwell-mcp"],
      "env": { "BOXWELL_PROVIDERS": "docker", "E2B_API_KEY": "…" }
    }
  }
}
```

| Env var | Default |
|---|---|
| `BOXWELL_PROVIDERS` | `docker` + each cloud provider whose key is set |
| `BOXWELL_STRATEGY` | `priority` |
| `BOXWELL_DOCKER_IMAGE` | `python:3.12-alpine` |
| `BOXWELL_DEFAULT_TIMEOUT_SEC` | `3600` |
| `BOXWELL_KEEP_ON_EXIT` | off — sandboxes created in a session are destroyed when the client disconnects |
| `BOXWELL_DEBUG` | off — logs routing events to stderr |

## Benchmarks

```bash
npx -p boxwell boxwell-bench --providers docker,e2b --iterations 10
```

Measures create, first/warm exec, file round-trip, destroy and **time to first output** (p50/p90), success rate and estimated cost,
and writes JSON + Markdown reports. Feed the results back into routing:

```ts
import { runBenchmark, latencyHints } from "boxwell";
const report = await runBenchmark([e2b, docker]);
const bw = new Boxwell({ providers: [e2b, docker], strategy: "fastest", latencyHints: latencyHints(report) });
```

Example (Docker, one Linux machine, 10 runs, ms p50 / p90): time to first output **239 / 275**, create 163 / 186, warm exec 60 / 82.

## Development

```bash
npm install
npm run typecheck
npm test                      # Docker tests auto-skip without Docker; live cloud tests run when keys are in .env
cp .env.example .env          # add E2B_API_KEY / DAYTONA_API_KEY for live tests
npm run demo                  # walkthrough: simulated outage → fallback → run code
npm run mcp                   # MCP server from source
npm run bench -- --providers docker
```

## Roadmap

- Live verification of E2B and Daytona; pause / resume (`persist`)
- A third provider (Modal or Vercel Sandbox)
- Public provider leaderboard from scheduled benchmarks
- Hosted gateway (one API key, unified billing) — only after real usage

## License

[MIT](LICENSE)