Repository navigation
feat(mcp-adapters): accept token AuthProviders and keep auth failures retryable - #11720
Merged
Merged
Conversation
|
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
oxfmt 0.65.0 (the version CI runs) wraps a few long expect() lines in src/tests/oauth.test.ts differently than the file was written; the "Check formatting" CI job fails on the drift. Run oxfmt over the file to match its output exactly.
isAuthenticationError only checked an error and its direct cause, but
the legacy HTTP->SSE fallback in _initializeStreamableHTTPConnection
wraps _initializeSSEConnection's own MCPClientError (whose cause is the
SSE transport's 401) in another MCPClientError. That put the real
signal two `.cause` hops down, so onConnectionError misclassified the
failure as non-auth and #discoverToolsets added the server to
#failedServers for the adapter's lifetime instead of retrying it on the
next discovery.
Walk the cause chain with a small depth bound instead of
checking just one hop. Add a regression test that reproduces the
double-wrapped legacy-mode fallback through the real in-process OAuth
fixture and asserts the server stays retryable.
_loadToolsForServer's own wrap of a failed client.listTools() was also
suspected of needing a multi-hop walk, but every reachable failure
there (bare {token} provider, onUnauthorized retry that still 401s, or
an OAuthClientProvider's adapted onUnauthorized) arrives as a single,
directly-recognizable UnauthorizedError or 401-carrying SdkHttpError -
confirmed empirically against the real fixture - so the pre-existing
one-hop check already classified it correctly and no second reachable
path exists there today.
Drop the httpOptionsSchema authProvider JSDoc's dangling reference to
MCPAdapter#finishAuth, which lands in the upper mcp-oauth PR and
doesn't exist on this branch.
Bring the README's Authentication section and the pending
mcp-adapters-sdk-2 changeset up to date with what this PR actually
adds: the AuthProvider ({ token, onUnauthorized? }) shape alongside
OAuthClientProvider, and provider-token-over-configured-header
precedence. Wording is kept identical to the upper branch's final text
(minus its finishAuth/pending-login additions, which belong there, not
here) so integrating this branch into it stays a no-op for these
sections.
Co-authored-by: Hunter Lovell <[email protected]> Signed-off-by: Hunter Lovell <[email protected]>
Hunter Lovell (hntrl)
approved these changes
Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the first of two stacked PRs adding OAuth to
@langchain/mcp-adapters2.0. It does four things:AuthProvider;It also documents these changes in the README. The follow-up PR, stacked on this one, adds
finishAuthto complete an OAuth redirect.Merging this releases nothing.
@langchain/mcp-adaptersis still in the changesetsignorelist (#11715).966 changed lines: implementation 143, tests 738 (of which the OAuth server fixture is 320), README 18, lockfile 67.
MCP SDK 2.1 replaces the header workaround
In SDK 2.0, a configured
Authorizationheader and the provider's token were sent joined into one value,Bearer <provider>, Bearer <configured>. #11663 worked around this withgetSdkHeaderCase. SDK 2.1.0 (modelcontextprotocol/typescript-sdk#2475) fixes it upstream by matching the transport's ownAuthorizationcase-insensitively, so the workaround is deleted rather than kept alongside.The SDK's fix also decides which value wins, and it picks the opposite of what #11663 did:
Authorization;We follow the SDK instead of patching over it. This order is also the useful one: a static API key works until an OAuth login completes, then the OAuth token takes over. The header tests now pin both cases.
@modelcontextprotocol/clientandcorenow require^2.1.0. The dev-only server packages move to^2.1.0with them.Token providers
authProvidernow accepts the SDK's minimalAuthProvider,{ token(), onUnauthorized?() }, as well as anOAuthClientProvider. The adapter hands it to the transport unchanged:token()before each request;onUnauthorized()once, then retries.This covers tokens the application already manages, without implementing the whole OAuth client interface.
AuthProvideris exported as a type.Authentication failures
When a provider-driven flow fails, the SDK throws
UnauthorizedError. That error carries no HTTP status, so the oldcode === 401check missed it. The newisAuthenticationErrorrecognizes eitherUnauthorizedErroror a 401, on the error itself or up to three levels down itscausechain. The depth matters because the legacy HTTP→SSE fallback wraps the SSE 401 twice. Both the HTTP and SSE paths now use it.As a result, a provider auth failure:
cause;Tests pin this on both transports.
UnauthorizedErroris exported so callers can check the cause.Auth failures stay retryable
Discovery used to add every server that failed to connect to
#failedServers, which skips that server for the rest of the adapter's lifetime. After a 401, completing the login couldn't bring the server back without creating a new adapter.Now:
#failedServers, so the next discovery retries them;token()still stay blocked;onConnectionErrorstill runs for every failure.Docs
Authorizationvalue wins, implementinginvalidateCredentials()(without it, a refresh the server rejects fails instead of restarting the login), andUnauthorizedErroras thecause.maincan't carry a changeset for the ignored adapter without blocking every release (Bug: changesets/action creates PRs for changesets that only reference ignored packages changesets/action#541). The release PR restores the 2.0 changeset, including this PR's notes.Testing
pnpm --filter @langchain/mcp-adapters test: 463 passed, 2 skipped, no type errors. oxlint and oxfmt report no issues.src/tests/fixtures/oauth-server.ts). The SDK is not mocked. The server is built from the SDK's ownmcpAuthRouterandrequireBearerAuthand supports:iss, which can be sent, omitted or unsupported;/mcpand/sse.isAuthenticationErrorchecks only onecauselevel.