Skip to main content

A secure and sandboxed python code interpreter for LLMs. Accessable via MCP, or directly in code

Project description

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

MCP - via Docker (recommended)

WIP - support is suboptimal, as the anthropic sandbox runtime does currently only run in privileged docker running configs. Waiting on them to add proper support for containers, the existing options does not seem to do anything :D

If you still want to use it, here is the command: docker run --privileged .

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

Note:

  • Docker automatically creates a separate UV python interpreter for the runtime - so you dont have to pass that :)
  • To control your python version & packages, use the docker build args PYTHON_VERSION and PYTHON_DEPENDENCIES (space separated list)

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.

But if you like to live dangerously, or you are the only one using this, or you are building a super quick prototype - this will be fine & should be safe, as it is still using sandboxed code execution.

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)

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_run_isolated_python-0.1.0.tar.gz (113.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_run_isolated_python-0.1.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file mcp_run_isolated_python-0.1.0.tar.gz.

File metadata

  • Download URL: mcp_run_isolated_python-0.1.0.tar.gz
  • Upload date:
  • Size: 113.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for mcp_run_isolated_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa183b23724d0236a0912477b3b916102eeae00bb7c1463d80a2d39eed5c7faf
MD5 b473234e58d26d0e2060e4e8fd636cbd
BLAKE2b-256 f24b15bac559ccad817a37f689891d9d872bd277332b9925e81ba88b91e7a4b0

See more details on using hashes here.

File details

Details for the file mcp_run_isolated_python-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mcp_run_isolated_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

File hashes

Hashes for mcp_run_isolated_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4513930e413062175c65cffa7eee798b9d528449f52f786105d50c1eb71e2772
MD5 f288c2161a6b3909e62aac5475d9aa5b
BLAKE2b-256 af4f7224328547cbdab04421b28dce9b42e30354685dc3401821fedadb156f36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page