@@ -265,41 +265,47 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
265265 message = ctx .session_message .message
266266 is_initialization = self ._is_initialization_request (message )
267267
268- async with ctx .client .stream (
269- "POST" ,
270- self .url ,
271- json = message .model_dump (by_alias = True , mode = "json" , exclude_none = True ),
272- headers = headers ,
273- ) as response :
274- if response .status_code == 202 :
275- logger .debug ("Received 202 Accepted" )
276- return
268+ try :
269+ async with ctx .client .stream (
270+ "POST" ,
271+ self .url ,
272+ json = message .model_dump (by_alias = True , mode = "json" , exclude_none = True ),
273+ headers = headers ,
274+ ) as response :
275+ if response .status_code == 202 :
276+ logger .debug ("Received 202 Accepted" )
277+ return
277278
278- if response .status_code == 404 :
279- if isinstance (message .root , JSONRPCRequest ):
280- await self ._send_session_terminated_error (
281- ctx .read_stream_writer ,
282- message .root .id ,
283- )
284- return
279+ if response .status_code == 404 :
280+ if isinstance (message .root , JSONRPCRequest ):
281+ await self ._send_session_terminated_error (
282+ ctx .read_stream_writer ,
283+ message .root .id ,
284+ )
285+ return
286+
287+ response .raise_for_status ()
288+ if is_initialization :
289+ self ._maybe_extract_session_id_from_response (response )
285290
286- response .raise_for_status ()
291+ # Per https://modelcontextprotocol.io/specification/2025-06-18/basic#notifications:
292+ # The server MUST NOT send a response to notifications.
293+ if isinstance (message .root , JSONRPCRequest ):
294+ content_type = response .headers .get (CONTENT_TYPE , "" ).lower ()
295+ if content_type .startswith (JSON ):
296+ await self ._handle_json_response (response , ctx .read_stream_writer , is_initialization )
297+ elif content_type .startswith (SSE ):
298+ await self ._handle_sse_response (response , ctx , is_initialization )
299+ else :
300+ await self ._handle_unexpected_content_type (
301+ content_type ,
302+ ctx .read_stream_writer ,
303+ )
304+ except Exception as exc :
287305 if is_initialization :
288- self ._maybe_extract_session_id_from_response (response )
289-
290- # Per https://modelcontextprotocol.io/specification/2025-06-18/basic#notifications:
291- # The server MUST NOT send a response to notifications.
292- if isinstance (message .root , JSONRPCRequest ):
293- content_type = response .headers .get (CONTENT_TYPE , "" ).lower ()
294- if content_type .startswith (JSON ):
295- await self ._handle_json_response (response , ctx .read_stream_writer , is_initialization )
296- elif content_type .startswith (SSE ):
297- await self ._handle_sse_response (response , ctx , is_initialization )
298- else :
299- await self ._handle_unexpected_content_type (
300- content_type ,
301- ctx .read_stream_writer ,
302- )
306+ raise exc
307+ else :
308+ await self ._send_error_response (ctx , exc )
303309
304310 async def _handle_json_response (
305311 self ,
0 commit comments