The full upstream README, mirrored here for reference. Install config, tool schemas, adoption signals, and an original overview live on the MCP Server listing page.
Capsule is a runtime for executing untrusted code in isolated environments. Each task runs inside its own WebAssembly sandbox, providing:
Simply annotate your Python functions with the @task decorator:
Use the task() wrapper function with full access to the npm ecosystem:
[!NOTE] The runtime requires a task named
"main"as the entry point. Python will create one automatically if none is defined, but it's recommended to set it explicitly.
When you run capsule run main.py (or main.ts), your code is compiled into a WebAssembly module and executed in isolated sandboxes.
Each task operates within its own sandbox with configurable resource limits, ensuring that failures are contained and don't cascade to other parts of your workflow. The host system controls every aspect of execution, from CPU allocation via Wasm fuel metering to memory constraints and timeout enforcement.
Create hello.py:
Run it:
Create hello.ts:
Run it:
[!TIP] Add
--verboseto see real-time task execution details.
The run() function lets you execute tasks programmatically from your code instead of using the CLI. The args are automatically forwarded as parameters to the main task.
Create sandbox.py:
[!IMPORTANT] You need
@capsule-run/cliin your dependencies to use the runner functions in TypeScript.
Create sandbox.ts:
[!TIP] If you're looking for a pre-configured, ready-to-use solution, check out the Python adapter or TypeScript adapter.
Configure your tasks with these parameters:
| Parameter | Description | Type | Default | Example |
|---|---|---|---|---|
name | Task identifier | str | function name (Python) / required (TS) | "process_data" |
compute | CPU allocation level: "LOW", "MEDIUM", or "HIGH" | str | "MEDIUM" | "HIGH" |
ram | Memory limit for the task | str | unlimited | "512MB", "2GB" |
timeout | Maximum execution time | str | unlimited | "30s", "5m", "1h" |
max_retries / maxRetries | Number of retry attempts on failure | int | 0 | 3 |
allowed_files / allowedFiles | Folders accessible in the sandbox (with optional access mode) | list | [] | ["./data"], [{"path": "./data", "mode": "ro"}] |
allowed_hosts / allowedHosts | Domains accessible in the sandbox | list | [] | ["api.openai.com", "*.anthropic.com"] |
env_variables / envVariables | Environment variables accessible in the sandbox | list | [] | ["API_KEY"] |
Capsule controls CPU usage through WebAssembly's fuel mechanism, which meters instruction execution. The compute level determines how much fuel your task receives.
compute="1000000") for precise control over execution limits.Every task returns a structured JSON envelope containing both the result and execution metadata:
Response fields:
success — Boolean indicating whether the task completed successfullyresult — The actual return value from your task (json, string, null on failure etc.)error — Error details if the task failed ({ error_type: string, message: string })execution — Performance metrics:
task_name — Name of the executed taskduration_ms — Execution time in millisecondsretries — Number of retry attempts that occurredfuel_consumed — CPU resources used (see Compute Levels)ram_used — Peak memory used in byteshost_requests — List of host requests made by the taskTasks can make HTTP requests to domains specified in allowed_hosts. By default, no outbound requests are allowed ([]). Provide an allowlist of domains to grant access, or use ["*"] to allow all domains.
Tasks can read and write files within directories specified in allowed_files. Any attempt to access files outside these directories is not possible.
[!NOTE]
allowed_filessupports directory paths only, not individual files.
Each entry can be a plain path (read-write by default) or a structured object with an explicit mode:
"read-only" (or "ro")"read-write" (or "rw")Python's standard file operations work normally. Use open(), os, pathlib, or any file manipulation library.
Plain strings are still accepted: allowed_files=["./output"] defaults to read-write.
Common Node.js built-ins are available. Use the standard fs module:
Plain strings are still accepted: allowedFiles: ["./output"] defaults to read-write.
--mount)The --mount flag (CLI) or mounts parameter (SDK) mount a host directory into the sandbox under an alias. Mounts propagate to sub-tasks and add access to new paths, they don't change the access mode of paths already declared in allowed_files.
Format: HOST_PATH[::GUEST_PATH][:ro|:rw]
| Part | Required | Description |
|---|---|---|
HOST_PATH | yes | Path on the host machine (relative to cwd, must stay inside project root) |
::GUEST_PATH | no | Path the task sees inside the sandbox. Defaults to HOST_PATH |
:ro / :rw | no | Access mode. Defaults to read-write |
CLI
Python SDK
TypeScript / JavaScript SDK
Inside the task, the directory is accessed via the guest path:
[!NOTE]
--mountpaths must be relative and must not escape the project root. Absolute paths are rejected.
Tasks can access environment variables to read configuration, API keys, or other runtime settings.
Use Python's standard os.environ to access environment variables:
Use the standard process.env to access environment variables:
You can create a capsule.toml file in your project root to set default options for all tasks and define workflow metadata:
With an entrypoint defined, you can simply run:
Task-level options always override these defaults when specified.
When you run your code, Capsule creates a .capsule folder in your project root. This is the build cache. It stores compiled artifacts so subsequent runs are fast (from seconds to few milliseconds).
[!TIP]
.capsuleshould be added to.gitignore. The cache is specific to your own environment and will be regenerated automatically.
Use capsule build to precompile ahead of time and skip the compilation cost on the first run:
Running source code directly (like .py or .ts) evaluates and compiles your file at runtime. While great for development, this compilation step adds a few seconds of latency on first call. For use cases where sub-second latency is critical, you should build your tasks ahead of time.
[!NOTE] Or from your existing code:
Executing a .wasm file bypasses the compiler completely, reducing initialization time to milliseconds while using a natively optimized (.cwasm) format behind the scenes.
[!NOTE] TypeScript/JavaScript has broader compatibility than Python since it doesn't rely on native bindings.
Python: Most standard Python libraries work perfectly. Packages that use C extensions require a wasm32-wasi compiled wheel. Many popular packages like numpy and pandas don't ship one yet, so they won't work inside the sandbox. However, your host code (using run()) has access to the full Python ecosystem, including any pip package and native extensions. see in-code usage
TypeScript/JavaScript: npm packages and ES modules work. Common Node.js built-ins are available. If you have any trouble with a built-in, do not hesitate to open an issue.
Contributions are welcome!
Prerequisites: Rust (latest stable), Python 3.13+, Node.js 22+
git checkout -b feature/amazing-featurecargo test (only needed if modifying crates/capsule-cli or crates/capsule-core)Need help? Open an issue
Capsule builds on these open source projects:
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.