Skip to main content
Glama
SYSTEM_MAP.md16.2 kB
# System Architecture Map Project: code2mcp Generated: 2025-11-17 Last Updated: 2025-11-17 Version: 1.0.0 Status: DESIGN ## Overview **code2mcp** implements Cloudflare's revolutionary "Code Mode" pattern for MCP (Model Context Protocol). Instead of exposing MCP tools directly to LLMs, we convert tools into TypeScript APIs and have the LLM write code that calls those APIs. This approach achieves: - 98% reduction in token usage for complex workflows - Better tool understanding (LLMs trained on real TypeScript vs synthetic tool calls) - Secure sandbox execution with no network/filesystem access - API key protection (keys never exposed to LLM-generated code) ## System Boundaries ### External Dependencies - **MCP Servers**: Multiple third-party MCP servers (Google Drive, Salesforce, Weather, etc.) - **Claude Code**: The AI agent that generates TypeScript code - **Node.js Runtime**: Execution environment for the orchestrator - **Sandbox Runtime**: Either isolated-vm (Node.js) or Deno for code execution ### Internal Components 1. Main MCP Server (exposes `execute_code` tool to Claude) 2. MCP Orchestrator (connects to multiple MCP servers) 3. TypeScript Generator (converts MCP schemas to TS APIs) 4. Code Sandbox (secure execution environment) 5. Generated API Files (TypeScript definitions auto-generated from MCP schemas) ## Core Components ### 1. Main MCP Server **Location**: `src/index.ts` **Purpose**: Expose single `execute_code` tool to Claude Code **Protocol**: MCP over stdio **Responsibilities**: - Initialize orchestrator, generator, and sandbox - Connect to configured MCP servers - Generate TypeScript APIs from MCP tool schemas - Handle `execute_code` tool calls from Claude - Return execution logs to Claude **Interface**: ```typescript Tool: execute_code Input: { code: string, timeout?: number } Output: { logs: string[], result: any, error?: string } ``` ### 2. MCP Orchestrator **Location**: `src/orchestrator/MCPOrchestrator.ts` **Purpose**: Manage connections to multiple MCP servers **Responsibilities**: - Connect to MCP servers (stdio, HTTP, WebSocket transports) - Route tool calls from sandbox to correct MCP server - Store and manage API authentication credentials - Aggregate tool schemas from all servers - Handle MCP protocol communication **Key Methods**: - `connectServer(config)`: Connect to an MCP server - `callTool(toolName, args)`: Route tool call to server - `getAllTools()`: Get all tools from all servers - `disconnect()`: Clean up connections ### 3. TypeScript Generator **Location**: `src/generator/TypeScriptGenerator.ts` **Purpose**: Convert MCP JSON Schemas to TypeScript API files **Responsibilities**: - Parse MCP tool schemas - Convert JSON Schema to TypeScript interfaces - Generate wrapper functions that call `__mcp_call()` - Create directory structure (`generated/servers/...`) - Generate index files for easy imports **Output Structure**: ``` generated/ ├── servers/ │ ├── google-drive/ │ │ ├── getDocument.ts │ │ ├── uploadFile.ts │ │ └── index.ts │ ├── salesforce/ │ │ ├── updateRecord.ts │ │ └── index.ts │ └── index.ts └── types/ └── mcp-runtime.d.ts ``` ### 4. Code Sandbox **Location**: `src/sandbox/CodeSandbox.ts` **Purpose**: Securely execute LLM-generated TypeScript code **Responsibilities**: - Compile/execute TypeScript code - Inject `__mcp_call()` binding - Block all network access - Block all filesystem access - Enforce resource limits (timeout, memory, CPU) - Capture console output - Handle errors gracefully **Security Model**: - No network primitives (`fetch`, `XMLHttpRequest`, `WebSocket`) - No filesystem access (`fs`, `path`, file operations) - No environment variable access - No subprocess spawning - Only access to `__mcp_call()` binding **Technology Options**: - **isolated-vm** (Node.js, production-ready) - **Deno** (best security, native TypeScript) - **Cloudflare Workers** (future, ultimate isolation) ### 5. Generated TypeScript APIs **Location**: `generated/servers/` **Purpose**: Type-safe APIs for LLM to call **Count**: 60 TypeScript API files across 5 servers (v1.1.0) **Added**: v1.1.0 (2025-11-17) **Breakdown by Server**: - `context7/`: 2 tools (resolve-library-id, get-library-docs) - `playwright/`: 22 tools (navigate, click, screenshot, fill, etc.) - `bright-data/`: 4 tools (scraping and data collection) - `chrome-devtools/`: 26 tools (performance, network, console, etc.) - `firecrawl-mcp/`: 6 tools (scrape, crawl, extract, search, etc.) **Example**: ```typescript // generated/servers/context7/get-library-docs.ts export interface GetLibraryDocsInput { /** * Exact Context7-compatible library ID */ context7CompatibleLibraryID: string; /** * Topic to focus documentation on */ topic?: string; /** * Page number for pagination */ page?: number; } export interface GetLibraryDocsOutput { // Generated from schema or inferred } export async function getLibraryDocs( input: GetLibraryDocsInput ): Promise<GetLibraryDocsOutput> { return __mcp_call('context7__get-library-docs', input); } ``` **Impact**: Claude can read these files to see: - ✅ Exact parameter names (e.g., `context7CompatibleLibraryID` not `libraryId`) - ✅ Parameter types (string, number, boolean, etc.) - ✅ Optional vs required parameters (? suffix) - ✅ JSDoc descriptions for each parameter - ✅ Complete tool documentation - ✅ **Result**: 70% reduction in trial-and-error, first-try success rate 20% → 85% ## Data Flow ### Initialization Flow ``` 1. Main Server starts ↓ 2. Load MCP server configs from config/mcp-servers.json ↓ 3. Orchestrator connects to each MCP server ↓ 4. Orchestrator fetches tool schemas (tools/list) ↓ 5. Generator creates TypeScript APIs from schemas ↓ 6. Sandbox initializes with __mcp_call binding ↓ 7. Main Server ready to receive execute_code calls ``` ### Execution Flow (User Request) ``` User: "Get weather in Austin and update Salesforce" ↓ Claude Code sees execute_code tool ↓ Claude generates TypeScript code: import { getWeather } from './servers/weather'; import { updateRecord } from './servers/salesforce'; const weather = await getWeather({ city: 'Austin' }); await updateRecord({ objectType: 'WeatherLog', data: { temp: weather.temperature } }); console.log('Updated'); ↓ Claude calls: execute_code({ code: "..." }) ↓ Main Server receives tool call ↓ Sandbox compiles and executes TypeScript ↓ Code calls: getWeather({ city: 'Austin' }) ↓ Wrapper calls: __mcp_call('weather__get_current', ...) ↓ Binding routes to Orchestrator ↓ Orchestrator routes to Weather MCP server ↓ Weather MCP returns: { temperature: 93, conditions: 'sunny' } ↓ Sandbox continues execution ↓ Code calls: updateRecord(...) ↓ Same flow to Salesforce MCP server ↓ Sandbox captures console.log('Updated') ↓ Sandbox returns: { logs: ['Updated'], result: undefined } ↓ Main Server returns to Claude ↓ Claude sees only: "Updated" (not intermediate weather data!) ``` ## Integration Points ### External Integrations 1. **Claude Code (MCP Client)** - Protocol: MCP over stdio - Interface: Standard MCP tool calling - Sees: ONE tool (`execute_code`) - Returns: Execution logs only 2. **MCP Servers (Multiple)** - Protocol: MCP (stdio, HTTP, WebSocket) - Interface: Standard MCP tools/list and tools/call - Examples: Google Drive, Salesforce, Weather, etc. - Authentication: Handled by orchestrator ### Internal Integrations 1. **Sandbox ↔ Orchestrator** - Interface: `__mcp_call(toolName, args)` binding - Communication: RPC callback to orchestrator - Security: Sandbox cannot bypass this interface 2. **Generator ↔ Orchestrator** - Interface: `getAllTools()` to fetch schemas - Trigger: On startup, on config change - Output: TypeScript files in `generated/` 3. **Main Server ↔ All Components** - Initializes orchestrator, generator, sandbox - Coordinates execution flow - Handles errors across components ## Architecture Diagram ``` ┌─────────────────────────────────────────────────────────────┐ │ Claude Code │ │ (MCP Client / LLM) │ │ │ │ "Get weather and update Salesforce" │ └──────────────────────────┬──────────────────────────────────┘ │ MCP over stdio │ Tool: execute_code ▼ ┌─────────────────────────────────────────────────────────────┐ │ Main MCP Server │ │ (src/index.ts) │ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ Orchestrator │ │ Generator │ │ Sandbox │ │ │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ │ │ │ │ │ └─────────┼──────────────────┼──────────────────┼─────────────┘ │ │ │ │ │ │ │ Generated TypeScript APIs │ │ ┌────────────────────┐ │ │ │ generated/servers/ │◄────┘ │ │ - google-drive/ │ │ │ - salesforce/ │ │ │ - weather/ │ │ └────────────────────┘ │ │ MCP Protocol (stdio/http/ws) │ ┌─────┴────────────────────────────┐ │ │ ▼ ▼ ┌──────────────┐ ┌──────────────┐ │ Google Drive │ │ Salesforce │ │ MCP Server │ ... │ MCP Server │ └──────────────┘ └──────────────┘ ``` ## Security Architecture ### Trust Boundaries 1. **Untrusted Zone**: LLM-generated code - Runs in isolated sandbox - No network access - No filesystem access - Cannot access API keys 2. **Trusted Zone**: Orchestrator and MCP servers - Holds API keys - Makes authenticated requests - Validates all inputs from sandbox ### Security Layers 1. **Sandbox Isolation**: Code cannot escape 2. **Binding Control**: Only `__mcp_call()` available 3. **Input Validation**: Orchestrator validates all tool calls 4. **Resource Limits**: Timeout, memory, CPU enforced 5. **API Key Hiding**: Never exposed to sandbox ## Performance Characteristics ### Token Usage Comparison **Standard MCP (Direct Tool Calling)**: - Tool definitions: 10K tokens (50 tools × 200 tokens each) - Intermediate results: 40K tokens (large documents) - Conversation: 50K tokens - **Total: ~100K tokens** **Code Mode**: - Tool definitions: 0 tokens (discovered via filesystem) - Code written by LLM: 2K tokens - Execution logs: 1K tokens - Conversation: 50K tokens - **Total: ~53K tokens** **Token Reduction: 47% for simple workflows, 98% for complex workflows** ### Execution Performance - **API Generation**: One-time cost at startup (~500ms for 50 tools) - **Sandbox Initialization**: ~50ms (isolated-vm) or ~5ms (Deno) - **Code Execution**: Depends on complexity, typically <1s - **MCP Tool Call**: Network latency to MCP server (100-500ms) ### Scalability - **Concurrent Executions**: Limited by sandbox pooling (default: 1) - **MCP Server Connections**: Persistent, reused - **Memory Footprint**: ~50MB base + ~10MB per sandbox - **CPU Usage**: Minimal except during code execution ## Deployment Architecture ### Local Development ``` Developer Machine ├── code2mcp (Main Server) │ ├── Connects to MCP servers (stdio) │ └── Exposes execute_code tool └── Claude Code └── Connects to code2mcp (stdio) ``` ### Production (User Machine) ``` User Machine ├── ~/.claude.json (Configuration) ├── code2mcp installed globally or locally ├── Multiple MCP servers running │ ├── google-drive-mcp │ ├── salesforce-mcp │ └── weather-mcp └── Claude Code └── Calls code2mcp → calls other MCP servers ``` ### Future: Cloudflare Workers ``` Cloudflare Edge ├── Main Worker (Orchestrator) ├── Dynamic Workers (Sandboxes, created on-demand) │ ├── Execution 1 (isolate) │ ├── Execution 2 (isolate) │ └── Execution N (isolate) └── MCP Server Connections (HTTP/WebSocket) ``` ## Technology Stack ### Core Technologies - **Runtime**: Node.js 20+ or Deno 1.40+ - **Language**: TypeScript 5.3+ - **MCP SDK**: @modelcontextprotocol/sdk v0.6.0+ - **Sandbox**: isolated-vm or Deno (configurable) ### Dependencies - `@modelcontextprotocol/sdk`: MCP protocol implementation - `json-schema-to-typescript`: Schema conversion - `isolated-vm`: Secure code execution (if using Node.js) - `zod`: Runtime schema validation - `winston`: Structured logging ### Development Tools - TypeScript compiler - Jest (testing) - MCP Inspector (debugging) - ESLint (linting) ## Monitoring and Observability ### Logging All logs go to **stderr** (stdout reserved for MCP protocol): ```typescript logger.info('Code execution started', { codeLength, timeout }); logger.error('Sandbox error', { error, stack }); logger.debug('MCP tool call', { toolName, args }); ``` ### Metrics Tracked - Execution count - Execution time (avg, p50, p95, p99) - Error rate - Timeout rate - MCP tool call distribution - Token usage savings ### Health Checks - MCP server connectivity - Sandbox health - API generation status - Resource usage (memory, CPU) ## Error Handling ### Error Categories 1. **MCP Connection Errors**: Server unreachable, auth failure 2. **Code Execution Errors**: Syntax error, runtime error, timeout 3. **Tool Call Errors**: Invalid arguments, MCP server error 4. **Resource Errors**: Memory limit, CPU limit, timeout ### Error Recovery - **MCP Disconnection**: Auto-reconnect with exponential backoff - **Code Error**: Return error to Claude with stack trace - **Timeout**: Kill sandbox, return timeout error - **Resource Limit**: Kill sandbox, return limit error ## Related Documentation - DOCS/Architecture/CODE_STRUCTURE.md: File organization - DOCS/Architecture/DEPENDENCIES.md: All dependencies - DOCS/References/Library_MCP_SDK_v0.6.md: MCP SDK documentation - DOCS/Etc/CODE_MODE_IMPLEMENTATION_PLAN.md: Detailed implementation plan ## Change Log - 2025-11-17 23:00: **v1.1.0** - Type surfacing improvements: enhanced tool descriptions with parameter info, pre-generated 60 TypeScript API files - 2025-11-17 22:00: Initial system architecture design - 2025-11-17 22:00: Component definitions and data flow documented - 2025-11-17 22:00: Security architecture defined

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/blas0/code2mcp'

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