fix(core): start OAuth on a modern/auto-era connect (#1805) - #1806
fix(core): start OAuth on a modern/auto-era connect (#1805)#1806rinormaloku wants to merge 1 commit into
Conversation
…ocol#1805) The SDK's `server/discover` negotiation probe runs before anything else on `protocolEra: "auto" | "modern"`, and its classifier reports whatever the transport threw as `SdkError(ERA_NEGOTIATION_FAILED)` with the real error moved to `data.cause`. That buried both connect-time auth signals: the remote path's `AuthRecoveryRequiredError` (matched with `instanceof`, and carrying the authorization URL) and a direct transport's `AuthChallengeError`. An OAuth-protected server that authorized fine on the legacy era instead produced a dead-end "Version negotiation probe failed" — no redirect, and the connection status flipped to `error` despite `isConnectAuthRecoveryError` existing to prevent exactly that. - `findNestedAuthError()` walks `cause` / `data.cause` for either typed error. Not gated on the SDK's error code or message, so it survives rewording. - `connect()` unwraps the rejection at the earliest point, ahead of `withDirectAuthRecovery` (whose `isAuthChallengeError` check is shallow), the outer status guard, and every client's connect error handling — so web, CLI, and TUI are all fixed without a client-side change. - On the direct path, enable `interceptAuthChallenges` for the probing eras even with no stored tokens. Without an authProvider the 401 reaches the SDK as a raw `SdkHttpError`, and the classifier ignores the HTTP status (it only looks for a JSON-RPC error body) — so pin mode rethrew it with the 401 discarded entirely, no status and not even a cause for the walk to find. Verified against a live dual-era server that answers 401 on both `initialize` and `server/discover`: `auto` and `modern` now reject with `AuthRecoveryRequiredError` carrying a real authorization URL. Each half of the fix is load-bearing — reverting either fails the new integration tests. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013SNjAqoEhUVVkr3UStR6om
Verified this end to end (details in #1805) — the diagnosis is right, both halves of the fix are load-bearing, and I found no regressions across web (4728), TUI (282) and CLI (304). The two halves have different lifespansWorth separating, because it drives what I'm asking for:
Requested: mark the workaround as removableKeep the That's the whole ask. Two notes on why I'm no longer pushing for the unconditional version: making it unconditional widens the surface of the code we intend to delete, and the era-scoped form is self-documenting about which connects need the compensation. The behavioral asymmetry it creates is real — on the direct path with no stored tokens, legacy rejects with a raw Not blocking
Before mergeCI hasn't run on this yet (external contributor — needs maintainer approval to trigger workflows). It needs to go green on the full |
|
Superseded by #1809, which carries your commit unchanged (authorship preserved), rebased onto current I ran the full Thanks for the thorough diagnosis and tests — closing this in favour of #1809. |
* fix(core): start OAuth on a modern/auto-era connect (#1805) The SDK's `server/discover` negotiation probe runs before anything else on `protocolEra: "auto" | "modern"`, and its classifier reports whatever the transport threw as `SdkError(ERA_NEGOTIATION_FAILED)` with the real error moved to `data.cause`. That buried both connect-time auth signals: the remote path's `AuthRecoveryRequiredError` (matched with `instanceof`, and carrying the authorization URL) and a direct transport's `AuthChallengeError`. An OAuth-protected server that authorized fine on the legacy era instead produced a dead-end "Version negotiation probe failed" — no redirect, and the connection status flipped to `error` despite `isConnectAuthRecoveryError` existing to prevent exactly that. - `findNestedAuthError()` walks `cause` / `data.cause` for either typed error. Not gated on the SDK's error code or message, so it survives rewording. - `connect()` unwraps the rejection at the earliest point, ahead of `withDirectAuthRecovery` (whose `isAuthChallengeError` check is shallow), the outer status guard, and every client's connect error handling — so web, CLI, and TUI are all fixed without a client-side change. - On the direct path, enable `interceptAuthChallenges` for the probing eras even with no stored tokens. Without an authProvider the 401 reaches the SDK as a raw `SdkHttpError`, and the classifier ignores the HTTP status (it only looks for a JSON-RPC error body) — so pin mode rethrew it with the 401 discarded entirely, no status and not even a cause for the walk to find. Verified against a live dual-era server that answers 401 on both `initialize` and `server/discover`: `auto` and `modern` now reject with `AuthRecoveryRequiredError` carrying a real authorization URL. Each half of the fix is load-bearing — reverting either fails the new integration tests. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013SNjAqoEhUVVkr3UStR6om * docs(core): mark the era-probe challenge interception as a removable workaround Per review on #1806: the `|| this.probesProtocolEra()` clause compensates for an upstream SDK gap (modelcontextprotocol/typescript-sdk#2561, tracked as #1807). Make its lifespan explicit so whoever picks up #1807 knows this block is the thing to delete, rather than having to re-derive it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016S7h83UKNQ7kvSiFDNksCt * fix(core): resolve connect() when auth recovery is satisfied silently Review findings on #1809: - connect() built its handshake promise once, outside the runConnect closure withDirectAuthRecovery retries. On a "satisfied" outcome the recovery runs a complete nested connect() via reconnectAfterAuthRecovery(), so the retry leg re-awaited the already-rejected promise and rejected a connect() whose client was in fact connected. Short-circuit the retry when already connected, and skip the post-connect block the nested connect already ran (a duplicate `connect` event re-triggers every list-state manager's refresh). - Document the accepted 403 side effect of enabling interceptAuthChallenges with no stored tokens, and why every `{ pin }` counts as probing. - Replace the private-method double cast in the era-probe test with an observable assertion on the transport options, and split the non-auth failure case per era instead of a message alternation that passed either way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016S7h83UKNQ7kvSiFDNksCt * fix(core): key the connect retry leg off attempt, bound nested recoveries Second-pass review findings on #1809: - The satisfied-recovery short-circuit keyed off live connection status, so an onclose landing between the nested connect() and the retry leg fell through to the rejected handshake promise and reported the stale 401 as the reason the session died. withDirectAuthRecovery now passes `attempt` to its operation, so connect() identifies the retry leg structurally and reports the teardown when the recovered session is already gone. - Bound nested silent recoveries. The `attempt >= 1` guard is per-call and a satisfied outcome recovers via a nested connect() with a fresh counter, so a server that keeps rejecting freshly refreshed tokens recursed without bound. Counted across the nesting boundary, capped at 3. - Strengthen the regression test (server info populated by the nested connect, no duplicate statusChange) and add the two missing cases: a failed reconnect still rejects, and the recursion bound terminates. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016S7h83UKNQ7kvSiFDNksCt * test(core): cover the recovery teardown branch; fix a duplicated case Third-pass review findings on #1809, both about tests added in the previous commit: - The retry leg's teardown error had no coverage, so a regression to the old status fall-through would have stayed green. Covered deterministically: the nested connect dispatches `connect` after setting "connected", so closing the live transport from that listener flips the status before the retry leg runs. Asserts the error identity, which is the point of the change. - "still rejects when the reconnect underneath the recovery fails" used a challenging transport for every connect, so it walked the same bounded chain as the recursion-bound test rather than covering a non-auth reconnect failure, and its comment claimed an "error" status the auth path never reaches. Now fails the reconnect with a plain error and asserts both. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016S7h83UKNQ7kvSiFDNksCt --------- Co-authored-by: rinormaloku <rinormaloku37@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Closes #1805
Connecting to an OAuth-protected HTTP server with Protocol Era = Auto or Modern never started the authorization flow. The same server authorized fine on Legacy, and the user saw a red toast instead of the OAuth redirect:
Root cause
Era
auto/modernsends the SDK'sserver/discovernegotiation probe before anything else, so the probe — notinitialize— is where authorization first surfaces. Its classifier reports whatever the transport threw asSdkError(ERA_NEGOTIATION_FAILED), keeping the real error atdata.cause. That buried both connect-time auth signals:interceptAuthChallenges: true, so a 401 becomesAuthChallengeError→auth_challenge→RemoteClientTransportrejects withAuthRecoveryRequiredError(carrying the authorization URL). Wrapped, it no longer matchederr instanceof AuthRecoveryRequiredErrorinApp.tsx, andisUnauthorizedError()couldn't see it either (nostatus/code). It also flipped connection status toerror, whichisConnectAuthRecoveryErrorexists to prevent.authProvider, so no interception, so the 401 reached the SDK as a rawSdkHttpError. The classifier ignores the HTTP status — it only looks for a JSON-RPC error body — and verdicts "not a modern server"; pin mode then rethrew that with the 401 discarded entirely: no status, not even acause.Reproduced against a live dual-era server answering 401 on both
initializeandserver/discover:client.connect()AuthRecoveryRequiredErrorSdkError/ERA_NEGOTIATION_FAILEDSdkError/ERA_NEGOTIATION_FAILEDcore/auth/utils.tsalready anticipated this wrapper shape but only walked the chain forUnauthorizedError— not the two typed errors the Inspector's own paths produce.Not caused by the
2026-07-28authorization SEPs (SEP-2468iss, SEP-837application_type, SEP-2352 issuer binding, DCR deprecated in favour of CIMD): legacy-era authorization against the same server worked.Changes
core/auth/challenge.ts—findNestedAuthError()walkscause/data.causefor a nestedAuthRecoveryRequiredError/AuthChallengeError. Deliberately not gated on the SDK's error code or message, so it survives SDK rewording.core/mcp/inspectorClient.ts—connect()unwraps theclient.connect()rejection at the earliest point: ahead ofwithDirectAuthRecovery(whoseisAuthChallengeErrorcheck is shallow), the outer status guard, and every client's connect-error handling. Web, CLI, and TUI are all fixed with no client-side change.core/mcp/inspectorClient.ts— on the direct path, enableinterceptAuthChallengesfor the probing eras even with no stored tokens, so the 401 becomes a typedAuthChallengeErrorthat survives the probe asdata.causeinstead of being silently reclassified.After the fix, against the same live server:
autoandmodernreject withAuthRecoveryRequiredErrorcarrying a real authorization URL.Tests
clients/web/src/test/core/auth/challenge.test.ts— 8 cases over the walk (both link names, multi-level, already-typed, absent, non-object/nulldata, cyclic chain).clients/web/src/test/core/mcp/inspectorClient-era-probe-auth.test.ts—connect()surfaces each typed error onauto/modern, leaves a non-auth probe failure untouched, and keeps the legacy baseline; plusprobesProtocolEra().clients/web/src/test/integration/mcp/inspectorClient-modern-era-oauth.test.ts— live modern-era server behindrequireAuth:connect()rejects with a recoverable auth error (not a negotiation failure) on both probing eras, and connects reporting the modern era once authorization completes.Each half of the fix is load-bearing — reverting either one fails the new integration tests.
npm run ciis green (4,714 tests; per-file ≥90 coverage gate passed).Follow-ups (not in this PR)
classifyHttpErrorshould treat HTTP 401/403 on the probe asauth-requiredrather than absence of modern evidence, and pin mode should not drop the status.AuthorizationServerMismatchError("discoveryState was not available on the callback leg") and the Inspector dead-ends instead of restarting authorization. Clearing OAuth state works around it. Needs its own issue.🤖 Generated with Claude Code
https://claude.ai/code/session_013SNjAqoEhUVVkr3UStR6om