@@ -185,7 +185,7 @@ def __init__(
185
185
self ._request_id = 0
186
186
self ._receive_request_type = receive_request_type
187
187
self ._receive_notification_type = receive_notification_type
188
- self ._read_timeout_seconds = read_timeout_seconds
188
+ self ._session_read_timeout_seconds = read_timeout_seconds
189
189
self ._in_flight = {}
190
190
191
191
self ._exit_stack = AsyncExitStack ()
@@ -213,10 +213,12 @@ async def send_request(
213
213
self ,
214
214
request : SendRequestT ,
215
215
result_type : type [ReceiveResultT ],
216
+ request_read_timeout_seconds : timedelta | None = None ,
216
217
) -> ReceiveResultT :
217
218
"""
218
219
Sends a request and wait for a response. Raises an McpError if the
219
- response contains an error.
220
+ response contains an error. If a request read timeout is provided, it
221
+ will take precedence over the session read timeout.
220
222
221
223
Do not use this method to emit notifications! Use send_notification()
222
224
instead.
@@ -243,12 +245,15 @@ async def send_request(
243
245
244
246
await self ._write_stream .send (JSONRPCMessage (jsonrpc_request ))
245
247
248
+ # request read timeout takes precedence over session read timeout
249
+ timeout = None
250
+ if request_read_timeout_seconds is not None :
251
+ timeout = request_read_timeout_seconds .total_seconds ()
252
+ elif self ._session_read_timeout_seconds is not None :
253
+ timeout = self ._session_read_timeout_seconds .total_seconds ()
254
+
246
255
try :
247
- with anyio .fail_after (
248
- None
249
- if self ._read_timeout_seconds is None
250
- else self ._read_timeout_seconds .total_seconds ()
251
- ):
256
+ with anyio .fail_after (timeout ):
252
257
response_or_error = await response_stream_reader .receive ()
253
258
except TimeoutError :
254
259
raise McpError (
@@ -257,7 +262,7 @@ async def send_request(
257
262
message = (
258
263
f"Timed out while waiting for response to "
259
264
f"{ request .__class__ .__name__ } . Waited "
260
- f"{ self . _read_timeout_seconds } seconds."
265
+ f"{ timeout } seconds."
261
266
),
262
267
)
263
268
)
0 commit comments