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:
75
bin/cli.js
75
bin/cli.js
@@ -7,7 +7,7 @@ import { fileURLToPath } from "url";
|
|||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
function delay(ms) {
|
function delay(ms) {
|
||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
@@ -73,42 +73,45 @@ async function main() {
|
|||||||
cancelled = true;
|
cancelled = true;
|
||||||
abort.abort();
|
abort.abort();
|
||||||
});
|
});
|
||||||
|
let server, serverOk;
|
||||||
const server = spawnPromise(
|
|
||||||
"node",
|
|
||||||
[
|
|
||||||
inspectorServerPath,
|
|
||||||
...(command ? [`--env`, command] : []),
|
|
||||||
...(mcpServerArgs ? [`--args=${mcpServerArgs.join(" ")}`] : []),
|
|
||||||
],
|
|
||||||
{
|
|
||||||
env: {
|
|
||||||
...process.env,
|
|
||||||
PORT: SERVER_PORT,
|
|
||||||
MCP_ENV_VARS: JSON.stringify(envVars),
|
|
||||||
},
|
|
||||||
signal: abort.signal,
|
|
||||||
echoOutput: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const client = 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 {
|
try {
|
||||||
await Promise.any([server, client]);
|
|
||||||
} catch (e) {
|
server = spawnPromise(
|
||||||
if (!cancelled || process.env.DEBUG) throw e;
|
"node",
|
||||||
|
[
|
||||||
|
inspectorServerPath,
|
||||||
|
...(command ? [`--env`, command] : []),
|
||||||
|
...(mcpServerArgs ? [`--args=${mcpServerArgs.join(" ")}`] : []),
|
||||||
|
],
|
||||||
|
{
|
||||||
|
env: {
|
||||||
|
...process.env,
|
||||||
|
PORT: SERVER_PORT,
|
||||||
|
MCP_ENV_VARS: JSON.stringify(envVars),
|
||||||
|
},
|
||||||
|
signal: abort.signal,
|
||||||
|
echoOutput: true,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
if (!cancelled || process.env.DEBUG) throw e;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -16,4 +16,18 @@ const server = http.createServer((request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const port = process.env.PORT || 5173;
|
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);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "tsc -b && vite build",
|
"build": "tsc -b && vite build",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview --port 5173",
|
||||||
"test": "jest --config jest.config.cjs",
|
"test": "jest --config jest.config.cjs",
|
||||||
"test:watch": "jest --config jest.config.cjs --watch"
|
"test:watch": "jest --config jest.config.cjs --watch"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -184,15 +184,18 @@ app.get("/config", (req, res) => {
|
|||||||
|
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
try {
|
const server = app.listen(PORT);
|
||||||
const server = app.listen(PORT);
|
server.on("listening", () => {
|
||||||
|
console.log(`⚙️ Proxy server listening on port ${PORT}`);
|
||||||
server.on("listening", () => {
|
});
|
||||||
const addr = server.address();
|
server.on("error", (err) => {
|
||||||
const port = typeof addr === "string" ? addr : addr?.port;
|
if (err.message.includes(`EADDRINUSE`)) {
|
||||||
console.log(`Proxy server listening on port ${port}`);
|
console.error(
|
||||||
});
|
`❌ Proxy Server PORT IS IN USE at port ${PORT} ❌ `,
|
||||||
} catch (error) {
|
);
|
||||||
console.error("Failed to start server:", error);
|
} else {
|
||||||
|
console.error(err.message);
|
||||||
|
}
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user