()
| 11 | let mcpRequestId = 1; |
| 12 | |
| 13 | function startCodeGraphMCP() { |
| 14 | const possiblePaths = [ |
| 15 | path.join(__dirname, '..', '..', 'code-graph-mcp', 'dist', 'index.js'), |
| 16 | path.join(__dirname, '..', 'node_modules', 'budget-aware-mcp', 'dist', 'index.js'), |
| 17 | path.join(__dirname, '..', 'node_modules', '.package-lock.json'), |
| 18 | ]; |
| 19 | |
| 20 | let mcpPath = null; |
| 21 | for (const p of possiblePaths) { |
| 22 | if (fs.existsSync(p)) { mcpPath = p; break; } |
| 23 | } |
| 24 | |
| 25 | if (!mcpPath) { |
| 26 | const linkedPath = path.join(__dirname, '..', 'node_modules', 'budget-aware-mcp'); |
| 27 | if (fs.existsSync(linkedPath)) { |
| 28 | const realPath = fs.realpathSync(linkedPath); |
| 29 | const candidate = path.join(realPath, 'dist', 'index.js'); |
| 30 | if (fs.existsSync(candidate)) mcpPath = candidate; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | if (!mcpPath) { |
| 35 | try { |
| 36 | const { execSync } = require('child_process'); |
| 37 | const globalPath = execSync('npm root -g', { encoding: 'utf-8' }).trim(); |
| 38 | const gp = path.join(globalPath, 'budget-aware-mcp', 'dist', 'index.js'); |
| 39 | if (fs.existsSync(gp)) mcpPath = gp; |
| 40 | } catch {} |
| 41 | } |
| 42 | |
| 43 | if (!mcpPath) return null; |
| 44 | |
| 45 | const child = spawn('node', [mcpPath], { |
| 46 | stdio: ['pipe', 'pipe', 'pipe'], |
| 47 | cwd: process.cwd(), |
| 48 | shell: false, |
| 49 | }); |
| 50 | |
| 51 | child.on('error', () => {}); |
| 52 | child.on('exit', () => { |
| 53 | mcpProcess = null; |
| 54 | if (mcpDemuxer) { try { mcpDemuxer.close(); } catch {} mcpDemuxer = null; } |
| 55 | }); |
| 56 | |
| 57 | mcpProcess = child; |
| 58 | // Single shared line demuxer — replaces the per-request 'data' listener |
| 59 | // pattern that leaked listeners and could resolve a request with another |
| 60 | // request's response bytes under load. |
| 61 | mcpDemuxer = createLineDemuxer(child.stdout); |
| 62 | return child; |
| 63 | } |
| 64 | |
| 65 | async function mcpCall(method, params = {}) { |
| 66 | if (!mcpProcess) return null; |
no test coverage detected