Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): route briefing footer choice via proxy; echo state on MCP OAuth errors (#15311)

Nicholas Tindleb5240e95cd7d
diff --git a/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/__tests__/route.test.ts b/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/__tests__/route.test.ts
index 291ace6384..228d1e3e3e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/__tests__/route.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/__tests__/route.test.ts
@@ -54,6 +54,33 @@ describe("MCP OAuth callback route", () => {
expect(message.message).toContain("Missing parameters");
});
+ it("echoes state and the provider error when sign-in is denied", async () => {
+ const response = await GET(
+ new Request(
+ `${origin}/auth/integrations/mcp_callback?error=access_denied&error_description=${encodeURIComponent("User denied access")}&state=xyz`,
+ ),
+ );
+
+ const message = embeddedMessage(await response.text());
+ expect(message.success).toBe(false);
+ // Without state the opener drops the message and waits out its timeout.
+ expect(message.state).toBe("xyz");
+ expect(message.message).toBe("access_denied: User denied access");
+ });
+
+ it("reports failure without state when the provider sends none", async () => {
+ const response = await GET(
+ new Request(
+ `${origin}/auth/integrations/mcp_callback?error=server_error`,
+ ),
+ );
+
+ const message = embeddedMessage(await response.text());
+ expect(message.success).toBe(false);
+ expect(message).not.toHaveProperty("state");
+ expect(message.message).toBe("server_error");
+ });
+
it("escapes script-breaking characters in query values", async () => {
const response = await GET(
new Request(
diff --git a/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/route.ts b/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/route.ts
index 821a2fe56a..bec47e763c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/route.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/auth/integrations/mcp_callback/route.ts
@@ -24,13 +24,20 @@ export async function GET(request: Request) {
const code = searchParams.get("code");
const state = searchParams.get("state");
const iss = searchParams.get("iss");
+ const error = searchParams.get("error");
+ const errorDescription = searchParams.get("error_description");
const success = Boolean(code && state);
const message = success
? { success: true, code, state, iss }
: {
success: false,
- message: `Missing parameters: ${searchParams.toString()}`,
+ // Echo back `state` when present so the opener's state-keyed listener
+ // routes the failure to its flow instead of waiting for the timeout.
+ ...(state ? { state } : {}),
+ message: error
+ ? `${error}${errorDescription ? `: ${errorDescription}` : ""}`
+ : `Missing parameters: ${searchParams.toString()}`,
};
return new NextResponse(
diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/account/__tests__/main.test.tsx b/autogpt_platform/frontend/src/app/(platform)/settings/account/__tests__/main.test.tsx
index 4ebd8dc8d1..b5b52bb006 100644
--- a/autogpt_platform/frontend/src/app/(platform)/settings/account/__tests__/main.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/settings/account/__tests__/main.test.tsx
@@ -255,6 +255,11 @@ describe("SettingsPreferencesPage", () => {
// One click, not two: the design calls these "live one-click links".
await waitFor(() => expect(applied).toBeDefined());
+ // Must reach the backend through the proxy, not the Next.js
+ // /api/auth/* (Better Auth) catch-all, which 404s.
+ expect(applied?.pathname).toBe(
+ "/api/proxy/api/auth/user/preferences/from-email",
+ );
expect(applied?.searchParams.get("choice")).toBe("alerts");
// Token-authenticated, so a bare ?f= from a third-party link is inert.
expect(applied?.searchParams.get("token")).toBe("signed-token");
diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/account/usePreferencesPage.ts b/autogpt_platform/frontend/src/app/(platform)/settings/account/usePreferencesPage.ts
index eaf29646e7..dcb2ce3ccc 100644
--- a/autogpt_platform/frontend/src/app/(platform)/settings/account/usePreferencesPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/settings/account/usePreferencesPage.ts
@@ -7,6 +7,7 @@ import { useQueryClient } from "@tanstack/react-query";
import {
getGetV1GetNotificationPreferencesQueryKey,
getGetV1GetUserTimezoneQueryKey,
+ postV1ApplyAVolumeKnobChoiceFromABriefingFooterLink,
useGetV1GetNotificationPreferences,
useGetV1GetUserTimezone,
usePostV1UpdateNotificationPreferences,
@@ -111,12 +112,13 @@ export function usePreferencesPage() {
next: NotificationSettings,
) {
// Token-authenticated, not session-authenticated — see the note on
- // footerChoice above.
- const res = await fetch(
- `/api/auth/user/preferences/from-email?choice=${encodeURIComponent(choice)}&token=${encodeURIComponent(token)}`,
- { method: "POST" },
- );
- if (!res.ok) throw new Error(`Preference link rejected (${res.status})`);
+ // footerChoice above. Goes through the backend proxy: a bare
+ // /api/auth/... fetch lands on the Next.js Better Auth catch-all and 404s.
+ // The generated client throws ApiError on a non-2xx response.
+ await postV1ApplyAVolumeKnobChoiceFromABriefingFooterLink({
+ choice,
+ token,
+ });
await queryClient.invalidateQueries({
queryKey: getGetV1GetNotificationPreferencesQueryKey(),
});