Client auth(): a transient token-endpoint failure during refresh falls through to a new authorization, as if the refresh token were dead
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- typescript
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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
- Connect a client with an
OAuthClientProviderthat holds a valid refresh token. - Let the access token expire.
- Make the token endpoint return
503(or drop the connection) for thegrant_type=refresh_tokenrequest. - Call any tool.
auth()logs the fallback warning and callsredirectToAuthorization(), 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:
- https://github.com/NimbleBrainInc/nimblebrain/blob/main/src/tools/oauth-refresh-fetch.ts (Apache-2.0)
- tests: https://github.com/NimbleBrainInc/nimblebrain/blob/main/test/unit/oauth-refresh-fetch.test.ts
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
catchblock. 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.
- 主要言語
- TypeScript
- スター
- 13.8k
- フォーク
- 2.5k
- 平均マージ
- 6日 23時間
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/typescript-sdk のほかの issue
-
v2
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
modelcontextprotocol/typescript-sdk#3270 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`対応中かも @mgoldsborough が 6 日前に担当しました。 オープンv2
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
modelcontextprotocol/typescript-sdk#3249 ·
メンテナーはふだん 1 日以内に返信
-
v2
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
modelcontextprotocol/typescript-sdk#3243 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` reply対応中かも @Tidehyc が 7 日前に担当しました。 オープンv2
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
modelcontextprotocol/typescript-sdk#3235 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
UriTemplate.match() throws a TypeError on an empty expression it accepts対応中かも @yunusemrejs が 12 日前に担当しました。 オープンv1 v2
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
modelcontextprotocol/typescript-sdk#3221 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/typescript-sdk の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Vessel9817/onion-site-template#512 ·
メンテナーはふだん 1 日以内に返信
-
nightly-tests
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
BlackBeltTechnology/pi-agent-dashboard#1256 ·
メンテナーはふだん 1 日以内に返信
-
approved
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
fritzprix/libr-agent#3266 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
better-auth/better-auth#13101 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信