@@ -75,7 +75,9 @@ class RequestContext:
75
75
76
76
77
77
class 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."""
79
81
80
82
async def get_token (self ) -> str :
81
83
"""Get an authentication token.
@@ -129,8 +131,9 @@ def _update_headers_with_session(
129
131
async def _update_headers_with_token (
130
132
self , base_headers : dict [str , str ]
131
133
) -> 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 :
134
137
return base_headers
135
138
136
139
token = await self .auth_token_provider .get_token ()
@@ -474,6 +477,12 @@ async def streamablehttp_client(
474
477
`sse_read_timeout` determines how long (in seconds) the client will wait for a new
475
478
event before disconnecting. All other HTTP operations are controlled by `timeout`.
476
479
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
+
477
486
Yields:
478
487
Tuple containing:
479
488
- read_stream: Stream for reading messages from the server
0 commit comments