[BUG] MCP OAuth flow drops activeOrganizationId from session #2895

Closed
opened 2026-03-13 10:26:07 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @Bekacru on GitHub (Feb 19, 2026).

Bug Report

When a user goes through the MCP OAuth flow, the activeOrganizationId from their browser session is dropped.

Root Cause

In authorizeMCPOAuth (packages/better-auth/src/plugins/mcp/authorize.ts), the verification value is created but only stores userId:

await ctx.context.internalAdapter.createVerificationValue({
    value: JSON.stringify({
        clientId: client.clientId,
        redirectURI: query.redirect_uri,
        scope: requestScope,
        userId: session.user.id,  // ← org is lost here
        authTime: new Date(session.session.createdAt).getTime(),
        // ...
    }),
    identifier: code,
    expiresAt,
});

The activeOrganizationId from session.session.activeOrganizationId needs to be captured and stored in the verification value so it can be restored when the token is issued.

Expected Behavior

The active organization context should be preserved through the OAuth flow, similar to how the OIDC Provider plugin handles organization sessions.

Affected File

packages/better-auth/src/plugins/mcp/authorize.ts

Originally created by @Bekacru on GitHub (Feb 19, 2026). ## Bug Report When a user goes through the MCP OAuth flow, the `activeOrganizationId` from their browser session is dropped. ### Root Cause In `authorizeMCPOAuth` (`packages/better-auth/src/plugins/mcp/authorize.ts`), the verification value is created but only stores `userId`: ```javascript await ctx.context.internalAdapter.createVerificationValue({ value: JSON.stringify({ clientId: client.clientId, redirectURI: query.redirect_uri, scope: requestScope, userId: session.user.id, // ← org is lost here authTime: new Date(session.session.createdAt).getTime(), // ... }), identifier: code, expiresAt, }); ``` The `activeOrganizationId` from `session.session.activeOrganizationId` needs to be captured and stored in the verification value so it can be restored when the token is issued. ### Expected Behavior The active organization context should be preserved through the OAuth flow, similar to how the OIDC Provider plugin handles organization sessions. ### Affected File `packages/better-auth/src/plugins/mcp/authorize.ts`
GiteaMirror added the P0bug labels 2026-03-13 10:26:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2895