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 解释器)
安装
- 克隆此存储库:
git clone https://github.com/yourusername/mcp-code-sandbox.git
cd mcp-code-sandbox
- 设置虚拟环境:
# 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
- 安装所需的软件包:
# Using pip
pip install fastmcp python-dotenv e2b-code-interpreter
# Or using uv
uv add fastmcp python-dotenv e2b-code-interpreter
- 配置环境变量:
# 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
用法
独立运行服务器
您可以直接从命令行运行服务器:
这将使用 stdio 传输启动服务器,使其与 Claude for Desktop 兼容。
与 Claude for Desktop 一起使用
- 确保您安装了最新版本的 Claude for Desktop
- 打开您的 Claude for Desktop 配置文件:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json
- Windows:
%APPDATA%\Claude\claude_desktop_config.json
- 添加您的代码沙盒服务器配置:
{
"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"
]
}
}
}
- 保存文件并重新启动 Claude for Desktop
可用工具
该服务器提供以下工具:
沙盒管理
- create_sandbox :创建一个新的沙盒环境
- close_sandbox :关闭并清理沙盒
- get_sandbox_status :检查沙箱的状态
代码执行
- 执行代码:在沙箱中运行 Python 代码
- install_package :安装 Python 包
- create_run_close :创建沙盒、运行代码和清理的一体化工具
文件操作
- list_files :列出沙盒中的文件
- read_file :读取文件的内容
- write_file :将内容写入文件
- upload_file :将文件上传到沙盒
使用新的解释器进行扩展
该系统设计为可扩展的。要添加新的代码解释器:
- 在
src/sandbox/
下为您的解释器实现创建一个新目录 - 实现
src/sandbox/code_interpreter.py
和src/sandbox/file_interface.py
中定义的接口 - 将新的解释器类型添加到
src/sandbox/interpreter_factory.py
- 将环境变量
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 文件操作实现
sandbox_tools.py
:沙盒管理工具code_execution_tools.py
:代码执行工具file_tools.py
:文件操作工具
主要应用
故障排除
如果您遇到问题:
- 确保您拥有所选解释器的正确 API 密钥
- 检查日志以获取详细的错误消息
- 验证所有必需的软件包都已安装
- 确保 Claude for Desktop 配置了正确的脚本路径
安全注意事项
- 为了安全起见,代码执行在沙盒环境中进行
- 不要使用此服务器在生产环境中执行不受信任的代码
- 该服务器目前未实现身份验证 - 它只能在受信任的环境中使用它
执照
MIT 许可证