|
| 1 | +/* |
| 2 | + * Copyright 2026 Grabtaxi Holdings Pte Ltd (GRAB), All rights reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * SSE Detection Server |
| 9 | + * |
| 10 | + * Listens on port 3056 for a short window after app start to detect legacy MCP clients |
| 11 | + * that are still configured to use the deprecated SSE transport (GET /sse). |
| 12 | + * When detected, fires a callback so the app can show a migration guide dialog. |
| 13 | + */ |
| 14 | + |
| 15 | +import * as http from 'http'; |
| 16 | +import { createLogger } from '../utils/logger'; |
| 17 | +import { PORTS } from '@/shared/constants'; |
| 18 | + |
| 19 | +const logger = createLogger('SseDetection'); |
| 20 | + |
| 21 | +const MIGRATION_RESPONSE = JSON.stringify({ |
| 22 | + error: 'SSE transport is no longer supported', |
| 23 | + message: |
| 24 | + 'This MCP server has been upgraded to use stdio transport. ' + |
| 25 | + 'Please update your MCP client configuration to use the stdio server instead. ' + |
| 26 | + 'Open TalkToFigma Desktop and go to Settings to get the new configuration.', |
| 27 | + migration: { |
| 28 | + from: `http://127.0.0.1:${PORTS.MCP_SSE}/sse`, |
| 29 | + to: 'stdio (see TalkToFigma Desktop → Settings)', |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +export class SseDetectionServer { |
| 34 | + private httpServer: http.Server | null = null; |
| 35 | + private onDetected: () => void; |
| 36 | + |
| 37 | + constructor(onDetected: () => void) { |
| 38 | + this.onDetected = onDetected; |
| 39 | + } |
| 40 | + |
| 41 | + start(): Promise<void> { |
| 42 | + return new Promise((resolve) => { |
| 43 | + this.httpServer = http.createServer((req, res) => { |
| 44 | + res.setHeader('Content-Type', 'application/json'); |
| 45 | + res.setHeader('Access-Control-Allow-Origin', '*'); |
| 46 | + |
| 47 | + if (req.url === '/sse' && req.method === 'GET') { |
| 48 | + logger.info('Legacy SSE client detected on port 3056 — showing migration dialog'); |
| 49 | + this.onDetected(); |
| 50 | + |
| 51 | + res.writeHead(426, { 'Upgrade': 'stdio' }); |
| 52 | + res.end(MIGRATION_RESPONSE); |
| 53 | + } else { |
| 54 | + res.writeHead(400); |
| 55 | + res.end(JSON.stringify({ error: 'SSE transport is no longer supported' })); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + this.httpServer.on('error', (error: NodeJS.ErrnoException) => { |
| 60 | + if (error.code === 'EADDRINUSE') { |
| 61 | + logger.warn(`Port ${PORTS.MCP_SSE} already in use — SSE detection disabled`); |
| 62 | + resolve(); // silently skip, don't block app start |
| 63 | + } else { |
| 64 | + logger.error('SSE detection server error:', error); |
| 65 | + resolve(); // non-fatal |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + this.httpServer.listen(PORTS.MCP_SSE, '127.0.0.1', () => { |
| 70 | + logger.info(`SSE detection server listening on port ${PORTS.MCP_SSE} (60s window)`); |
| 71 | + resolve(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + stop(): Promise<void> { |
| 77 | + return new Promise((resolve) => { |
| 78 | + if (!this.httpServer) { |
| 79 | + resolve(); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + this.httpServer.close(() => { |
| 84 | + logger.info('SSE detection server stopped'); |
| 85 | + this.httpServer = null; |
| 86 | + resolve(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments