Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
MCP over HTTP: read a stateless server's last event without its closing blank line
AdityaUmale2cb82212ab86
diff --git a/server/mcp-http-framing.test.ts b/server/mcp-http-framing.test.tsindex 04501de19c..0c2189dc76 100644--- a/server/mcp-http-framing.test.ts+++ b/server/mcp-http-framing.test.ts@@ -34,4 +34,42 @@ describe("MCP event stream framing", () => {await client.close();}});++ // A stateless server (one reply, then it hangs up) may close the stream without the blank line after its last event.+ it.each([+ ["LF", "\n", false],+ ["CRLF", "\r\n", false],+ ["LF split across chunks", "\n", true],+ ["no newline at all", "", false],+ ] as const)("reads a last event the server closed without a blank line (%s)", async (_label, newline, split) => {+ const text = ["event: message", 'data: {"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"get_quote"}]}}'].join(newline || "\n") + newline;+ const client = clientAnswering(text, split);+ try {+ await expect(client.request("tools/list", {}, new AbortController().signal)).resolves.toEqual({ tools: [{ name: "get_quote" }] });+ } finally {+ await client.close();+ }+ });++ it("still refuses a stream cut off in the middle of its last event", async () => {+ const client = clientAnswering('event: message\ndata: {"jsonrpc":"2.0","id":1,"res', false);+ try {+ await expect(client.request("tools/list", {}, new AbortController().signal)).rejects.toMatchObject({ kind: "protocol" });+ } finally {+ await client.close();+ }+ });});++function clientAnswering(text: string, split: boolean): RemoteMcpClient {+ const bytes = new TextEncoder().encode(text);+ const chunks = split ? Array.from(bytes, (byte) => Uint8Array.of(byte)) : [bytes];+ return 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" } }),+ });+}diff --git a/server/mcp-http.ts b/server/mcp-http.tsindex 50933dd023..e7a7b745f1 100644--- a/server/mcp-http.ts+++ b/server/mcp-http.ts@@ -117,6 +117,8 @@ async function readSse(signal: AbortSignal,onEvent: (event: SseEvent) => "stop" | undefined,maxBytes = MAX_REMOTE_MCP_BYTES,+ /** One POST's reply: a stateless server may hang up right after its last event, leaving off the closing blank line. */+ lastEventAtEnd = false,): Promise<void> {const reader = response.body?.getReader();if (!reader) throw new McpHttpError("protocol", "empty event stream");@@ -129,7 +131,11 @@ async function readSse(try {for (;;) {const { value, done } = await reader.read();- if (done) return;+ if (done) {+ const last = lastEventAtEnd ? parseSseBlock(buffer + decoder.decode()) : null;+ if (last) onEvent(last);+ return;+ }bytes += value.byteLength;if (bytes > maxBytes) throw new McpHttpError("protocol", "event stream too large");const chunk = decoder.decode(value, { stream: true });@@ -311,7 +317,7 @@ export class RemoteMcpClient {if (!message) return undefined;found = message;return "stop";- }, this.maxBytes);+ }, this.maxBytes, true);if (signal.aborted) throw new McpHttpError("network", "aborted");if (!found) throw new McpHttpError("protocol", "event stream ended without the response");return unwrap(found);