@@ -185,7 +185,7 @@ def __init__(
185185 self ._request_id = 0
186186 self ._receive_request_type = receive_request_type
187187 self ._receive_notification_type = receive_notification_type
188- self ._read_timeout_seconds = read_timeout_seconds
188+ self ._session_read_timeout_seconds = read_timeout_seconds
189189 self ._in_flight = {}
190190 self ._exit_stack = AsyncExitStack ()
191191
@@ -212,10 +212,12 @@ async def send_request(
212212 self ,
213213 request : SendRequestT ,
214214 result_type : type [ReceiveResultT ],
215+ request_read_timeout_seconds : timedelta | None = None ,
215216 ) -> ReceiveResultT :
216217 """
217218 Sends a request and wait for a response. Raises an McpError if the
218- response contains an error.
219+ response contains an error. If a request read timeout is provided, it
220+ will take precedence over the session read timeout.
219221
220222 Do not use this method to emit notifications! Use send_notification()
221223 instead.
@@ -240,12 +242,15 @@ async def send_request(
240242
241243 await self ._write_stream .send (JSONRPCMessage (jsonrpc_request ))
242244
245+ # request read timeout takes precedence over session read timeout
246+ timeout = None
247+ if request_read_timeout_seconds is not None :
248+ timeout = request_read_timeout_seconds .total_seconds ()
249+ elif self ._session_read_timeout_seconds is not None :
250+ timeout = self ._session_read_timeout_seconds .total_seconds ()
251+
243252 try :
244- with anyio .fail_after (
245- None
246- if self ._read_timeout_seconds is None
247- else self ._read_timeout_seconds .total_seconds ()
248- ):
253+ with anyio .fail_after (timeout ):
249254 response_or_error = await response_stream_reader .receive ()
250255 except TimeoutError :
251256 raise McpError (
@@ -254,7 +259,7 @@ async def send_request(
254259 message = (
255260 f"Timed out while waiting for response to "
256261 f"{ request .__class__ .__name__ } . Waited "
257- f"{ self . _read_timeout_seconds } seconds."
262+ f"{ timeout } seconds."
258263 ),
259264 )
260265 )
0 commit comments