UNPKG

simple-init-service

Version:

MCP server for semantic codebase search using Transformers.js

51 lines (47 loc) 1.38 kB
#!/usr/bin/env node /** * Minimal MCP server implementation * - Content-Length framing * - Responds to 'initialize' method */ console.log("[mcp-server] MCP server started and waiting for commands..."); function sendMessage(msg) { const json = JSON.stringify(msg); const contentLength = Buffer.byteLength(json, 'utf8'); const frame = `Content-Length: ${contentLength}\r\n\r\n${json}`; process.stdout.write(frame); } process.stdin.on('data', (chunk) => { let buffer = chunk; while (true) { const headerEnd = buffer.indexOf('\r\n\r\n'); if (headerEnd === -1) break; const header = buffer.slice(0, headerEnd).toString('utf8'); const m = header.match(/Content-Length:\s*(\d+)/i); if (!m) { buffer = buffer.slice(headerEnd + 4); continue; } const contentLength = parseInt(m[1], 10); const total = headerEnd + 4 + contentLength; if (buffer.length < total) break; const body = buffer.slice(headerEnd + 4, total).toString('utf8'); buffer = buffer.slice(total); let message; try { message = JSON.parse(body); } catch (err) { continue; } if (message.method === 'initialize') { sendMessage({ jsonrpc: '2.0', id: message.id, result: { capabilities: {}, serverInfo: { name: 'mcp-server', version: '1.0.0' } } }); } } });