MCP Docker Sandbox Interpreter
MCP Docker 沙盒解释器
针对模型上下文协议 (MCP) 的安全的基于 Docker 的代码执行环境。
概述
该项目提供了一个通过 MCP(模型上下文协议)执行代码的安全沙盒。通过在隔离的 Docker 容器中执行所有代码,它允许 AI 助手安全地运行代码,而无需直接访问主机系统。
graph LR
A[Claude/Cursor] -->|Sends Code| B[MCP Server]
B -->|Executes Code| C[Docker Sandbox]
C -->|Returns Results| ARelated MCP server: SandboxRunner
特征
安全执行:代码在具有严格安全限制的隔离 Docker 容器中运行
多语言支持:目前支持 Python,可轻松扩展其他语言
资源限制:CPU 和内存限制,以防止滥用
MCP 集成:与模型上下文协议完全兼容
自动设置:处理容器创建、依赖项安装和清理
要求
Docker(桌面或引擎)
Python 3.10+
MCP SDK(
pip install mcp)Docker Python SDK(
pip install docker)
安装
克隆此存储库:
git clone https://github.com/yourusername/mcp-docker-interpreter.git cd mcp-docker-interpreter创建并激活虚拟环境:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate安装依赖项:
pip install -r requirements.txt
用法
启动 MCP 服务器
通过运行以下命令启动服务器:
# For Colima users:
export DOCKER_HOST="unix:///Users/username/.colima/default/docker.sock"
# Run the server
uv run mcp dev main.py连接到AI助手
您可以将此 MCP 服务器连接到支持模型上下文协议的 AI 助手:
光标
在 Cursor 中,将以下内容添加到您的 MCP 设置中:
{
"mcpServers": {
"docker-sandbox": {
"command": "python",
"args": ["/absolute/path/to/your/main.py"],
"env": {
"DOCKER_HOST": "unix:///path/to/your/docker.sock"
}
}
}
}用您的实际文件路径替换路径。
克劳德桌面
与 Cursor 类似,将配置添加到 Claude Desktop 的 MCP 设置中。
MCP 工具
该 MCP 服务器公开了三个主要工具:
初始化沙盒:创建一个新的 Docker 容器用于代码执行
Arguments: - image: The Docker image to use (default: "alpine:latest")执行代码:在初始化的沙箱中运行代码
Arguments: - code: The code string to execute - language: Programming language (default: "python")stop_sandbox :停止并删除容器
No arguments needed
工作原理
当调用
initialize_sandbox时,系统:创建基于 Alpine Linux 的 Docker 容器
安装 Python 和其他依赖项
设置安全限制
当调用
execute_code时:代码在隔离容器内执行
捕获标准输出和错误
结果返回给调用应用程序
当调用
stop_sandbox时:容器被停止并移除
所有资源已释放
安全注意事项
该沙盒实施了多项安全措施:
容器的 CPU 和内存使用受到限制
容器以最小权限运行
默认情况下禁用网络访问
容器是一次性的,使用后要清洗干净
发展
项目结构
mcp-docker-interpreter/
├── main.py # Main implementation of MCP server and Docker sandbox
├── requirements.txt # Project dependencies
└── README.md # This file添加新语言支持
要添加对新编程语言的支持,请修改DockerSandbox类中的run_code方法来处理新语言。
故障排除
常见问题
Docker连接错误:
确保 Docker 正在运行
检查 Docker 安装的 DOCKER_HOST 环境变量是否正确设置
容器创建失败:
验证您是否有创建 Docker 容器的权限
确保指定的基础镜像可访问
代码执行失败:
检查语言运行时是否正确安装在容器中
验证代码对于指定语言是否有效
执照
致谢
该项目使用模型上下文协议
This server cannot be deployed
Maintenance
Related MCP Connectors
Run AI agent tasks in isolated sandboxes with a cost cap, saved workflows and scoped connections.
Remote Linux boxes for coding agents: Docker, a browser, screenshots, logs, human takeover.
Real Linux labs your AI agent deploys, routes and runs, with domains, TLS, DBs and an audit log.
Cloud Linux VMs for your agent: run code, Docker, files and HTTPS previews in a Sandboxer microVM.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables LLMs to safely execute code in isolated Docker containers with resource limits and security controls, supporting session management and automatic dependency installation.MIT
- AlicenseAqualityCmaintenanceEnables AI assistants to securely execute Python and C++ code snippets inside disposable, isolated Docker containers and receive structured results.31MIT
- FlicenseNot gradedqualityDmaintenanceEnables AI agents to safely execute Python, JavaScript, and Bash code in an isolated Docker sandbox with strict security constraints.1-
- FlicenseAqualityCmaintenanceA secure, containerized workspace for LLMs to autonomously code, test, and debug projects without risking the host machine.61-