mongodb/node-mongodb-native · error · MongoOIDCError

Attempted to get an access token when none exists.

Error message

Attempted to get an access token when none exists.

What it means

Thrown by the OIDC TokenCache.getAccessToken() when no access token is cached (src/cmap/auth/mongodb_oidc/token_cache.ts:26). This is an internal driver invariant: callers are expected to check hasAccessToken before calling getAccessToken. Surfaced as a MongoDriverError (local MongoOIDCError subclass), it indicates a logic error in the workflow orchestration rather than a user misconfiguration.

Source

Thrown at src/cmap/auth/mongodb_oidc/token_cache.ts:27

  private refreshToken?: string;
  private idpInfo?: IdPInfo;
  private expiresInSeconds?: number;

  get hasAccessToken(): boolean {
    return !!this.accessToken;
  }

  get hasRefreshToken(): boolean {
    return !!this.refreshToken;
  }

  get hasIdpInfo(): boolean {
    return !!this.idpInfo;
  }

  getAccessToken(): string {
    if (!this.accessToken) {
      throw new MongoOIDCError('Attempted to get an access token when none exists.');
    }
    return this.accessToken;
  }

  getRefreshToken(): string {
    if (!this.refreshToken) {
      throw new MongoOIDCError('Attempted to get a refresh token when none exists.');
    }
    return this.refreshToken;
  }

  getIdpInfo(): IdPInfo {
    if (!this.idpInfo) {
      throw new MongoOIDCError('Attempted to get IDP information when none exists.');
    }
    return this.idpInfo;
  }

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Upgrade to the latest driver patch release in case the issue is already fixed.
  2. If reproducible, file a bug with the driver version, OIDC environment, and a trace of operations preceding the error.
  3. As a workaround, ensure connections are freshly created (new MongoClient) rather than reusing a client in a partially failed state.
  4. Avoid manually constructing or reusing internal AuthContext objects.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await client.connect();
} catch (e) {
  if (e instanceof MongoDriverError && /get an access token when none exists/.test(e.message)) {
    // Internal invariant - recreate client and report
    client.close();
    throw new Error('Driver OIDC cache invariant violated - please file a bug.');
  }
  throw e;
}

Prevention

When it happens

Trigger: Internal: a workflow path calls cache.getAccessToken() without first verifying cache.hasAccessToken. Should not be reachable through normal public API usage; would indicate a bug in the driver's OIDC workflow state machine.

Common situations: Not user-triggered in normal operation. Could surface if a future driver regression changes the workflow ordering, or if an internal reauthenticate path clears the token mid-flow. Effectively always a driver bug report candidate.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/ab063953e8f7eadd.json. Report an issue: GitHub.