多语言代码沙箱

一个本地MCP服务器,允许在隔离的Docker容器中执行多种编程语言代码,提供快速、私密且可定制的代码执行环境。

  • by Pit-CL
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 Pit-CL/mcp-multilang-sandbox

使用说明

MCP多语言沙盒

使用Docker隔离,以6种编程语言安全执行代码,专为 克劳德代码 通过 模型上下文协议(MCP).

这是什么?

一个本地MCP服务器,允许Claude在隔离的Docker容器中执行代码。把它想象成你自己的私人代码沙箱- 100%免费, 100%本地,不依赖云。

为什么使用这个而不是云沙盒?

  • 自由:无每次执行成本(与云服务上约0.10美元/次运行相比)
  • 快速:0ms容器采集与池化(与2-5s冷启动相比)
  • 私人:代码永远不会离开你的机器
  • 可定制的:添加您自己的语言、软件包、安全规则

特性

  • 6种语言:Python、TypeScript、JavaScript、Go、Rust、Bash
  • 容器池:预热容器,便于即时执行
  • 包缓存:安装一次,永远重复使用(基于SHA256)
  • ML运行时:numpy、pandas、sklearn、torch、mlx预装
  • 安全:Seccomp配置文件、功能删除、审计日志记录
  • 会话:具有TTL和自动清理功能的持久状态

快速开始

先决条件

安装

# Clone the repository
git clone https://github.com/Pit-CL/mcp-multilang-sandbox.git
cd mcp-multilang-sandbox

# Install dependencies
npm install

# Build
npm run build

# Run tests (optional)
npm run test:mcp

添加到克劳德代码

# Add as MCP server
claude mcp add multilang-sandbox node /path/to/mcp-multilang-sandbox/dist/mcp/server.js

# Verify it's connected
claude mcp list
# Should show: multilang-sandbox ✓ Connected

手动配置

添加到您的Claude设置(~/.claude.json 或VS代码设置):

{
  "mcpServers": {
    "multilang-sandbox": {
      "command": "node",
      "args": ["/path/to/mcp-multilang-sandbox/dist/mcp/server.js"],
      "env": {
        "LOG_LEVEL": "info"
      }
    }
  }
}

用法

配置后,Claude可以使用这些工具:

执行代码

// Python
sandbox_execute({ language: 'python', code: 'print("Hello!")' })

// TypeScript
sandbox_execute({ language: 'typescript', code: 'console.log("Hello!")' })

// With ML libraries (numpy, pandas, sklearn, torch)
sandbox_execute({
  language: 'python',
  code: 'import numpy as np; print(np.array([1,2,3]))',
  ml: true
})

持续会话

// Create a session
sandbox_session({ action: 'create', name: 'my-project', language: 'python' })

// Execute in session (state persists)
sandbox_execute({ language: 'python', code: 'x = 42', session: 'my-project' })
sandbox_execute({ language: 'python', code: 'print(x)', session: 'my-project' })  // prints 42

// Install packages
sandbox_install({ session: 'my-project', packages: ['pandas', 'requests'] })

// Cleanup
sandbox_session({ action: 'destroy', name: 'my-project' })

文件操作

// Write a file
sandbox_file_ops({ session: 'my-project', operation: 'write', path: 'data.csv', content: 'a,b\n1,2' })

// Read it back
sandbox_file_ops({ session: 'my-project', operation: 'read', path: 'data.csv' })

系统统计信息

// View pool, cache, and session stats
sandbox_inspect({ target: 'all' })

// Security audit
sandbox_security({ action: 'stats' })

MCP工具参考

工具 说明
sandbox_execute 以任何支持的语言执行代码
sandbox_session 创建/列出/暂停/恢复/销毁会话
sandbox_install 安装带有缓存的软件包
sandbox_file_ops 在会话中读/写/列出/删除文件
sandbox_inspect 查看系统统计数据(池、缓存、会话)
sandbox_security 查看审核日志和安全事件

建筑

┌─────────────────────────────────────────────────────────┐
│                    Claude / MCP Client                   │
└───────────────────────────┬─────────────────────────────┘
                            │ JSON-RPC (stdio)
┌───────────────────────────▼─────────────────────────────┐
│                    MCP Sandbox Server                    │
│  ┌────────────────────────────────────────────────────┐ │
│  │  Tools: execute | session | install | file_ops     │ │
│  │         inspect | security                          │ │
│  ├────────────────────────────────────────────────────┤ │
│  │  Core: ContainerPool | PackageCache | Sessions     │ │
│  ├────────────────────────────────────────────────────┤ │
│  │  Security: Seccomp | Capabilities | AuditLogger    │ │
│  ├────────────────────────────────────────────────────┤ │
│  │  Runtimes: Python | TS | JS | Go | Rust | Bash     │ │
│  └────────────────────────────────────────────────────┘ │
└───────────────────────────┬─────────────────────────────┘
                            │ Dockerode
┌───────────────────────────▼─────────────────────────────┐
│                      Docker Engine                       │
│    [Container Pool]  [Active Sessions]  [Image Cache]   │
└─────────────────────────────────────────────────────────┘

安全

6层保护

  1. 代码验证 -模式块列表(操作系统、子进程、eval、exec)
  2. Seccomp配置文件 -按语言筛选系统调用
  3. 能力下降 -CAP_全部删除
  4. 网络隔离 -网络模式:无
  5. 资源限制 -内存、CPU、PID、ulimits
  6. 审计日志 -跟踪所有操作

被阻止的系统调用

ptrace, mount, umount, kexec_load, init_module, delete_module, reboot, bpf, userfaultfd,以及 更多

演出

度量 值
泳池冲击(温暖) 0ms
池漏(冷) ~80-100ms
会话创建 ~85ms
包缓存命中 <1ms
Python执行 ~60ms
Bash执行 ~35ms

发展

# Watch mode (auto-rebuild)
npm run dev

# Type checking
npm run typecheck

# Run tests
npm run test:all        # All tests
npm run test:mcp        # MCP tools (19 tests)
npm run test:runtimes   # Language runtimes

# Clean build
npm run clean && npm run build

项目结构

src/
├── mcp/server.ts           # MCP server & tool handlers
├── core/
│   ├── ContainerPool.ts    # Pre-warmed container pooling
│   ├── PackageCache.ts     # SHA256-based package caching
│   └── SessionManager.ts   # Persistent sessions with TTL
├── security/
│   ├── seccomp.ts          # Syscall filtering profiles
│   └── AuditLogger.ts      # Operation audit logging
├── runtimes/
│   ├── PythonRuntime.ts    # + PythonMLRuntime for ML
│   ├── TypeScriptRuntime.ts
│   ├── JavaScriptRuntime.ts
│   ├── GoRuntime.ts
│   ├── RustRuntime.ts
│   └── BashRuntime.ts
└── docker/
    ├── DockerClient.ts     # Dockerode wrapper
    └── Container.ts        # Container abstraction

贡献

问题和PR欢迎!这最初是一个个人项目,用本地和免费的东西取代云沙盒。

许可证

麻省理工学院

鸣谢

内置于 @模型上下文协议/sdk, Dockode,以及 萨德.