From ca8db1f4178baf582e5441fca381f6527d343c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Betts?= Date: Tue, 26 Nov 2024 16:52:55 +0100 Subject: [PATCH] Handle spawning the client and server on Windows using correct paths --- bin/cli.js | 97 +++++++++++++++++++++++++--------------------------- package.json | 2 +- 2 files changed, 47 insertions(+), 52 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 27cb612..655624a 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,65 +1,60 @@ #!/usr/bin/env node import { join, dirname } from "path"; +import { spawnPromise } from "spawn-rx"; import { fileURLToPath } from "url"; -import concurrently from "concurrently"; const __dirname = dirname(fileURLToPath(import.meta.url)); -// Get command line arguments -const [, , command, ...mcpServerArgs] = process.argv; +async function main() { + // Get command line arguments + const [, , command, ...mcpServerArgs] = process.argv; -const inspectorServerPath = join(__dirname, "../server/build/index.js"); + const inspectorServerPath = join(__dirname, "../server/build/index.js"); -// Path to the client entry point -const inspectorClientPath = join(__dirname, "../client/bin/cli.js"); + // Path to the client entry point + const inspectorClientPath = join(__dirname, "../client/bin/cli.js"); -console.log("Starting MCP inspector..."); + const CLIENT_PORT = process.env.CLIENT_PORT ?? "5173"; + const SERVER_PORT = process.env.SERVER_PORT ?? "3000"; -function escapeArg(arg) { - if (arg.includes(" ") || arg.includes("'") || arg.includes('"')) { - return `\\"${arg.replace(/"/g, '\\\\\\"')}\\"`; + console.log("Starting MCP inspector..."); + + const abort = new AbortController(); + + let cancelled = false; + process.on("SIGINT", () => { + cancelled = true; + abort.abort(); + }); + + const server = spawnPromise( + "node", + [ + inspectorServerPath, + ...(command ? [`--env`, command] : []), + ...(mcpServerArgs ?? []), + ], + { env: { PORT: SERVER_PORT }, signal: abort.signal }, + ); + + const client = spawnPromise("node", [inspectorClientPath], { + env: { PORT: CLIENT_PORT }, + signal: abort.signal, + }); + + try { + await Promise.any([server, client]); + } catch (e) { + if (!cancelled) throw e; } - return arg; + + return 0; } -const serverCommand = [ - `node`, - inspectorServerPath, - command ? `--env ${escapeArg(command)}` : "", - mcpServerArgs.length - ? `--args="${mcpServerArgs.map(escapeArg).join(" ")}"` - : "", -] - .filter(Boolean) - .join(" "); - -const CLIENT_PORT = process.env.CLIENT_PORT ?? ""; -const SERVER_PORT = process.env.SERVER_PORT ?? ""; - -const { result } = concurrently( - [ - { - command: `PORT=${SERVER_PORT} ${serverCommand}`, - name: "server", - }, - { - command: `PORT=${CLIENT_PORT} node ${inspectorClientPath}`, - name: "client", - }, - ], - { - prefix: "name", - killOthers: ["failure", "success"], - restartTries: 3, - }, -); - -console.log( - `\nšŸ” MCP Inspector is up and running at http://localhost:${CLIENT_PORT || 5173} šŸš€`, -); - -result.catch((err) => { - console.error("An error occurred:", err); - process.exit(1); -}); +main() + .then((_) => process.exit(0)) + .catch((e) => { + console.error(e); + process.exit(1); + }); diff --git a/package.json b/package.json index fab50ad..87925df 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "build": "npm run build-server && npm run build-client", "start-server": "cd server && npm run start", "start-client": "cd client && npm run preview", - "start": "node ./bin/cli.ts", + "start": "node ./bin/cli.js", "prepare": "npm run build", "prettier-fix": "prettier --write .", "publish-all": "npm publish --workspaces --access public && npm publish --access public"