Skip to content

Commit 1b754f5

Browse files
committed
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
1 parent 4d4bb91 commit 1b754f5

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

bin/cli.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from "url";
77
const __dirname = dirname(fileURLToPath(import.meta.url));
88

99
function delay(ms) {
10-
return new Promise((resolve) => setTimeout(resolve, ms));
10+
return new Promise((resolve) => setTimeout(resolve, ms, true));
1111
}
1212

1313
async function main() {
@@ -73,42 +73,45 @@ async function main() {
7373
cancelled = true;
7474
abort.abort();
7575
});
76+
let server, serverOk;
77+
try {
7678

77-
const server = spawnPromise(
78-
"node",
79-
[
80-
inspectorServerPath,
81-
...(command ? [`--env`, command] : []),
82-
...(mcpServerArgs ? [`--args=${mcpServerArgs.join(" ")}`] : []),
83-
],
84-
{
85-
env: {
86-
...process.env,
87-
PORT: SERVER_PORT,
88-
MCP_ENV_VARS: JSON.stringify(envVars),
79+
server = spawnPromise(
80+
"node",
81+
[
82+
inspectorServerPath,
83+
...(command ? [`--env`, command] : []),
84+
...(mcpServerArgs ? [`--args=${mcpServerArgs.join(" ")}`] : []),
85+
],
86+
{
87+
env: {
88+
...process.env,
89+
PORT: SERVER_PORT,
90+
MCP_ENV_VARS: JSON.stringify(envVars),
91+
},
92+
signal: abort.signal,
93+
echoOutput: true,
8994
},
90-
signal: abort.signal,
91-
echoOutput: true,
92-
},
93-
);
95+
)
9496

95-
const client = spawnPromise("node", [inspectorClientPath], {
96-
env: { ...process.env, PORT: CLIENT_PORT },
97-
signal: abort.signal,
98-
echoOutput: true,
99-
});
97+
// Make sure server started before starting client
98+
serverOk = await Promise.race([server, delay(2 * 1000)]);
10099

101-
// Make sure our server/client didn't immediately fail
102-
await Promise.any([server, client, delay(2 * 1000)]);
103-
const portParam = SERVER_PORT === "3000" ? "" : `?proxyPort=${SERVER_PORT}`;
104-
console.log(
105-
`\n🔍 MCP Inspector is up and running at http://127.0.0.1:${CLIENT_PORT}${portParam} 🚀`,
106-
);
100+
} catch(error) {}
101+
102+
if (serverOk) {
103+
try {
104+
105+
await spawnPromise("node", [inspectorClientPath], {
106+
env: { ...process.env, PORT: CLIENT_PORT },
107+
signal: abort.signal,
108+
echoOutput: true,
109+
});
110+
111+
} catch (e) {
112+
if (!cancelled || process.env.DEBUG) throw e;
113+
}
107114

108-
try {
109-
await Promise.any([server, client]);
110-
} catch (e) {
111-
if (!cancelled || process.env.DEBUG) throw e;
112115
}
113116

114117
return 0;

client/bin/cli.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ const server = http.createServer((request, response) => {
1616
});
1717

1818
const port = process.env.PORT || 5173;
19-
server.listen(port, () => {});
19+
server.on("listening", () => {
20+
console.log(
21+
`🔍 MCP Inspector is up and running at http://127.0.0.1:${port} 🚀`,
22+
);
23+
})
24+
server.on("error", (err) => {
25+
if (err.message.includes(`EADDRINUSE`)) {
26+
console.error(
27+
`❌ MCP Inspector PORT IS IN USE at http://127.0.0.1:${port} ❌ `,
28+
);
29+
} else {
30+
throw err;
31+
}
32+
})
33+
server.listen(port);

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dev": "vite",
1919
"build": "tsc -b && vite build",
2020
"lint": "eslint .",
21-
"preview": "vite preview",
21+
"preview": "vite preview --port 5173",
2222
"test": "jest --config jest.config.cjs",
2323
"test:watch": "jest --config jest.config.cjs --watch"
2424
},

server/src/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,18 @@ app.get("/config", (req, res) => {
184184

185185
const PORT = process.env.PORT || 3000;
186186

187-
try {
188-
const server = app.listen(PORT);
189-
190-
server.on("listening", () => {
191-
const addr = server.address();
192-
const port = typeof addr === "string" ? addr : addr?.port;
193-
console.log(`Proxy server listening on port ${port}`);
194-
});
195-
} catch (error) {
196-
console.error("Failed to start server:", error);
187+
const server = app.listen(PORT);
188+
server.on("listening", () => {
189+
console.log(`⚙️ Proxy server listening on port ${PORT}`);
190+
});
191+
server.on("error", (err) => {
192+
if (err.message.includes(`EADDRINUSE`)) {
193+
console.error(
194+
`❌ Proxy Server PORT IS IN USE at port ${PORT} ❌ `,
195+
);
196+
} else {
197+
console.error(err.message);
198+
}
197199
process.exit(1);
198-
}
200+
})
201+

0 commit comments

Comments
 (0)