|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { createServer } from 'http' |
| 4 | +import { styleText } from 'node:util' |
| 5 | +import { execa } from 'execa' |
| 6 | +import getPort from 'get-port' |
| 7 | +import httpProxy from 'http-proxy' |
| 8 | + |
| 9 | +const { createProxyServer } = httpProxy |
| 10 | + |
| 11 | +const [, , ...args] = process.argv |
| 12 | +const [transport] = args |
| 13 | + |
| 14 | +const serverPort = await getPort({ |
| 15 | + port: 10000, |
| 16 | + exclude: [process.env.PORT].filter(Boolean).map(Number), |
| 17 | +}) |
| 18 | +const clientPort = await getPort({ |
| 19 | + port: 9000, |
| 20 | + exclude: [process.env.PORT, serverPort].filter(Boolean).map(Number), |
| 21 | +}) |
| 22 | + |
| 23 | +// Spawn mcp-inspector as a sidecar process |
| 24 | +const inspectorProcess = execa('mcp-inspector', [], { |
| 25 | + env: { |
| 26 | + ...process.env, |
| 27 | + SERVER_PORT: serverPort, |
| 28 | + CLIENT_PORT: clientPort, |
| 29 | + }, |
| 30 | + stdio: ['inherit', 'pipe', 'inherit'], // capture stdout |
| 31 | +}) |
| 32 | + |
| 33 | +// Wait for the inspector to be up before starting the proxy server |
| 34 | +function waitForInspectorReady() { |
| 35 | + return new Promise((resolve) => { |
| 36 | + inspectorProcess.stdout.on('data', (data) => { |
| 37 | + const str = data.toString() |
| 38 | + // Suppress specific logs from inspector |
| 39 | + if ( |
| 40 | + str.includes('Proxy server listening on port') || |
| 41 | + str.includes('MCP Inspector is up and running') |
| 42 | + ) { |
| 43 | + // Do not print these lines |
| 44 | + if (str.includes('MCP Inspector is up and running')) { |
| 45 | + resolve() |
| 46 | + } |
| 47 | + return |
| 48 | + } |
| 49 | + process.stdout.write(str) // print all other inspector logs |
| 50 | + }) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +await waitForInspectorReady() |
| 55 | + |
| 56 | +const proxy = createProxyServer({ |
| 57 | + target: `http://localhost:${clientPort}`, |
| 58 | + ws: true, |
| 59 | + changeOrigin: true, |
| 60 | +}) |
| 61 | + |
| 62 | +const server = createServer((req, res) => { |
| 63 | + if (req.url === '/' || req.url.startsWith('/?')) { |
| 64 | + const url = new URL(req.url, `http://localhost:${clientPort}`) |
| 65 | + const transport = 'stdio' |
| 66 | + const command = 'npm' |
| 67 | + const args = `--silent --prefix "${process.cwd()}" run dev:mcp` |
| 68 | + url.searchParams.set('transport', transport) |
| 69 | + url.searchParams.set('serverCommand', command) |
| 70 | + url.searchParams.set('serverArgs', args) |
| 71 | + const correctedUrl = url.pathname + url.search |
| 72 | + if (correctedUrl !== req.url) { |
| 73 | + res.writeHead(302, { Location: correctedUrl }) |
| 74 | + res.end() |
| 75 | + return |
| 76 | + } |
| 77 | + } |
| 78 | + proxy.web(req, res, {}, (err) => { |
| 79 | + res.writeHead(502, { 'Content-Type': 'text/plain' }) |
| 80 | + res.end('Proxy error: ' + err.message) |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +server.on('upgrade', (req, socket, head) => { |
| 85 | + proxy.ws(req, socket, head) |
| 86 | +}) |
| 87 | + |
| 88 | +server.listen(process.env.PORT, () => { |
| 89 | + // Enhanced, colorized logs |
| 90 | + const proxyUrl = `http://localhost:${process.env.PORT}` |
| 91 | + console.log( |
| 92 | + styleText('cyan', `🐨 Proxy server running: `) + |
| 93 | + styleText('green', proxyUrl), |
| 94 | + ) |
| 95 | + console.log( |
| 96 | + styleText('gray', `- Client port: `) + |
| 97 | + styleText('magenta', clientPort.toString()), |
| 98 | + ) |
| 99 | + console.log( |
| 100 | + styleText('gray', `- Server port: `) + |
| 101 | + styleText('yellow', serverPort.toString()), |
| 102 | + ) |
| 103 | +}) |
| 104 | + |
| 105 | +// Ensure proper cleanup |
| 106 | +function cleanup() { |
| 107 | + if (inspectorProcess && !inspectorProcess.killed) { |
| 108 | + inspectorProcess.kill() |
| 109 | + } |
| 110 | + proxy.close() |
| 111 | + server.close(() => { |
| 112 | + console.log('HTTP server closed') |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +process.on('exit', cleanup) |
| 117 | +process.on('SIGINT', () => { |
| 118 | + cleanup() |
| 119 | + process.exit(0) |
| 120 | +}) |
| 121 | +process.on('SIGTERM', () => { |
| 122 | + cleanup() |
| 123 | + process.exit(0) |
| 124 | +}) |
0 commit comments