|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -import { join, dirname } from "path"; |
| 3 | +import { resolve, dirname } from "path"; |
| 4 | +import { spawnPromise } from "spawn-rx"; |
4 | 5 | import { fileURLToPath } from "url"; |
5 | | -import concurrently from "concurrently"; |
6 | 6 |
|
7 | 7 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
8 | 8 |
|
9 | | -// Get command line arguments |
10 | | -const [, , command, ...mcpServerArgs] = process.argv; |
| 9 | +function delay(ms) { |
| 10 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 11 | +} |
| 12 | + |
| 13 | +async function main() { |
| 14 | + // Get command line arguments |
| 15 | + const [, , command, ...mcpServerArgs] = process.argv; |
| 16 | + |
| 17 | + const inspectorServerPath = resolve( |
| 18 | + __dirname, |
| 19 | + "..", |
| 20 | + "server", |
| 21 | + "build", |
| 22 | + "index.js", |
| 23 | + ); |
| 24 | + |
| 25 | + // Path to the client entry point |
| 26 | + const inspectorClientPath = resolve( |
| 27 | + __dirname, |
| 28 | + "..", |
| 29 | + "client", |
| 30 | + "bin", |
| 31 | + "cli.js", |
| 32 | + ); |
| 33 | + |
| 34 | + const CLIENT_PORT = process.env.CLIENT_PORT ?? "5173"; |
| 35 | + const SERVER_PORT = process.env.SERVER_PORT ?? "3000"; |
11 | 36 |
|
12 | | -const inspectorServerPath = join(__dirname, "../server/build/index.js"); |
| 37 | + console.log("Starting MCP inspector..."); |
13 | 38 |
|
14 | | -// Path to the client entry point |
15 | | -const inspectorClientPath = join(__dirname, "../client/bin/cli.js"); |
| 39 | + const abort = new AbortController(); |
16 | 40 |
|
17 | | -console.log("Starting MCP inspector..."); |
| 41 | + let cancelled = false; |
| 42 | + process.on("SIGINT", () => { |
| 43 | + cancelled = true; |
| 44 | + abort.abort(); |
| 45 | + }); |
18 | 46 |
|
19 | | -function escapeArg(arg) { |
20 | | - if (arg.includes(" ") || arg.includes("'") || arg.includes('"')) { |
21 | | - return `\\"${arg.replace(/"/g, '\\\\\\"')}\\"`; |
| 47 | + const server = spawnPromise( |
| 48 | + "node", |
| 49 | + [ |
| 50 | + inspectorServerPath, |
| 51 | + ...(command ? [`--env`, command] : []), |
| 52 | + ...(mcpServerArgs ? ["--args", mcpServerArgs.join(" ")] : []), |
| 53 | + ], |
| 54 | + { env: { ...process.env, PORT: SERVER_PORT }, signal: abort.signal }, |
| 55 | + ); |
| 56 | + |
| 57 | + const client = spawnPromise("node", [inspectorClientPath], { |
| 58 | + env: { ...process.env, PORT: CLIENT_PORT }, |
| 59 | + signal: abort.signal, |
| 60 | + }); |
| 61 | + |
| 62 | + // Make sure our server/client didn't immediately fail |
| 63 | + await Promise.any([server, client, delay(2 * 1000)]); |
| 64 | + console.log( |
| 65 | + `\n🔍 MCP Inspector is up and running at http://localhost:${CLIENT_PORT} 🚀`, |
| 66 | + ); |
| 67 | + |
| 68 | + try { |
| 69 | + await Promise.any([server, client]); |
| 70 | + } catch (e) { |
| 71 | + if (!cancelled || process.env.DEBUG) throw e; |
22 | 72 | } |
23 | | - return arg; |
| 73 | + |
| 74 | + return 0; |
24 | 75 | } |
25 | 76 |
|
26 | | -const serverCommand = [ |
27 | | - `node`, |
28 | | - inspectorServerPath, |
29 | | - command ? `--env ${escapeArg(command)}` : "", |
30 | | - mcpServerArgs.length |
31 | | - ? `--args="${mcpServerArgs.map(escapeArg).join(" ")}"` |
32 | | - : "", |
33 | | -] |
34 | | - .filter(Boolean) |
35 | | - .join(" "); |
36 | | - |
37 | | -const CLIENT_PORT = process.env.CLIENT_PORT ?? ""; |
38 | | -const SERVER_PORT = process.env.SERVER_PORT ?? ""; |
39 | | - |
40 | | -const { result } = concurrently( |
41 | | - [ |
42 | | - { |
43 | | - command: `PORT=${SERVER_PORT} ${serverCommand}`, |
44 | | - name: "server", |
45 | | - }, |
46 | | - { |
47 | | - command: `PORT=${CLIENT_PORT} node ${inspectorClientPath}`, |
48 | | - name: "client", |
49 | | - }, |
50 | | - ], |
51 | | - { |
52 | | - prefix: "name", |
53 | | - killOthers: ["failure", "success"], |
54 | | - restartTries: 3, |
55 | | - }, |
56 | | -); |
57 | | - |
58 | | -console.log( |
59 | | - `\n🔍 MCP Inspector is up and running at http://localhost:${CLIENT_PORT || 5173} 🚀`, |
60 | | -); |
61 | | - |
62 | | -result.catch((err) => { |
63 | | - console.error("An error occurred:", err); |
64 | | - process.exit(1); |
65 | | -}); |
| 77 | +main() |
| 78 | + .then((_) => process.exit(0)) |
| 79 | + .catch((e) => { |
| 80 | + console.error(e); |
| 81 | + process.exit(1); |
| 82 | + }); |
0 commit comments