LibreChat/packages/api/src
Danny Avila 1754297038
🛞 fix: Re-Exchange Rejected OBO Tokens (#15577)
* 🔑 fix: Re-Exchange Rejected OBO Tokens on MCP Auth Failure

An OBO MCP connection registered the non-OAuth fallback handler, so a 401/403
from the downstream server became `oauthFailed` without ever running another
token exchange. Every reconnect attempt replayed the credential the server had
just rejected.

Unlike interactive OAuth, an OBO bearer is minted from the user's live upstream
session, so a rejection is recoverable with no user-facing step — but only while
that session is reachable. `handleOboEvents` re-runs the exchange for a
connection whose creating request is still in flight, and retires the connection
otherwise, since `upstreamTokenProvider` closes over a finished request past that
point and the next borrower rebuilds against a live provider.

Three parts:

- `handleOboEvents` replaces the fallback for OBO connections: it re-exchanges
  once per attempt, updates the transport bearer, and emits `oauthHandled` so the
  connect retry proceeds.
- The refreshed token also replaces the runtime `authorization` header. Runtime
  headers outrank the transport's in `buildFetchInit`, so without this the stale
  bearer a previous tool call installed would keep winning on the retry.
- `resolveOboToken` gains `forceRefresh`, which bypasses the OBO token cache. A
  revoked or scope-invalidated token is still inside its cached lifetime, so a
  cached read would hand back the same rejected bearer.

A connection that cannot refresh now stops reconnecting rather than replaying the
rejected bearer through every backoff step and spending the circuit breaker's
cycle budget.

* 🔁 fix: Re-Exchange Rejected OBO Tokens on Tool Calls Too

Addresses both codex findings on c77be03.

A tool-call 401 never reached the connection-level handler: `oauthRequired` is
emitted only by `connectClient`, and `callTool`'s own recovery attaches solely
when `isOAuthServer(currentOptions) || connection.usesOAuth()` — both false for
an OBO-only config. It rethrew untouched, and the resolver kept serving the
rejected bearer from cache on every later call until it expired. This is the
dominant path for OBO traffic, so the cache-invalidation half of the previous
commit was not actually reachable in practice.

The per-call token resolution moves into `applyOboAuthorization(forceRefresh)`,
which the auth-error path re-runs with the cache bypassed before retrying once.
Recovery belongs here rather than at the connection level: the request is still
live, so `upstreamTokenProvider` can mint a replacement.

Also propagate the re-exchange failure. `connectClient` rejects its handling
promise with the mapped error and then rethrows the server's original 401, so a
diagnosis like an unrefreshable sign-in session was lost. `createConnection` now
surfaces it in place of the 401 — the same substitution the initial OBO
resolution already makes — and only for a genuine exchange failure, not for the
control-flow deferrals.

* 🔗 fix: Persist the Re-Exchanged OBO Token on the Connection

Addresses the round-2 codex finding on 09ccb34.

The tool-call recovery updated only the runtime request headers, leaving
`connection.oauthTokens` holding the rejected credential. That is not merely
untidy: a legacy SSE connection's event stream is served by
`eventSourceInit.fetch`, which bypasses `createFetchFunction` entirely and never
consults `getRequestHeaders()`. It sends the headers `constructTransport`
captured from `oauthTokens`, so the next transport rebuild re-baked the dead
bearer, 401'd, and — through the cached-connection handler added earlier in this
PR — retired a connection that had already recovered.

`applyOboAuthorization` now stores the resolved token via `setOAuthTokens`, the
same thing the connection-level refresh path does. Storing it on every
resolution rather than only on the retry also ends the pre-existing drift where
a connection's baked bearer aged out while tool calls used newer ones.
2026-09-04 00:06:44 -04:00
..
acl 🕹️ feat: Configure BYOM Tool Permissions (#15504) 2026-09-02 10:38:00 -04:00
actions 🔑 fix: Decode Action OAuth Credentials After Decryption (#14698) 2026-09-01 18:42:28 -04:00
admin 🛞 feat: Manage Personal Code Environment Lifecycles (#15369) 2026-09-01 21:10:05 -04:00
agents ➕ feat: Add Agent Management Creation Endpoint (#15564) 2026-09-03 23:45:04 -04:00
apiKeys 🛎️ fix: Enroll Remote Agent Runs in the Generation Lifecycle (#15349) 2026-08-30 07:14:13 -04:00
app 🚰 fix: Record the Provider Drain When Shutdown Cuts a Generation Short (#15532) 2026-09-03 07:11:38 -04:00
artifacts
assistants
auth 📮 fix: Validate Invite Email Claims (#15542) 2026-09-03 22:27:49 -04:00
cache 💓 fix: Detect Silently Dead Redis Sockets With Deadline-Bounded Heartbeats (#15539) 2026-09-03 09:45:16 -04:00
cdn
cluster
code 🕹️ feat: Configure BYOM Tool Permissions (#15504) 2026-09-02 10:38:00 -04:00
conversations
crypto
db
endpoints ⚡ feat: Add Gemini 3.8 Flash Support (#15519) 2026-09-02 15:06:40 -04:00
favorites
files 🖼️ fix: Stop Reporting a Failed Sandbox Image Read as an Unreadable Format (#15453) 2026-09-01 09:08:01 -04:00
flow
html
images
insights
langfuse
mcp 🛞 fix: Re-Exchange Rejected OBO Tokens (#15577) 2026-09-04 00:06:44 -04:00
memory 🧲 fix: Anchor Memory Partitions to Accessible Agents (#15475) 2026-09-01 18:37:52 -04:00
middleware 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
modelSpecs
oauth 🔑 fix: Decode Action OAuth Credentials After Decryption (#14698) 2026-09-01 18:42:28 -04:00
plugins
projects 🗜️ fix: Bound Conversation Page Size (#15576) 2026-09-03 23:52:36 -04:00
prompts
protection 🔭 feat: Audit PII Filter Findings (#15508) 2026-09-02 10:36:06 -04:00
rum
schedules
security
shared-links 🔭 feat: Audit PII Filter Findings (#15508) 2026-09-02 10:36:06 -04:00
skills
storage
stream 💓 fix: Detect Silently Dead Redis Sockets With Deadline-Bounded Heartbeats (#15539) 2026-09-03 09:45:16 -04:00
telemetry 🪵 feat: Add OpenTelemetry Logs Pipeline via Winston Bridge (#15565) 2026-09-03 21:16:09 -04:00
tools 🔭 feat: Audit PII Filter Findings (#15508) 2026-09-02 10:36:06 -04:00
types 🧊 feat: Persist Context Fading Tier Across Agent Runs (#15505) 2026-09-02 20:29:43 -04:00
user
utils 🗜️ fix: Bound Conversation Page Size (#15576) 2026-09-03 23:52:36 -04:00
web
credentials.spec.ts
credentials.ts
imports.spec.ts
imports.ts
index.ts 🧳 feat: Register Principal-Owned Code Environments (#15365) 2026-08-30 19:33:19 -04:00
telemetry.ts