send errors to pending requests if server closes (#333)

Co-authored-by: ihrpr <inna@anthropic.com>
This commit is contained in:
Marshall Roch
2025-05-27 17:55:27 -04:00
committed by GitHub
parent 9dad26620f
commit 7901552eba
3 changed files with 71 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ from typing_extensions import Self
from mcp.shared.exceptions import McpError
from mcp.shared.message import MessageMetadata, ServerMessageMetadata, SessionMessage
from mcp.types import (
CONNECTION_CLOSED,
CancelledNotification,
ClientNotification,
ClientRequest,
@@ -417,6 +418,14 @@ class BaseSession(
)
)
# after the read stream is closed, we need to send errors
# to any pending requests
for id, stream in self._response_streams.items():
error = ErrorData(code=CONNECTION_CLOSED, message="Connection closed")
await stream.send(JSONRPCError(jsonrpc="2.0", id=id, error=error))
await stream.aclose()
self._response_streams.clear()
async def _received_request(
self, responder: RequestResponder[ReceiveRequestT, SendResultT]
) -> None: