将任何 JavaScript 模块转换为自动反射和类型推断的沙箱 MCP(模型上下文协议)服务器。
MCP 沙箱自动将 JavaScript 模块转换为与 MCP(模型上下文协议)兼容的服务器,使任何 JavaScript 函数都能被 AI 系统访问。它使用 VM 沙箱进行安全隔离、自动类型推断,并生成适当的 MCP 配置。
# 全局安装以供 CLI 使用
npm install -g @mcp-sandbox/cli
# 或在项目中使用
npm install @mcp-sandbox/core @mcp-sandbox/cli
# 启动 JavaScript 模块的 MCP 服务器
$ mcp-sandbox start ./math-utils.js
🏗️ 初始化 MCP 沙箱...
🔍 反射模块
📊 发现 2 个工具:
- circleArea: 计算圆的面积
- fibonacci: 生成斐波那契数列
🚀 MCP 沙箱服务器运行于 http://localhost:3000
📋 MCP 工具:http://localhost:3000/mcp/tools
⚡ MCP 执行:http://localhost:3000/mcp/execute
🔄 MCP SSE:http://localhost:3000/sse
📡 MCP JSON-RPC:http://localhost:3000/mcp/jsonrpc
⚙️ MCP 配置:http://localhost:3000/mcp-config
💡 对于 MCP 检查器,请使用:http://localhost:3000/sse
/**
* 计算圆的面积
* @param radius 圆的半径
*/
function circleArea(radius = 1) {
return Math.PI * radius * radius;
}
/**
* 生成斐波那契数列
* @param count 要生成的斐波那契数的数量
*/
function fibonacci(count = 10) {
const seq = [0, 1];
for (let i = 2; i < count; i++) {
seq[i] = seq[i - 1] + seq[i - 2];
}
return seq.slice(0, count);
}
module.exports = { circleArea, fibonacci };
运行 mcp-sandbox start math-utils.js 自动:
http://localhost:3000 启动 MCP 服务器mcp-config.json 供 MCP 客户端使用服务器暴露了 MCP 和 REST 端点:
POST /mcp/jsonrpc - 主 MCP 端点GET /sse - 实时更新的 Server-Sent EventsGET /tools - 列出可用工具POST /execute/:toolName - 执行特定工具GET /mcp-config - 获取 MCP 服务器配置GET /health - 健康检查# 列出工具
curl http://localhost:3000/tools
# 通过 REST 执行函数
curl -X POST http://localhost:3000/execute/circleArea \
-H "Content-Type: application/json" \
-d '{"args": {"radius": 1}}'
# MCP JSON-RPC 调用
curl -X POST http://localhost:3000/mcp/jsonrpc \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "fibonacci", "arguments": {"count": 8}}}'
import { MCPSandbox } from '@mcp-sandbox/core';
const sandbox = new MCPSandbox({
port: 3000,
timeout: 5000,
});
// 加载并分析模块
await sandbox.loadModule('./my-module.js');
// 启动 MCP 服务器
await sandbox.start();
// 直接执行工具
const result = await sandbox.executeTool('myFunction', {
param1: 'value1',
});
仓库包含多个示例模块,展示了不同的用例:
examples/math-utils.js)examples/string-utils.js)examples/array-utils.js)examples/filesystem-utils.js)# 克隆仓库
git clone https://github.com/danstarns/mcp-sandbox.git
cd mcp-sandbox
# 安装依赖
pnpm install
# 构建所有包
pnpm build
# 运行示例
pnpm example:math # 数学工具
pnpm example:filesystem # 文件操作
pnpm example:string # 字符串操作
# 代码检查和格式化
pnpm lint && pnpm format
mcp-sandbox start <module> [options]
选项:
-p, --port <port> 服务器端口(默认:3000)
-h, --host <host> 服务器主机(默认:localhost)
-t, --timeout <ms> 执行超时(默认:5000ms)
-o, --output <file> 输出 MCP 配置到文件
interface SandboxOptions {
port?: number; // 服务器端口(默认:3000)
host?: string; // 服务器主机(默认:'localhost')
timeout?: number; // 执行超时(默认:5000ms)
maxMemory?: number; // 内存限制(默认:64MB)
}
mcp-sandbox start examples/math-utils.jshttp://localhost:3000/mcp/jsonrpc这是一个包含多个包的单体仓库:
@mcp-sandbox/cli - 命令行界面@mcp-sandbox/core - 核心库@mcp-sandbox/utils - 共享工具MIT 许可证 - 详情见 LICENSE。