@@ -75,7 +75,9 @@ class RequestContext:
7575
7676
7777class AuthTokenProvider (Protocol ):
78- """Protocol for providers that supply authentication tokens."""
78+ """Protocol that can be extended to implement custom client-to-server authentication
79+ The get_token method is invoked before each request to the MCP Server to retrieve a
80+ fresh authentication token and update the request headers."""
7981
8082 async def get_token (self ) -> str :
8183 """Get an authentication token.
@@ -129,8 +131,9 @@ def _update_headers_with_session(
129131 async def _update_headers_with_token (
130132 self , base_headers : dict [str , str ]
131133 ) -> dict [str , str ]:
132- """Update headers with token if token provider is specified."""
133- if self .auth_token_provider is None :
134+ """Update headers with token if token provider is specified and authorization
135+ header is not present."""
136+ if self .auth_token_provider is None or "Authorization" in base_headers :
134137 return base_headers
135138
136139 token = await self .auth_token_provider .get_token ()
@@ -474,6 +477,12 @@ async def streamablehttp_client(
474477 `sse_read_timeout` determines how long (in seconds) the client will wait for a new
475478 event before disconnecting. All other HTTP operations are controlled by `timeout`.
476479
480+ `auth_token_provider` is an optional protocol that can be extended to implement
481+ custom client-to-server authentication. Before each request to the MCP Server,
482+ the get_token method is invoked to retrieve a fresh authentication token and
483+ update the request headers. Note that if the passed in headers already
484+ contain an authorization header, this provider will not be called.
485+
477486 Yields:
478487 Tuple containing:
479488 - read_stream: Stream for reading messages from the server
0 commit comments