Skip to content

refactor(api): simplify MCP OAuth authorization and consent - #1034

Merged
izadoesdev merged 5 commits into
stagingfrom
codex/mcp-consent-cleanup
Oct 1, 2026
Merged

izadoesdev merged 5 commits into
stagingfrom
codex/mcp-consent-cleanup

Conversation

@izadoesdev

@izadoesdev izadoesdev commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

The scoped MCP connection flow carried the same authorization through several optional fields and used handwritten OAuth requests and response types. This cleanup uses Better Auth's native OAuth client and one required OAuth principal from the API through MCP and RPC. Consent and Connected apps now show the provider's actual client_name.

  • Load the OAuth grant and user in parallel once per HTTP request and reuse the existing RPC authorization type; remove duplicate organization/scope fields and obsolete account-wide OAuth discovery helpers and redundant awaits on synchronous organization resolution.
  • Store the encoded consent reference directly in request state, use TanStack Query's mutation state for consent submission, let the native OAuth client handle redirects, and keep disconnect pending until Connected apps refreshes.
  • Keep organization boundaries, selected websites, approved scopes, fresh membership checks before cached discovery, and disconnect/reconnect revocation intact. Net change: 57 fewer lines, including test updates.

Validation: repository lint and all 37 typecheck/build tasks; 93 focused MCP tool and selected-site regressions; native TanStack mutation check confirming disconnect stays pending until revoked rows disappear; normal pre-push monorepo tests. The separately landed staging test-budget fix passed all 23 revenue integration cases and the full 31-task CI test workload with ClickHouse and website-reader integration enabled.

Slice: MCP OAuth authorization and consent cleanup. Based on current staging; #1032 is already merged. No unmerged dependencies. Checked open PRs; #692 touches auth initialization/storage, with no overlapping files or contracts in this slice.

Maintainer-requested change; implemented and reviewed with Codex assistance.

@vercel

vercel Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
dashboard Ready Ready Preview Oct 1, 2026 8:06pm UTC
databuddy-status Ready Ready Preview Oct 1, 2026 8:06pm UTC
documentation Ready Ready Preview Oct 1, 2026 8:06pm UTC

@coderabbitai

coderabbitai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: bcf7178c-2b42-4cc6-93ac-117a6b6a40f0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 20 files

Architecture diagram
sequenceDiagram
    participant Client as Dashboard Client
    participant API as API Route (Elysia)
    participant Auth as Auth Service (Better Auth)
    participant MCP as MCP Handler
    participant Ctx as Tool Context
    participant RPC as RPC Layer
    participant DB as Database

    Note over Client,DB: MCP OAuth Authorization Flow

    Client->>Auth: Request /oauth2/authorize
    Auth->>Auth: mcpConsentAccess - encode consent reference
    Auth->>Auth: mcpPostLogin - store encoded reference

    Client->>Auth: POST /oauth2/consent (accept/deny)
    Auth->>Auth: Native OAuth client - validate consent
    alt Accept
        Auth-->>Client: Redirect URL
        Client->>API: MCP request with OAuth token
        API->>API: Validate token scopes (no API scope filter)
        API->>DB: Find user by subject ID
        alt User exists
            API->>MCP: handleDatabuddyMcpRequest (oauth principal + user)
            MCP->>Ctx: ensureWebsiteAccess / buildRpcContext
            Ctx->>DB: Check fresh membership
            alt Valid membership
                Ctx->>RPC: Execute tool with OAuth context
                RPC->>DB: Access websites with organization boundary
                RPC-->>Ctx: Result
                Ctx-->>MCP: Response
                MCP-->>API: Success
            else No membership
                Ctx-->>MCP: Access denied
                MCP-->>API: Unauthorized
            end
        else User not found
            API-->>Client: Unauthorized
        end
    else Deny
        Auth-->>Client: Redirect URL
    end

    Note over Client,DB: Connected Apps Display

    Client->>Auth: oauth2.getConsents (native client)
    Auth-->>Client: Consent list with clientIds
    Client->>Auth: oauth2.publicClient (per consent)
    Auth-->>Client: client metadata (client_name)
    Client->>Client: Display provider client_name

    Note over Client,DB: Disconnect/Revocation

    Client->>Auth: oauth2.deleteConsent (native client)
    Auth->>DB: Revoke OAuth grant
    DB-->>Auth: Confirmation
    Auth-->>Client: Success
    Client->>Client: Invalidate connected apps query
Loading

Shadow auto-approve: would not auto-approve because issues were found.

Re-trigger cubic

Comment thread apps/api/src/routes/mcp.ts Outdated
@greptile-apps

greptile-apps Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[High risk] Refactors OAuth authorization flow and MCP request handling.

The PR appears safe to merge; adding a successful browser consent test would strengthen the redirect coverage.

Findings

  1. P2 Consent redirect goes untested ▶

Summary

MCP OAuth now carries one user, grant, and scope record from the API through MCP tools and RPC. Consent and Connected apps use Better Auth’s OAuth client methods and display the provider’s client_name.

  • Keeps organization, website, membership, and scope checks tied to the same OAuth grant.
  • Stores the encoded consent choice in request state and uses native OAuth calls in the dashboard.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[OAuth MCP request] --> B[Load grant and user]
  B --> C[MCP tools]
  C --> D[Check scopes and website access]
  C --> E[RPC with the same OAuth principal]
  F[Consent page] --> G[Better Auth OAuth client]
  G --> H[Consent and connected apps]
Loading

Reviews (1) · Last reviewed commit: "refactor(api): simplify MCP OAuth author..."

Comment thread apps/dashboard/test/e2e/specs/regressions/mcp-consent.spec.ts
@unkey-deploy

unkey-deploy Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Unkey Deploy

Name Status Preview Inspect Updated (UTC)
links (preview) Ready Visit Preview Inspect Oct 1, 2026 8:05pm

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 3 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would require human review. Refactors MCP OAuth authorization/consent to use Better Auth's native OAuth client and one required OAuth principal. Human review needed: the route now passes unfiltered token scopes into grant validation, and consent redirect handling is only partially visible.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 3 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would require human review. Refactors MCP OAuth authorization/consent to use Better Auth's native OAuth client with a single OAuth principal. Human review is still needed because scope enforcement in getMcpAccessGrant and consent redirect handling are not fully verifiable from the diff.

Re-trigger cubic

@izadoesdev
izadoesdev merged commit 977e8af into staging Oct 1, 2026
23 checks passed
@izadoesdev
izadoesdev deleted the codex/mcp-consent-cleanup branch October 1, 2026 20:16

This branch was successfully deployed

4 active deployments
Preview – documentation — d02c43ce Deployed Oct 1, 2026 by vercel[bot]
Preview – dashboard — d02c43ce Deployed Oct 1, 2026 by vercel[bot]
Preview – databuddy-status — d02c43ce Deployed Oct 1, 2026 by vercel[bot]
links - preview — d02c43ce Deployed Oct 1, 2026 by unkey-deploy[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant