Skip to content

Commit 7fe7ae6

Browse files
authored
Handle connection errors in StreamableHTTPTransport
Add proper error handling avoiding cancellation of the whole task group
1 parent 62eb08e commit 7fe7ae6

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/mcp/client/streamable_http.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,17 @@ async def post_writer(
466466
read_stream_writer=read_stream_writer,
467467
)
468468

469-
async def handle_request_async():
470-
if is_resumption:
471-
await self._handle_resumption_request(ctx)
472-
else:
473-
await self._handle_post_request(ctx)
469+
async def handle_request_async() -> None:
470+
try:
471+
if is_resumption:
472+
await self._handle_resumption_request(ctx)
473+
else:
474+
await self._handle_post_request(ctx)
475+
except anyio.get_cancelled_exc_class():
476+
raise
477+
except BaseException as exc:
478+
with contextlib.suppress(Exception):
479+
await read_stream_writer.send(exc)
474480

475481
# If this is a request, start a new task to handle it
476482
if isinstance(message, JSONRPCRequest):

0 commit comments

Comments
 (0)