Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/mcp/shared/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
INVALID_PARAMS,
REQUEST_TIMEOUT,
CancelledNotification,
CancelledNotificationParams,
ClientNotification,
ClientRequest,
ClientResult,
Expand Down Expand Up @@ -292,9 +293,14 @@ async def send_request(
with anyio.fail_after(timeout):
response_or_error = await response_stream_reader.receive()
except TimeoutError:
await self._send_cancelled_notification(request_id, "request timed out")
class_name = request.__class__.__name__
message = f"Timed out while waiting for response to {class_name}. Waited {timeout} seconds."
raise MCPError(code=REQUEST_TIMEOUT, message=message)
except anyio.get_cancelled_exc_class():
with anyio.CancelScope(shield=True):
await self._send_cancelled_notification(request_id, "request cancelled")
raise

if isinstance(response_or_error, JSONRPCError):
raise MCPError.from_jsonrpc_error(response_or_error)
Expand Down Expand Up @@ -325,6 +331,25 @@ async def send_notification(
)
await self._write_stream.send(session_message)

async def _send_cancelled_notification(
self,
request_id: RequestId,
reason: str,
) -> None:
"""Best-effort delivery of a notifications/cancelled for an in-flight request."""
try:
notification = CancelledNotification(
method="notifications/cancelled",
params=CancelledNotificationParams(request_id=request_id, reason=reason),
)
jsonrpc_notification = JSONRPCNotification(
jsonrpc="2.0",
**notification.model_dump(by_alias=True, mode="json", exclude_none=True),
)
await self._write_stream.send(SessionMessage(message=jsonrpc_notification))
except Exception:
logging.debug("Failed to send cancellation notification for request %s", request_id)

async def _send_response(self, request_id: RequestId, response: SendResultT | ErrorData) -> None:
if isinstance(response, ErrorData):
jsonrpc_error = JSONRPCError(jsonrpc="2.0", id=request_id, error=response)
Expand Down
Loading