| 6 | |
| 7 | // Helper: make an MCP JSON-RPC request |
| 8 | function mcpRequest( |
| 9 | port: number, |
| 10 | body: object |
| 11 | ): Promise<{ status: number; body: string }> { |
| 12 | return new Promise((resolve, reject) => { |
| 13 | const data = JSON.stringify(body) |
| 14 | const req = http.request( |
| 15 | { |
| 16 | hostname: "localhost", |
| 17 | port, |
| 18 | path: "/mcp", |
| 19 | method: "POST", |
| 20 | headers: { |
| 21 | "Content-Type": "application/json", |
| 22 | Accept: "application/json, text/event-stream", |
| 23 | "Content-Length": Buffer.byteLength(data), |
| 24 | }, |
| 25 | }, |
| 26 | (res) => { |
| 27 | let body = "" |
| 28 | res.on("data", (chunk) => (body += chunk)) |
| 29 | res.on("end", () => resolve({ status: res.statusCode!, body })) |
| 30 | } |
| 31 | ) |
| 32 | req.on("error", reject) |
| 33 | req.write(data) |
| 34 | req.end() |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | // Helper: parse SSE response to get the JSON-RPC result |
| 39 | function parseSSE(body: string): any { |