Note
本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
OpenSandbox 是一个面向 AI 应用的通用沙箱平台(general-purpose sandbox platform),提供多语言 SDK、统一的沙箱 API,以及适用于 Coding Agents、GUI Agents、Agent Evaluation、AI Code Execution、RL Training 等场景的 Docker/Kubernetes 运行时。
Python:
pip install opensandbox
Java/Kotlin (Gradle Kotlin DSL):
dependencies {
implementation("com.alibaba.opensandbox:sandbox:{latest_version}")
}
Java/Kotlin (Maven):
<dependency>
<groupId>com.alibaba.opensandbox</groupId>
<artifactId>sandbox</artifactId>
<version>{latest_version}</version>
</dependency>
JavaScript/TypeScript:
npm install @alibaba-group/opensandbox
C#/.NET:
dotnet add package Alibaba.OpenSandbox
Go:
go get github.com/alibaba/OpenSandbox/sdks/sandbox/go
OpenSandbox 还提供 osb,一款用于常见沙箱工作流的终端 CLI:创建沙箱、运行命令、移动文件、查看诊断信息以及管理运行时出口策略。
安装:
pip install opensandbox-cli
# or
uv tool install opensandbox-cli
快速开始:
osb config init
osb config set connection.domain localhost:8080
osb config set connection.protocol http
osb config set connection.api_key <your-api-key>
osb sandbox create --image python:3.12 --timeout 30m -o json
osb command run <sandbox-id> -o raw -- python -c "print(1 + 1)"
完整命令参考请参阅 CLI README。
OpenSandbox MCP 服务器向 Claude Code、Cursor 等支持 MCP 的客户端暴露沙箱创建、命令执行以及文本文件操作能力。
安装并运行:
pip install opensandbox-mcp
opensandbox-mcp --domain localhost:8080 --protocol http
最小 stdio 配置:
{
"mcpServers": {
"opensandbox": {
"command": "opensandbox-mcp",
"args": ["--domain", "localhost:8080", "--protocol", "http"]
}
}
}
各客户端的配置说明请参阅 MCP README。
环境要求:
uvx opensandbox-server init-config ~/.sandbox.toml --example docker
uvx opensandbox-server
# Show help
# uvx opensandbox-server -h
安装 Code Interpreter SDK
uv pip install opensandbox-code-interpreter
创建沙箱并执行命令与代码。
import asyncio
from datetime import timedelta
from code_interpreter import CodeInterpreter, SupportedLanguage
from opensandbox import Sandbox
from opensandbox.models import WriteEntry
async def main() -> None:
# 1. Create a sandbox
sandbox = await Sandbox.create(
"opensandbox/code-interpreter:v1.1.0",
entrypoint=["/opt/code-interpreter/code-interpreter.sh"],
env={"PYTHON_VERSION": "3.11"},
timeout=timedelta(minutes=10),
)
async with sandbox:
# 2. Execute a shell command
execution = await sandbox.commands.run("echo 'Hello OpenSandbox!'")
print(execution.logs.stdout[0].text)
# 3. Write a file
await sandbox.files.write_files([
WriteEntry(path="/tmp/hello.txt", data="Hello World", mode=644)
])
# 4. Read a file
content = await sandbox.files.read_file("/tmp/hello.txt")
print(f"Content: {content}") # Content: Hello World
# 5. Create a code interpreter
interpreter = await CodeInterpreter.create(sandbox)
# 6. Execute Python code (single-run, pass language directly)
result = await interpreter.codes.run(
"""
import sys
print(sys.version)
result = 2 + 2
result
""",
language=SupportedLanguage.PYTHON,
)
print(result.result[0].text) # 4
print(result.logs.stdout[0].text) # 3.11.14
# 7. Cleanup the sandbox
await sandbox.kill()
if __name__ == "__main__":
asyncio.run(main())
OpenSandbox 提供涵盖 SDK 用法、智能体集成、浏览器自动化与训练工作负载的示例。所有示例代码位于 examples/ 目录。
更多详情请参阅示例文档。
| 目录 | 说明 |
|---|---|
sdks/ |
多语言 SDK(Python、Java/Kotlin、TypeScript/JavaScript、C#/.NET) |
specs/ |
OpenAPI 规范与生命周期(lifecycle)规范 |
server/ |
Python FastAPI 沙箱生命周期服务器 |
cli/ |
OpenSandbox 命令行界面 |
kubernetes/ |
Kubernetes 部署与示例 |
components/execd/ |
沙箱执行守护进程(命令与文件操作) |
components/ingress/ |
沙箱流量入口代理 |
components/egress/ |
沙箱网络出口控制 |
sandboxes/ |
运行时沙箱实现 |
examples/ |
可运行的示例代码 |
docs/examples/ |
示例文档与用例 |
oseps/ |
OpenSandbox Enhancement Proposals |
docs/ |
架构与设计文档 |
tests/ |
跨组件端到端(E2E)测试 |
scripts/ |
开发与维护脚本 |
有关详细架构,请参阅架构。
本项目在 Apache 2.0 License 下开源。
当前项目路线图、规划范围以及路线图条目管理方式,请参阅 ROADMAP.md。