11import warnings
22from contextlib import asynccontextmanager
33from datetime import timedelta
4- from typing import Optional , List , Dict , Tuple , Callable , AsyncIterator , Awaitable , Dict
4+ from typing import (
5+ Optional ,
6+ List ,
7+ Dict ,
8+ Tuple ,
9+ Callable ,
10+ AsyncIterator ,
11+ Awaitable ,
12+ Dict ,
13+ Any ,
14+ )
515from urllib .parse import urlparse , parse_qs
616from mcp .client .session import ClientSession , ProgressFnT
717from mcp .client .sse import sse_client
@@ -73,6 +83,7 @@ class BasicMCPClient(ClientSession):
7383 timeout: The timeout for the command in seconds.
7484 auth: Optional OAuth client provider for authentication.
7585 sampling_callback: Optional callback for handling sampling messages.
86+ headers: Optional headers to pass by sse client or streamable http client
7687
7788 """
7889
@@ -88,13 +99,15 @@ def __init__(
8899 [types .CreateMessageRequestParams ], Awaitable [types .CreateMessageResult ]
89100 ]
90101 ] = None ,
102+ headers : Optional [Dict [str , Any ]] = None ,
91103 ):
92104 self .command_or_url = command_or_url
93105 self .args = args or []
94106 self .env = env or {}
95107 self .timeout = timeout
96108 self .auth = auth
97109 self .sampling_callback = sampling_callback
110+ self .headers = headers
98111
99112 @classmethod
100113 def with_oauth (
@@ -161,7 +174,9 @@ async def _run_session(self) -> AsyncIterator[ClientSession]:
161174 # Check if this is a streamable HTTP endpoint (default) or SSE
162175 if enable_sse (self .command_or_url ):
163176 # SSE transport
164- async with sse_client (self .command_or_url , auth = self .auth ) as streams :
177+ async with sse_client (
178+ self .command_or_url , auth = self .auth , headers = self .headers
179+ ) as streams :
165180 async with ClientSession (
166181 * streams ,
167182 read_timeout_seconds = timedelta (seconds = self .timeout ),
@@ -172,7 +187,7 @@ async def _run_session(self) -> AsyncIterator[ClientSession]:
172187 else :
173188 # Streamable HTTP transport (recommended)
174189 async with streamablehttp_client (
175- self .command_or_url , auth = self .auth
190+ self .command_or_url , auth = self .auth , headers = self . headers
176191 ) as (read , write , _ ):
177192 async with ClientSession (
178193 read ,
0 commit comments