The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 08:30 UTC
fix(shared): large MCP SSE results in small chunks parse in linear time (#38249)
Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 17, 2026 at 19:23 UTC
1063b42768cb2ae43f123a220b6a2188df4b5998
2 files changed
+47
-11
packages/shared/src/mcpResponsePayloads.test.ts
+24
@@ -71,6 +71,30 @@ it.effect("reads SSE line endings across byte-chunk boundaries without splitting
71
}),
72
);
73
74
+it.effect("reads a multi-megabyte SSE event delivered in small chunks", () =>
75
+ Effect.gen(function* () {
76
+ const text = "x".repeat(8 * 1024 * 1024);
77
+ const bytes = new TextEncoder().encode(
78
+ `data: ${JSON.stringify({ id: 1, result: { text } })}\n\n`,
79
+ );
80
+ let offset = 0;
81
+ const response = new Response(
82
+ new ReadableStream<Uint8Array>({
83
+ pull(controller) {
84
+ if (offset === bytes.length) return controller.close();
85
+ controller.enqueue(bytes.subarray(offset, offset + 256));
86
+ offset = Math.min(offset + 256, bytes.length);
87
+ },
88
+ }),
89
+ { headers: { "content-type": "text/event-stream" } },
90
+ );
91
+ const payloads = yield* Stream.runCollect(
92
+ responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })),
93
+ );
94
+ expect(Array.from(payloads)).toEqual([{ id: 1, result: { text } }]);
95
+ }),
96
+);
97
+
98
it.effect("picks the SSE reader from the media type alone, whatever its case", () =>
99
Effect.gen(function* () {
100
const response = new Response('data: {"id":1}\n\n', {
packages/shared/src/mcpResponsePayloads.ts
+23
-11
@@ -3,28 +3,40 @@ import * as Effect from "effect/Effect";
3
import * as Stream from "effect/Stream";
4
5
const SSE_EVENT_SEPARATOR = /(?:\r\n|\r(?!\n)|\n){2}/u;
6
+// A separator is at most four characters, so one that a new chunk completes starts within the
7
+// last three characters already read. Scanning only those plus the chunk keeps parsing linear.
8
+const SEPARATOR_OVERLAP = 3;
9
+
10
+function sseEventData(rawEvent: string): string {
11
+ return rawEvent
12
+ .split(/\r\n|\r|\n/u)
13
+ .filter((line) => line.startsWith("data:"))
14
+ .map((line) => line.slice("data:".length).trimStart())
15
+ .join("\n");
16
+}
17
18
async function* sseDataLines(
19
reader: ReadableStreamDefaultReader<Uint8Array>,
20
): AsyncGenerator<string> {
21
const decoder = new TextDecoder();
11
- let buffered = "";
22
+ let eventParts: string[] = [];
23
+ let unscanned = "";
24
while (true) {
25
const { done, value } = await reader.read();
26
if (done) return;
15
- buffered += decoder.decode(value, { stream: true });
16
- let separator = SSE_EVENT_SEPARATOR.exec(buffered);
27
+ let text = unscanned + decoder.decode(value, { stream: true });
28
+ let separator = SSE_EVENT_SEPARATOR.exec(text);
29
while (separator !== null) {
18
- const rawEvent = buffered.slice(0, separator.index);
19
- buffered = buffered.slice(separator.index + separator[0].length);
20
- const data = rawEvent
21
- .split(/\r\n|\r|\n/u)
22
- .filter((line) => line.startsWith("data:"))
23
- .map((line) => line.slice("data:".length).trimStart())
24
- .join("\n");
30
+ eventParts.push(text.slice(0, separator.index));
31
+ const data = sseEventData(eventParts.join(""));
32
+ eventParts = [];
33
+ text = text.slice(separator.index + separator[0].length);
34
if (data.length > 0) yield data;
26
- separator = SSE_EVENT_SEPARATOR.exec(buffered);
35
+ separator = SSE_EVENT_SEPARATOR.exec(text);
36
}
37
+ const scannedLength = Math.max(0, text.length - SEPARATOR_OVERLAP);
38
+ eventParts.push(text.slice(0, scannedLength));
39
+ unscanned = text.slice(scannedLength);
40
}
41
}
42