MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / readMcpJson

Function readMcpJson

apps/host-cloudflare/src/worker.e2e.node.test.ts:54–86  ·  view source on GitHub ↗
(response: JsonReadableResponse)

Source from the content-addressed store, hash-verified

52}
53
54const readMcpJson = async <A>(response: JsonReadableResponse): Promise<A> => {
55 const contentType = response.headers.get("content-type") ?? "";
56 if (contentType.includes("application/json")) {
57 return (await response.json()) as A;
58 }
59 // Parse the SSE stream properly instead of grabbing the first `data:` line:
60 // the server may legally interleave other events (e.g. the priming event a
61 // tools/call stream emits so clients can reconnect) before the JSON-RPC
62 // message. Only `message`-typed events (the SSE default) carry JSON-RPC.
63 const text = await response.text();
64 let responseMessage: unknown;
65 for (const block of text.split("\n\n")) {
66 let eventType = "message";
67 let data = "";
68 for (const line of block.replace(/\r/g, "").split("\n")) {
69 if (line.startsWith("event:")) eventType = line.slice("event:".length).trim();
70 if (line.startsWith("data:")) data += line.slice("data:".length).trimStart();
71 }
72 if (eventType !== "message" || data.length === 0) continue;
73 const parsed = decodeUnknownJson(data);
74 // Skip protocol-legal notifications; the tests want the response.
75 if (
76 typeof parsed === "object" &&
77 parsed !== null &&
78 ("result" in parsed || "error" in parsed)
79 ) {
80 responseMessage = parsed;
81 break;
82 }
83 }
84 expect(responseMessage, "a JSON-RPC response arrives on the SSE stream").toBeTruthy();
85 return responseMessage as A;
86};
87
88describe("cloudflare host e2e (workerd/miniflare)", () => {
89 let worker: Unstable_DevWorker;

Callers

nothing calls this directly

Calls 4

getMethod · 0.65
jsonMethod · 0.65
textMethod · 0.65
replaceMethod · 0.65

Tested by

no test coverage detected