Skip to main content
Glama
README.mdโ€ข8.36 kB
# MCP Sandbox [![npm version](https://badge.fury.io/js/@mcp-sandbox%2Fcli.svg)](https://badge.fury.io/js/@mcp-sandbox%2Fcli) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/danstarns/mcp-sandbox/blob/main/LICENSE) <p align=center> <img width="80%" src="https://raw.githubusercontent.com/danstarns/mcp-sandbox/main/docs/banner.png#gh-dark-mode-only" alt="MCP Sandbox"/> </p> **Turn any JavaScript module into a sandboxed MCP (Model Context Protocol) server with automatic reflection and type inference.** ## ๐ŸŽฏ What is MCP Sandbox? MCP Sandbox automatically converts JavaScript modules into MCP (Model Context Protocol) compatible servers, making any JavaScript function accessible to AI systems. It uses VM sandboxing for security, automatic type inference, and generates proper MCP configurations. ## โœจ Features - ๐Ÿ” **Automatic Reflection** - Analyzes JS modules and extracts function signatures - ๐Ÿ›ก๏ธ **Secure Sandboxing** - Executes code in isolated VM contexts with timeouts - ๐Ÿง  **Smart Type Inference** - Detects parameter types from defaults and naming patterns - ๐Ÿ“š **JSDoc Integration** - Extracts documentation from function comments - ๐Ÿ“ก **MCP Protocol** - Full JSON-RPC 2.0 and SSE support - ๐ŸŒ **REST API** - Legacy REST endpoints for easy testing - โš™๏ธ **TypeScript** - Full type safety and IntelliSense support ## ๐Ÿš€ Quick Start ### Installation ```bash # Install globally for CLI usage npm install -g @mcp-sandbox/cli # Or use in a project npm install @mcp-sandbox/core @mcp-sandbox/cli ``` ### Basic Usage ```bash # 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 ``` ### Example Module ```javascript /** * 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 }; ``` Running `mcp-sandbox start math-utils.js` automatically: 1. ๐Ÿ” Reflects the module and discovers functions 2. ๐Ÿ“Š Generates type schemas from parameters 3. ๐Ÿš€ Starts MCP server at `http://localhost:3000` 4. ๐Ÿ’พ Creates `mcp-config.json` for MCP clients ## ๐Ÿ“ก API Endpoints The server exposes both MCP and REST endpoints: ### MCP Protocol (JSON-RPC 2.0) - `POST /mcp/jsonrpc` - Main MCP endpoint - `GET /sse` - Server-Sent Events for real-time updates ### REST API (for testing) - `GET /tools` - List available tools - `POST /execute/:toolName` - Execute a specific tool - `GET /mcp-config` - Get MCP server configuration - `GET /health` - Health check ### Example Usage ```bash # 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}}}' ``` ## ๐Ÿ—๏ธ Programmatic Usage ```typescript 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', }); ``` ## ๐Ÿ“š Example Modules Included The repository includes several example modules demonstrating different use cases: ### Mathematical Operations ([`examples/math-utils.js`](https://github.com/danstarns/mcp-sandbox/blob/main/examples/math-utils.js)) - Circle area calculation - Fibonacci sequence generation - Compound interest calculation - Prime number checking - Degree/radian conversion - Factorial calculation ### String Manipulation ([`examples/string-utils.js`](https://github.com/danstarns/mcp-sandbox/blob/main/examples/string-utils.js)) - Title case conversion - Random string generation - Word counting - Palindrome detection - String reversal - Capitalization ### Array Operations ([`examples/array-utils.js`](https://github.com/danstarns/mcp-sandbox/blob/main/examples/array-utils.js)) - Array shuffling (Fisher-Yates) - Unique value extraction - Array chunking - Set operations (intersection, difference) - Array flattening ### Filesystem Operations ([`examples/filesystem-utils.js`](https://github.com/danstarns/mcp-sandbox/blob/main/examples/filesystem-utils.js)) - File reading/writing (async) - Directory listing and creation - File searching with patterns - Disk usage calculation - File copying and deletion - Line-by-line file reading ## ๐Ÿ› ๏ธ Development ```bash # 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 ``` ## ๐Ÿ”ง Configuration Options ### CLI Options ```bash 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 ``` ### Programmatic Options ```typescript 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) } ``` ## ๐Ÿ”’ Security Features - **VM Isolation** - Code runs in separate V8 contexts - **Execution Timeouts** - Configurable time limits prevent infinite loops - **Memory Limits** - Prevent memory exhaustion attacks - **Controlled Requires** - Limited module access in sandbox - **Input Validation** - Parameter type checking and validation ## ๐ŸŽฎ Testing with MCP Inspector 1. Start your MCP server: `mcp-sandbox start examples/math-utils.js` 2. Open [MCP Inspector](https://github.com/modelcontextprotocol/inspector) 3. Set Transport Type to "Streamable HTTP" 4. Enter URL: `http://localhost:3000/mcp/jsonrpc` 5. Connect and test your tools! ## ๐Ÿ“ฆ Packages This is a monorepo containing multiple packages: - [`@mcp-sandbox/cli`](https://www.npmjs.com/package/@mcp-sandbox/cli) - Command-line interface - [`@mcp-sandbox/core`](https://www.npmjs.com/package/@mcp-sandbox/core) - Core library - [`@mcp-sandbox/utils`](https://www.npmjs.com/package/@mcp-sandbox/utils) - Shared utilities ## ๐Ÿค Contributing Contributions are welcome! Please read our [Contributing Guide](https://github.com/danstarns/mcp-sandbox/blob/main/CONTRIBUTING.md) and check out the [open issues](https://github.com/danstarns/mcp-sandbox/issues). ## ๐Ÿ“„ License MIT License - see [LICENSE](https://github.com/danstarns/mcp-sandbox/blob/main/LICENSE) for details. ## ๐Ÿ”— Links - **GitHub Repository**: [https://github.com/danstarns/mcp-sandbox](https://github.com/danstarns/mcp-sandbox) - **Issues**: [https://github.com/danstarns/mcp-sandbox/issues](https://github.com/danstarns/mcp-sandbox/issues) - **NPM CLI Package**: [https://www.npmjs.com/package/@mcp-sandbox/cli](https://www.npmjs.com/package/@mcp-sandbox/cli) - **NPM Core Package**: [https://www.npmjs.com/package/@mcp-sandbox/core](https://www.npmjs.com/package/@mcp-sandbox/core)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/danstarns/mcp-sandbox'

If you have feedback or need assistance with the MCP directory API, please join our Discord server