Skip to content

Commit 7fefe2a

Browse files
ochafikclaude
andcommitted
fix(examples): startServer returns promise that resolves/rejects on listen
Use httpServer.on('listening') and httpServer.on('error') events to properly resolve or reject the returned promise. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 46a1596 commit 7fefe2a

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

examples/shared/server-utils.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,27 @@ export async function startServer(
140140
}
141141
});
142142

143-
const httpServer = app.listen(port, () => {
144-
console.log(`${name} listening on http://localhost:${port}/mcp`);
143+
return new Promise<void>((resolve, reject) => {
144+
const httpServer = app.listen(port);
145+
146+
httpServer.on("listening", () => {
147+
console.log(`${name} listening on http://localhost:${port}/mcp`);
148+
resolve();
149+
});
150+
151+
httpServer.on("error", (err: Error) => {
152+
reject(err);
153+
});
154+
155+
const shutdown = () => {
156+
console.log("\nShutting down...");
157+
sessions.forEach((t) => t.close().catch(() => {}));
158+
httpServer.close(() => process.exit(0));
159+
};
160+
161+
process.on("SIGINT", shutdown);
162+
process.on("SIGTERM", shutdown);
145163
});
146-
147-
const shutdown = () => {
148-
console.log("\nShutting down...");
149-
sessions.forEach((t) => t.close().catch(() => {}));
150-
httpServer.close(() => process.exit(0));
151-
};
152-
153-
process.on("SIGINT", shutdown);
154-
process.on("SIGTERM", shutdown);
155164
}
156165

157166
/** Helper to get port from args/env, returns undefined for stdio mode */

0 commit comments

Comments
 (0)