fix: accept wildcard media types in Accept header per RFC 7231 (#2152)

Co-authored-by: Shivam <shivam@Shivams-MacBook-Air-2.local>
This commit is contained in:
Shivam Aggarwal 2026-03-09 22:00:02 +05:30 • committed by GitHub
commit 51c53f2c18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -391,12 +391,19 @@ class StreamableHTTPServerTransport:
await self._handle_unsupported_request(request, send)
def _check_accept_headers(self, request: Request) -> tuple[bool, bool]:
"""Check if the request accepts the required media types."""
accept_header = request.headers.get("accept", "")
accept_types = [media_type.strip() for media_type in accept_header.split(",")]
"""Check if the request accepts the required media types.
has_json = any(media_type.startswith(CONTENT_TYPE_JSON) for media_type in accept_types)
has_sse = any(media_type.startswith(CONTENT_TYPE_SSE) for media_type in accept_types)
Supports wildcard media types per RFC 7231, section 5.3.2:
- */* matches any media type
- application/* matches any application/ subtype
- text/* matches any text/ subtype
"""
accept_header = request.headers.get("accept", "")
accept_types = [media_type.strip().split(";")[0].strip().lower() for media_type in accept_header.split(",")]
has_wildcard = "*/*" in accept_types
has_json = has_wildcard or any(t in (CONTENT_TYPE_JSON, "application/*") for t in accept_types)
has_sse = has_wildcard or any(t in (CONTENT_TYPE_SSE, "text/*") for t in accept_types)
return has_json, has_sse