@@ -306,20 +306,36 @@ def _check_content_type(self, request: Request) -> bool:
306306
307307 return any (part == CONTENT_TYPE_JSON for part in content_type_parts )
308308
309+ async def _validate_accept_header (self , request : Request , scope : Scope , send : Send ) -> bool :
310+ """Validate Accept header based on response mode. Returns True if valid."""
311+ has_json , has_sse = self ._check_accept_headers (request )
312+ if self .is_json_response_enabled :
313+ # For JSON-only responses, only require application/json
314+ if not has_json :
315+ response = self ._create_error_response (
316+ "Not Acceptable: Client must accept application/json" ,
317+ HTTPStatus .NOT_ACCEPTABLE ,
318+ )
319+ await response (scope , request .receive , send )
320+ return False
321+ # For SSE responses, require both content types
322+ elif not (has_json and has_sse ):
323+ response = self ._create_error_response (
324+ "Not Acceptable: Client must accept both application/json and text/event-stream" ,
325+ HTTPStatus .NOT_ACCEPTABLE ,
326+ )
327+ await response (scope , request .receive , send )
328+ return False
329+ return True
330+
309331 async def _handle_post_request (self , scope : Scope , request : Request , receive : Receive , send : Send ) -> None :
310332 """Handle POST requests containing JSON-RPC messages."""
311333 writer = self ._read_stream_writer
312334 if writer is None :
313335 raise ValueError ("No read stream writer available. Ensure connect() is called first." )
314336 try :
315- # Check Accept headers
316- has_json , has_sse = self ._check_accept_headers (request )
317- if not (has_json and has_sse ):
318- response = self ._create_error_response (
319- ("Not Acceptable: Client must accept both application/json and text/event-stream" ),
320- HTTPStatus .NOT_ACCEPTABLE ,
321- )
322- await response (scope , receive , send )
337+ # Validate Accept header
338+ if not await self ._validate_accept_header (request , scope , send ):
323339 return
324340
325341 # Validate Content-Type
0 commit comments