|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 3 | +import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; |
| 4 | +import type { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js"; |
| 5 | +import { |
| 6 | + armResumeExhaustionSignal, |
| 7 | + disarmTransportDeathSignal, |
| 8 | + isStreamableResumeExhausted, |
| 9 | + type LiveHttpTransport, |
| 10 | +} from "./client.js"; |
| 11 | + |
| 12 | +describe("isStreamableResumeExhausted", () => { |
| 13 | + test.each([ |
| 14 | + "Maximum reconnection attempts (1000) exceeded.", |
| 15 | + "Maximum reconnection attempts (1) exceeded.", |
| 16 | + "Maximum reconnection attempts (999999) exceeded.", |
| 17 | + ])("matches the SDK exhaustion copy (%j)", (message) => { |
| 18 | + expect(isStreamableResumeExhausted(new Error(message))).toBe(true); |
| 19 | + }); |
| 20 | + |
| 21 | + test.each([ |
| 22 | + ["a transient stream error", new Error("Failed to open SSE stream")], |
| 23 | + ["a reconnect-loop error", new Error("Failed to reconnect SSE stream")], |
| 24 | + [ |
| 25 | + "an exhaustion prefix without the tail", |
| 26 | + new Error("Maximum reconnection attempts (1000)"), |
| 27 | + ], |
| 28 | + [ |
| 29 | + "an exhaustion copy without the count", |
| 30 | + new Error("Maximum reconnection attempts exceeded."), |
| 31 | + ], |
| 32 | + ["a non-error", "Maximum reconnection attempts (1000) exceeded."], |
| 33 | + ["nothing", undefined], |
| 34 | + ])("rejects %s", (_label, value) => { |
| 35 | + expect(isStreamableResumeExhausted(value)).toBe(false); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("armResumeExhaustionSignal", () => { |
| 40 | + test("leaves the transport untouched without an owner hook", () => { |
| 41 | + const transport: Pick<{ onerror?: (error: Error) => void }, "onerror"> = {}; |
| 42 | + armResumeExhaustionSignal(transport, () => true, undefined); |
| 43 | + expect(transport.onerror).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + test("routes exhaustion to the owner hook and keeps the prior handler", () => { |
| 47 | + const seen: string[] = []; |
| 48 | + let disconnects = 0; |
| 49 | + const transport: { onerror?: (error: Error) => void } = { |
| 50 | + onerror: (error) => { |
| 51 | + seen.push(error.message); |
| 52 | + }, |
| 53 | + }; |
| 54 | + armResumeExhaustionSignal( |
| 55 | + transport, |
| 56 | + () => true, |
| 57 | + () => { |
| 58 | + disconnects += 1; |
| 59 | + }, |
| 60 | + ); |
| 61 | + transport.onerror?.( |
| 62 | + new Error("Maximum reconnection attempts (1000) exceeded."), |
| 63 | + ); |
| 64 | + expect(seen).toEqual(["Maximum reconnection attempts (1000) exceeded."]); |
| 65 | + expect(disconnects).toBe(1); |
| 66 | + }); |
| 67 | + |
| 68 | + test("ignores non-exhaustion transport errors", () => { |
| 69 | + let disconnects = 0; |
| 70 | + const transport: { onerror?: (error: Error) => void } = {}; |
| 71 | + armResumeExhaustionSignal( |
| 72 | + transport, |
| 73 | + () => true, |
| 74 | + () => { |
| 75 | + disconnects += 1; |
| 76 | + }, |
| 77 | + ); |
| 78 | + transport.onerror?.(new Error("Failed to open SSE stream")); |
| 79 | + expect(disconnects).toBe(0); |
| 80 | + }); |
| 81 | + |
| 82 | + test("stays silent for a transport superseded by a recovery redial", () => { |
| 83 | + let disconnects = 0; |
| 84 | + const seen: string[] = []; |
| 85 | + const transport: { onerror?: (error: Error) => void } = { |
| 86 | + onerror: (error) => { |
| 87 | + seen.push(error.message); |
| 88 | + }, |
| 89 | + }; |
| 90 | + armResumeExhaustionSignal( |
| 91 | + transport, |
| 92 | + () => false, |
| 93 | + () => { |
| 94 | + disconnects += 1; |
| 95 | + }, |
| 96 | + ); |
| 97 | + transport.onerror?.( |
| 98 | + new Error("Maximum reconnection attempts (1000) exceeded."), |
| 99 | + ); |
| 100 | + expect(seen).toEqual(["Maximum reconnection attempts (1000) exceeded."]); |
| 101 | + expect(disconnects).toBe(0); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe("disarmTransportDeathSignal", () => { |
| 106 | + test("keeps protocol onclose and stays silent for the owner", () => { |
| 107 | + let disconnects = 0; |
| 108 | + let protocolCloses = 0; |
| 109 | + const live: LiveHttpTransport = {}; |
| 110 | + const transport: { |
| 111 | + onclose?: () => void; |
| 112 | + onerror?: (error: Error) => void; |
| 113 | + } = { |
| 114 | + onclose: () => { |
| 115 | + protocolCloses += 1; |
| 116 | + }, |
| 117 | + }; |
| 118 | + live.current = transport; |
| 119 | + armResumeExhaustionSignal( |
| 120 | + transport, |
| 121 | + () => live.current === transport && live.disarmed !== true, |
| 122 | + () => { |
| 123 | + disconnects += 1; |
| 124 | + }, |
| 125 | + ); |
| 126 | + |
| 127 | + disarmTransportDeathSignal(live); |
| 128 | + |
| 129 | + live.current?.onclose?.(); |
| 130 | + live.current?.onerror?.( |
| 131 | + new Error("Maximum reconnection attempts (1000) exceeded."), |
| 132 | + ); |
| 133 | + expect(disconnects).toBe(0); |
| 134 | + expect(protocolCloses).toBe(1); |
| 135 | + expect(live.disarmed).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + test("tolerates an empty handle", () => { |
| 139 | + expect(() => disarmTransportDeathSignal({})).not.toThrow(); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +const RESUME_EXHAUSTED = "Maximum reconnection attempts (1000) exceeded."; |
| 144 | + |
| 145 | +/** Transport that answers initialize and then never completes a request. */ |
| 146 | +class HungToolsTransport implements Transport { |
| 147 | + onclose?: () => void; |
| 148 | + onerror?: (error: Error) => void; |
| 149 | + onmessage?: (message: JSONRPCMessage) => void; |
| 150 | + |
| 151 | + async start(): Promise<void> { |
| 152 | + return undefined; |
| 153 | + } |
| 154 | + |
| 155 | + async close(): Promise<void> { |
| 156 | + this.onclose?.(); |
| 157 | + } |
| 158 | + |
| 159 | + async send(message: JSONRPCMessage): Promise<void> { |
| 160 | + if (!("method" in message) || !("id" in message)) return; |
| 161 | + if (message.method !== "initialize") return; |
| 162 | + this.onmessage?.({ |
| 163 | + jsonrpc: "2.0", |
| 164 | + id: message.id, |
| 165 | + result: { |
| 166 | + protocolVersion: "2025-11-25", |
| 167 | + capabilities: { tools: {} }, |
| 168 | + serverInfo: { name: "hung", version: "0" }, |
| 169 | + }, |
| 170 | + }); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +describe("resume exhaustion closes in-flight SDK requests", () => { |
| 175 | + test("rejects listTools when the SDK reports resume exhaustion", async () => { |
| 176 | + const client = new Client({ name: "resume-exhaustion", version: "0" }); |
| 177 | + const transport = new HungToolsTransport(); |
| 178 | + const live: LiveHttpTransport = {}; |
| 179 | + let disconnects = 0; |
| 180 | + armResumeExhaustionSignal( |
| 181 | + transport, |
| 182 | + () => live.current === transport && live.disarmed !== true, |
| 183 | + () => { |
| 184 | + disconnects += 1; |
| 185 | + // Owner redial: MCPClient.close disarms, then Protocol.close. |
| 186 | + disarmTransportDeathSignal(live); |
| 187 | + void client.close(); |
| 188 | + }, |
| 189 | + ); |
| 190 | + live.current = transport; |
| 191 | + await client.connect(transport); |
| 192 | + |
| 193 | + const pending = client.listTools(); |
| 194 | + transport.onerror?.(new Error(RESUME_EXHAUSTED)); |
| 195 | + |
| 196 | + const outcome = await Promise.race([ |
| 197 | + pending.then( |
| 198 | + () => "resolved" as const, |
| 199 | + (err: unknown) => err, |
| 200 | + ), |
| 201 | + new Promise<"pending">((resolve) => { |
| 202 | + setTimeout(() => resolve("pending"), 500); |
| 203 | + }), |
| 204 | + ]); |
| 205 | + |
| 206 | + expect(disconnects).toBe(1); |
| 207 | + expect(outcome).not.toBe("pending"); |
| 208 | + expect(outcome).not.toBe("resolved"); |
| 209 | + expect(outcome).toBeInstanceOf(Error); |
| 210 | + if (!(outcome instanceof Error)) return; |
| 211 | + expect(outcome.message).toBe("MCP error -32000: Connection closed"); |
| 212 | + expect("code" in outcome && outcome.code).toBe(-32000); |
| 213 | + expect(client.transport).toBeUndefined(); |
| 214 | + }); |
| 215 | +}); |
0 commit comments