Client auth(): a transient token-endpoint failure during refresh falls through to a new authorization, as if the refresh token were dead

Ouverte
#3,238 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@ella-ai-labs y travaille déjà.

Depuis le 27/11/2026.

  • #3247 par @ella-ai-labs — ouverte

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
25/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
typescript

Piste de recherche

Start with the refresh error handling in packages/client/src/client/auth.ts and trace how token endpoint errors reach that catch block. Review the linked pull request #3247 and the related issues #1760 and #2915 before deciding on changes. The issue is resolved when transient refresh failures can be retried by callers without starting a new authorization, while definitive credential rejection still falls through to authorization; add tests for both paths.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

v1 v2

Describe the bug

When auth() refreshes tokens and the token request fails transiently (a network error, a timeout, or a 5xx/server_error from the authorization server), the client falls through to startAuthorization() and redirectToAuthorization(). That is the same path it takes for a refresh token that was actually revoked. The refresh token is still valid, but the client can no longer tell, and nothing retries the refresh.

In packages/client/src/client/auth.ts (current main), the catch around refreshAuthorization() rethrows only an OAuthError whose code is not ServerError. A non-OAuthError (for example TypeError: fetch failed) or a ServerError logs Could not refresh OAuth tokens; falling back to a new authorization request and continues to a fresh authorization.

For an interactive client this is a needless sign-in prompt. For a headless client it is worse. redirectToAuthorization() cannot open a browser, so the provider throws, the caller sees UnauthorizedError, and the connection gets marked as needing the user to reconnect. One blip at the authorization server forces a reconnect of a connection whose credentials were fine.

To reproduce

  1. Connect a client with an OAuthClientProvider that holds a valid refresh token.
  2. Let the access token expire.
  3. Make the token endpoint return 503 (or drop the connection) for the grant_type=refresh_token request.
  4. Call any tool. auth() logs the fallback warning and calls redirectToAuthorization(), and the refresh is never retried.

Expected behavior

A transient refresh failure should be retried with bounded backoff, and if it still fails it should surface as a transient error the caller can retry later. Only a definitive rejection (invalid_grant, invalid_client, unauthorized_client) should fall through to a new authorization. The refresh token stays stored either way.

Suggested fix

In the refresh catch, split the cases:

  • invalid_grant / invalid_client / unauthorized_client: invalidate as today and fall through to authorization.
  • Network errors, 5xx, server_error, temporarily_unavailable, 429: retry a small bounded number of times (exponential backoff with jitter), then throw a distinct error without starting authorization.
  • Anything unrecognized: treat as permanent (fail closed), as today.

Retrying only the refresh_token grant keeps the one-shot authorization_code exchange and ordinary requests unchanged.

Reference implementation

We hit this running a headless multi-tenant client and worked around it outside the SDK. We wrap the transport's fetch, which the SDK threads into the token request. The wrapper retries only the refresh grant on transient failures and passes definitive rejections through unmodified on the first attempt, so the SDK's existing dead-token path still fires:

The workaround is fetch-level only because the transient/dead distinction is already gone by the time control returns to the provider. The right place for it is auth() itself. We're happy to open a PR with the fix above and tests, if maintainers agree on the approach.

Related

  • #1760, #2915 (concurrent refreshes with rotating refresh tokens): a separate bug in the same catch block. A fix there should keep transient failures distinguishable too.

Environment

@modelcontextprotocol/client 2.3.0; behavior confirmed unchanged on main as of 2026-11-26.

Langage dominant
TypeScript
Étoiles
13.8k
Forks
2.5k
Merge moyen
6 j 23 h
PR mergées (30 j)
2

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de modelcontextprotocol/typescript-sdk

Toutes les issues de modelcontextprotocol/typescript-sdk

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.