Skip to main content

mcp-wasm-sandbox

CI PyPI version rust wasmtime license

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

pip install mcp-wasm-sandbox==0.1.1

To build from source instead:

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.

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

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:

{"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:

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:

{"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

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

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:

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.

Metadata

Release files for mcp-wasm-sandbox 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mcp-wasm-sandbox 0.1.1
File Size Uploaded Provenance Actions
mcp_wasm_sandbox-0.1.1.tar.gz 36.8 kB Without provenance Details for mcp_wasm_sandbox-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mcp-wasm-sandbox 0.1.1
File Interpreter ABI Platform Actions
mcp_wasm_sandbox-0.1.1-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for mcp_wasm_sandbox-0.1.1-cp312-cp312-macosx_11_0_arm64.whl

Total release size: 6.0 MB

Release files / mcp_wasm_sandbox-0.1.1.tar.gz

Download URL mcp_wasm_sandbox-0.1.1.tar.gz
Size 36.8 kB
Tags Source
SHA-256 checksum
How to use checksums
906e56127b243aa9052be5116c5c05b444fe5695025591ca437f21c726e5a173
BLAKE2b-256 checksum
How to use checksums
f99b0e0656f37c3bc829afe8f552cd7bc2c50aab2b35521b64e7d01334e326d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / mcp_wasm_sandbox-0.1.1-cp312-cp312-macosx_11_0_arm64.whl

Download URL mcp_wasm_sandbox-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Size 6.0 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5971454d938fd56d7f7d9d89fdef0bd0a5ed1a33e187d05292dc030067802374
BLAKE2b-256 checksum
How to use checksums
848a0356246046b5861972df23a0e4d10cc80ba93a45279c618f68f109a5bf51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for mcp-wasm-sandbox 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page