| 25 | new RefineUiTool().register(server); |
| 26 | |
| 27 | async function runServer() { |
| 28 | const transport = new StdioServerTransport(); |
| 29 | console.log(`Starting server v${VERSION} (PID: ${process.pid})`); |
| 30 | |
| 31 | let isShuttingDown = false; |
| 32 | |
| 33 | const cleanup = () => { |
| 34 | if (isShuttingDown) return; |
| 35 | isShuttingDown = true; |
| 36 | |
| 37 | console.log(`Shutting down server (PID: ${process.pid})...`); |
| 38 | try { |
| 39 | transport.close(); |
| 40 | } catch (error) { |
| 41 | console.error(`Error closing transport (PID: ${process.pid}):`, error); |
| 42 | } |
| 43 | console.log(`Server closed (PID: ${process.pid})`); |
| 44 | process.exit(0); |
| 45 | }; |
| 46 | |
| 47 | transport.onerror = (error: Error) => { |
| 48 | console.error(`Transport error (PID: ${process.pid}):`, error); |
| 49 | cleanup(); |
| 50 | }; |
| 51 | |
| 52 | transport.onclose = () => { |
| 53 | console.log(`Transport closed unexpectedly (PID: ${process.pid})`); |
| 54 | cleanup(); |
| 55 | }; |
| 56 | |
| 57 | process.on("SIGTERM", () => { |
| 58 | console.log(`Received SIGTERM (PID: ${process.pid})`); |
| 59 | cleanup(); |
| 60 | }); |
| 61 | |
| 62 | process.on("SIGINT", () => { |
| 63 | console.log(`Received SIGINT (PID: ${process.pid})`); |
| 64 | cleanup(); |
| 65 | }); |
| 66 | |
| 67 | process.on("beforeExit", () => { |
| 68 | console.log(`Received beforeExit (PID: ${process.pid})`); |
| 69 | cleanup(); |
| 70 | }); |
| 71 | |
| 72 | await server.connect(transport); |
| 73 | console.log(`Server started (PID: ${process.pid})`); |
| 74 | } |
| 75 | |
| 76 | runServer().catch((error) => { |
| 77 | console.error(`Fatal error running server (PID: ${process.pid}):`, error); |