[GH-ISSUE #3862] Redirect not working for MCP after logging in from an empty state auth #27069

Closed
opened 2026-04-17 17:52:12 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @iboughtbed on GitHub (Aug 7, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3862

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

You can use the MCP plugin, set up any social provider for login. Empty the state, remove the cookies. After that, head to your MCP agents (e.g. Cursor), click on login, and proceed to login to your user account. It returns JSON, instead of redirecting after user oAuth.

Image

Current vs. Expected behavior

It should've redirected to the MCP agents (e.g. Cursor), but I had to manually copy the url.

What version of Better Auth are you using?

1.3.1

Provide environment information

- OS: Windows 10
- Browser: Chrome

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

mcp plugin

Additional context

No response

Originally created by @iboughtbed on GitHub (Aug 7, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3862 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce You can use the MCP plugin, set up any social provider for login. Empty the state, remove the cookies. After that, head to your MCP agents (e.g. Cursor), click on login, and proceed to login to your user account. It returns JSON, instead of redirecting after user oAuth. <img width="1920" height="140" alt="Image" src="https://github.com/user-attachments/assets/1b5f602f-24a7-4027-b2cb-a365cb8b1b79" /> ### Current vs. Expected behavior It should've redirected to the MCP agents (e.g. Cursor), but I had to manually copy the url. ### What version of Better Auth are you using? 1.3.1 ### Provide environment information ```bash - OS: Windows 10 - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript mcp plugin ``` ### Additional context _No response_
GiteaMirror added the locked label 2026-04-17 17:52:12 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 7, 2025):

Hi @iboughtbed! I'm Dosu and I’m helping the better-auth team.

This is a known and intentional behavior in Better Auth's MCP plugin. When state or cookies are missing (such as after clearing cookies or starting from an empty state), the backend can't determine where to redirect and instead returns a JSON response with a redirect URL. This is by design to support frontend-driven redirects, especially for custom URI schemes and mobile/SPA flows, since browsers often block automatic redirects to custom schemes for security reasons [source].

To handle this, patch your frontend to detect the JSON response (which includes {redirect: true, url: ...}) and programmatically redirect to the provided URL (for example, using window.location = url). If you don't handle this in the frontend, users will see the JSON and have to copy the URL manually [source].

There is currently no backend-side config or fix for this in v1.3.1; frontend handling is the recommended solution. Make sure your callback URLs and trustedOrigins are set correctly in your config for production [source].

References for more details and example workarounds:

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3165477336 --> @dosubot[bot] commented on GitHub (Aug 7, 2025): <!-- Greeting --> Hi @iboughtbed! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known and intentional behavior in Better Auth's MCP plugin. When state or cookies are missing (such as after clearing cookies or starting from an empty state), the backend can't determine where to redirect and instead returns a JSON response with a redirect URL. This is by design to support frontend-driven redirects, especially for custom URI schemes and mobile/SPA flows, since browsers often block automatic redirects to custom schemes for security reasons [[source](https://github.com/better-auth/better-auth/issues/3714)]. To handle this, patch your frontend to detect the JSON response (which includes `{redirect: true, url: ...}`) and programmatically redirect to the provided URL (for example, using `window.location = url`). If you don't handle this in the frontend, users will see the JSON and have to copy the URL manually [[source](https://github.com/better-auth/better-auth/issues/2914)]. There is currently no backend-side config or fix for this in v1.3.1; frontend handling is the recommended solution. Make sure your callback URLs and `trustedOrigins` are set correctly in your config for production [[source](https://github.com/better-auth/better-auth/issues/3289)]. References for more details and example workarounds: - [Issue 2914: MCP OAuth flow returns JSON instead of redirect](https://github.com/better-auth/better-auth/issues/2914) - [Issue 3714: MCP plugin expects frontend to handle JSON redirect](https://github.com/better-auth/better-auth/issues/3714) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5d7d1442-9154-42c7-8c69-e196758f3d38?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3862)
Author
Owner

@Kinfe123 commented on GitHub (Aug 7, 2025):

PR #3838

<!-- gh-comment-id:3165557306 --> @Kinfe123 commented on GitHub (Aug 7, 2025): PR #3838
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27069