|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | + |
| 4 | +process.env.MCP_PATH_TOKEN = "test-token"; |
| 5 | +process.env.HOME_AGENT_URL = "http://127.0.0.1:9"; |
| 6 | +process.env.MAX_STREAM_MS = "250"; |
| 7 | + |
| 8 | +const { handle } = await import("../server.js"); |
| 9 | +const { boundStream } = await import("../src/stream-bound.js"); |
| 10 | + |
| 11 | +const ENDPOINT = "http://localhost/mcp/test-token"; |
| 12 | +const META = { |
| 13 | + "io.modelcontextprotocol/protocolVersion": "2026-07-28", |
| 14 | + "io.modelcontextprotocol/clientCapabilities": {}, |
| 15 | +}; |
| 16 | + |
| 17 | +function post(body: unknown, headers: Record<string, string> = {}): Promise<Response> { |
| 18 | + return handle( |
| 19 | + new Request(ENDPOINT, { |
| 20 | + method: "POST", |
| 21 | + headers: { |
| 22 | + "content-type": "application/json", |
| 23 | + accept: "application/json, text/event-stream", |
| 24 | + ...headers, |
| 25 | + }, |
| 26 | + body: JSON.stringify(body), |
| 27 | + }), |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +function modern(method: string, params: Record<string, unknown> = {}, id = 1) { |
| 32 | + return post( |
| 33 | + { jsonrpc: "2.0", id, method, params: { ...params, _meta: META } }, |
| 34 | + { "mcp-protocol-version": "2026-07-28", "mcp-method": method }, |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/** Reads the whole body, or fails if the stream is still open after `ms`. */ |
| 39 | +async function readWithin(res: Response, ms: number): Promise<string> { |
| 40 | + let timer: NodeJS.Timeout; |
| 41 | + const timeout = new Promise<never>((_, reject) => { |
| 42 | + timer = setTimeout(() => reject(new Error(`response body still open after ${ms}ms`)), ms); |
| 43 | + }); |
| 44 | + try { |
| 45 | + return await Promise.race([res.text(), timeout]); |
| 46 | + } finally { |
| 47 | + clearTimeout(timer!); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +test("a wrong path token is answered 404", async () => { |
| 52 | + const res = await handle(new Request("http://localhost/mcp/nope", { method: "POST", body: "{}" })); |
| 53 | + assert.equal(res.status, 404); |
| 54 | +}); |
| 55 | + |
| 56 | +test("legacy (2025) tools/list answers and closes the stream", async () => { |
| 57 | + const res = await post({ jsonrpc: "2.0", id: 1, method: "tools/list", params: {} }); |
| 58 | + assert.equal(res.status, 200); |
| 59 | + const body = await readWithin(res, 2000); |
| 60 | + assert.match(body, /"name":"get_transcript"/); |
| 61 | +}); |
| 62 | + |
| 63 | +test("server/discover does not advertise tools.listChanged (nothing can deliver it on Vercel)", async () => { |
| 64 | + const res = await modern("server/discover"); |
| 65 | + assert.equal(res.status, 200); |
| 66 | + const json = JSON.parse(await readWithin(res, 2000)) as { |
| 67 | + result: { capabilities: { tools?: { listChanged?: boolean } } }; |
| 68 | + }; |
| 69 | + assert.ok(json.result.capabilities.tools, "tools capability is still advertised"); |
| 70 | + assert.notEqual(json.result.capabilities.tools.listChanged, true); |
| 71 | +}); |
| 72 | + |
| 73 | +test("subscriptions/listen is acknowledged and closed at once instead of being held open", async () => { |
| 74 | + const started = Date.now(); |
| 75 | + const res = await modern("subscriptions/listen", { notifications: { toolsListChanged: true } }, 2); |
| 76 | + assert.equal(res.status, 200); |
| 77 | + const body = await readWithin(res, 2000); |
| 78 | + assert.match(body, /notifications\/subscriptions\/acknowledged/); |
| 79 | + assert.match(body, /"resultType":"complete"/); |
| 80 | + assert.ok(Date.now() - started < 2000); |
| 81 | +}); |
| 82 | + |
| 83 | +test("modern tools/list still works", async () => { |
| 84 | + const res = await modern("tools/list", {}, 3); |
| 85 | + assert.equal(res.status, 200); |
| 86 | + assert.match(await readWithin(res, 2000), /"name":"extract_wisdom"/); |
| 87 | +}); |
| 88 | + |
| 89 | +test("boundStream ends a stream that never closes on its own", async () => { |
| 90 | + const never = new ReadableStream<Uint8Array>({ |
| 91 | + start(controller) { |
| 92 | + controller.enqueue(new TextEncoder().encode(": hello\n\n")); |
| 93 | + // and then nothing, forever |
| 94 | + }, |
| 95 | + }); |
| 96 | + const started = Date.now(); |
| 97 | + const text = await new Response(boundStream(never, 200)).text(); |
| 98 | + assert.match(text, /hello/); |
| 99 | + assert.ok(Date.now() - started < 1500, `took ${Date.now() - started}ms`); |
| 100 | +}); |
| 101 | + |
| 102 | +test("boundStream passes a finishing stream through untouched", async () => { |
| 103 | + const src = new Response("a\nb\n").body!; |
| 104 | + assert.equal(await new Response(boundStream(src, 5000)).text(), "a\nb\n"); |
| 105 | +}); |
| 106 | + |
| 107 | +test("an event stream through handle() is cut at MAX_STREAM_MS even if the SDK holds it", async () => { |
| 108 | + // MAX_STREAM_MS=250 for this process. tools/list closes well before that; this checks the |
| 109 | + // wrapper does not break a normal, short stream. |
| 110 | + const res = await post({ jsonrpc: "2.0", id: 9, method: "ping" }); |
| 111 | + assert.equal(res.status, 200); |
| 112 | + assert.match(await readWithin(res, 2000), /"result":\{\}/); |
| 113 | +}); |
0 commit comments