Skip to content

Make MCP OAuth attempts atomic and eliminate orphan auth sessions #833

Closed

Description

@dcramer

Problem

Junior eagerly persists an MCP auth session whenever an MCP provider is activated, before knowing whether interaction is required. Successful already-authorized calls leave those records behind for 24 hours.

The per-user/provider index keeps stale IDs and can grow indefinitely for an active user. Same-session link reuse can also delete the live auth session needed by the already-delivered callback URL.

Root causes

  • auth attempts are inferred from loosely coupled session/index/pending-auth records
  • lookup uses "latest session for user/provider" rather than deterministic attempt identity
  • attempt state is created before the SDK reaches an interactive redirect
  • link reuse deletes the newly prepared session without proving it is distinct from the reused attempt
  • callback claim/completion is not an atomic, idempotent transition

Acceptance criteria

  • Silent authenticated MCP calls create no authorization-attempt record.
  • Attempts are created lazily only when interaction is required.
  • Attempt identity is deterministic from the owning conversation/session/provider or represented by an explicit opaque attempt ID.
  • Reusing a live link cannot delete or invalidate its attempt state.
  • Attempt indexes prune expired entries and cannot grow indefinitely.
  • Callback claim and completion are atomic and duplicate callbacks are idempotent.
  • Persisted attempt context includes source, destination, actor, scope, resource, issuer, redirect URI, and resume identity.
  • Denied, expired, failed, superseded, and completed attempts all reach terminal states.

Related: #782.

Activity

  1. added
    bug
    on Jul 11, 2026
  2. sentry-pierre commented on Jul 11, 2026

    @sentry-pierre

    createMcpOAuthClientProvider in packages/junior/src/chat/mcp/oauth.ts writes the auth session before the SDK decides whether a redirect is needed, so silent already-authorized calls still append to the index.

    getLatestMcpAuthSessionForUserProvider picks the latest by updatedAtMs, and putMcpAuthSession resets the index TTL while appending, so the per-user/provider index grows without pruning stale IDs.

    finalizeMcpAuthorization has no atomic claim step, so duplicate callbacks would both run transport.finishAuth.

    All root causes line up with the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: Refactorbug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions