/** * 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 + '
  • ').join('\\n'); const html = \` \${SERVER_NAME} — MCP Server

    \${SERVER_NAME}

    ● Live
    Version \${SERVER_VERSION} · \${TOOL_NAMES.length} tools · Powered by MCPEngine

    Available Tools

    POST /mcp — JSON-RPC 2.0 endpoint
    \`; return new Response(html, { headers: { 'Content-Type': 'text/html; charset=utf-8' } }); } // ── Utilities ──────────────────────────────────────────────────────────── function jsonResponse(data, status = 200) { return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', }, }); } // ── Worker entry ───────────────────────────────────────────────────────── export default { async fetch(request, env, ctx) { const url = new URL(request.url); // CORS preflight if (request.method === 'OPTIONS') { return new Response(null, { status: 204, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', }, }); } // Routes if (url.pathname === '/mcp' && request.method === 'POST') { return handleMCPRequest(request); } if (url.pathname === '/health') { return jsonResponse({ status: 'ok', server: SERVER_NAME, tools: TOOL_NAMES.length }); } if (url.pathname === '/') { return infoPage(); } return jsonResponse({ error: 'Not found' }, 404); }, }; `.trimStart(); }