/** * MCPEngine Studio — Cloudflare Worker Template * * Wraps a compiled MCP server in an HTTP handler: * POST /mcp → MCP JSON-RPC handler * GET /health → 200 OK * GET / → Info page with server name and tool list */ export interface WorkerTemplateParams { serverName: string; serverSlug: string; compiledCode: string; toolNames: string[]; toolCount: number; version?: string; } export function generateWorkerScript(params: WorkerTemplateParams): string { const { serverName, serverSlug, compiledCode, toolNames, toolCount, version = '1.0.0', } = params; const toolListJSON = JSON.stringify(toolNames, null, 2); const escapedName = serverName.replace(/'/g, "\\'"); const builtAt = new Date().toISOString(); return ` // ═══════════════════════════════════════════════════════════════════════════ // ${serverName} — MCPEngine Worker // Deployed via MCPEngine Studio // Built: ${builtAt} | Version: ${version} | Tools: ${toolCount} // ═══════════════════════════════════════════════════════════════════════════ // ── Compiled server code ───────────────────────────────────────────────── ${compiledCode} // ── Tool registry ──────────────────────────────────────────────────────── const SERVER_NAME = '${escapedName}'; const SERVER_SLUG = '${serverSlug}'; const SERVER_VERSION = '${version}'; const TOOL_NAMES = ${toolListJSON}; // ── MCP JSON-RPC handler ───────────────────────────────────────────────── async function handleMCPRequest(request) { try { const body = await request.json(); const { method, params, id } = body; // Standard MCP methods if (method === 'initialize') { return jsonResponse({ jsonrpc: '2.0', id, result: { protocolVersion: '2024-11-05', capabilities: { tools: { listChanged: false } }, serverInfo: { name: SERVER_NAME, version: SERVER_VERSION, }, }, }); } if (method === 'tools/list') { // Delegate to server module if available if (typeof serverModule?.listTools === 'function') { const tools = await serverModule.listTools(); return jsonResponse({ jsonrpc: '2.0', id, result: { tools } }); } return jsonResponse({ jsonrpc: '2.0', id, result: { tools: TOOL_NAMES.map((name) => ({ name, description: name + ' tool', inputSchema: { type: 'object', properties: {} }, })), }, }); } if (method === 'tools/call') { if (typeof serverModule?.callTool === 'function') { const result = await serverModule.callTool(params?.name, params?.arguments ?? {}); return jsonResponse({ jsonrpc: '2.0', id, result }); } return jsonResponse({ jsonrpc: '2.0', id, error: { code: -32601, message: 'Tool not found: ' + (params?.name ?? 'unknown') }, }); } // Notifications (no response needed per spec) if (method === 'notifications/initialized') { return new Response(null, { status: 204 }); } return jsonResponse({ jsonrpc: '2.0', id, error: { code: -32601, message: 'Method not found: ' + method }, }); } catch (err) { return jsonResponse( { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error: ' + err.message } }, 400, ); } } // ── Info page ──────────────────────────────────────────────────────────── function infoPage() { const toolList = TOOL_NAMES.map((t) => '
' + t + '