代码沙箱服务器

一个可扩展的消息通信协议(MCP)服务器,提供在隔离沙箱环境中的安全代码执行能力。

  • by chrishayuk
  • Python
  • 本地部署
  • 16 星标
  • 更新于
在 GitHub 查看 chrishayuk/mcp-code-sandbox

使用说明

MCP代码沙盒服务器

一种可扩展的消息通信协议(MCP)服务器,在隔离的沙盒环境中提供安全的代码执行功能。此服务器遵循MCP标准,使其与Claude for Desktop和其他MCP客户端兼容。

特性

  • 为代码执行创建隔离的沙盒环境
  • 安全地执行Python代码
  • 执行文件操作(列出、读取、写入)
  • 在沙盒中安装Python包
  • 具有抽象代码解释器接口的可扩展架构
  • 模块化设计,关注点清晰分离

建筑

服务器采用模块化、可扩展的架构构建:

核心组件

  • 抽象解释器接口:允许集成不同的代码执行后端
  • 沙盒管理:用于创建和管理沙盒环境的工具
  • 代码执行:用于运行代码和安装软件包的工具
  • 文件操作:用于管理沙盒中文件的工具

项目结构

├── src/
│   └── sandbox/
│       ├── __pycache__/
│       ├── e2b/
│       │   ├── __pycache__/
│       │   ├── __init__.py
│       │   ├── e2b_file_interface.py
│       │   └── e2b_interpreter.py
│       ├── __init__.py
│       ├── code_interpreter.py
│       ├── file_interface.py
│       └── interpreter_factory.py
├── tools/
│   ├── __pycache__/
│   ├── __init__.py
│   ├── code_execution_tools.py
│   ├── file_tools.py
│   └── sandbox_tools.py
├── main.py
├── .env
├── .gitignore
├── .python-version
├── pyproject.toml
├── README.md
└── uv.lock

先决条件

  • Python 3.10或更高版本
  • E2B API密钥(用于默认的E2B解释器)

安装

  1. 克隆此存储库:

    git clone https://github.com/yourusername/mcp-code-sandbox.git
    cd mcp-code-sandbox
    
  2. 设置虚拟环境:

    # Using venv
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
    # Or using uv (recommended)
    uv init
    uv venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. 安装所需的软件包:

    # Using pip
    pip install fastmcp python-dotenv e2b-code-interpreter
    
    # Or using uv
    uv add fastmcp python-dotenv e2b-code-interpreter
    
  4. 配置环境变量:

    # Create a .env file with the following variables
    E2B_API_KEY=your_e2b_api_key_here
    INTERPRETER_TYPE=e2b  # Default, can be changed to other implemented interpreters
    

用法

独立运行服务器

您可以直接从命令行运行服务器:

python main.py

这将使用stdio传输启动服务器,使其与Claude for Desktop兼容。

使用Claude for Desktop

  1. 确保您安装了最新版本的Claude for Desktop

  2. 打开Claude for Desktop配置文件:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • 窗户: %APPDATA%\Claude\claude_desktop_config.json
  3. 添加您的代码沙盒服务器配置:

    {
      "mcpServers": {
        "code-sandbox": {
          "command": "python",
          "args": [
            "/ABSOLUTE/PATH/TO/main.py"
          ]
        }
      }
    }
    

    或者,如果你正在使用 uv:

    {
      "mcpServers": {
        "code-sandbox": {
          "command": "uv",
          "args": [
            "--directory",
            "/ABSOLUTE/PATH/TO/PROJECT_DIRECTORY",
            "run",
            "main.py"
          ]
        }
      }
    }
    
  4. 保存文件并重新启动Claude for Desktop

可用工具

服务器提供以下工具:

沙盒管理

  • create_sandbox:创建新的沙盒环境
  • 关闭沙箱:关闭并清理沙箱
  • get_sandbox_status:检查沙盒的状态

代码执行

  • execute_code:在沙盒中运行Python代码
  • 安装包:安装Python包
  • create_run_close:创建沙箱、运行代码和清理的多功能工具

文件操作

  • 列表文件:列出沙盒中的文件
  • read_file:读取文件内容
  • write_file:将内容写入文件
  • 上传文件:将文件上传到沙盒

与新口译员一起扩展

该系统被设计为可扩展的。要添加新的代码解释器,请执行以下操作:

  1. 在下创建新目录 src/sandbox/ 用于您的解释器实现
  2. 实现中定义的接口 src/sandbox/code_interpreter.py 和 src/sandbox/file_interface.py
  3. 将新的解释器类型添加到 src/sandbox/interpreter_factory.py
  4. 配置环境变量 INTERPRETER_TYPE 给你的新口译员

实现新解释器的示例:

# src/sandbox/my_backend/my_interpreter.py
from src.sandbox.code_interpreter import CodeInterpreter, ExecutionResult
from src.sandbox.file_interface import FileInterface

class MyFileInterface(FileInterface):
    # Implement the required methods
    
class MyInterpreter(CodeInterpreter):
    # Implement the required methods

# Update src/sandbox/interpreter_factory.py to include your new interpreter

模块说明

沙盒核心(src/sandbox/)

  • code_interpreter.py:代码解释器的抽象基类
  • file_interface.py:文件操作的抽象接口
  • interpreter_factory.py:用于创建代码解释器实例的工厂

E2B实施(src/sandbox/e2b/)

  • e2b_interpreter.py:代码解释器的E2B实现
  • e2b_file_interface.py:文件操作的E2B实现

工具(tools/)

  • sandbox_tools.py:沙盒管理工具
  • code_execution_tools.py:代码执行工具
  • file_tools.py:文件操作工具

主要应用

  • main.py:主要应用程序入口点

故障排除

如果您遇到问题:

  • 确保您有正确的API密钥用于您选择的解释器
  • 检查日志以了解详细的错误消息
  • 验证是否安装了所有必需的软件包
  • 确保Claude for Desktop配置了正确的脚本路径

安全考虑

  • 为了安全起见,代码执行发生在沙盒环境中
  • 请勿在生产环境中使用此服务器执行不受信任的代码
  • 服务器当前未实现身份验证-它只应在受信任的环境中使用

许可证

MIT许可证