Skip to content

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

fix: retain uncorrelated RPC errors only as diagnostics

Afloat13faee0bf135
diff --git a/server/connector-tools-list.test.ts b/server/connector-tools-list.test.ts
index 3cb82d8dde..fde2e60519 100644
--- a/server/connector-tools-list.test.ts
+++ b/server/connector-tools-list.test.ts
@@ -39,6 +39,33 @@ describe("connector response correlation", () => {
expect(parseMcpResponse(events(error), "list-1")).toEqual(error);
expect(parseMcpResponse(events(error), "list-2")).toBeNull();
});
+
+ it.each(["json", "sse"] as const)("keeps null-ID %s errors as diagnostics without claiming correlation", (format) => {
+ const error = { code: -32700, message: "The request body could not be parsed" };
+ const frame = { jsonrpc: "2.0", id: null, error, result: { tools: [{ name: "must_not_be_used" }] } };
+ const answer = parseMcpResponse(format === "json" ? JSON.stringify(frame) : events(frame), "list-1");
+ expect(answer).toEqual({ error });
+ expect(answer).not.toHaveProperty("id");
+ expect(answer).not.toHaveProperty("result");
+ });
+
+ it("prefers a correlated response over null-ID diagnostics in either SSE order", () => {
+ const answer = response("list-1");
+ const diagnostic = { jsonrpc: "2.0", id: null, error: { code: -32600, message: "Invalid request" } };
+ expect(parseMcpResponse(events(diagnostic, answer), "list-1")).toEqual(answer);
+ expect(parseMcpResponse(events(answer, diagnostic), "list-1")).toEqual(answer);
+ });
+
+ it.each([
+ { jsonrpc: "2.0", id: null, result: { tools: [] } },
+ { jsonrpc: "2.0", id: null, method: "ping", error: { message: "not a reply" } },
+ { jsonrpc: "2.0", error: { message: "missing id" } },
+ { jsonrpc: "2.0", id: null, error: "invalid error shape" },
+ ])("does not promote invalid null-ID frames to replies", (frame) => {
+ expect(parseMcpResponse(JSON.stringify(frame), "list-1")).toBeNull();
+ expect(parseMcpResponse(events(frame), "list-1")).toBeNull();
+ });
+
});
describe("tools/list fallback cache", () => {
@@ -63,4 +90,26 @@ describe("tools/list fallback cache", () => {
expect(fallback.status).toBe(200);
expect(await fallback.json()).toEqual(response("list-2"));
});
+
+ it.each(["json", "sse"] as const)("never caches a null-ID %s error even if it also carries result data", async (format) => {
+ const backend = `null-error-${format}`;
+ const payload = { jsonrpc: "2.0", id: "list-1", method: "tools/list" };
+ const error = { code: -32700, message: "Invalid JSON from this client" };
+ const frame = { jsonrpc: "2.0", id: null, error, result: { tools: [{ name: "untrusted" }] } };
+ const failed = await withToolsListFallback(backend, payload, async () => new Response(format === "json" ? JSON.stringify(frame) : events(frame)));
+ expect(parseMcpResponse(await failed.text(), payload.id)).toEqual({ error });
+ const later = await withToolsListFallback(backend, { ...payload, id: "list-2" }, async () => new Response("unavailable", { status: 503 }));
+ expect(later.status).toBe(503);
+ expect(await later.text()).toBe("unavailable");
+ });
+
+ it("does not cache a correlated frame that contains an RPC error", async () => {
+ const payload = { jsonrpc: "2.0", id: "list-1", method: "tools/list" };
+ await withToolsListFallback("correlated-error", payload, async () => Response.json({
+ ...response("list-1"), error: { code: -32603, message: "Internal error" },
+ }));
+ const later = await withToolsListFallback("correlated-error", payload, async () => new Response(null, { status: 503 }));
+ expect(later.status).toBe(503);
+ });
+
});
diff --git a/server/connector-tools-list.ts b/server/connector-tools-list.ts
index a801f3c818..28127c436e 100644
--- a/server/connector-tools-list.ts
+++ b/server/connector-tools-list.ts
@@ -1,15 +1,23 @@
// An engine reads a connector's tools/list once, when its session starts: a list that fails then leaves the whole
// session without connected-app tools. So each backend's last good list stands in for a failed or rate-limited one.
+/** Return an exact reply, or an error-only diagnostic when the server could not identify the request. */
export function parseMcpResponse(text: string, id: unknown): Record<string, unknown> | null {
const trimmed = text.trim();
if (!trimmed) return null;
const candidate = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === "object" && !Array.isArray(value) &&
- "id" in value && value.id === id && !("method" in value) && ("result" in value || "error" in value);
+ "id" in value && value.id !== null && value.id === id && !("method" in value) && ("result" in value || "error" in value);
+ const diagnostic = (value: unknown): Record<string, unknown> | null => {
+ if (value === null || typeof value !== "object" || Array.isArray(value) || !("id" in value) || value.id !== null || "method" in value) return null;
+ const error = "error" in value ? value.error : undefined;
+ if (error === null || typeof error !== "object" || Array.isArray(error) || !("message" in error) || typeof error.message !== "string") return null;
+ // This is deliberately not a correlated response: it has no id or result to consume or cache.
+ return { error };
+ };
if (trimmed.startsWith("{")) {
const parsed: unknown = JSON.parse(trimmed);
- return candidate(parsed) ? parsed : null;
+ return candidate(parsed) ? parsed : diagnostic(parsed);
}
const frames = trimmed
.split(/\r?\n/)
@@ -22,9 +30,8 @@ export function parseMcpResponse(text: string, id: unknown): Record<string, unkn
} catch {
return [];
}
- })
- .filter(candidate);
- return frames.at(-1) ?? null;
+ });
+ return frames.filter(candidate).at(-1) ?? frames.map(diagnostic).filter((frame) => frame !== null).at(-1) ?? null;
}
const lastToolsLists = new Map<string, unknown>();
@@ -37,7 +44,9 @@ function toolsListId(payload: unknown): { id: unknown } | null {
function toolsResult(text: string, id: unknown): unknown {
try {
- const result = parseMcpResponse(text, id)?.result;
+ const frame = parseMcpResponse(text, id);
+ if (!frame || frame.id !== id || frame.id === null || "error" in frame) return undefined;
+ const result = frame.result;
return Array.isArray((result as { tools?: unknown } | undefined)?.tools) ? result : undefined;
} catch {
return undefined;