使用说明
MCP沙箱
将任何JavaScript模块转换为具有自动反射和类型推理的沙盒MCP(模型上下文协议)服务器。
🎯 什么是MCP沙盒?
MCP Sandbox会自动将JavaScript模块转换为与MCP(模型上下文协议)兼容的服务器,使AI系统可以访问任何JavaScript函数。它使用VM沙盒进行安全性、自动类型推理,并生成适当的MCP配置。
✨ 特性
- 🔍 自动反射 -分析JS模块并提取函数签名
- 🛡️ 安全沙盒 -在有超时的隔离VM上下文中执行代码
- 🧠 智能类型推断 -从默认值和命名模式中检测参数类型
- 📚 JSDoc集成 -从函数注释中提取文档
- 📡 MCP协议 -完全支持JSON-RPC 2.0和SSE
- 🌐 REST API -遗留REST端点,便于测试
- ⚙️ TypeScript -完全类型安全和IntelliSense支持
🚀 快速开始
安装
# Install globally for CLI usage
npm install -g @mcp-sandbox/cli
# Or use in a project
npm install @mcp-sandbox/core @mcp-sandbox/cli
基本用法
# Start MCP server for a JavaScript module
$ mcp-sandbox start ./math-utils.js
🏗️ Initializing MCP Sandbox...
🔍 Reflecting module
📊 Discovered 2 tools:
- circleArea: Calculate area of a circle
- fibonacci: Generate Fibonacci sequence
🚀 MCP Sandbox server running at http://localhost:3000
📋 MCP Tools: http://localhost:3000/mcp/tools
⚡ MCP Execute: http://localhost:3000/mcp/execute
🔄 MCP SSE: http://localhost:3000/sse
📡 MCP JSON-RPC: http://localhost:3000/mcp/jsonrpc
⚙️ MCP Config: http://localhost:3000/mcp-config
💡 For MCP Inspector, use: http://localhost:3000/sse
示例模块
/**
* Calculate the area of a circle
* @param radius The radius of the circle
*/
function circleArea(radius = 1) {
return Math.PI * radius * radius;
}
/**
* Generate fibonacci sequence
* @param count Number of fibonacci numbers to generate
*/
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 自动:
- 🔍 反映模块并发现功能
- 📊 从参数生成类型架构
- 🚀 在以下位置启动MCP服务器
http://localhost:3000 - 💾 创建
mcp-config.json对于MCP客户
📡 API终点
服务器公开MCP和REST端点:
MCP协议(JSON-RPC 2.0)
POST /mcp/jsonrpc-主MCP端点GET /sse-服务器发送实时更新事件
REST API(用于测试)
GET /tools-列出可用工具POST /execute/:toolName-执行特定工具GET /mcp-config-获取MCP服务器配置GET /health-健康检查
示例用法
# List tools
curl http://localhost:3000/tools
# Execute function via REST
curl -X POST http://localhost:3000/execute/circleArea \
-H "Content-Type: application/json" \
-d '{"args": {"radius": 5}}'
# MCP JSON-RPC call
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,
});
// Load and analyze module
await sandbox.loadModule('./my-module.js');
// Start MCP server
await sandbox.start();
// Execute tools directly
const result = await sandbox.executeTool('myFunction', {
param1: 'value1',
});
📚 示例模块包括
该存储库包括几个演示不同用例的示例模块:
数学运算(examples/math-utils.js)
- 圆面积计算
- 斐波那契数列生成
- 复利计算
- 素数校验
- 度/弧度转换
- 因子计算
字符串操作(examples/string-utils.js)
- 标题案例转换
- 随机字符串生成
- 字数统计
- 回文检测
- 字符串反转
- 大写
阵列操作(examples/array-utils.js)
- 阵法洗牌(费希尔-耶茨)
- 独特价值提取
- 数组分块
- 设置操作(交叉、差异)
- 阵列展平
文件系统操作(examples/filesystem-utils.js)
- 文件读/写(异步)
- 目录列表和创建
- 使用模式搜索文件
- 磁盘使用量计算
- 文件复制和删除
- 逐行读取文件
🛠️ 发展
# Clone repository
git clone https://github.com/danstarns/mcp-sandbox.git
cd mcp-sandbox
# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run examples
pnpm example:math # Math utilities
pnpm example:filesystem # File operations
pnpm example:string # String manipulation
# Lint and format
pnpm lint && pnpm format
🔧 配置选项
CLI选项
mcp-sandbox start <module> [options]
Options:
-p, --port <port> Server port (default: 3000)
-h, --host <host> Server host (default: localhost)
-t, --timeout <ms> Execution timeout (default: 5000ms)
-o, --output <file> Output MCP configuration to file
程序选项
interface SandboxOptions {
port?: number; // Server port (default: 3000)
host?: string; // Server host (default: 'localhost')
timeout?: number; // Execution timeout (default: 5000ms)
maxMemory?: number; // Memory limit (default: 64MB)
}
🔒 安全功能
- VM隔离 -代码在单独的V8上下文中运行
- 执行超时 -可配置的时间限制可防止无限循环
- 内存限制 -防止内存耗尽攻击
- 受控需求 -沙盒中的模块访问受限
- 输入验证 -参数类型检查和验证
🎮 MCP检验员测试
- 启动MCP服务器:
mcp-sandbox start examples/math-utils.js - 打开 MCP检查员
- 将传输类型设置为“流式HTTP”
- 输入URL:
http://localhost:3000/mcp/jsonrpc - 连接并测试您的工具!
📦 包裹
这是一个包含多个包的monorepo:
@mcp-sandbox/cli-命令行界面@mcp-sandbox/core-核心库@mcp-sandbox/utils-共享公用设施
🤝 贡献
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
