Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: preserve MCP SSE framing across split CRLF bytes
Afloat930ff7d757a0
diff --git a/server/mcp-http-framing.test.ts b/server/mcp-http-framing.test.tsnew file mode 100644index 0000000000..04501de19c--- /dev/null+++ b/server/mcp-http-framing.test.ts@@ -0,0 +1,37 @@+import { describe, expect, it } from "vitest";++import { RemoteMcpClient } from "./mcp-http.ts";++describe("MCP event stream framing", () => {+ it.each([+ ["CRLF split across chunks", "\r\n", true],+ ["CRLF in one chunk", "\r\n", false],+ ["LF split across chunks", "\n", true],+ ["CR split across chunks", "\r", true],+ ] as const)("reads multiline data with %s", async (_label, newline, split) => {+ const text = [+ "event: message",+ 'data: {"jsonrpc":"2.0",',+ 'data: "id":1,"result":{"text":"日本語"}}',+ "",+ "",+ ].join(newline);+ const bytes = new TextEncoder().encode(text);+ const chunks = split ? Array.from(bytes, (byte) => Uint8Array.of(byte)) : [bytes];+ const client = new RemoteMcpClient({ type: "http", url: "https://fixture.example/mcp", headers: {} }, {+ fetch: async () => new Response(new ReadableStream<Uint8Array>({+ start(controller) {+ for (const chunk of chunks) controller.enqueue(chunk);+ controller.close();+ },+ }), { headers: { "content-type": "text/event-stream" } }),+ });++ try {+ await expect(client.request("tools/call", { name: "read" }, new AbortController().signal))+ .resolves.toEqual({ text: "日本語" });+ } finally {+ await client.close();+ }+ });+});diff --git a/server/mcp-http.ts b/server/mcp-http.tsindex 26e7584e94..50933dd023 100644--- a/server/mcp-http.ts+++ b/server/mcp-http.ts@@ -122,6 +122,7 @@ async function readSse(if (!reader) throw new McpHttpError("protocol", "empty event stream");const decoder = new TextDecoder();let buffer = "";+ let afterCr = false;let bytes = 0;const onAbort = () => void reader.cancel().catch(() => {});signal.addEventListener("abort", onAbort, { once: true });@@ -131,7 +132,12 @@ async function readSse(if (done) return;bytes += value.byteLength;if (bytes > maxBytes) throw new McpHttpError("protocol", "event stream too large");- buffer += decoder.decode(value, { stream: true }).replace(/\r\n?/g, "\n");+ const chunk = decoder.decode(value, { stream: true });+ if (chunk) {+ const start = afterCr && chunk.startsWith("\n") ? 1 : 0;+ afterCr = chunk.endsWith("\r");+ buffer += chunk.slice(start).replace(/\r\n?/g, "\n");+ }let separator: number;while ((separator = buffer.indexOf("\n\n")) !== -1) {const block = buffer.slice(0, separator);