Skip to main content

MCP Server for Sandboxed Python Code Execution

This MCP server allows your LLM to execute python code securely and returns the results - including files. The code is executed in a configurable sandboxed environment, with strong defaults - like no network access and heavily limited file write permissions.

It's great for use cases complicated use cases where LLMs run into hallucination. For example, stuff that requires a lot of math - where LLMs are notoriously finicky - or if you want to generate cool graphs. If you this, your LLM will always be able to count the number of "r" in strawberry!

Quick Start

For this to run we have to set some security options on the container. This is due to bubblewrap needing to create user, mount and pid namespaces, which the container has to be allowed to do:

  • seccomp=unconfined
  • apparmor=unconfined
  • systempaths=unconfined

This is mandatory - to my knowledge these are the miniumum needed permissions, but feel free to experiment :)

docker run -p 6400:6400 \
  --pids-limit 512 --memory 2g --cpus 2 \
  --security-opt seccomp=unconfined \
  --security-opt apparmor=unconfined \
  --security-opt systempaths=unconfined \
  -e PYTHON_DEPENDENCIES="pydantic numpy" \
  kigstn/mcp-run-isolated-python

You can pass your CLI settings directly after that, the dockerfile uses entrypoint to start the server and listens to all args.

Note:

  • If you are running on Ubuntu 24 you will need to adapt your apparmor profile. We have a script, just run setup_host.sh. More Info
  • These three options only loosen the container. The code your LLM runs is still sandboxed by srt inside it - no network, no writes outside its temp dir, own pid namespace.
  • Docker automatically creates a separate UV python interpreter for the runtime - so you dont have to pass that :)
  • To control your packages, use the env var PYTHON_DEPENDENCIES (space separated list). For greater control you need to build the image yourself
  • We provide a sample docker-compose file, which you can use to run the server with a single command. Just adapt the env vars to your needs.

Configuration

To configure what packages are available to the LLM set PYTHON_DEPENDENCIES as an env var (space seperated list).

docker run ... -e PYTHON_DEPENDENCIES="numpy pandas" kigstn/mcp-run-isolated-python

The server only accepts requests whose Host header is localhost, 127.0.0.1 or ::1 (this blocks DNS rebinding attacks from websites). If clients reach it under another name, e.g. the compose service name from another container, allow that name (comma separated list):

docker run ... -e MCP_ALLOWED_HOSTS="mcp-run-isolated-python,mcp.internal" kigstn/mcp-run-isolated-python

Or bake your own image on top, which is better if you restart often, deploy offline, or want a pinned set:

FROM kigstn/mcp-run-isolated-python
RUN uv pip install --python /sandbox/.venv/bin/python numpy pandas

Build args, if you build the image yourself:

  • ENVIROMENT - Default: "trixie-slim"
  • PYTHON_VERSION - Default: "3.13"
  • PYTHON_DEPENDENCIES - Default: "pydantic" (space separated list)

Consult the Dockerfile for detailed info

MCP - via direct hosting

pip install mcp-run-isolated-python

Then, just run the command to start the server: mcp-run-isolated-python

As a python package

This approached is generally discouraged for any production use, as it removes a lot of this projects security features.

TBH - it should be fine, as this is used by procts such claude code in production on your PC. Just be warned :)

from mcp_run_isolated_python import CodeSandbox, CodeSandboxSettings

settings = CodeSandboxSettings(...)

# sync use
with CodeSandbox(settings=settings) as sandbox:
    result = sandbox.eval("print(1 + 1)")
    print(result)
    
# async use
async with CodeSandbox(settings=settings) as sandbox:
    result = await sandbox.eval("print(1 + 1)")
    print(result)

When using the library like this, it will use your current python interpreter and environment, so you will have to make sure that the packages you need are installed there.


Why this tool?

I built this out of frustration with the existing ecosystem. Most of the existing tools do not set focus on security, which is a no-go if you are living in an enterprise environment or want to use this for more than a single user on your own computer.

Security Considerations

This tool was designed great focus on security - after all, giving an LLM unchecked access to a code executor is quite risky. To harden security, it is heavily recommended to use this server in an isolated container, like docker.

Security Features

  • Use of srt, a shell sandbox build by anthropic to limit LLM access, more info here
    • Remove network access
    • Remove write access to any non-allowed folders
    • Remove read access to specified folders
    • Restrict access to unix sockets
  • Use of docker to isolate the host system from the system where the code is executed
  • Removal of any env variables for the LLM process

Open security concerns

  • Reading of file contents on host system - needs to be restricted on case by case basis using the srt settings

Security Review

This repo has been audited used claude code with opus 5.5 using the cloudflare security assessment skills. Results can be found in the ./security_audits folder.

The only issue found is missing authentication. This is up to the user to configure, MCP support this.

Comparison to (some) other tools

There really are too many to count. I am not including most here, as most simply do not care about sandboxing at all.

Here is what I find to be the most relevant ones with a focus on security.

Name Strong Sandboxing Open Source & Selfhostable Maintained Released Full python & package support File output support
This Project ✅ ✅ ✅ ✅ ✅ ✅
Monty ✅ ✅ ✅ ❌ ❌ ❌
Pydantic MCP Server ✅ ✅ ❌ ❌ ❌ ❌
Sandboxing Service, like Daytona ✅ ❌ ✅ ❌ ✅ ✅
Build-In, like for Gemini ✅ ❌ ✅ ❌ ❌ ✅

Release files for mcp_run_isolated_python 1.1.0

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_run_isolated_python 1.1.0
File Size Uploaded
mcp_run_isolated_python-1.1.0.tar.gz 249.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mcp_run_isolated_python 1.1.0
File Interpreter ABI Platform
mcp_run_isolated_python-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 266.6 kB

Release files / mcp_run_isolated_python-1.1.0.tar.gz

Download URL mcp_run_isolated_python-1.1.0.tar.gz
Size 249.3 kB
Tags Source
SHA-256 checksum
How to use checksums
87d766bba6e081560c08787e0f0d0d22b00fd9ce1493ccff00a47769c5936665
BLAKE2b-256 checksum
How to use checksums
6d3a7ed52d65c4134c0744c2cf6e8da42e25cac6253e96150c05d2819c21b5a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / mcp_run_isolated_python-1.1.0-py3-none-any.whl

Download URL mcp_run_isolated_python-1.1.0-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5309d4967fdfbe75b747c511fb0ede2b0f54cbfb3d5263b4818d9c5c902b6acf
BLAKE2b-256 checksum
How to use checksums
c412d5137a3abefaec1c7b13ba538d6f05fc88105b0ef6b42b020fde51fe0361
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.0

2 release files

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