|
| 1 | +import { documentRegistry, getPrimaryWorkspaceFolder } from '#core'; |
| 2 | +import { |
| 3 | + buildPortFilePath, |
| 4 | + DOCUMENT_ROUTE, |
| 5 | + encodePortFile, |
| 6 | + LOOPBACK_HOST, |
| 7 | + TOKEN_HEADER, |
| 8 | +} from '@lemoncode/quickmock-registry-protocol'; |
| 9 | +import { randomBytes } from 'node:crypto'; |
| 10 | +import { unlinkSync, writeFileSync } from 'node:fs'; |
| 11 | +import { |
| 12 | + createServer, |
| 13 | + type IncomingMessage, |
| 14 | + type ServerResponse, |
| 15 | +} from 'node:http'; |
| 16 | +import * as vscode from 'vscode'; |
| 17 | +import { PORT_FILE_MODE, TOKEN_BYTE_LENGTH } from './constants'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Starts the MCP document bridge server, which serves the content of documents open in the editor to the MCP server. |
| 21 | + * @param context The VS Code extension context. |
| 22 | + * @returns A promise that resolves when the server has started. |
| 23 | + */ |
| 24 | +export const startDocumentBridge = async ( |
| 25 | + context: vscode.ExtensionContext |
| 26 | +): Promise<void> => { |
| 27 | + const workspaceRoot = getPrimaryWorkspaceFolder()?.uri.fsPath; |
| 28 | + if (!workspaceRoot) return; |
| 29 | + |
| 30 | + const portFile = buildPortFilePath(workspaceRoot); |
| 31 | + const token = randomBytes(TOKEN_BYTE_LENGTH).toString('hex'); |
| 32 | + |
| 33 | + const handleRequest = (req: IncomingMessage, res: ServerResponse): void => { |
| 34 | + if (req.headers[TOKEN_HEADER] !== token) { |
| 35 | + res.writeHead(401); |
| 36 | + res.end(); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const url = new URL(req.url ?? '/', 'http://localhost'); |
| 41 | + |
| 42 | + if (url.pathname !== DOCUMENT_ROUTE) { |
| 43 | + res.writeHead(404); |
| 44 | + res.end(); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const path = url.searchParams.get('path'); |
| 49 | + if (!path) { |
| 50 | + res.writeHead(400); |
| 51 | + res.end('Missing path parameter'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const content = documentRegistry.get(path); |
| 56 | + if (content === undefined) { |
| 57 | + res.writeHead(404); |
| 58 | + res.end('Document not open in editor'); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); |
| 63 | + res.end(content); |
| 64 | + }; |
| 65 | + |
| 66 | + const server = createServer(handleRequest); |
| 67 | + |
| 68 | + await new Promise<void>((resolve, reject) => { |
| 69 | + server.on('error', reject); |
| 70 | + // Get the assigned port and write it to the port file |
| 71 | + server.listen(0, LOOPBACK_HOST, () => { |
| 72 | + const { port } = server.address() as { port: number }; |
| 73 | + try { |
| 74 | + writeFileSync(portFile, encodePortFile(port, token), { |
| 75 | + mode: PORT_FILE_MODE, |
| 76 | + }); |
| 77 | + } catch (err) { |
| 78 | + reject(err); |
| 79 | + return; |
| 80 | + } |
| 81 | + resolve(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + context.subscriptions.push({ |
| 86 | + dispose: () => { |
| 87 | + server.closeAllConnections(); |
| 88 | + server.close(); |
| 89 | + try { |
| 90 | + unlinkSync(portFile); |
| 91 | + } catch {} |
| 92 | + }, |
| 93 | + }); |
| 94 | +}; |
0 commit comments