Python沙箱执行服务

PRIMS是一个开源的Python运行时解释器MCP服务器,允许LLM代理在安全的一次性沙箱中运行任意Python代码。

  • by hileamlakB
  • Python
  • 本地部署
  • 30 星标
  • 更新于
在 GitHub 查看 hileamlakB/PRIMS

使用说明

PRIMS–Python运行时解释器MCP服务器

PRIMS是一个小型开源软件 模型上下文协议(MCP) 该服务器允许LLM代理在安全的一次性沙箱中运行任意Python代码。

• 一个工具,一份工作。 展示一个MCP工具—— run_code –执行用户提供的Python并流式返回 stdout / stderr.

• 分离和可重复。 每次调用都会启动一个新的虚拟环境,安装任何请求的pip包,挂载可选的只读文件,然后清空工作区。

• 零配置。 在MCP/stdio上工作或将其放入Docker中。


快速启动

1.当地发展环境

chmod +x scripts/setup_env.sh   # once, to make the script executable
./scripts/setup_env.sh          # creates .venv & installs deps

# activate the venv in each new shell
source .venv/bin/activate

2.启动服务器

python -m server.main         # binds http://0.0.0.0:9000/mcp

3.Docker

# Quick one-liner (build + run)
chmod +x scripts/docker_run.sh
./scripts/docker_run.sh         # prints the MCP URL when ready

例子

列出可用工具

您可以使用提供的脚本列出服务器公开的所有工具:

python examples/list_tools.py

预期输出(工具名称和描述可能有所不同):

Available tools:
- run_code: Execute Python code in a secure sandbox with optional dependencies & file mounts.
- list_dir: List files/directories in your session workspace.
- preview_file: Preview up to 8 KB of a text file from your session workspace.
- persist_artifact: Upload an output/ file to a presigned URL for permanent storage.
- mount_file: Download a remote file once per session to `mounts/<path>`.

通过MCP服务器运行代码

python examples/run_code.py

挂载一次数据集并重用它

python examples/mount_and_run.py

这将装载一个CSV mount_file 然后把它读进去 run_code 而无需重新提供URL。

检查您的会话工作区

python examples/inspect_workspace.py

这显示了如何使用 list_dir 和 preview_file 浏览代码创建的文件的工具。

将工件永久存储

这 persist_artifact 该工具从您的 output/ 将目录转换为预签名的URL。

示例(Python):

await client.call_tool("persist_artifact", {
    "relative_path": "plots/plot.png",
    "presigned_url": "https://bucket.s3.amazonaws.com/...signature...",
})

下载工件

可以直接提取小工件:

curl -H "mcp-session-id: <your-session-id>" \
     http://localhost:9000/artifacts/plots/plot.png -o plot.png

可用工具

工具 目的
run_code 在带有可选pip deps的隔离沙箱中执行Python。
list_dir 列出会话工作区内的文件/目录。
preview_file 返回高达8KB的文本文件,以便快速检查。
persist_artifact 上传 output/ 将文件发送到客户端提供的预签名URL
mount_file 每个会话下载一次远程文件到 mounts/<path>.

请参阅 examples/ 端到端演示目录。

贡献

欢迎投稿!请随时打开问题、建议功能或提交拉取请求,以帮助改进PRIMS。

如果你觉得这个项目有用,请考虑留下一个⭐ 以示支持。