Python Exec Sandbox

io.github.lu-zhengda由 GitHub 账号 lu-zhengda 发布v0.1.5更新于 Oct 25, 2026

Sandboxed Python execution for AI agents. PEP 723 inline deps, multi-version Python, zero pollution.

已验证STDIO仅桌面开发者工具安全与监控

概览

AI 生成的概览

本地 MCP 服务器,让 AI 助手在隔离沙箱中运行 Python 脚本,支持 PEP 723 内联依赖和可选 Python 版本。

可以做什么
执行 Python 脚本,自动解析依赖,可附加额外依赖声明并设置单次运行超时,输出会被截断以免淹没助手上下文。在不执行的情况下校验脚本的 PEP 723 元数据和依赖。查看执行环境信息,包括 Python 与 uv 版本、平台、沙箱状态和配置。可在 Python 3.13、3.14 或 3.15 上运行脚本,由 uv 自动下载目标版本。
适用场景
适合助手需要运行 Python 做计算、数据检查或快速实验,又不希望污染本机 Python、site-packages 或虚拟环境的场景。也适合需要每次运行隔离、环境即用即弃和可控超时的工作流。
运行要求
以 stdio 在本地运行,通常通过 uvx 启动。服务器进程需要 Python 3.13+,依赖解析和 Python 版本下载需要 uv。沙箱在 Linux 上需要 bubblewrap,在 macOS 及其他平台需要 Docker;Docker 镜像首次使用时自动拉取。无沙箱后端则两者都不需要。
安装前请注意
无沙箱后端会直接在主机上运行脚本,不建议使用;沙箱工具不可用时服务器会回退到 none 并给出警告。Linux 原生沙箱允许网络访问,脚本可从 PyPI 安装软件包,应留意助手实际运行的内容。Docker 镜像首次使用时会从远程仓库拉取。

安装

在 SourceWeft 中

  1. 打开 控制台中的 Python Exec Sandbox,将其添加到工作区。
  2. 为需要使用其工具的对话启用该服务。

Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。

其他 MCP 客户端

参照 仓库 中的启动说明。

README

mcp-python-exec-sandbox

[CI] [PyPI] [Python] [License]

Sandboxed Python execution for AI agents. Scripts run in ephemeral, isolated environments with inline dependencies (PEP 723) -- zero host pollution, zero leftover venvs, zero package conflicts.

Why?

Every coding agent can already run Python on your host. The problem is what happens next: packages accumulate, venvs sprawl, and a rogue pip install breaks your system. mcp-python-exec-sandbox eliminates this:

  • Scripts execute in a sandbox (bubblewrap on Linux, Docker on macOS/other platforms)
  • Dependencies are declared inline and resolved ephemerally via uv
  • Nothing touches your host's Python, site-packages, or virtualenvs
  • Each execution is isolated and disposable

Features

  • Sandboxed execution -- platform-specific isolation prevents host filesystem access
  • PEP 723 inline metadata -- declare dependencies directly in scripts with # /// script blocks
  • Multi-version Python -- run scripts on Python 3.13, 3.14, or 3.15 (uv downloads the right version automatically)
  • Ephemeral environments -- dependencies are resolved per-execution, never persisted
  • Package caching -- uv's global cache makes repeat installs near-instant
  • Timeout enforcement -- configurable per-execution timeouts
  • Output truncation -- prevents runaway output from overwhelming the agent

Prerequisites

All setups require:

  • Python 3.13+ -- to run the MCP server process
  • uv -- manages script execution, dependency resolution, and Python version downloads. Also provides uvx for running the server without installing it globally.

Additional requirements depend on your chosen sandbox backend:

SetupAdditional requirementsInstall
Native sandbox (Linux)bubblewrapsudo apt install bubblewrap
Docker sandbox (macOS, any)Docker EngineSee Docker docs
No sandboxNone--

Host Python vs. execution Python: These are independent. Python 3.13+ is needed to run the server process itself. The --python-version flag controls which Python version your scripts execute on -- uv downloads the target version automatically. You do not need to install Python 3.14 or 3.15 on your host to run scripts on those versions.

Quick start

Claude Code (Linux -- native sandbox)

bash
claude mcp add python-sandbox -- uvx mcp-python-exec-sandbox

Claude Code (macOS -- Docker sandbox, recommended)

bash
claude mcp add python-sandbox -- uvx mcp-python-exec-sandbox

The Docker sandbox image is pulled automatically from GHCR on first use. No manual build required.

Claude Code (no sandbox)

bash
claude mcp add python-sandbox -- uvx mcp-python-exec-sandbox --sandbox-backend none

Cursor

Add to .cursor/mcp.json (project-level) or ~/.cursor/mcp.json (global):

json
{  "mcpServers": {    "python-sandbox": {      "command": "uvx",      "args": ["mcp-python-exec-sandbox"]    }  }}

OpenAI Codex CLI

bash
codex mcp add python-sandbox -- uvx mcp-python-exec-sandbox

Or add to .codex/config.toml:

toml
[mcp_servers.python-sandbox]command = "uvx"args = ["mcp-python-exec-sandbox"]

Other MCP clients

Any client that supports the MCP stdio transport can use this server:

json
{  "mcpServers": {    "python-sandbox": {      "command": "uvx",      "args": ["mcp-python-exec-sandbox"]    }  }}

Multi-version Python

Use --python-version to target a specific Python version. uv downloads it automatically -- no manual install needed.

bash
# Python 3.13 (default)uvx mcp-python-exec-sandbox --python-version 3.13
# Python 3.14uvx mcp-python-exec-sandbox --python-version 3.14
# Python 3.15uvx mcp-python-exec-sandbox --python-version 3.15

This works across all sandbox backends. The Docker sandbox uses uv inside the container to manage Python versions, so the same --python-version flag applies.

Tools

execute_python

Execute a Python script with automatic dependency management.

ParameterTypeDefaultDescription
scriptstrrequiredPython source code, may include PEP 723 inline metadata
dependencieslist[str][]Extra PEP 508 dependency specifiers to merge
timeout_secondsint30Maximum execution time (1--300)
python
# Simple scriptexecute_python(script="print('hello world')")
# Script with dependenciesexecute_python(    script="import requests; print(requests.get('https://httpbin.org/get').status_code)",    dependencies=["requests"])
# Script with inline PEP 723 metadataexecute_python(script="""# /// script# dependencies = ["pandas", "matplotlib"]# ///
import pandas as pdprint(pd.DataFrame({'a': [1,2,3]}).describe())""")

check_environment

Returns information about the execution environment: Python version, uv version, platform, sandbox status, and configuration.

validate_script

Validates a script's PEP 723 metadata and dependencies without executing it.

ParameterTypeDefaultDescription
scriptstrrequiredPython source code to validate
dependencieslist[str][]Extra dependency specifiers to validate

Sandbox backends

BackendPlatformToolNotes
nativeLinuxbubblewrapNamespace isolation, network allowed
dockerAnyDockerContainer isolation, resource limits
noneAny--No sandboxing (not recommended)

The default backend is native (bubblewrap) on Linux and docker on macOS/other platforms. Specifying --sandbox-backend native on macOS automatically redirects to Docker. If the sandbox tool is unavailable, the server falls back to none with a warning.

Docker sandbox setup

The Docker sandbox image is published to GHCR and pulled automatically when the server starts. No manual setup is needed.

To build locally for development:

bash
docker build -t ghcr.io/lu-zhengda/mcp-python-exec-sandbox profiles/

CLI options

mcp-python-exec-sandbox [OPTIONS]
Options:  --python-version TEXT     Python version for execution (default: 3.13)  --sandbox-backend TEXT    native | docker | none (default: native on Linux, docker on macOS)  --max-timeout INT         Maximum allowed timeout in seconds (default: 300)  --default-timeout INT     Default timeout in seconds (default: 30)  --max-output-bytes INT    Maximum output size in bytes (default: 102400)  --no-warm-cache           Skip cache warming on startup  --uv-path TEXT            Path to uv binary (default: uv)

Development

Setup

bash
git clone https://github.com/lu-zhengda/mcp-python-exec-sandbox.gitcd mcp-python-exec-sandboxuv sync --dev

Project structure

src/mcp_python_exec_sandbox/   # Package source  server.py               # FastMCP server + tool definitions  executor.py             # uv subprocess orchestration  script.py               # PEP 723 metadata parsing/merging  sandbox.py              # Sandbox ABC + factory  sandbox_linux.py        # bubblewrap sandbox (Linux)  sandbox_docker.py       # Docker sandbox (macOS/any)  config.py, cache.py, output.py, errors.pytests/                    # Unit + integration tests (mocked or local uv)e2e_tests/                # End-to-end tests (require uv + network)profiles/                 # Dockerfile, warmup packages.devcontainer/            # Devcontainer for Linux sandbox testing from macOS

Running tests

Unit and integration tests -- fast, run everywhere:

bash
uv run pytest tests/ -v

E2E tests -- require uv and network access. These exercise real script execution, package installation, MCP protocol flow, and sandbox enforcement:

bash
uv run pytest e2e_tests/ -v

Docker sandbox tests

The Docker E2E tests (e2e_tests/test_docker_sandbox.py) verify execution, dependency installation, read-only filesystem enforcement, host isolation, and timeout handling through the Docker backend.

Prerequisites:

  1. Docker must be installed and running
  2. Build the sandbox image:
bash
docker build -t ghcr.io/lu-zhengda/mcp-python-exec-sandbox profiles/

Then run:

bash
uv run pytest e2e_tests/test_docker_sandbox.py -v

These tests are automatically skipped if Docker is unavailable or the image hasn't been built.

Linux sandbox tests (devcontainer)

The Linux sandbox tests (e2e_tests/test_sandbox_enforcement.py::test_linux_sandbox_blocks_etc_shadow) use bubblewrap (bwrap) for namespace isolation. They are skipped on macOS because bwrap is Linux-only.

To run them from macOS, use the included devcontainer which provides Ubuntu 24.04 with bwrap pre-installed:

VS Code:

  1. Install the Dev Containers extension
  2. Open the project and select Reopen in Container
  3. In the integrated terminal:
bash
uv run pytest e2e_tests/test_sandbox_enforcement.py -v

CLI:

bash
# Install the devcontainer CLI (once)npm install -g @devcontainers/cli
# Build and start the containerdevcontainer up --workspace-folder .
# Run the Linux sandbox tests inside the containerdevcontainer exec --workspace-folder . uv run pytest e2e_tests/test_sandbox_enforcement.py -v

Test matrix

Test suiteCommandRequirements
Unit testsuv run pytest tests/ -vuv
Integration testsuv run pytest tests/test_integration.py -vuv
E2E (general)uv run pytest e2e_tests/ -vuv, network
E2E (Docker sandbox)uv run pytest e2e_tests/test_docker_sandbox.py -vuv, Docker, sandbox image
E2E (Linux/bwrap sandbox)uv run pytest e2e_tests/test_sandbox_enforcement.py -vuv, Linux with bwrap (or devcontainer)

Contributing

  • One logical change per commit. Descriptive commit message (imperative mood).
  • Run uv run pytest tests/ -v before committing -- all tests must pass.
  • Add tests for new functionality: unit tests in tests/, E2E in e2e_tests/ if it needs real execution.
  • Keep dependencies minimal. Do not add runtime deps without strong justification.
  • Tool docstrings in server.py are user-facing MCP tool descriptions. Write them for an LLM audience.
  • Sandbox backends must degrade gracefully: if the required tool (bwrap, docker) is missing, fall back to NoopSandbox with a warning.

License

MIT

来源:README.md,提交 ffdee0e

工具

0
SourceWeft 尚未在沙箱中启动过该软件包,因此其工具未知。

版本历史

1
  1. v0.1.5最新Sep 16, 2026

评分

已满足适用检查项的 50%

该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。

  • 近 30 天内可连通25 分

    请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。

  • 已声明认证方式10 分

    请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。

  • 已列出工具15 分

    请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。

  • 描述必需15 分
  • 图标10 分
  • 文档15 分
  • 许可证10 分