Kern Sandbox
Your model writes the code. This runs it where it can't reach your files unless you hand them over.
Works with Claude Code · Cursor · Claude Desktop · LM Studio · LangChain · pi
rootless · no daemon · no socket · no VM · no cloud · no account
An agent's tool-call, a generated snippet, a notebook cell, a CI step: code nobody has read, run in a container of its own.
# if the venv line fails: sudo apt install python3-venv
python3 -m venv .venv && . .venv/bin/activate
pip install -U kern-sandbox # on Linux x86_64 and aarch64, this brings kern with it
On a Mac, run it inside a Linux VM, with your code: macOS.
import kern_sandbox as kern
r = kern.run_code("print(sum(range(100)))")
print(r.stdout, r.fault) # 4950 None
That ran in a fresh container from an OCI image, with no network, memory and PID caps and a deadline set from outside, and the container was thrown away before the call returned.
- Cheap enough for every call: a hundred calls are a hundred containers, nothing left behind.
- State when you want it: a
Sandbox()you keep open shares/workspace;kernel()keeps one warm interpreter where variables carry too. - Imports compiled once: the image's standard library is precompiled the first time you use it
and mounted READ-ONLY into later boxes, so
import json, reis about 3x cheaper.pyc_cache=Falseturns it off. - One install: the Linux wheels carry the
kernbinary, so the venv haskern doctorandkern pstoo.
A hallucinated rm -rf ~ hits the container's own /root, mounted read-only. A step that hangs or
runs out of memory comes back as a value your loop can branch on.
The basics, one call each
Each block starts from import kern_sandbox as kern and prints what the comment says.
Give it a file, get a file back. A Sandbox() has a /workspace you and the code both see.
job = """
import csv
rows = list(csv.DictReader(open("in.csv")))
hottest = max(rows, key=lambda row: int(row["temp"]))
open("out.txt", "w").write(hottest["city"])
"""
with kern.Sandbox() as sb:
sb.write_file("in.csv", "city,temp\nRome,24\nOslo,9\n")
sb.run_code(job)
print(sb.read_file("out.txt")) # b'Rome'
Keep variables between steps. kernel() is one warm interpreter, like a notebook.
with kern.Sandbox() as sb, sb.kernel() as k:
k.run_code("x = 40")
r = k.run_code("print(x + 2)")
print(r.stdout) # 42
Install a package first. setup= runs once, with network; your code still runs without.
with kern.Sandbox(setup="pip install numpy") as sb:
r = sb.run_code("import numpy as np; print(np.pi)")
print(r.stdout) # 3.141592653589793
Let it reach one host. No network by default. egress_allow opens the hosts you name and
nothing else; network=True opens everything, your own loopback included.
code = ("import urllib.request as u; "
"print(u.urlopen('https://pypi.org').status)")
r = kern.run_code(code)
print(r.exit_code) # 1: no network
r = kern.run_code(code, egress_allow=["pypi.org"])
print(r.stdout) # 200
Run a shell command.
r = kern.run_code("uname -s && python3 --version", language="bash")
print(r.stdout) # Linux, then the image's Python 3.12
Get a chart back. A matplotlib figure open when the code ends comes back as a PNG on
r.results; fig or display(fig) draws one the code holds. In a kernel() a figure is closed
once its cell returned it, as in Jupyter.
plot = """
import matplotlib.pyplot as plt
plt.plot([1, 4, 9, 16])
plt.show()
"""
with kern.Sandbox(setup="pip install matplotlib") as sb:
r = sb.run_code(plot)
open("chart.png", "wb").write(r.results[0].png)
The result says who stopped the run
Exit 137 alone does not say whether it was your timeout or the OOM killer. fault does. Every row
was run:
| the code | fault.type |
exit_code |
|---|---|---|
print(sum(range(100))) |
None |
0 |
while True: pass, timeout_s=3 |
timeout |
137 |
bytearray(400<<20), memory_mb=128 |
oom |
137 |
urlopen(...), network off |
None |
1 |
print(1) on alpine:3.19, no python3 |
exec_failed |
127 |
print(1) on an image that is not there |
startup_failed |
1 |
In the fourth row the code raised and the sandbox did nothing. fault is read from a pipe kern
writes, not from stdout, so code that prints [exit 0] can't fake it. Also killed,
workspace_full and escape_blocked. These values and exit codes are stable: one changes only to
correct an outcome reported wrong, and that release's
CHANGELOG entry says so first.
Works with
- Any MCP client: Claude Code, Cursor, Claude Desktop, LM Studio, Zed. The package ships
kern-mcp, a stdio server; charts come back as images the model can see. One session backs the connection, so files carry between tool calls and variables do not, unlessKERN_MCP_KERNEL=1: docs/MCP.md. - LangChain:
kern_code_tool()is aStructuredTool, and a fault comes back labelled for the model. Shell policy: LANGCHAIN-SHELL.md. - pi:
kern-piroutes its file and shell tools into a box, your working directory at/workspace. - Python and Node: the same API,
pip install kern-sandboxandnpm i kern-sandbox.
{
"mcpServers": {
"kern": { "command": "uvx", "args": ["--from", "kern-sandbox", "kern-mcp"] }
}
}
A client spawns the server from its own PATH, so it cannot see a venv: uvx fetches the
package, kern included on Linux, or use pipx install kern-sandbox. From macOS or Windows, swap the
command for wsl or ssh.
Safe by default
A bare Sandbox() runs python:3.12-slim with a 30 second deadline, no network, no host mounts,
seccomp on and capabilities dropped. The timeout is mandatory: no value disables it. Every
relaxation is a named argument.
- A compromised dependency is stopped by the filesystem and the network. A prompt-injected agent runs the code you asked for, so the defence is that credentials are never in the box.
- Mounts over sensitive sources are refused, with no opt-out: the host's own directories, kern's
own state, and 17 credential directories by name (
.ssh,.aws,.kube,.gnupg,.netrc,.npmrc,.git-credentialsand the rest), plus~/.config/ghand~/.config/gcloud. A parent of any of these is refused too (/homeholds your home), and so is a directory with a credential directory or a shell history in its first two levels (.npmrc,.pypircand.dockerexcepted, since projects carry their own). Mount a copy. A source whose listing does not come back within 10 s is refused unchecked; if it is a network or FUSE filesystem that never answers, the scan's thread stays in the kernel's uninterruptible wait, and the process cannot exit until that filesystem answers or is unmounted. network=Trueincludes the host's loopback, where unauthenticated services live: a test read the host's SSH banner off127.0.0.1:22.egress_allowis the route-level middle setting.
gpus= hands the box NVIDIA cards, in docker's grammar: gpus="2" is how many, gpus="device=0,1"
is which, gpus="all" is all, and a card's UUID works where its index does
(gpus="device=GPU-1950be8a-…"), with a UUID no card carries refused and the present ones named. The host's driver libraries come with them, so the image does not
have to be a CUDA one. A granted card talks to the host's driver and is not a boundary against
the code in the box; a card you did not ask for has no device node there, so CUDA cannot initialise
it. Every box the session starts gets the cards, the setup= box included, because a wheel that
picks its build from the visible card has to see the same one the later calls will.
How fast
Most of a call's cost is CPython starting inside the box, not the box. A precompiled image removes the stock image's compile step, and a prewarm pool serves calls from boxes already started. Method and numbers: BENCHMARKS.md.
Current limitations
- Not a boundary against deliberately hostile code. Namespaces, cgroups and seccomp are for your own or semi-trusted code. For hostile or someone else's code, use a microVM or gVisor: SECURITY.md.
- Caps bind only where your host delegates a cgroup.
kern doctorsays whether yours does;require_limits=Truerefuses to start rather than run uncapped. - The workspace is bounded only if you ask. It is a host directory, so by default a job can fill
your disk.
workspace_max_bytesis measured while a call runs and stops the box with aworkspace_fullfault. The overshoot depends on what the tree costs to walk: ~1.2x for a few files, ~13x for 50,000. A measurement and a kill, not a refusal from the filesystem. - A directory the box closes to the host comes back at
0o500. The host walks the workspace to report changes, so read and search are restored on directories it owns, never write, and a warning says how many. A file at mode 0 is left alone. - A non-root
user=shares the workspace through a POSIX ACL. It needs a filesystem with ACLs and a host range in/etc/subuid. A file that account closes on purpose (mkstemp's 0600,mkdtemp's 0700) is reached through a short-lived box running as that account, one box start per access. - Not inside a container without
--privileged, and not on Google Colab. A box mounts a fresh/proc, which the kernel allows only where an unmasked/procexists; a container masks its own, so the box stops atmount(proc) failed: Permission denied. Measured on Colab, kernel 6.6.122. - The binary ships with the package only on Linux x86_64 and aarch64. Elsewhere pip picks the
wheel without it, and the package drives a
kernonPATHor in$KERN_BIN, installed with the install script.$KERN_BINalways wins over the bundled one.
More
The full API, mime-typed results, snapshots and the measured sharp edges: SANDBOX-NOTES.md.
Linux with unprivileged user namespaces and cgroup v2, Python 3.9+. Windows through WSL2; on a Mac,
inside a Linux VM, where the same pip install is the whole install:
install notes. Apache-2.0.
Metadata
Release files for kern-sandbox 0.2.53
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kern_sandbox-0.2.53.tar.gz | 431.2 kB | Without provenance | Details for kern_sandbox-0.2.53.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kern_sandbox-0.2.53-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl | Python 3 | none | Linux glibc 2.17+ x86-64, Linux musl 1.1+ x86-64 | Details for kern_sandbox-0.2.53-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl |
| kern_sandbox-0.2.53-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl | Python 3 | none | Linux glibc 2.17+ ARM64, Linux musl 1.1+ ARM64 | Details for kern_sandbox-0.2.53-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl |
| kern_sandbox-0.2.53-py3-none-any.whl | Python 3 | none | any | Details for kern_sandbox-0.2.53-py3-none-any.whl |
Total release size: 4.3 MB
Release files / kern_sandbox-0.2.53.tar.gz
| Download URL | kern_sandbox-0.2.53.tar.gz |
|---|---|
| Size | 431.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d5535ddacfe816d646122b2665be78cf7bf4a7148100ba93f1ec60d44303e12f
|
|
BLAKE2b-256 checksum How to use checksums |
dab92ecc4c6c6dcf02f6f3635434c94e000e3510e17923e13422bd0fd6697675
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / kern_sandbox-0.2.53-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
| Download URL | kern_sandbox-0.2.53-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl |
|---|---|
| Size | 1.9 MB |
| Tags | Linux glibc 2.17+ x86-64 Linux musl 1.1+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
967b2b57caa94dcfc8325ea4537042638b8bd472264e8db9d14c4c394f67ea68
|
|
BLAKE2b-256 checksum How to use checksums |
ec20f83b5225716383b5b3c8c118d05c2c02f4f3ba0866ddb7023dd77cc984df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / kern_sandbox-0.2.53-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
| Download URL | kern_sandbox-0.2.53-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl |
|---|---|
| Size | 1.8 MB |
| Tags | Linux glibc 2.17+ ARM64 Linux musl 1.1+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
848d5b55f30c66af7818fdad90c9866e243310f580f1d6ce88e556448107a3f2
|
|
BLAKE2b-256 checksum How to use checksums |
f88929c71eb5519375324a9cb4e464d9e771f3757cc068162c0dcba7b66f7cfe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / kern_sandbox-0.2.53-py3-none-any.whl
| Download URL | kern_sandbox-0.2.53-py3-none-any.whl |
|---|---|
| Size | 229.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f6a78a2a4f11292dc3e8559088df9bfe0597c6910628ef887a393fcd811b7d42
|
|
BLAKE2b-256 checksum How to use checksums |
1c3ee68ce17b08228d10f980eb8906797ceb609f2d226f96686f14c289440685
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for kern-sandbox 0.2.53
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.