|
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; |
| 4 | +import { createIBMCPServer } from "./server.js"; |
| 5 | + |
| 6 | +try { |
| 7 | + const PORT = process.env.PORT || process.env.MCP_PORT || 8000; |
| 8 | + |
| 9 | + console.log(`🚀 Starting Interactive Brokers MCP Server in HTTP/SSE mode on port ${PORT}`); |
| 10 | + |
| 11 | + const app = express(); |
| 12 | + app.use(cors()); |
| 13 | + app.use(express.json()); |
| 14 | + |
| 15 | + // Store active SSE transports |
| 16 | + const transports: { [sessionId: string]: SSEServerTransport } = {}; |
| 17 | + |
| 18 | + // Health check endpoint |
| 19 | + app.get('/health', (req, res) => { |
| 20 | + res.json({ |
| 21 | + status: 'ok', |
| 22 | + service: 'interactive-brokers-mcp', |
| 23 | + transport: 'http/sse' |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + // SSE endpoint - establishes the server-to-client event stream |
| 28 | + app.get('/mcp', async (req, res) => { |
| 29 | + console.log('📡 New SSE connection request received'); |
| 30 | + |
| 31 | + try { |
| 32 | + // Create SSE transport |
| 33 | + const transport = new SSEServerTransport('/mcp/messages', res); |
| 34 | + const sessionId = transport.sessionId; |
| 35 | + transports[sessionId] = transport; |
| 36 | + |
| 37 | + console.log(`✅ SSE transport created with session ID: ${sessionId}`); |
| 38 | + |
| 39 | + // Create a new MCP server instance for this connection |
| 40 | + const server = createIBMCPServer({ config: {} }); |
| 41 | + |
| 42 | + // Handle transport closure |
| 43 | + res.on("close", () => { |
| 44 | + console.log(`🔌 SSE connection closed for session: ${sessionId}`); |
| 45 | + delete transports[sessionId]; |
| 46 | + }); |
| 47 | + |
| 48 | + // Connect the server to the transport |
| 49 | + await server.connect(transport); |
| 50 | + console.log(`🔗 MCP server connected to SSE transport: ${sessionId}`); |
| 51 | + } catch (error) { |
| 52 | + console.error('❌ Failed to connect MCP server to SSE transport:', error); |
| 53 | + if (!res.headersSent) { |
| 54 | + res.status(500).send('Internal server error'); |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + // Message endpoint - receives client-to-server messages |
| 60 | + app.post('/mcp/messages', async (req, res) => { |
| 61 | + const sessionId = req.query.sessionId as string; |
| 62 | + console.log(`📨 Received message for session: ${sessionId}`); |
| 63 | + |
| 64 | + const transport = transports[sessionId]; |
| 65 | + if (transport) { |
| 66 | + try { |
| 67 | + await transport.handlePostMessage(req, res, req.body); |
| 68 | + } catch (error) { |
| 69 | + console.error('❌ Error handling POST message:', error); |
| 70 | + if (!res.headersSent) { |
| 71 | + res.status(500).json({ error: 'Internal server error' }); |
| 72 | + } |
| 73 | + } |
| 74 | + } else { |
| 75 | + console.warn(`⚠️ No transport found for session ID: ${sessionId}`); |
| 76 | + res.status(400).json({ error: 'No transport found for sessionId' }); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + // Start the server |
| 81 | + const server = app.listen(PORT, () => { |
| 82 | + console.log(`✅ HTTP/SSE server listening on http://localhost:${PORT}`); |
| 83 | + console.log(`📡 SSE endpoint: http://localhost:${PORT}/mcp`); |
| 84 | + console.log(`📨 Messages endpoint: http://localhost:${PORT}/mcp/messages`); |
| 85 | + console.log(`🏥 Health check: http://localhost:${PORT}/health`); |
| 86 | + }); |
| 87 | + |
| 88 | + // Handle server errors |
| 89 | + server.on('error', (error: any) => { |
| 90 | + if (error.code === 'EADDRINUSE') { |
| 91 | + console.error(`❌ Port ${PORT} is already in use`); |
| 92 | + process.exit(1); |
| 93 | + } else { |
| 94 | + console.error('❌ Server error:', error); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | +} catch (error) { |
| 100 | + console.error('❌ Fatal error starting HTTP/SSE server:', error); |
| 101 | + process.exit(1); |
| 102 | +} |
0 commit comments