沙盒 MCP 服务器
一个 MCP 服务器,提供隔离的 Docker 环境以执行代码。此服务器允许您:
使用任意 Docker 镜像创建容器
使用多种编程语言编写和执行代码
安装软件包并设置开发环境
在隔离的容器中运行命令
先决条件
Python 3.9 或更高版本
Docker 安装并运行
uv 包管理器(推荐)
Docker MCP 服务器(推荐)
Related MCP server: Python MCP Sandbox
安装
克隆此存储库:
git clone <your-repo-url>
cd sandbox_server使用 uv 创建并激活虚拟环境:
uv venv
source .venv/bin/activate # On Unix/MacOS
# Or on Windows:
# .venv\Scripts\activate安装依赖项:
uv pip install .与 Claude Desktop 集成
打开Claude Desktop的配置文件:
macOS:
~/Library/Application Support/Claude/claude_desktop_config.jsonWindows:
%APPDATA%\Claude\claude_desktop_config.json
添加沙盒服务器配置:
{
"mcpServers": {
"sandbox": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/sandbox_server",
"run",
"sandbox_server.py"
],
"env": {
"PYTHONPATH": "/absolute/path/to/sandbox_server"
}
}
}
}将/absolute/path/to/sandbox_server替换为项目目录的实际路径。
重启Claude桌面
使用示例
基本用法
一旦连接到 Claude Desktop,您可以:
创建 Python 容器:
Could you create a Python container and write a simple hello world program?以不同的语言运行代码:
Could you create a C program that calculates the fibonacci sequence and run it?安装包并使用它们:
Could you create a Python script that uses numpy to generate and plot some random data?保存和重现环境
服务器提供了几种保存和重现开发环境的方法:
创建持久容器
创建容器时,您可以使其持久化:
Could you create a persistent Python container with numpy and pandas installed?这将创建一个容器:
Claude Desktop 关闭后仍继续运行
可以通过Docker直接访问
保留所有已安装的软件包和文件
服务器将提供以下说明:
直接访问容器(
docker exec)停止和启动容器
不再需要时删除
保存容器状态
设置好环境后,您可以将其保存为 Docker 镜像:
Could you save the current container state as an image named 'my-ds-env:v1'?这将:
创建一个新的 Docker 镜像,其中包含以下内容:
已安装的软件包
创建的文件
配置更改
提供重复使用环境的说明
然后,您可以共享此图像或将其用作新容器的起点:
Could you create a new container using the my-ds-env:v1 image?生成 Dockerfile
为了使您的环境完全可重现,您可以生成一个 Dockerfile:
Could you export a Dockerfile that recreates this environment?生成的 Dockerfile 将包括:
基础镜像规范
创建的文件
附加设置步骤的模板
您可以使用此 Dockerfile 来执行以下操作:
与他人共享您的环境设置
版本控制您的开发环境
修改和定制构建过程
部署到不同的系统
推荐的工作流程
对于可重复的开发环境:
创建持久容器:
Create a persistent Python container for data science work安装所需的软件包并设置环境:
Install numpy, pandas, and scikit-learn in the container测试您的设置:
Create and run a test script to verify the environment保存状态:
Save this container as 'ds-workspace:v1'导出 Dockerfile:
Generate a Dockerfile for this environment这为您提供了重新创建环境的多种选择:
直接使用保存的Docker镜像
通过修改 Dockerfile 进行构建
如果需要,可以访问原始容器
安全说明
所有代码都在隔离的 Docker 容器中执行
容器使用后自动移除
容器之间的文件系统是隔离的
主机系统访问受到限制
项目结构
sandbox_server/
├── sandbox_server.py # Main server implementation
├── pyproject.toml # Project configuration
└── README.md # This file可用工具
该服务器提供三个主要工具:
create_container_environment:使用指定的镜像创建一个新的 Docker 容器create_file_in_container:在容器中创建文件execute_command_in_container:在容器中运行命令save_container_state:将容器状态保存到持久容器export_dockerfile:导出docker文件以创建持久环境exit_container:完成后关闭容器以清理环境