|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | + |
| 3 | +import * as http from 'http'; |
| 4 | +import { IDebuggingHandler } from './debuggingHandler'; |
| 5 | +import { logger } from './utils/logger'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Per-window loopback HTTP server that runs debug operations against this |
| 9 | + * window's local DebuggingHandler. The router window forwards each MCP tool |
| 10 | + * call here so debugging happens in the window that owns the workspace. |
| 11 | + * |
| 12 | + * Bound to 127.0.0.1 and gated by a per-window token read from the registry. |
| 13 | + */ |
| 14 | +export class ControlServer { |
| 15 | + private server: http.Server | undefined; |
| 16 | + private boundPort = 0; |
| 17 | + |
| 18 | + constructor( |
| 19 | + private readonly handler: IDebuggingHandler, |
| 20 | + private readonly token: string |
| 21 | + ) {} |
| 22 | + |
| 23 | + /** The ephemeral loopback port chosen by the OS, or 0 before start(). */ |
| 24 | + public getPort(): number { |
| 25 | + return this.boundPort; |
| 26 | + } |
| 27 | + |
| 28 | + /** Start listening on an ephemeral loopback port. Resolves with the port. */ |
| 29 | + public async start(): Promise<number> { |
| 30 | + return new Promise<number>((resolve, reject) => { |
| 31 | + const server = http.createServer((req, res) => this.onRequest(req, res)); |
| 32 | + server.on('error', reject); |
| 33 | + server.listen(0, '127.0.0.1', () => { |
| 34 | + const address = server.address(); |
| 35 | + this.boundPort = typeof address === 'object' && address ? address.port : 0; |
| 36 | + this.server = server; |
| 37 | + logger.info(`DebugMCP control server listening on 127.0.0.1:${this.boundPort}`); |
| 38 | + resolve(this.boundPort); |
| 39 | + }); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + /** Stop listening. */ |
| 44 | + public async stop(): Promise<void> { |
| 45 | + if (this.server) { |
| 46 | + await new Promise<void>((resolve) => this.server!.close(() => resolve())); |
| 47 | + this.server = undefined; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private onRequest(req: http.IncomingMessage, res: http.ServerResponse): void { |
| 52 | + if (req.method !== 'POST' || req.url !== '/op') { |
| 53 | + res.writeHead(404).end(); |
| 54 | + return; |
| 55 | + } |
| 56 | + if (req.headers['x-debugmcp-token'] !== this.token) { |
| 57 | + res.writeHead(403).end(); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + let body = ''; |
| 62 | + req.on('data', (chunk) => { |
| 63 | + body += chunk; |
| 64 | + }); |
| 65 | + req.on('end', async () => { |
| 66 | + try { |
| 67 | + const { op, args } = JSON.parse(body || '{}') as { op: string; args?: unknown }; |
| 68 | + const result = await this.dispatch(op, args ?? {}); |
| 69 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 70 | + res.end(JSON.stringify({ result })); |
| 71 | + } catch (error) { |
| 72 | + const message = error instanceof Error ? error.message : String(error); |
| 73 | + res.writeHead(500, { 'Content-Type': 'application/json' }); |
| 74 | + res.end(JSON.stringify({ error: message })); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + /** Map a control op name onto the local debugging handler. */ |
| 80 | + private dispatch(op: string, args: any): Promise<string> { |
| 81 | + switch (op) { |
| 82 | + case 'handleStartDebugging': |
| 83 | + return this.handler.handleStartDebugging(args); |
| 84 | + case 'handleStopDebugging': |
| 85 | + return this.handler.handleStopDebugging(); |
| 86 | + case 'handleStepOver': |
| 87 | + return this.handler.handleStepOver(); |
| 88 | + case 'handleStepInto': |
| 89 | + return this.handler.handleStepInto(); |
| 90 | + case 'handleStepOut': |
| 91 | + return this.handler.handleStepOut(); |
| 92 | + case 'handleContinue': |
| 93 | + return this.handler.handleContinue(); |
| 94 | + case 'handleRestart': |
| 95 | + return this.handler.handleRestart(); |
| 96 | + case 'handleAddBreakpoint': |
| 97 | + return this.handler.handleAddBreakpoint(args); |
| 98 | + case 'handleRemoveBreakpoint': |
| 99 | + return this.handler.handleRemoveBreakpoint(args); |
| 100 | + case 'handleClearAllBreakpoints': |
| 101 | + return this.handler.handleClearAllBreakpoints(); |
| 102 | + case 'handleListBreakpoints': |
| 103 | + return this.handler.handleListBreakpoints(); |
| 104 | + case 'handleGetVariables': |
| 105 | + return this.handler.handleGetVariables(args); |
| 106 | + case 'handleEvaluateExpression': |
| 107 | + return this.handler.handleEvaluateExpression(args); |
| 108 | + default: |
| 109 | + return Promise.reject(new Error(`Unknown control op: ${op}`)); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments