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 22, 04:30 UTC
fix(mcp): accept CR and mixed line endings in response streams (#29727)
Co-authored-by: maria-rcks <maria@kuuro.net>
Romil committed
Nov 4, 2026 at 10:41 UTC
76f29667c6c51b19adc8108e4c52752c35e06111
2 files changed
+79
-6
packages/shared/src/mcpResponsePayloads.test.ts
new
+71
@@ -0,0 +1,71 @@
1
+import { expect, it } from "@effect/vitest";
2
+import * as Effect from "effect/Effect";
3
+import * as Stream from "effect/Stream";
4
+
5
+import { responsePayloads } from "./mcpResponsePayloads.ts";
6
+
7
+it.effect("reads SSE line endings across byte-chunk boundaries without splitting CRLF", () =>
8
+ Effect.gen(function* () {
9
+ for (const separator of [
10
+ "\n\n",
11
+ "\r\r",
12
+ "\r\n\r\n",
13
+ "\r\n\n",
14
+ "\n\r",
15
+ "\n\r\n",
16
+ "\r\r\n",
17
+ "\r\n\r",
18
+ ]) {
19
+ for (const dataLineSeparator of ["\n", "\r", "\r\n"]) {
20
+ const bytes = new TextEncoder().encode(
21
+ `: keep-alive${separator}data: {"id":1,${dataLineSeparator}data: "result":{"text":"café😀"}}${separator}` +
22
+ 'data: {"id":2,"result":{}}\n\ndata: {"id":3}',
23
+ );
24
+ for (let cut = 0; cut <= bytes.length; cut += 1) {
25
+ const response = new Response(
26
+ new ReadableStream<Uint8Array>({
27
+ start(controller) {
28
+ controller.enqueue(bytes.subarray(0, cut));
29
+ controller.enqueue(bytes.subarray(cut));
30
+ controller.close();
31
+ },
32
+ }),
33
+ { headers: { "content-type": "text/event-stream" } },
34
+ );
35
+ const payloads = yield* Stream.runCollect(
36
+ responsePayloads(response, (cause) => ({
37
+ _tag: "ResponsePayloadError" as const,
38
+ cause,
39
+ separator,
40
+ dataLineSeparator,
41
+ cut,
42
+ })),
43
+ );
44
+ expect(
45
+ Array.from(payloads),
46
+ JSON.stringify({ separator, dataLineSeparator, cut }),
47
+ ).toEqual([
48
+ { id: 1, result: { text: "café😀" } },
49
+ { id: 2, result: {} },
50
+ ]);
51
+ }
52
+ }
53
+ const response = new Response(
54
+ new ReadableStream<Uint8Array>({
55
+ start(controller) {
56
+ controller.enqueue(new TextEncoder().encode(`data: {"id":4}${separator}`));
57
+ },
58
+ }),
59
+ { headers: { "content-type": "text/event-stream" } },
60
+ );
61
+ const payloads = yield* Stream.runCollect(
62
+ responsePayloads(response, (cause) => ({
63
+ _tag: "ResponsePayloadError" as const,
64
+ cause,
65
+ separator,
66
+ })).pipe(Stream.take(1)),
67
+ );
68
+ expect(Array.from(payloads)).toEqual([{ id: 4 }]);
69
+ }
70
+ }),
71
+);
packages/shared/src/mcpResponsePayloads.ts
+8
-6
@@ -2,23 +2,25 @@
2
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
+
7
async function* sseDataLines(response: Response): AsyncGenerator<string> {
8
if (response.body === null) return;
9
const decoder = new TextDecoder();
10
let buffered = "";
11
for await (const chunk of response.body) {
12
buffered += decoder.decode(chunk as Uint8Array, { stream: true });
11
- let separatorIndex = buffered.search(/\n\n|\r\n\r\n/u);
12
- while (separatorIndex !== -1) {
13
- const rawEvent = buffered.slice(0, separatorIndex);
14
- buffered = buffered.slice(separatorIndex).replace(/^(?:\r?\n){2}/u, "");
13
+ let separator = SSE_EVENT_SEPARATOR.exec(buffered);
14
+ while (separator !== null) {
15
+ const rawEvent = buffered.slice(0, separator.index);
16
+ buffered = buffered.slice(separator.index + separator[0].length);
17
const data = rawEvent
16
- .split(/\r?\n/u)
18
+ .split(/\r\n|\r|\n/u)
19
.filter((line) => line.startsWith("data:"))
20
.map((line) => line.slice("data:".length).trimStart())
21
.join("\n");
22
if (data.length > 0) yield data;
21
- separatorIndex = buffered.search(/\n\n|\r\n\r\n/u);
23
+ separator = SSE_EVENT_SEPARATOR.exec(buffered);
24
}
25
}
26
}