Skip to content

add a timeout arguments on per-request basis (as per MCP specifications) #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mcp/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@ async def unsubscribe_resource(self, uri: AnyUrl) -> types.EmptyResult:
)

async def call_tool(
self, name: str, arguments: dict[str, Any] | None = None
self,
name: str,
arguments: dict[str, Any] | None = None,
read_timeout_seconds: timedelta | None = None,
) -> types.CallToolResult:
"""Send a tools/call request."""
return await self.send_request(
Expand All @@ -265,6 +268,7 @@ async def call_tool(
)
),
types.CallToolResult,
request_read_timeout_seconds=read_timeout_seconds,
)

async def list_prompts(self) -> types.ListPromptsResult:
Expand Down
21 changes: 13 additions & 8 deletions src/mcp/shared/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def __init__(
self._request_id = 0
self._receive_request_type = receive_request_type
self._receive_notification_type = receive_notification_type
self._read_timeout_seconds = read_timeout_seconds
self._session_read_timeout_seconds = read_timeout_seconds
self._in_flight = {}

self._exit_stack = AsyncExitStack()
Expand Down Expand Up @@ -213,10 +213,12 @@ async def send_request(
self,
request: SendRequestT,
result_type: type[ReceiveResultT],
request_read_timeout_seconds: timedelta | None = None,
) -> ReceiveResultT:
"""
Sends a request and wait for a response. Raises an McpError if the
response contains an error.
response contains an error. If a request read timeout is provided, it
will take precedence over the session read timeout.

Do not use this method to emit notifications! Use send_notification()
instead.
Expand All @@ -243,12 +245,15 @@ async def send_request(

await self._write_stream.send(JSONRPCMessage(jsonrpc_request))

# request read timeout takes precedence over session read timeout
timeout = None
if request_read_timeout_seconds is not None:
timeout = request_read_timeout_seconds.total_seconds()
elif self._session_read_timeout_seconds is not None:
timeout = self._session_read_timeout_seconds.total_seconds()

try:
with anyio.fail_after(
None
if self._read_timeout_seconds is None
else self._read_timeout_seconds.total_seconds()
):
with anyio.fail_after(timeout):
response_or_error = await response_stream_reader.receive()
except TimeoutError:
raise McpError(
Expand All @@ -257,7 +262,7 @@ async def send_request(
message=(
f"Timed out while waiting for response to "
f"{request.__class__.__name__}. Waited "
f"{self._read_timeout_seconds} seconds."
f"{timeout} seconds."
),
)
)
Expand Down
Loading