Client auth(): concurrent 401s refresh with the same refresh token, and invalid_grant is rethrown instead of re-authorizing

オープン
#2,915 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
50/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
node.js, typescript

調査の方向性

Start by tracing auth() and OAuthClientProvider token handling in @modelcontextprotocol/client, then reproduce the race with concurrent callTool requests or parallel refresh_token grants. Done means concurrent refreshes share one result, while invalid_grant re-reads provider.tokens(), retries changed tokens, or starts fresh authorization when they have not changed.

索引モデルが issue の本文から書いたものです。

説明

v1 v2

Describe the bug

When an access token expires while several requests are in flight, each request gets a 401 and calls auth() on its own. Each call reads the same stored refresh token and sends its own refresh_token grant. Nothing makes the refresh single-flight.

An authorization server that rotates refresh tokens (OAuth 2.1, RFC 9700 section 4.14.2) accepts the first of these requests. It treats the second as reuse of a consumed token, rejects it with invalid_grant, and usually revokes the whole grant, including the tokens the first request just received. The client then rethrows OAuthError: invalid_grant to the caller instead of falling back to a full authorization, so the session is left unusable until the user reconnects.

To reproduce

  1. Connect a client with an OAuthClientProvider to a server whose authorization server rotates refresh tokens.
  2. Let the access token expire.
  3. Issue two or more requests at once (for example Promise.all of callTool).
  4. One refresh succeeds and the other gets invalid_grant. Because of reuse detection, later requests also fail with invalid_grant.

In our tests this happened in about 2 of 12 runs with real request concurrency. With two refresh_token grants fired directly at once it happened 10 of 10 times.

Expected behavior

  • Concurrent refreshes for the same provider share one in-flight refresh, and every caller reuses its result.
  • If a refresh still fails with invalid_grant, the client re-reads provider.tokens(). If the tokens changed, it retries with them. Otherwise it invalidates the stored tokens and starts a fresh authorization rather than throwing.

Environment

@modelcontextprotocol/client 2.2.0, Node 22, Streamable HTTP transport.

Workaround

Wrap the provider so that refresh is single-flight, or refresh once before fanning out parallel requests.

AI disclosure: I used an AI coding assistant to draft this; I have reviewed it and can answer for it.

主要言語
TypeScript
スター
13.8k
フォーク
2.5k
平均マージ
6日 23時間
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/typescript-sdk のほかの issue

modelcontextprotocol/typescript-sdk の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。