MCP Docker Sandbox Interpreter

Integrations

  • Uses Alpine Linux as the base container image for the secure execution environment

  • Provides a secure sandbox for executing code within isolated Docker containers, with resource limitations and security restrictions

  • Includes Mermaid diagram support for visualizing the MCP server architecture and workflow

MCP Docker 沙盒解释器

针对模型上下文协议 (MCP) 的安全的基于 Docker 的代码执行环境。

概述

该项目提供了一个通过 MCP(模型上下文协议)执行代码的安全沙盒。通过在隔离的 Docker 容器中执行所有代码,它允许 AI 助手安全地运行代码,而无需直接访问主机系统。

特征

  • 安全执行:代码在具有严格安全限制的隔离 Docker 容器中运行
  • 多语言支持:目前支持 Python,可轻松扩展其他语言
  • 资源限制:CPU 和内存限制,以防止滥用
  • MCP 集成:与模型上下文协议完全兼容
  • 自动设置:处理容器创建、依赖项安装和清理

要求

  • Docker(桌面或引擎)
  • Python 3.10+
  • MCP SDK( pip install mcp
  • Docker Python SDK( pip install docker

安装

  1. 克隆此存储库:
    git clone https://github.com/yourusername/mcp-docker-interpreter.git cd mcp-docker-interpreter
  2. 创建并激活虚拟环境:
    python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
  3. 安装依赖项:
    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 服务器公开了三个主要工具:

  1. 初始化沙盒:创建一个新的 Docker 容器用于代码执行
    Arguments: - image: The Docker image to use (default: "alpine:latest")
  2. 执行代码:在初始化的沙箱中运行代码
    Arguments: - code: The code string to execute - language: Programming language (default: "python")
  3. stop_sandbox :停止并删除容器
    No arguments needed

工作原理

  1. 当调用initialize_sandbox时,系统:
    • 创建基于 Alpine Linux 的 Docker 容器
    • 安装 Python 和其他依赖项
    • 设置安全限制
  2. 当调用execute_code时:
    • 代码在隔离容器内执行
    • 捕获标准输出和错误
    • 结果返回给调用应用程序
  3. 当调用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方法来处理新语言。

故障排除

常见问题

  1. Docker连接错误
    • 确保 Docker 正在运行
    • 检查 Docker 安装的 DOCKER_HOST 环境变量是否正确设置
  2. 容器创建失败
    • 验证您是否有创建 Docker 容器的权限
    • 确保指定的基础镜像可访问
  3. 代码执行失败
    • 检查语言运行时是否正确安装在容器中
    • 验证代码对于指定语言是否有效

执照

MIT 许可证

致谢

-
security - not tested
F
license - not found
-
quality - not tested

基于 Docker 的安全环境,通过在隔离的容器中运行所有代码,允许 AI 助手安全地执行代码,而无需直接访问主机系统。

  1. Overview
    1. Features
      1. Requirements
        1. Installation
          1. Usage
            1. Starting the MCP Server
            2. Connecting to an AI Assistant
            3. MCP Tools
          2. How It Works
            1. Security Considerations
              1. Development
                1. Project Structure
                2. Adding New Language Support
              2. Troubleshooting
                1. Common Issues
              3. License
                1. Acknowledgements
                  ID: fqh4e3dpgp