|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * LeanSpec MCP Server Wrapper |
| 4 | + * |
| 5 | + * Spawns the platform-specific Rust `leanspec-mcp` binary and pipes stdio |
| 6 | + * through unchanged. The binary speaks MCP over stdin/stdout, so this |
| 7 | + * wrapper only handles binary discovery — no protocol translation. |
| 8 | + * |
| 9 | + * Discovery order: |
| 10 | + * 1. rust/target/{debug,release}/leanspec-mcp (local dev) |
| 11 | + * 2. @leanspec/mcp-<platform>/leanspec-mcp (npm platform package) |
| 12 | + * 3. ./binaries/<platform>/leanspec-mcp (local binaries fallback) |
| 13 | + */ |
| 14 | + |
| 15 | +import { spawn } from 'child_process'; |
| 16 | +import { createRequire } from 'module'; |
| 17 | +import { fileURLToPath } from 'url'; |
| 18 | +import { dirname, join } from 'path'; |
| 19 | +import { accessSync, openSync, readSync, closeSync } from 'fs'; |
| 20 | + |
| 21 | +const require = createRequire(import.meta.url); |
| 22 | +const __filename = fileURLToPath(import.meta.url); |
| 23 | +const __dirname = dirname(__filename); |
| 24 | + |
| 25 | +const DEBUG = process.env.LEANSPEC_DEBUG === '1'; |
| 26 | +const debug = (...args) => DEBUG && console.error('[leanspec-mcp debug]', ...args); |
| 27 | + |
| 28 | +const PLATFORM_MAP = { |
| 29 | + darwin: { x64: 'darwin-x64', arm64: 'darwin-arm64' }, |
| 30 | + linux: { x64: 'linux-x64' }, |
| 31 | + win32: { x64: 'windows-x64' }, |
| 32 | +}; |
| 33 | + |
| 34 | +const MACHO_MAGICS = new Set([ |
| 35 | + 0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe, 0xcafebabe, 0xbebafeca, |
| 36 | +]); |
| 37 | + |
| 38 | +function readHeaderBytes(filePath) { |
| 39 | + const fd = openSync(filePath, 'r'); |
| 40 | + try { |
| 41 | + const buffer = Buffer.alloc(4); |
| 42 | + const bytesRead = readSync(fd, buffer, 0, 4, 0); |
| 43 | + return bytesRead === 4 ? buffer : null; |
| 44 | + } finally { |
| 45 | + closeSync(fd); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function isValidBinaryHeader(filePath, platform) { |
| 50 | + try { |
| 51 | + const header = readHeaderBytes(filePath); |
| 52 | + if (!header) return false; |
| 53 | + if (platform === 'linux') { |
| 54 | + return header[0] === 0x7f && header[1] === 0x45 && header[2] === 0x4c && header[3] === 0x46; |
| 55 | + } |
| 56 | + if (platform === 'win32') { |
| 57 | + return header[0] === 0x4d && header[1] === 0x5a; |
| 58 | + } |
| 59 | + if (platform === 'darwin') { |
| 60 | + const magicBE = header.readUInt32BE(0); |
| 61 | + const magicLE = header.readUInt32LE(0); |
| 62 | + return MACHO_MAGICS.has(magicBE) || MACHO_MAGICS.has(magicLE); |
| 63 | + } |
| 64 | + return false; |
| 65 | + } catch (error) { |
| 66 | + debug('Failed to read binary header:', error.message); |
| 67 | + return false; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function isExecutableBinary(filePath, platform) { |
| 72 | + if (!isValidBinaryHeader(filePath, platform)) { |
| 73 | + debug('Invalid binary header:', filePath); |
| 74 | + return false; |
| 75 | + } |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +function getBinaryPath() { |
| 80 | + const platform = process.platform; |
| 81 | + const arch = process.arch; |
| 82 | + debug('Platform detection:', { platform, arch }); |
| 83 | + |
| 84 | + const platformKey = PLATFORM_MAP[platform]?.[arch]; |
| 85 | + if (!platformKey) { |
| 86 | + console.error(`Unsupported platform: ${platform}-${arch}`); |
| 87 | + console.error('Supported: macOS (x64/arm64), Linux (x64), Windows (x64)'); |
| 88 | + process.exit(1); |
| 89 | + } |
| 90 | + |
| 91 | + const isWindows = platform === 'win32'; |
| 92 | + const binaryName = isWindows ? 'leanspec-mcp.exe' : 'leanspec-mcp'; |
| 93 | + const packageName = `@leanspec/mcp-${platformKey}`; |
| 94 | + |
| 95 | + // 1. Local Rust build (dev workflow). |
| 96 | + for (const targetDir of ['debug', 'release']) { |
| 97 | + try { |
| 98 | + const rustPath = join(__dirname, '..', '..', '..', 'rust', 'target', targetDir, binaryName); |
| 99 | + debug(`Trying rust ${targetDir} binary:`, rustPath); |
| 100 | + accessSync(rustPath); |
| 101 | + if (isExecutableBinary(rustPath, platform)) { |
| 102 | + debug(`Found rust ${targetDir} binary:`, rustPath); |
| 103 | + return rustPath; |
| 104 | + } |
| 105 | + } catch (e) { |
| 106 | + debug(`Rust ${targetDir} binary not found:`, e.message); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // 2. Platform npm package shared with the CLI distribution. |
| 111 | + try { |
| 112 | + const resolvedPath = require.resolve(`${packageName}/${binaryName}`); |
| 113 | + if (isExecutableBinary(resolvedPath, platform)) { |
| 114 | + debug('Found platform package binary:', resolvedPath); |
| 115 | + return resolvedPath; |
| 116 | + } |
| 117 | + } catch (e) { |
| 118 | + debug('Platform package not found:', packageName, '-', e.message); |
| 119 | + } |
| 120 | + |
| 121 | + // 3. Local binaries directory populated by scripts/copy-rust-binaries.mjs. |
| 122 | + try { |
| 123 | + const localPath = join(__dirname, '..', 'binaries', platformKey, binaryName); |
| 124 | + debug('Trying local binaries path:', localPath); |
| 125 | + accessSync(localPath); |
| 126 | + if (isExecutableBinary(localPath, platform)) { |
| 127 | + debug('Found local binary:', localPath); |
| 128 | + return localPath; |
| 129 | + } |
| 130 | + } catch (e) { |
| 131 | + debug('Local binary not found:', e.message); |
| 132 | + } |
| 133 | + |
| 134 | + console.error(`leanspec-mcp binary not found for ${platform}-${arch}`); |
| 135 | + console.error(''); |
| 136 | + console.error('Install the LeanSpec CLI to get the MCP binary:'); |
| 137 | + console.error(' npm install -g leanspec'); |
| 138 | + console.error(''); |
| 139 | + console.error('Or run from a checkout: `cargo build -p leanspec-mcp --release`.'); |
| 140 | + process.exit(1); |
| 141 | +} |
| 142 | + |
| 143 | +const binaryPath = getBinaryPath(); |
| 144 | +const args = process.argv.slice(2); |
| 145 | +debug('Spawning binary:', binaryPath); |
| 146 | + |
| 147 | +const child = spawn(binaryPath, args, { |
| 148 | + stdio: 'inherit', |
| 149 | + windowsHide: true, |
| 150 | +}); |
| 151 | + |
| 152 | +child.on('exit', (code, signal) => { |
| 153 | + debug('Binary exited:', { code, signal }); |
| 154 | + if (signal) { |
| 155 | + process.kill(process.pid, signal); |
| 156 | + return; |
| 157 | + } |
| 158 | + process.exit(code ?? 1); |
| 159 | +}); |
| 160 | + |
| 161 | +child.on('error', (err) => { |
| 162 | + console.error('Failed to start leanspec-mcp:', err.message); |
| 163 | + debug('Spawn error:', err); |
| 164 | + process.exit(1); |
| 165 | +}); |
| 166 | + |
| 167 | +const forwardSignal = (sig) => { |
| 168 | + try { |
| 169 | + child.kill(sig); |
| 170 | + } catch (e) { |
| 171 | + debug('Signal forward failed:', e.message); |
| 172 | + } |
| 173 | +}; |
| 174 | +process.on('SIGINT', () => forwardSignal('SIGINT')); |
| 175 | +process.on('SIGTERM', () => forwardSignal('SIGTERM')); |
0 commit comments