Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: reject requests after the MCP event stream disconnects

Afloatd33e42a5f3df
diff --git a/server/mcp-http.ts b/server/mcp-http.ts
index 93862dd36c..036ae21576 100644
--- a/server/mcp-http.ts
+++ b/server/mcp-http.ts
@@ -200,6 +200,7 @@ export class RemoteMcpClient {
private closed = false;
private readonly pending = new Map<number, Pending>();
private sseEndpoint: Promise<string> | null = null;
+ private sseFailure: Error | undefined;
private readonly sseAbort = new AbortController();
constructor(target: RemoteMcpSpec, options: { fetch?: typeof fetch; maxBytes?: number; onNotification?: (message: JsonRpcMessage) => void } = {}) {
@@ -338,6 +339,7 @@ export class RemoteMcpClient {
this.sseEndpoint = new Promise<string>((resolveEndpoint, rejectEndpoint) => {
let settled = false;
const fail = (error: Error) => {
+ this.sseFailure = error;
if (!settled) {
settled = true;
rejectEndpoint(error);
@@ -400,6 +402,7 @@ export class RemoteMcpClient {
private async ssePost(frame: unknown, signal: AbortSignal): Promise<void> {
const endpoint = await this.openSseStream(signal);
+ if (this.sseFailure) throw this.sseFailure;
const response = await this.post(endpoint, frame, signal);
drain(response);
}
diff --git a/server/mcp-sse-disconnect.test.ts b/server/mcp-sse-disconnect.test.ts
new file mode 100644
index 0000000000..6f6bb3bc8a
--- /dev/null
+++ b/server/mcp-sse-disconnect.test.ts
@@ -0,0 +1,57 @@
+import { once } from "node:events";
+import { createServer, type ServerResponse } from "node:http";
+import type { AddressInfo } from "node:net";
+import { describe, expect, it } from "vitest";
+
+import { McpHttpError, RemoteMcpClient } from "./mcp-http.ts";
+
+describe("a disconnected legacy MCP event stream", () => {
+ it.each([
+ ["EOF", "request"], ["EOF", "notification"],
+ ["reset", "request"], ["reset", "notification"],
+ ] as const)("refuses a later %s %s without posting to the orphaned endpoint", async (end, operation) => {
+ let stream: ServerResponse | undefined;
+ const posted: string[] = [];
+ const server = createServer(async (req, res) => {
+ if (req.method === "GET") {
+ stream = res;
+ res.writeHead(200, { "content-type": "text/event-stream" });
+ res.write("event: endpoint\ndata: /messages\n\n");
+ return;
+ }
+ const chunks: Buffer[] = [];
+ for await (const chunk of req) chunks.push(Buffer.from(chunk));
+ const frame = JSON.parse(Buffer.concat(chunks).toString()) as { id?: number; method: string };
+ posted.push(frame.method);
+ res.writeHead(202).end();
+ if (frame.method === "initialize") {
+ stream!.write(`data: ${JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: { name: "fixture", version: "1" } } })}\n\n`);
+ } else if (frame.method === "tools/call") {
+ if (end === "EOF") stream!.end();
+ else stream!.destroy();
+ }
+ });
+ server.listen(0, "127.0.0.1");
+ await once(server, "listening");
+ const client = new RemoteMcpClient({ type: "sse", url: `http://127.0.0.1:${(server.address() as AddressInfo).port}/sse`, headers: {} });
+ try {
+ await client.initialize("fixture", AbortSignal.timeout(5_000));
+ const failure: unknown = await client.request("tools/call", { name: "first" }, AbortSignal.timeout(5_000)).catch((error: unknown) => error);
+ expect(failure).toBeInstanceOf(McpHttpError);
+ expect(failure).toMatchObject({ kind: "network", message: end === "EOF" ? "event stream ended" : "event stream failed" });
+ const before = [...posted];
+ const signal = AbortSignal.timeout(1_000);
+ const later = operation === "request"
+ ? client.request("tools/call", { name: "later" }, signal)
+ : client.notify("notifications/tools/list_changed", undefined, signal);
+ await expect(later).rejects.toBe(failure);
+ expect(posted).toEqual(before);
+ } finally {
+ await client.close();
+ const closed = once(server, "close");
+ server.close();
+ server.closeAllConnections();
+ await closed;
+ }
+ });
+});