Metadata-Version: 2.4
Name: mcp-wasm-sandbox
Version: 0.1.1
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: 3
Summary: Model Context Protocol server that runs untrusted WebAssembly inside a Wasmtime sandbox.
Author: Adrian Fernandez Chamizo espinete@duck.org
License: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Project-URL: Homepage, https://github.com/padrisimo/mcp-wasm-sandbox
Project-URL: Issues, https://github.com/padrisimo/mcp-wasm-sandbox/issues
Project-URL: Repository, https://github.com/padrisimo/mcp-wasm-sandbox

# mcp-wasm-sandbox

[![CI](https://github.com/padrisimo/mcp-wasm-sandbox/actions/workflows/ci.yml/badge.svg)](https://github.com/padrisimo/mcp-wasm-sandbox/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/mcp-wasm-sandbox.svg)](https://pypi.org/project/mcp-wasm-sandbox/)
[![version](https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpadrisimo%2Fmcp-wasm-sandbox%2Fmain%2FCargo.toml&query=%24.workspace.package.version&label=version&color=blue)](https://github.com/padrisimo/mcp-wasm-sandbox/releases)
[![rust](https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpadrisimo%2Fmcp-wasm-sandbox%2Fmain%2FCargo.toml&query=%24.workspace.package.rust-version&label=rust&color=dea584)](https://github.com/padrisimo/mcp-wasm-sandbox/blob/main/Cargo.toml)
[![wasmtime](https://img.shields.io/badge/wasmtime-50-orange.svg)](https://wasmtime.dev/)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)

A Model Context Protocol (MCP) server that runs untrusted WebAssembly inside a
Wasmtime sandbox with a hard fuel budget and a hard linear-memory ceiling.

## Status

| Phase | Scope | State |
| --- | --- | --- |
| 1 | JSON-RPC 2.0 wire types, stdio and HTTP transports, dispatcher | done |
| 2 | Isolation runtime: fuel metering, memory ceiling, module cache | done |
| 3 | Python bindings (`mcp_wasm_sandbox`) | done |
| 4 | Benchmarks and CI | done |
| 5 | Hardening, observability and supply chain | done |

Everything below is verified by the test suite and by the commands shown here.
Version 0.1.1 of the Python bindings is published on PyPI; the first wheel is
for CPython 3.12 on macOS arm64. Build from source for other platforms.

## Requirements

- Rust 1.97 or newer (the workspace uses edition 2024 and wasmtime 50).
- Python 3.10 or newer, only for the bindings. The interpreter needs a shared
  `libpython` to build the bindings.

## Build and install

### Python bindings

```sh
pip install mcp-wasm-sandbox==0.1.1
```

To build from source instead:

```sh
pip install maturin
cd crates/python-bindings
maturin build --release          # writes target/wheels/mcp_wasm_sandbox-*.whl
pip install target/wheels/mcp_wasm_sandbox-*.whl
```

`maturin develop` installs the module in editable mode into the active virtual
environment, which is the fastest loop while working on the bindings.

```python
import mcp_wasm_sandbox

WAT = b'(module (func (export "add") (param i32 i32) (result i32) local.get 0 local.get 1 i32.add))'

sandbox = mcp_wasm_sandbox.WasmSandbox(32 * 1024 * 1024, 1_000_000_000)
sandbox.run(WAT, "add", [2, 3])  # -> [5]
```

`WasmSandbox(max_memory_bytes, fuel)` takes the linear-memory ceiling and the
fuel budget. The GIL is released while the module runs, so a long module never
blocks the interpreter.

The guest output is exposed with `WasmSandbox.run_with_output(wasm, export,
args)`, which returns the results together with everything the guest wrote to
stdout or stderr. `WasmSandbox.run` returns only the results.

### MCP server

```sh
cargo build --release -p mcp-server
./target/release/mcp-server                        # newline delimited json-rpc over stdio
./target/release/mcp-server --http 127.0.0.1:3000  # json-rpc over http
./target/release/mcp-server --fuel 5000000000      # raise the fuel budget
```

Both transports speak the same protocol: the same dispatcher answers, only the
bytes differ. The stdio transport is one session per process and enforces the
MCP lifecycle (the client must `initialize` first, `ping` excepted). The HTTP
transport has no session, so every request stands alone: it builds a fresh
dispatcher per request, does not enforce the lifecycle, and one slow call never
blocks another client.

The default fuel budget is `1_000_000_000` units, roughly a second of guest
CPU. Fuel is counted in wasm instructions, so a module doing real work may need
more: `--fuel` sets it per process, and `SandboxConfig::fuel` per sandbox.

Over stdio the server speaks newline-delimited JSON-RPC 2.0 on stdin and stdout
and exposes one tool, `run_wasm`:

```json
{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"run_wasm","arguments":{"source":"(module (func (export \"add\") (param i32 i32) (result i32) local.get 0 local.get 1 i32.add))","export":"add","args":[2,3]}}}
```

The response is `{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"5"}]}}`.

Over HTTP the endpoint accepts a POST with one JSON-RPC message and answers
with the response, `202 Accepted` for notifications and `400 Bad Request` for a
body that is not a valid JSON-RPC message:

```sh
curl -s -X POST http://127.0.0.1:3000/mcp \
  -H 'content-type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
```

The module travels either as text (`source`, WebAssembly text format) or as a
binary (`module_base64`, base64 encoded), which is what agents usually produce:

```json
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"run_wasm","arguments":{"module_base64":"AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABags=","export":"add","args":[2,3]}}}
```

The `args` are numbers: the sandbox adapts them to the signature of the
exported function, so a module that takes an `i64` works with the same call.

### Container image

```sh
docker build -t mcp-wasm-sandbox:bench .
docker run --rm -i mcp-wasm-sandbox:bench
```

The image ships the release server in a minimal Debian layer, so the container
baseline measures the same workload as the sub-process one.

## Development

```sh
cargo test --workspace
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --all -- --check
```

The interpreter-backed test that proves the GIL is released needs a Python with
a shared `libpython`:

```sh
PYO3_PYTHON=$(uv python find 3.12) cargo test -p mcp-wasm-sandbox --no-default-features -- --ignored
```

## Performance

`cargo bench -p sandbox-core` measures the hot paths. Criterion writes the full
distributions, p50, p95 and p99 included, under `target/criterion`.

| Benchmark | Median | What it measures |
| --- | --- | --- |
| `compile_module` | ~98 µs | cold path, the module is compiled from WAT every time |
| `instantiate` | ~1.3 µs | store plus instantiation of an already compiled module |
| `cached_call` | ~6.6 µs | the full public API, offloaded to a blocking thread |
| `subprocess_spawn_floor` | ~29 µs | baseline: spawning a trivial process |
| `subprocess_round_trip` | ~2.5 ms | baseline: one execution behind a process boundary, through the MCP server |
| `container_round_trip` | ~178 ms | baseline: the same execution inside a container |

The container baseline is skipped unless the image is built first
(`docker build -t mcp-wasm-sandbox:bench .`).

Measured on an Apple Silicon laptop: a cached execution is roughly 375 times
faster than the same work behind a sub-process and two orders of magnitude below
the 1 ms target of the architecture.

## Known limitations

- WASI `preview1` is available, but locked down: no preopened directories, no
  environment variables, no arguments and no inherited streams. Everything the
  guest writes to stdout or stderr comes back in the tool result.
- The tool arguments are numbers; the guest decides their type, since the
  sandbox adapts them to the signature of the exported function.
- The module cache holds at most 64 modules and is not shared between processes.
- Stdio handles one request at a time; the HTTP transport runs requests
  concurrently.
- The wall-clock timeout stops the caller from waiting, not the guest: a call
  that times out keeps its blocking thread until the fuel budget ends it. Fuel
  is the real bound.

## License

MIT, see [LICENSE.md](https://github.com/padrisimo/mcp-wasm-sandbox/blob/main/LICENSE.md).

