|
| 1 | +/** |
| 2 | + * Entry point for running the MCP server. |
| 3 | + * Run with: npx @modelcontextprotocol/server-basic-react |
| 4 | + * Or: node dist/index.js [--stdio] |
| 5 | + */ |
| 6 | + |
| 7 | +import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js"; |
| 8 | +import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 9 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 10 | +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
| 11 | +import cors from "cors"; |
| 12 | +import type { Request, Response } from "express"; |
| 13 | +import { createServer } from "./server.js"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Starts an MCP server with Streamable HTTP transport in stateless mode. |
| 17 | + * |
| 18 | + * @param createServer - Factory function that creates a new McpServer instance per request. |
| 19 | + */ |
| 20 | +export async function startStreamableHTTPServer( |
| 21 | + createServer: () => McpServer, |
| 22 | +): Promise<void> { |
| 23 | + const port = parseInt(process.env.PORT ?? "3001", 10); |
| 24 | + |
| 25 | + const app = createMcpExpressApp({ host: "0.0.0.0" }); |
| 26 | + app.use(cors()); |
| 27 | + |
| 28 | + app.all("/mcp", async (req: Request, res: Response) => { |
| 29 | + const server = createServer(); |
| 30 | + const transport = new StreamableHTTPServerTransport({ |
| 31 | + sessionIdGenerator: undefined, |
| 32 | + }); |
| 33 | + |
| 34 | + res.on("close", () => { |
| 35 | + transport.close().catch(() => {}); |
| 36 | + server.close().catch(() => {}); |
| 37 | + }); |
| 38 | + |
| 39 | + try { |
| 40 | + await server.connect(transport); |
| 41 | + await transport.handleRequest(req, res, req.body); |
| 42 | + } catch (error) { |
| 43 | + console.error("MCP error:", error); |
| 44 | + if (!res.headersSent) { |
| 45 | + res.status(500).json({ |
| 46 | + jsonrpc: "2.0", |
| 47 | + error: { code: -32603, message: "Internal server error" }, |
| 48 | + id: null, |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + const httpServer = app.listen(port, (err) => { |
| 55 | + if (err) { |
| 56 | + console.error("Failed to start server:", err); |
| 57 | + process.exit(1); |
| 58 | + } |
| 59 | + console.log(`MCP server listening on http://localhost:${port}/mcp`); |
| 60 | + }); |
| 61 | + |
| 62 | + const shutdown = () => { |
| 63 | + console.log("\nShutting down..."); |
| 64 | + httpServer.close(() => process.exit(0)); |
| 65 | + }; |
| 66 | + |
| 67 | + process.on("SIGINT", shutdown); |
| 68 | + process.on("SIGTERM", shutdown); |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Starts an MCP server with stdio transport. |
| 73 | + * |
| 74 | + * @param createServer - Factory function that creates a new McpServer instance. |
| 75 | + */ |
| 76 | +export async function startStdioServer( |
| 77 | + createServer: () => McpServer, |
| 78 | +): Promise<void> { |
| 79 | + await createServer().connect(new StdioServerTransport()); |
| 80 | +} |
| 81 | + |
| 82 | +async function main() { |
| 83 | + if (process.argv.includes("--stdio")) { |
| 84 | + await startStdioServer(createServer); |
| 85 | + } else { |
| 86 | + await startStreamableHTTPServer(createServer); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +main().catch((e) => { |
| 91 | + console.error(e); |
| 92 | + process.exit(1); |
| 93 | +}); |
0 commit comments