| 166 | logger.exception("Reading from the MCP server's stdout failed mid-session") |
| 167 | |
| 168 | async def stdin_writer() -> None: |
| 169 | assert process.stdin, "Opened process is missing stdin" |
| 170 | |
| 171 | try: |
| 172 | async with write_stream_reader: |
| 173 | async for session_message in write_stream_reader: |
| 174 | json = session_message.message.model_dump_json(by_alias=True, exclude_unset=True) |
| 175 | data = (json + "\n").encode(encoding=server.encoding, errors=server.encoding_error_handler) |
| 176 | await process.stdin.send(data) |
| 177 | except (anyio.ClosedResourceError, anyio.BrokenResourceError, OSError): |
| 178 | # The server may still be alive: close the read stream so the session |
| 179 | # sees the connection end instead of a request hanging forever. |
| 180 | await read_stream_writer.aclose() |
| 181 | finally: |
| 182 | writer_done.set() |
| 183 | |
| 184 | async def shutdown() -> None: |
| 185 | """Winds the transport down: stop traffic, flush, stop the server, release the streams.""" |