[MCP plugin] - authClient.signIn.social fails with INVALID_ORIGIN when redirect URI is localhost #1632

Closed
opened 2026-03-13 08:52:32 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @ericpullukaran on GitHub (Aug 3, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Hello 👋, when calling authClient.signIn.social({ provider: "google" }) from a local MCP client (http://localhost:17892) using mcp-remote, the Better Auth server running on https://mydomain.com returns a 403 with:

{ code: "INVALID_ORIGIN", message: "Invalid origin" }

I'm testing this using the recommended mcp-remote CLI command, which sets the redirect_uri to http://localhost:17892/oauth/callback. This works when the auth server is also local, but fails in production due to the origin mismatch.

Auth Config

mcp({
  loginPage: "/sign-in",
  oidcConfig: {
    allowDynamicClientRegistration: true,
  },
})

Current vs. Expected behavior

The flow should succeed as long as the redirect URI is valid and reachable, even if its origin differs from the request origin. This seems like the kind of scenario dynamic client registration is meant to support, but it doesn't appear to be working from what is happening.

What version of Better Auth are you using?

1.3.4

Provide environment information

macOS
Arc

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import { mcp } from "better-auth/plugins"

export const auth = betterAuth({
  baseURL: "https://mydomain.com",
  socialProviders: {
    google: {
      clientId: "GOOGLE_CLIENT_ID",
      clientSecret: "GOOGLE_CLIENT_SECRET",
    },
  },
  plugins: [
    mcp({
      loginPage: "/sign-in",
      oidcConfig: {
        allowDynamicClientRegistration: true,
      },
    }),
  ],
})

Additional context

Is this something that is being worked on and I can track its progress? (Or possibly something I’ve missed?)

Originally created by @ericpullukaran on GitHub (Aug 3, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Hello 👋, when calling `authClient.signIn.social({ provider: "google" })` from a local MCP client (`http://localhost:17892`) using `mcp-remote`, the Better Auth server running on `https://mydomain.com` returns a 403 with: ``` { code: "INVALID_ORIGIN", message: "Invalid origin" } ``` I'm testing this using the recommended `mcp-remote` CLI command, which sets the `redirect_uri` to `http://localhost:17892/oauth/callback`. **_This works when the auth server is also local_**, but fails in production due to the origin mismatch. **Auth Config** ```ts mcp({ loginPage: "/sign-in", oidcConfig: { allowDynamicClientRegistration: true, }, }) ``` ### Current vs. Expected behavior The flow should succeed as long as the redirect URI is valid and reachable, even if its origin differs from the request origin. This seems like the kind of scenario dynamic client registration is meant to support, but it doesn't appear to be working from what is happening. ### What version of Better Auth are you using? 1.3.4 ### Provide environment information ```bash macOS Arc ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import { mcp } from "better-auth/plugins" export const auth = betterAuth({ baseURL: "https://mydomain.com", socialProviders: { google: { clientId: "GOOGLE_CLIENT_ID", clientSecret: "GOOGLE_CLIENT_SECRET", }, }, plugins: [ mcp({ loginPage: "/sign-in", oidcConfig: { allowDynamicClientRegistration: true, }, }), ], }) ``` ### Additional context Is this something that is being worked on and I can track its progress? (Or possibly something I’ve missed?)
GiteaMirror added the plugin label 2026-03-13 08:52:32 -05:00
Author
Owner

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

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

The INVALID_ORIGIN error happens because Better Auth enforces strict origin and redirect URI validation, especially when the auth server is remote. Even with allowDynamicClientRegistration: true, the MCP plugin does not relax these checks for social login flows—this flag only enables dynamic client registration, but does not affect origin or redirect URI validation for sign-in flows. The code requires the redirect_uri to exactly match what was registered and does not treat localhost as a special case, so using a local redirect URI with a remote server will trigger this error unless explicitly allowed.

To fix this for local development, add your local frontend origin (e.g., http://localhost:17892) to the trustedOrigins array in your Better Auth config. Also, always use absolute callback URLs when your client and server are on different origins. This is a known limitation and is discussed in several issues—it's not currently being tracked as a bug, but as a configuration requirement for multi-origin setups, including localhost during development [1] [2] [3].

Let me know if you want a config example or more details on the workaround.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Aug 3, 2025): <!-- Greeting --> Hi @ericpullukaran! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> The `INVALID_ORIGIN` error happens because Better Auth enforces strict origin and redirect URI validation, especially when the auth server is remote. Even with `allowDynamicClientRegistration: true`, the MCP plugin does not relax these checks for social login flows—this flag only enables dynamic client registration, but does not affect origin or redirect URI validation for sign-in flows. The code requires the `redirect_uri` to exactly match what was registered and does not treat localhost as a special case, so using a local redirect URI with a remote server will trigger this error unless explicitly allowed. To fix this for local development, add your local frontend origin (e.g., `http://localhost:17892`) to the `trustedOrigins` array in your Better Auth config. Also, always use absolute callback URLs when your client and server are on different origins. This is a known limitation and is discussed in several issues—it's not currently being tracked as a bug, but as a configuration requirement for multi-origin setups, including localhost during development [[1]](https://github.com/better-auth/better-auth/issues/3289) [[2]](https://github.com/better-auth/better-auth/issues/2203) [[3]](https://github.com/better-auth/better-auth/issues/1066). Let me know if you want a config example or more details on the workaround. <!-- 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/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/a83dbdc8-e90c-4d74-9ad7-cd29d5b8ee0a?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/3768)
Author
Owner

@ericpullukaran commented on GitHub (Aug 4, 2025):

Actually, ignore, this was an issue with my production env var's being stale

@ericpullukaran commented on GitHub (Aug 4, 2025): Actually, ignore, this was an issue with my production env var's being stale
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1632