This fixes #214 where when the inspector is started, it can report that the inspector is up, when in fact it isn't because the address is already in use. It catches this condition, as well as the condition where the proxy server port is in use, reports it, and exits.

* In bin/cli.js
  - in the delay function, have the setTimeout return a true value.
  - try the server's spawnPromise call and within the try block, use Promise.race to get the return value of the first promise to resolve. If the server is up, it will not resolve and the 2 second delay will resolve with true, telling us the server is ok. Any error will have been reported by the server startup process, so we will not pile on with more output in the catch block.
  - If the server started ok, then we will await the spawnPromise call for starting the client. If the client fails to start it will report and exit, otherwise we are done and both servers are running and have reported as much. If an error is caught and it isn't SIGINT or if process.env.DEBUG is true then the error will be thrown before exiting.
* In client/bin/cli.js
  - add a "listening" handler to the server, logging that the MCP inspector is up at the given port
  - Add an "error" handler to the server that reports  that the client port is in use if the error message includes "EADDRINUSE", otherwise throw the error so the entire contents can be seen.
* In server/src/index.ts
  - add a "listening" handler to the server, logging that the Proxy server is up at the given port
  - Add an "error" handler to the server that reports  that the server port is in use if the error message includes "EADDRINUSE", otherwise throw the error so the entire contents can be seen.
* In package.json
  - in preview script
    - add --port 5173 to start the client on the proper port. This was useful in getting an instance of the client running before trying the start script. otherwise it starts on 4173
This commit is contained in:
cliffhall
2025-03-28 11:59:21 -04:00
parent 4d4bb9110c
commit 1b754f52ca
4 changed files with 69 additions and 49 deletions

View File

@@ -7,7 +7,7 @@ import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, ms, true));
}
async function main() {
@@ -73,8 +73,10 @@ async function main() {
cancelled = true;
abort.abort();
});
let server, serverOk;
try {
const server = spawnPromise(
server = spawnPromise(
"node",
[
inspectorServerPath,
@@ -90,27 +92,28 @@ async function main() {
signal: abort.signal,
echoOutput: true,
},
);
)
const client = spawnPromise("node", [inspectorClientPath], {
// Make sure server started before starting client
serverOk = await Promise.race([server, delay(2 * 1000)]);
} catch(error) {}
if (serverOk) {
try {
await spawnPromise("node", [inspectorClientPath], {
env: { ...process.env, PORT: CLIENT_PORT },
signal: abort.signal,
echoOutput: true,
});
// Make sure our server/client didn't immediately fail
await Promise.any([server, client, delay(2 * 1000)]);
const portParam = SERVER_PORT === "3000" ? "" : `?proxyPort=${SERVER_PORT}`;
console.log(
`\n🔍 MCP Inspector is up and running at http://127.0.0.1:${CLIENT_PORT}${portParam} 🚀`,
);
try {
await Promise.any([server, client]);
} catch (e) {
if (!cancelled || process.env.DEBUG) throw e;
}
}
return 0;
}

View File

@@ -16,4 +16,18 @@ const server = http.createServer((request, response) => {
});
const port = process.env.PORT || 5173;
server.listen(port, () => {});
server.on("listening", () => {
console.log(
`🔍 MCP Inspector is up and running at http://127.0.0.1:${port} 🚀`,
);
})
server.on("error", (err) => {
if (err.message.includes(`EADDRINUSE`)) {
console.error(
`❌ MCP Inspector PORT IS IN USE at http://127.0.0.1:${port}`,
);
} else {
throw err;
}
})
server.listen(port);

View File

@@ -18,7 +18,7 @@
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview",
"preview": "vite preview --port 5173",
"test": "jest --config jest.config.cjs",
"test:watch": "jest --config jest.config.cjs --watch"
},

View File

@@ -184,15 +184,18 @@ app.get("/config", (req, res) => {
const PORT = process.env.PORT || 3000;
try {
const server = app.listen(PORT);
server.on("listening", () => {
const addr = server.address();
const port = typeof addr === "string" ? addr : addr?.port;
console.log(`Proxy server listening on port ${port}`);
console.log(`⚙️ Proxy server listening on port ${PORT}`);
});
} catch (error) {
console.error("Failed to start server:", error);
process.exit(1);
server.on("error", (err) => {
if (err.message.includes(`EADDRINUSE`)) {
console.error(
`❌ Proxy Server PORT IS IN USE at port ${PORT}`,
);
} else {
console.error(err.message);
}
process.exit(1);
})