From 1e96882a344fd583ff4d0bea2500cf4eff3342a7 Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Mon, 8 Dec 2025 17:20:35 +0100 Subject: [PATCH] handle server errors --- docs/quickstart.md | 6 +++++- examples/basic-host/serve.ts | 12 ++++++++++-- examples/basic-server-react/server.ts | 6 +++++- examples/basic-server-vanillajs/server.ts | 6 +++++- examples/budget-allocator-server/server.ts | 6 +++++- examples/cohort-heatmap-server/server.ts | 6 +++++- examples/customer-segmentation-server/server.ts | 6 +++++- examples/scenario-modeler-server/server.ts | 6 +++++- examples/system-monitor-server/server.ts | 6 +++++- 9 files changed, 50 insertions(+), 10 deletions(-) diff --git a/docs/quickstart.md b/docs/quickstart.md index 115219ca..9fdda056 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -153,7 +153,11 @@ app.post("/mcp", async (req, res) => { await transport.handleRequest(req, res, req.body); }); -app.listen(3001, () => { +app.listen(3001, (err) => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log("Server listening on http://localhost:3001/mcp"); }); ``` diff --git a/examples/basic-host/serve.ts b/examples/basic-host/serve.ts index 7e6af90d..450ec571 100644 --- a/examples/basic-host/serve.ts +++ b/examples/basic-host/serve.ts @@ -70,11 +70,19 @@ sandboxApp.use((_req, res) => { }); // ============ Start both servers ============ -hostApp.listen(HOST_PORT, () => { +hostApp.listen(HOST_PORT, err => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log(`Host server: http://localhost:${HOST_PORT}`); }); -sandboxApp.listen(SANDBOX_PORT, () => { +sandboxApp.listen(SANDBOX_PORT, err => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log(`Sandbox server: http://localhost:${SANDBOX_PORT}`); console.log("\nPress Ctrl+C to stop\n"); }); diff --git a/examples/basic-server-react/server.ts b/examples/basic-server-react/server.ts index 3f6d6d34..eff3120d 100644 --- a/examples/basic-server-react/server.ts +++ b/examples/basic-server-react/server.ts @@ -85,7 +85,11 @@ app.post("/mcp", async (req: Request, res: Response) => { } }); -const httpServer = app.listen(PORT, () => { +const httpServer = app.listen(PORT, err => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log(`Server listening on http://localhost:${PORT}/mcp`); }); diff --git a/examples/basic-server-vanillajs/server.ts b/examples/basic-server-vanillajs/server.ts index 3f6d6d34..eff3120d 100644 --- a/examples/basic-server-vanillajs/server.ts +++ b/examples/basic-server-vanillajs/server.ts @@ -85,7 +85,11 @@ app.post("/mcp", async (req: Request, res: Response) => { } }); -const httpServer = app.listen(PORT, () => { +const httpServer = app.listen(PORT, err => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log(`Server listening on http://localhost:${PORT}/mcp`); }); diff --git a/examples/budget-allocator-server/server.ts b/examples/budget-allocator-server/server.ts index 03bed630..c05c873f 100644 --- a/examples/budget-allocator-server/server.ts +++ b/examples/budget-allocator-server/server.ts @@ -330,7 +330,11 @@ async function main() { } }); - const httpServer = app.listen(PORT, () => { + const httpServer = app.listen(PORT, (err) => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log( `Budget Allocator Server listening on http://localhost:${PORT}/mcp`, ); diff --git a/examples/cohort-heatmap-server/server.ts b/examples/cohort-heatmap-server/server.ts index cc155f6b..76fa86e7 100644 --- a/examples/cohort-heatmap-server/server.ts +++ b/examples/cohort-heatmap-server/server.ts @@ -240,7 +240,11 @@ async function main() { } }); - const httpServer = app.listen(PORT, () => { + const httpServer = app.listen(PORT, (err) => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log( `Cohort Heatmap Server listening on http://localhost:${PORT}/mcp`, ); diff --git a/examples/customer-segmentation-server/server.ts b/examples/customer-segmentation-server/server.ts index 0f9a979c..63f8bb92 100644 --- a/examples/customer-segmentation-server/server.ts +++ b/examples/customer-segmentation-server/server.ts @@ -139,7 +139,11 @@ async function main() { } }); - const httpServer = app.listen(PORT, () => { + const httpServer = app.listen(PORT, (err) => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log( `Customer Segmentation Server listening on http://localhost:${PORT}/mcp`, ); diff --git a/examples/scenario-modeler-server/server.ts b/examples/scenario-modeler-server/server.ts index 7aac927b..cc92a7cc 100644 --- a/examples/scenario-modeler-server/server.ts +++ b/examples/scenario-modeler-server/server.ts @@ -346,7 +346,11 @@ async function main() { } }); - const httpServer = app.listen(PORT, () => { + const httpServer = app.listen(PORT, (err) => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log( `SaaS Scenario Modeler Server listening on http://localhost:${PORT}/mcp`, ); diff --git a/examples/system-monitor-server/server.ts b/examples/system-monitor-server/server.ts index e7606f7d..8ccd1340 100644 --- a/examples/system-monitor-server/server.ts +++ b/examples/system-monitor-server/server.ts @@ -211,7 +211,11 @@ async function main() { } }); - const httpServer = app.listen(PORT, () => { + const httpServer = app.listen(PORT, (err) => { + if (err) { + console.error("Error starting server:", err); + process.exit(1); + } console.log( `System Monitor Server listening on http://localhost:${PORT}/mcp`, );