|
5 | 5 | * and industry benchmarks by company stage. |
6 | 6 | */ |
7 | 7 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 8 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
8 | 9 | import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
9 | 10 | import type { |
10 | 11 | CallToolResult, |
@@ -292,50 +293,60 @@ server.registerResource( |
292 | 293 | ); |
293 | 294 |
|
294 | 295 | // --------------------------------------------------------------------------- |
295 | | -// Express Server |
| 296 | +// Server Startup |
296 | 297 | // --------------------------------------------------------------------------- |
297 | 298 |
|
298 | | -const app = express(); |
299 | | -app.use(cors()); |
300 | | -app.use(express.json()); |
301 | | - |
302 | | -app.post("/mcp", async (req: Request, res: Response) => { |
303 | | - try { |
304 | | - const transport = new StreamableHTTPServerTransport({ |
305 | | - sessionIdGenerator: undefined, |
306 | | - enableJsonResponse: true, |
| 299 | +async function main() { |
| 300 | + if (process.argv.includes("--stdio")) { |
| 301 | + const transport = new StdioServerTransport(); |
| 302 | + await server.connect(transport); |
| 303 | + console.error("Budget Allocator Server running in stdio mode"); |
| 304 | + } else { |
| 305 | + const app = express(); |
| 306 | + app.use(cors()); |
| 307 | + app.use(express.json()); |
| 308 | + |
| 309 | + app.post("/mcp", async (req: Request, res: Response) => { |
| 310 | + try { |
| 311 | + const transport = new StreamableHTTPServerTransport({ |
| 312 | + sessionIdGenerator: undefined, |
| 313 | + enableJsonResponse: true, |
| 314 | + }); |
| 315 | + res.on("close", () => { |
| 316 | + transport.close(); |
| 317 | + }); |
| 318 | + |
| 319 | + await server.connect(transport); |
| 320 | + await transport.handleRequest(req, res, req.body); |
| 321 | + } catch (error) { |
| 322 | + console.error("Error handling MCP request:", error); |
| 323 | + if (!res.headersSent) { |
| 324 | + res.status(500).json({ |
| 325 | + jsonrpc: "2.0", |
| 326 | + error: { code: -32603, message: "Internal server error" }, |
| 327 | + id: null, |
| 328 | + }); |
| 329 | + } |
| 330 | + } |
307 | 331 | }); |
308 | | - res.on("close", () => { |
309 | | - transport.close(); |
| 332 | + |
| 333 | + const httpServer = app.listen(PORT, () => { |
| 334 | + console.log( |
| 335 | + `Budget Allocator Server listening on http://localhost:${PORT}/mcp`, |
| 336 | + ); |
310 | 337 | }); |
311 | 338 |
|
312 | | - await server.connect(transport); |
313 | | - await transport.handleRequest(req, res, req.body); |
314 | | - } catch (error) { |
315 | | - console.error("Error handling MCP request:", error); |
316 | | - if (!res.headersSent) { |
317 | | - res.status(500).json({ |
318 | | - jsonrpc: "2.0", |
319 | | - error: { code: -32603, message: "Internal server error" }, |
320 | | - id: null, |
| 339 | + function shutdown() { |
| 340 | + console.log("\nShutting down..."); |
| 341 | + httpServer.close(() => { |
| 342 | + console.log("Server closed"); |
| 343 | + process.exit(0); |
321 | 344 | }); |
322 | 345 | } |
323 | | - } |
324 | | -}); |
325 | 346 |
|
326 | | -const httpServer = app.listen(PORT, () => { |
327 | | - console.log( |
328 | | - `Budget Allocator Server listening on http://localhost:${PORT}/mcp`, |
329 | | - ); |
330 | | -}); |
331 | | - |
332 | | -function shutdown() { |
333 | | - console.log("\nShutting down..."); |
334 | | - httpServer.close(() => { |
335 | | - console.log("Server closed"); |
336 | | - process.exit(0); |
337 | | - }); |
| 347 | + process.on("SIGINT", shutdown); |
| 348 | + process.on("SIGTERM", shutdown); |
| 349 | + } |
338 | 350 | } |
339 | 351 |
|
340 | | -process.on("SIGINT", shutdown); |
341 | | -process.on("SIGTERM", shutdown); |
| 352 | +main().catch(console.error); |
0 commit comments