|
| 1 | +import type { McpTransport, McpTransportHandlers } from "./mcp-transport"; |
| 2 | + |
| 3 | +export type HttpTransportOptions = { |
| 4 | + url: string; |
| 5 | + headers?: Record<string, string>; |
| 6 | +}; |
| 7 | + |
| 8 | +/** |
| 9 | + * Remote transport implementing the MCP "Streamable HTTP" protocol |
| 10 | + * (spec 2025-03-26). Each outbound JSON-RPC message is delivered with an HTTP |
| 11 | + * POST; the server replies either with a single JSON object or with an SSE |
| 12 | + * stream that may carry the response together with server notifications. |
| 13 | + * |
| 14 | + * The `Mcp-Session-Id` returned on initialize is echoed on every subsequent |
| 15 | + * request. The legacy two-endpoint HTTP+SSE transport (2024-11-05) is not |
| 16 | + * handled here. |
| 17 | + */ |
| 18 | +export class HttpTransport implements McpTransport { |
| 19 | + private handlers: McpTransportHandlers | null = null; |
| 20 | + private sessionId: string | null = null; |
| 21 | + private closed = false; |
| 22 | + private readonly activeRequests = new Set<AbortController>(); |
| 23 | + |
| 24 | + constructor( |
| 25 | + private readonly serverName: string, |
| 26 | + private readonly options: HttpTransportOptions |
| 27 | + ) {} |
| 28 | + |
| 29 | + async start(handlers: McpTransportHandlers): Promise<void> { |
| 30 | + this.handlers = handlers; |
| 31 | + this.closed = false; |
| 32 | + this.sessionId = null; |
| 33 | + // Streamable HTTP is connectionless until the first message is sent, so |
| 34 | + // there is nothing to open here. |
| 35 | + } |
| 36 | + |
| 37 | + send(message: object): void { |
| 38 | + void this.post(message); |
| 39 | + } |
| 40 | + |
| 41 | + close(): void { |
| 42 | + this.closed = true; |
| 43 | + for (const controller of this.activeRequests) { |
| 44 | + try { |
| 45 | + controller.abort(); |
| 46 | + } catch { |
| 47 | + // ignore abort errors |
| 48 | + } |
| 49 | + } |
| 50 | + this.activeRequests.clear(); |
| 51 | + } |
| 52 | + |
| 53 | + isConnected(): boolean { |
| 54 | + return !this.closed; |
| 55 | + } |
| 56 | + |
| 57 | + decorateError(message: string): Error { |
| 58 | + return new Error(message); |
| 59 | + } |
| 60 | + |
| 61 | + private async post(message: object): Promise<void> { |
| 62 | + if (this.closed) return; |
| 63 | + |
| 64 | + const controller = new AbortController(); |
| 65 | + this.activeRequests.add(controller); |
| 66 | + try { |
| 67 | + const headers: Record<string, string> = { |
| 68 | + "content-type": "application/json", |
| 69 | + accept: "application/json, text/event-stream", |
| 70 | + ...(this.options.headers ?? {}), |
| 71 | + }; |
| 72 | + if (this.sessionId) { |
| 73 | + headers["mcp-session-id"] = this.sessionId; |
| 74 | + } |
| 75 | + |
| 76 | + const response = await fetch(this.options.url, { |
| 77 | + method: "POST", |
| 78 | + headers, |
| 79 | + body: JSON.stringify(message), |
| 80 | + signal: controller.signal, |
| 81 | + }); |
| 82 | + |
| 83 | + const sessionId = response.headers.get("mcp-session-id"); |
| 84 | + if (sessionId) { |
| 85 | + this.sessionId = sessionId; |
| 86 | + } |
| 87 | + |
| 88 | + // 202 Accepted (or 204) acknowledges a notification/response with no body. |
| 89 | + if (response.status === 202 || response.status === 204) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (!response.ok) { |
| 94 | + const body = await safeReadText(response); |
| 95 | + this.reportClose( |
| 96 | + `MCP server "${this.serverName}" returned HTTP ${response.status}${body ? `: ${truncate(body)}` : ""}` |
| 97 | + ); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const contentType = response.headers.get("content-type") ?? ""; |
| 102 | + if (contentType.includes("text/event-stream") && response.body) { |
| 103 | + await this.consumeEventStream(response.body); |
| 104 | + } else { |
| 105 | + const text = await safeReadText(response); |
| 106 | + if (text) { |
| 107 | + this.dispatchPayload(text); |
| 108 | + } |
| 109 | + } |
| 110 | + } catch (err) { |
| 111 | + if (this.closed || controller.signal.aborted) { |
| 112 | + return; |
| 113 | + } |
| 114 | + const message = err instanceof Error ? err.message : String(err); |
| 115 | + this.reportClose(`MCP server "${this.serverName}" request failed: ${message}`); |
| 116 | + } finally { |
| 117 | + this.activeRequests.delete(controller); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private async consumeEventStream(body: ReadableStream<Uint8Array>): Promise<void> { |
| 122 | + const reader = body.getReader(); |
| 123 | + const decoder = new TextDecoder(); |
| 124 | + let buffer = ""; |
| 125 | + try { |
| 126 | + for (;;) { |
| 127 | + const { done, value } = await reader.read(); |
| 128 | + if (done) break; |
| 129 | + buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n"); |
| 130 | + |
| 131 | + let boundary: number; |
| 132 | + while ((boundary = buffer.indexOf("\n\n")) !== -1) { |
| 133 | + const rawEvent = buffer.slice(0, boundary); |
| 134 | + buffer = buffer.slice(boundary + 2); |
| 135 | + const data = readSseData(rawEvent); |
| 136 | + if (data) { |
| 137 | + this.dispatchPayload(data); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (this.closed) break; |
| 142 | + } |
| 143 | + } catch { |
| 144 | + // Stream aborted or errored; close handling happens elsewhere. |
| 145 | + } finally { |
| 146 | + try { |
| 147 | + reader.releaseLock(); |
| 148 | + } catch { |
| 149 | + // ignore |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private dispatchPayload(text: string): void { |
| 155 | + let parsed: unknown; |
| 156 | + try { |
| 157 | + parsed = JSON.parse(text); |
| 158 | + } catch { |
| 159 | + return; |
| 160 | + } |
| 161 | + if (Array.isArray(parsed)) { |
| 162 | + for (const item of parsed) { |
| 163 | + if (item && typeof item === "object") { |
| 164 | + this.handlers?.onMessage(item as object); |
| 165 | + } |
| 166 | + } |
| 167 | + return; |
| 168 | + } |
| 169 | + if (parsed && typeof parsed === "object") { |
| 170 | + this.handlers?.onMessage(parsed as object); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private reportClose(reason: string): void { |
| 175 | + if (this.closed) return; |
| 176 | + this.closed = true; |
| 177 | + this.handlers?.onClose(reason); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +function readSseData(rawEvent: string): string { |
| 182 | + const dataLines: string[] = []; |
| 183 | + for (const line of rawEvent.split("\n")) { |
| 184 | + if (line.startsWith("data:")) { |
| 185 | + dataLines.push(line.slice(5).replace(/^ /, "")); |
| 186 | + } |
| 187 | + // event:, id:, retry: and comment (":") lines are ignored. |
| 188 | + } |
| 189 | + return dataLines.join("\n"); |
| 190 | +} |
| 191 | + |
| 192 | +async function safeReadText(response: Response): Promise<string> { |
| 193 | + try { |
| 194 | + return (await response.text()).trim(); |
| 195 | + } catch { |
| 196 | + return ""; |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +function truncate(text: string, max = 500): string { |
| 201 | + return text.length > max ? `${text.slice(0, max)}…` : text; |
| 202 | +} |
0 commit comments