Skip to content

Commit c202846

Browse files
committed
fix: implemented exception handling for client indefinite blocks
- propagated the error once value error is raised instead of send it to stream - added exception groups to handle value error as well as connection error etc with exception groups - added errors list to add the exception groups and convert it to normal exception and throw to client
1 parent 70115b9 commit c202846

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/mcp/client/sse.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ async def sse_client(
4040
read_stream_writer, read_stream = anyio.create_memory_object_stream(0)
4141
write_stream, write_stream_reader = anyio.create_memory_object_stream(0)
4242

43+
errors: list[Exception] = []
44+
4345
async with anyio.create_task_group() as tg:
4446
try:
4547
logger.info(f"Connecting to SSE endpoint: {remove_request_params(url)}")
@@ -104,7 +106,7 @@ async def sse_reader(
104106
)
105107
except Exception as exc:
106108
logger.error(f"Error in sse_reader: {exc}")
107-
await read_stream_writer.send(exc)
109+
raise
108110
finally:
109111
await read_stream_writer.aclose()
110112

@@ -141,6 +143,12 @@ async def post_writer(endpoint_url: str):
141143
yield read_stream, write_stream
142144
finally:
143145
tg.cancel_scope.cancel()
146+
except* ValueError as eg:
147+
errors.extend(eg.exceptions)
148+
except* Exception as eg:
149+
errors.extend(eg.exceptions)
144150
finally:
145151
await read_stream_writer.aclose()
146152
await write_stream.aclose()
153+
if errors:
154+
raise Exception("TaskGroup failed with: " + " ".join([str(e) for e in errors]))

0 commit comments

Comments
 (0)