Skip to content

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

fix(mcp): sign-in shows why a server refused to register the app

ARAYN7011a27236595b7b
diff --git a/server/mcp-oauth.test.ts b/server/mcp-oauth.test.ts
index 2f3786924a..284a7c4a35 100644
--- a/server/mcp-oauth.test.ts
+++ b/server/mcp-oauth.test.ts
@@ -114,6 +114,18 @@ describe("McpOAuthManager sign-in", () => {
await expect(manager.start("docs", mcp.url)).rejects.toMatchObject({ code: "no-registration" });
});
+ 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.");
+ });
+
+ it("keeps the plain refusal when the server gives no reason", async () => {
+ await setup({ registerRefusal: { status: 500, body: {} } });
+ 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. 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 1fd75a9999..aef04a6c33 100644
--- a/server/mcp-oauth.ts
+++ b/server/mcp-oauth.ts
@@ -203,6 +203,14 @@ function tokensFrom(body: Record<string, unknown>, previous?: McpOAuthTokens): M
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}.` : "";
+ 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.`;
+}
+
export class McpOAuthManager {
private readonly store: McpOAuthStore;
private readonly lifetimeMs: number;
@@ -504,9 +512,9 @@ export class McpOAuthManager {
redirect: "error",
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
- const body = await response.json().catch(() => ({})) as { client_id?: unknown };
+ const body = await response.json().catch(() => ({})) as { client_id?: unknown; error_description?: unknown };
if (!response.ok || typeof body.client_id !== "string" || !body.client_id) {
- throw new RegistrationRefused("This server would not register OpenMausBot for sign-in. Try again later.");
+ throw new RegistrationRefused(registrationRefusal(response, body.error_description));
}
return body.client_id;
}
diff --git a/server/testing/fake-oauth-server.ts b/server/testing/fake-oauth-server.ts
index 135d7c2830..75d98015cf 100644
--- a/server/testing/fake-oauth-server.ts
+++ b/server/testing/fake-oauth-server.ts
@@ -14,6 +14,8 @@ export interface FakeOAuthOptions {
noRegistration?: boolean;
/** 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> };
/** answer every refresh with invalid_grant */
rejectRefresh?: boolean;
/** /authorize answers ?error=access_denied instead of a code */
@@ -133,6 +135,10 @@ export async function startFakeOAuth(options: FakeOAuthOptions = {}): Promise<Fa
if (req.method === "POST" && url.pathname === "/register") {
counts.register += 1;
await options.beforeRegister?.();
+ if (options.registerRefusal) {
+ res.writeHead(options.registerRefusal.status, { "content-type": "application/json", ...options.registerRefusal.headers });
+ return res.end(JSON.stringify(options.registerRefusal.body));
+ }
const body = JSON.parse(await readBody(req)) as { redirect_uris?: string[] };
if (body.redirect_uris?.some((uri) => options.refuseRedirect?.(uri))) return json(res, 400, { error: "invalid_redirect_uri" });
const clientId = `client_${counts.register}`;