|
| 1 | +/** |
| 2 | + * Shared utilities for running MCP servers with various transports. |
| 3 | + */ |
| 4 | + |
| 5 | +import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js"; |
| 6 | +import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 7 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 8 | +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
| 9 | +import cors from "cors"; |
| 10 | +import type { Request, Response } from "express"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Starts an MCP server using the appropriate transport based on command-line arguments. |
| 14 | + * |
| 15 | + * If `--stdio` is passed, uses stdio transport. Otherwise, uses Streamable HTTP transport. |
| 16 | + * |
| 17 | + * @param createServer - Factory function that creates a new McpServer instance. |
| 18 | + */ |
| 19 | +export async function startServer( |
| 20 | + createServer: () => McpServer, |
| 21 | +): Promise<void> { |
| 22 | + try { |
| 23 | + if (process.argv.includes("--stdio")) { |
| 24 | + await startStdioServer(createServer); |
| 25 | + } else { |
| 26 | + await startStreamableHttpServer(createServer); |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + console.error(e); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Starts an MCP server with stdio transport. |
| 36 | + * |
| 37 | + * @param createServer - Factory function that creates a new McpServer instance. |
| 38 | + */ |
| 39 | +export async function startStdioServer( |
| 40 | + createServer: () => McpServer, |
| 41 | +): Promise<void> { |
| 42 | + await createServer().connect(new StdioServerTransport()); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Starts an MCP server with Streamable HTTP transport in stateless mode. |
| 47 | + * |
| 48 | + * Each request creates a fresh server and transport instance, which are |
| 49 | + * closed when the response ends (no session tracking). |
| 50 | + * |
| 51 | + * The server listens on the port specified by the PORT environment variable, |
| 52 | + * defaulting to 3001 if not set. |
| 53 | + * |
| 54 | + * @param createServer - Factory function that creates a new McpServer instance per request. |
| 55 | + */ |
| 56 | +export async function startStreamableHttpServer( |
| 57 | + createServer: () => McpServer, |
| 58 | +): Promise<void> { |
| 59 | + const port = parseInt(process.env.PORT ?? "3001", 10); |
| 60 | + |
| 61 | + // Express app - bind to all interfaces for development/testing |
| 62 | + const expressApp = createMcpExpressApp({ host: "0.0.0.0" }); |
| 63 | + expressApp.use(cors()); |
| 64 | + |
| 65 | + expressApp.all("/mcp", async (req: Request, res: Response) => { |
| 66 | + // Create fresh server and transport for each request (stateless mode) |
| 67 | + const server = createServer(); |
| 68 | + const transport = new StreamableHTTPServerTransport({ |
| 69 | + sessionIdGenerator: undefined, |
| 70 | + }); |
| 71 | + |
| 72 | + // Clean up when response ends |
| 73 | + res.on("close", () => { |
| 74 | + transport.close().catch(() => {}); |
| 75 | + server.close().catch(() => {}); |
| 76 | + }); |
| 77 | + |
| 78 | + try { |
| 79 | + await server.connect(transport); |
| 80 | + await transport.handleRequest(req, res, req.body); |
| 81 | + } catch (error) { |
| 82 | + console.error("MCP error:", error); |
| 83 | + if (!res.headersSent) { |
| 84 | + res.status(500).json({ |
| 85 | + jsonrpc: "2.0", |
| 86 | + error: { code: -32603, message: "Internal server error" }, |
| 87 | + id: null, |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + const { promise, resolve, reject } = Promise.withResolvers<void>(); |
| 94 | + |
| 95 | + const httpServer = expressApp.listen(port, (err?: Error) => { |
| 96 | + if (err) return reject(err); |
| 97 | + console.log(`Server listening on http://localhost:${port}/mcp`); |
| 98 | + resolve(); |
| 99 | + }); |
| 100 | + |
| 101 | + const shutdown = () => { |
| 102 | + console.log("\nShutting down..."); |
| 103 | + httpServer.close(() => process.exit(0)); |
| 104 | + }; |
| 105 | + |
| 106 | + process.on("SIGINT", shutdown); |
| 107 | + process.on("SIGTERM", shutdown); |
| 108 | + |
| 109 | + return promise; |
| 110 | +} |
0 commit comments