一个可扩展的消息通信协议(MCP)服务器,提供在隔离沙箱环境中安全执行代码的能力。该服务器遵循MCP标准,使其与桌面版Claude和其他MCP客户端兼容。
服务器采用模块化、可扩展的架构:
├── 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
克隆此仓库:
git clone https://github.com/yourusername/mcp-code-sandbox.git
cd mcp-code-sandbox
设置虚拟环境:
# 使用 venv
python -m venv venv
source venv/bin/activate # 在Windows上:venv\Scripts\activate
# 或者使用 uv(推荐)
uv init
uv venv
source .venv/bin/activate # 在Windows上:.venv\Scripts\activate
安装所需的包:
# 使用 pip
pip install fastmcp python-dotenv e2b-code-interpreter
# 或者使用 uv
uv add fastmcp python-dotenv e2b-code-interpreter
配置环境变量:
# 创建一个包含以下变量的 .env 文件
E2B_API_KEY=your_e2b_api_key_here
INTERPRETER_TYPE=e2b # 默认值,可以更改为其他已实现的解释器
你可以直接从命令行运行服务器:
python main.py
这将使用stdio传输启动服务器,使其与桌面版Claude兼容。
确保你已经安装了最新版本的桌面版Claude
打开你的桌面版Claude配置文件:
~/Library/Application Support/Claude/claude_desktop_config.json%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
服务器提供了以下工具:
系统设计为可扩展。要添加新的代码解释器:
src/sandbox/ 下为你的解释器实现创建一个新的目录src/sandbox/code_interpreter.py 和 src/sandbox/file_interface.py 中定义的接口src/sandbox/interpreter_factory.pyINTERPRETER_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):
# 实现所需的方法
class MyInterpreter(CodeInterpreter):
# 实现所需的方法
# 更新 src/sandbox/interpreter_factory.py 以包含你的新解释器
src/sandbox/)code_interpreter.py:代码解释器的抽象基类file_interface.py:文件操作的抽象接口interpreter_factory.py:创建代码解释器实例的工厂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:主应用程序入口点如果遇到问题: