|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | import { FilteredStdioServerTransport } from './custom-stdio.js'; |
4 | | -import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'; |
5 | | -import express from "express"; |
6 | | -import { Request, Response } from "express"; |
| 4 | +import { runHttpServer } from './http-transport.js'; |
7 | 5 | import { server } from './server.js'; |
8 | 6 | import { configManager } from './config-manager.js'; |
9 | 7 | import { join, dirname } from 'path'; |
@@ -140,55 +138,6 @@ async function runServer() { |
140 | 138 | } |
141 | 139 | } |
142 | 140 |
|
143 | | -async function runHttpServer(port: number) { |
144 | | - // We return a new promise that never resolves to keep the server alive. |
145 | | - return new Promise((resolve, reject) => { |
146 | | - // Start a server based on the official @modelcontextprotocol/sdk documentation. |
147 | | - const app = express(); |
148 | | - app.use(express.json()); |
149 | | - |
150 | | - app.post('/', async (req: Request, res: Response) => { |
151 | | - // In stateless mode, create a new instance of transport and server for each request |
152 | | - // to ensure complete isolation. A single instance would cause request ID collisions |
153 | | - // when multiple clients connect concurrently. |
154 | | - |
155 | | - try { |
156 | | - const transport: StreamableHTTPServerTransport = new StreamableHTTPServerTransport({ |
157 | | - sessionIdGenerator: undefined, |
158 | | - }); |
159 | | - res.on('close', () => { |
160 | | - transport.close(); |
161 | | - server.close(); |
162 | | - }); |
163 | | - await server.connect(transport); |
164 | | - await transport.handleRequest(req, res, req.body); |
165 | | - } catch (error) { |
166 | | - console.error('Error handling MCP request:', error); |
167 | | - if (!res.headersSent) { |
168 | | - res.status(500).json({ |
169 | | - jsonrpc: '2.0', |
170 | | - error: { |
171 | | - code: -32603, |
172 | | - message: 'Internal server error', |
173 | | - }, |
174 | | - id: null, |
175 | | - }); |
176 | | - } |
177 | | - } |
178 | | - }); |
179 | | - |
180 | | - // Start the server |
181 | | - const httpServerInstance = app.listen(port, () => { |
182 | | - console.log(`MCP Stateless Streamable HTTP Server listening on port ${port}`); |
183 | | - }); |
184 | | - |
185 | | - httpServerInstance.on('error', (err) => { |
186 | | - console.error(`HTTP Server failed to start or encountered an error: ${err.message}`); |
187 | | - reject(err); |
188 | | - }); |
189 | | - }); |
190 | | -} |
191 | | - |
192 | 141 | async function runStdioServer() { |
193 | 142 | console.error("Connecting server through stdio transport ..."); |
194 | 143 | const transport = new FilteredStdioServerTransport(); |
|
0 commit comments