feature(auth): Allow delegating OAuth authorization to existing app-level implementations #485
+153
−5
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.
An optional method that clients can use whenever the authorization should be delegated to an existing implementation.
This PR introduces a new optional method
delegateAuthorization
to theOAuthClientProvider
interface. It allows clients to short-circuit the standard OAuth flow when they already manage authorization through another mechanism (e.g. platform tokens, ambient credentials, preexisting identity systems). When implemented, this method gives control back to the host application to determine whether it considers the session authorized - if so, the SDK skips its internal flow entirely.Motivation and Context
Some applications embedding the MCP SDK already have fully functional authorization systems. In such cases, the SDK’s built-in OAuth flow can be redundant or even problematic - especially when the app simply needs to know when authorization is required, not how to perform it.
Prior to this change, the only way to hook into the authorization process was by subclassing
StreamableHTTPClientTransport
orSSEClientTransport
and overriding enough methods to reimplement_authThenStart
. However, because the relevant methods are private and deeply interwoven (e.g.send
,_startOrAuthSse
), doing so required replicating a significant amount of transport code - leading to maintenance burden and fragile overrides.This change introduces a clean, focused mechanism for opting into externally-managed auth without needing to reimplement large portions of the transport logic.
How Has This Been Tested?
The design was validated by subclassing SSEClientTransport and making the necessary changes to use this new hook.
Breaking Changes
No: the new method is purely opt-in, backward-compatible, and safely ignored if unimplemented. It’s designed to be as simple and low-friction as possible while avoiding the need to subclass transports or bypass internal behavior.
Types of changes
Checklist
Additional context
Notes about the changes:
auth
function insrc/client/auth.ts
now checks fordelegateAuthorization
(if provided) before entering the standard flow.