Handle spawning the client and server on Windows using correct paths

This commit is contained in:
Anaïs Betts
2024-11-26 16:52:55 +01:00
parent eb4456d1e3
commit ca8db1f417
2 changed files with 47 additions and 52 deletions

View File

@@ -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);
});