8000 handle BrokenResourceError gracefully · modelcontextprotocol/python-sdk@a5b633e · GitHub
[go: up one dir, main page]

Skip to content

Commit a5b633e

Browse files
committed
handle BrokenResourceError gracefully
1 parent 775f879 commit a5b633e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/mcp/server/stdio.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,27 @@ async def stdin_reader():
6767
continue
6868

6969
await read_stream_writer.send(message)
70+
except anyio.BrokenResourceError as ex:
71+
print(f"BrokenResourceError(reader): {ex}")
72+
pass
7073
except anyio.ClosedResourceError:
7174
await anyio.lowlevel.checkpoint()
75+
except Exception as ex:
76+
print(f"read error: {ex}")
7277

7378
async def stdout_writer():
7479
try:
7580
async with write_stream_reader:
7681
async for message in write_stream_reader:
77-
json = message.model_dump_json(by_alias=True, exclude_none=True)
78-
await stdout.write(json + "\n")
79-
await stdout.flush()
82+
try:
83+
json = message.model_dump_json(by_alias=True, exclude_none=True)
84+
await stdout.write(json + "\n")
85+
await stdout.flush()
86+
except Exception as ex:
87+
print(f"problem with writer: {ex}")
88+
except anyio.BrokenResourceError as ex:
89+
print(f"BrokenResourceError(writer): {ex}")
90+
pass
8091
except anyio.ClosedResourceError:
8192
await anyio.lowlevel.checkpoint()
8293

0 commit comments

Comments
 (0)
0