@@ -182,19 +182,6 @@ def __init__(
182182 self ._in_flight = {}
183183
184184 self ._exit_stack = AsyncExitStack ()
185- self ._incoming_message_stream_writer , self ._incoming_message_stream_reader = (
186- anyio .create_memory_object_stream [
187- RequestResponder [ReceiveRequestT , SendResultT ]
188- | ReceiveNotificationT
189- | Exception
190- ]()
191- )
192- self ._exit_stack .push_async_callback (
193- lambda : self ._incoming_message_stream_reader .aclose ()
194- )
195- self ._exit_stack .push_async_callback (
196- lambda : self ._incoming_message_stream_writer .aclose ()
197- )
198185
199186 async def __aenter__ (self ) -> Self :
200187 self ._task_group = anyio .create_task_group ()
@@ -300,11 +287,10 @@ async def _receive_loop(self) -> None:
300287 async with (
301288 self ._read_stream ,
302289 self ._write_stream ,
303- self ._incoming_message_stream_writer ,
304290 ):
305291 async for message in self ._read_stream :
306292 if isinstance (message , Exception ):
307- await self ._incoming_message_stream_writer . send (message )
293+ await self ._handle_incoming (message )
308294 elif isinstance (message .root , JSONRPCRequest ):
309295 validated_request = self ._receive_request_type .model_validate (
310296 message .root .model_dump (
@@ -325,7 +311,7 @@ async def _receive_loop(self) -> None:
325311 self ._in_flight [responder .request_id ] = responder
326312 await self ._received_request (responder )
327313 if not responder ._completed :
328- await self ._incoming_message_stream_writer . send (responder )
314+ await self ._handle_incoming (responder )
329315
330316 elif isinstance (message .root , JSONRPCNotification ):
331317 try :
@@ -341,9 +327,7 @@ async def _receive_loop(self) -> None:
341327 await self ._in_flight [cancelled_id ].cancel ()
342328 else :
343329 await self ._received_notification (notification )
344- await self ._incoming_message_stream_writer .send (
345- notification
346- )
330+ await self ._handle_incoming (notification )
347331 except Exception as e :
348332 # For other validation errors, log and continue
349333 logging .warning (
@@ -355,7 +339,7 @@ async def _receive_loop(self) -> None:
355339 if stream :
356340 await stream .send (message .root )
357341 else :
358- await self ._incoming_message_stream_writer . send (
342+ await self ._handle_incoming (
359343 RuntimeError (
360344 "Received response with an unknown "
361345 f"request ID: { message } "
@@ -387,12 +371,11 @@ async def send_progress_notification(
387371 processed.
388372 """
389373
390- @property
391- def incoming_messages (
374+ async def _handle_incoming (
392375 self ,
393- ) -> MemoryObjectReceiveStream [
394- RequestResponder [ReceiveRequestT , SendResultT ]
376+ req : RequestResponder [ReceiveRequestT , SendResultT ]
395377 | ReceiveNotificationT
396- | Exception
397- ]:
398- return self ._incoming_message_stream_reader
378+ | Exception ,
379+ ) -> None :
380+ """A generic handler for incoming messages. Overwritten by subclasses."""
381+ await anyio .lowlevel .checkpoint ()
0 commit comments