Skip to content

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

fix: classify JSON errors in connection health

Afloat8862a905c330
diff --git a/server/connection-outcome-json.test.ts b/server/connection-outcome-json.test.ts
new file mode 100644
index 0000000000..bce7cd9679
--- /dev/null
+++ b/server/connection-outcome-json.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+
+import { appCallOutcome } from "./connection-outcome.ts";
+
+const answer = (texts: string[], isError: boolean) => new TextEncoder().encode(JSON.stringify({
+ jsonrpc: "2.0", id: 1,
+ result: { isError, content: texts.map((text) => ({ type: "text", text })) },
+}));
+
+describe("JSON text in a failed MCP tool result", () => {
+ it.each([
+ ['"Unauthorized: token expired"', "sign-in"],
+ [JSON.stringify({ error: "invalid_token" }), "sign-in"],
+ [JSON.stringify({ message: "403 Forbidden: insufficient_scope" }), "refused"],
+ ])("classifies %s from the explicit error result", (text, error) => {
+ expect(appCallOutcome(200, answer([text], true), "application/json"))
+ .toEqual({ ok: false, error });
+ });
+
+ it("still reads a plain error when another content part is valid JSON", () => {
+ expect(appCallOutcome(200, answer([JSON.stringify({ requestId: "test" }), "Token expired; reconnect"], true), "application/json"))
+ .toEqual({ ok: false, error: "sign-in" });
+ });
+
+ it.each([
+ JSON.stringify({ message: "Unauthorized access happened in 2024" }),
+ '"403 Forbidden is an HTTP status"',
+ ])("does not classify successful returned data: %s", (text) => {
+ expect(appCallOutcome(200, answer([text], false), "application/json")).toEqual({ ok: true });
+ });
+
+ it("keeps an unrelated tool error as evidence the account works", () => {
+ expect(appCallOutcome(200, answer([JSON.stringify({ error: "Message not found" })], true), "application/json"))
+ .toEqual({ ok: true });
+ });
+});
diff --git a/server/connection-outcome.ts b/server/connection-outcome.ts
index 429bad6567..71bd83eb57 100644
--- a/server/connection-outcome.ts
+++ b/server/connection-outcome.ts
@@ -65,11 +65,10 @@ export function appCallOutcome(status: number, bytes: Uint8Array, contentType: s
? frame.result.content.flatMap((item: { type?: unknown; text?: unknown }) => (item?.type === "text" && typeof item.text === "string" ? [item.text] : []))
: [];
const errors: string[] = [];
- let parsed = false;
for (const text of texts) {
- try { failedErrors(JSON.parse(text), 0, errors); parsed = true; } catch {}
+ try { failedErrors(JSON.parse(text), 0, errors); } catch {}
}
- if (!parsed && frame.result.isError === true) errors.push(...texts);
+ if (frame.result.isError === true) errors.push(...texts);
if (!errors.length) return { ok: true };
return classify(errors.join("\n").slice(0, 4_000)) ?? { ok: true };
}