Skip to content

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

fix(mcp): handle null refusal body and label Retry-After seconds

ARAYN7011e8e97f8e82a9
diff --git a/server/mcp-oauth.test.ts b/server/mcp-oauth.test.ts
index 284a7c4a35..79a93ac22c 100644
--- a/server/mcp-oauth.test.ts
+++ b/server/mcp-oauth.test.ts
@@ -117,7 +117,7 @@ describe("McpOAuthManager sign-in", () => {
it("shows the server's own reason when it refuses to register the app", async () => {
await setup({ registerRefusal: { status: 429, body: { error: "too_many_requests", error_description: "Too many client registrations from this address. Please try again in an hour." }, headers: { "retry-after": "3600" } } });
const error = await manager.start("docs", mcp.url).catch((cause: unknown) => cause);
- expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in (HTTP 429): Too many client registrations from this address. Please try again in an hour. It asked to wait: Retry-After 3600. Try again later.");
+ expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in (HTTP 429): Too many client registrations from this address. Please try again in an hour. It asked to wait: Retry-After 3600 seconds. Try again later.");
});
it("keeps the plain refusal when the server gives no reason", async () => {
@@ -126,6 +126,12 @@ describe("McpOAuthManager sign-in", () => {
expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in. Try again later.");
});
+ it("still reports the refusal when the body is JSON null", async () => {
+ await setup({ registerRefusal: { status: 429, body: null, headers: { "retry-after": "Wed, 21 Oct 2026 07:28:00 GMT" } } });
+ const error = await manager.start("docs", mcp.url).catch((cause: unknown) => cause);
+ expect((error as Error).message).toBe("This server would not register OpenMausBot for sign-in (HTTP 429). It asked to wait: Retry-After Wed, 21 Oct 2026 07:28:00 GMT. Try again later.");
+ });
+
it("refuses a server that does not use OAuth", async () => {
await setup();
const open = await startFakeHttpMcp();
diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.ts
index aef04a6c33..ae0b7661e2 100644
--- a/server/mcp-oauth.ts
+++ b/server/mcp-oauth.ts
@@ -206,7 +206,7 @@ class RegistrationRefused extends Error {}
function registrationRefusal(response: Response, description: unknown): string {
const reason = typeof description === "string" ? description.replace(/\s+/g, " ").trim().slice(0, 300) : "";
const retryAfter = response.headers.get("retry-after")?.trim().slice(0, 40);
- const wait = retryAfter ? ` It asked to wait: Retry-After ${retryAfter}.` : "";
+ const wait = retryAfter ? ` It asked to wait: Retry-After ${/^\d+$/.test(retryAfter) ? `${retryAfter} seconds` : retryAfter}.` : "";
if (!reason && !wait) return "This server would not register OpenMausBot for sign-in. Try again later.";
return `This server would not register OpenMausBot for sign-in (HTTP ${response.status})${reason ? `: ${reason}` : "."}${wait} Try again later.`;
}
@@ -512,7 +512,8 @@ export class McpOAuthManager {
redirect: "error",
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
- const body = await response.json().catch(() => ({})) as { client_id?: unknown; error_description?: unknown };
+ const parsed: unknown = await response.json().catch(() => null);
+ const body = (parsed && typeof parsed === "object" ? parsed : {}) as { client_id?: unknown; error_description?: unknown };
if (!response.ok || typeof body.client_id !== "string" || !body.client_id) {
throw new RegistrationRefused(registrationRefusal(response, body.error_description));
}
diff --git a/server/testing/fake-oauth-server.ts b/server/testing/fake-oauth-server.ts
index 75d98015cf..3a8cc41c1e 100644
--- a/server/testing/fake-oauth-server.ts
+++ b/server/testing/fake-oauth-server.ts
@@ -15,7 +15,7 @@ export interface FakeOAuthOptions {
/** refuse to register a client for a redirect URI this matches */
refuseRedirect?: (redirectUri: string) => boolean;
/** answer every registration with this status, JSON body and headers */
- registerRefusal?: { status: number; body: Record<string, unknown>; headers?: Record<string, string> };
+ registerRefusal?: { status: number; body: unknown; headers?: Record<string, string> };
/** answer every refresh with invalid_grant */
rejectRefresh?: boolean;
/** /authorize answers ?error=access_denied instead of a code */