fix(server): respect Accept header q=0 (RFC 7231 "not acceptable")#3158
Draft
SpiliosDimakopoulos wants to merge 1 commit into
Draft
fix(server): respect Accept header q=0 (RFC 7231 "not acceptable")#3158SpiliosDimakopoulos wants to merge 1 commit into
SpiliosDimakopoulos wants to merge 1 commit into
Conversation
check_accept_headers() discarded every Accept-header parameter after the media type, so \�pplication/json;q=0\ (explicitly rejecting JSON) was parsed the same as plain \�pplication/json\ (accepting it). Parse the q value per RFC 7231 §5.3.1 and treat q<=0 as not-acceptable, with a specific type's rejection taking precedence over a broader wildcard. Fixes modelcontextprotocol#3154
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3154
What
check_accept_headers()stripped every Accept-header parameter after the mediatype (
media_type.split(";")[0]), soapplication/json;q=0— which per RFC 7231§5.3.1 explicitly marks JSON as not acceptable — was parsed identically to
plain
application/json. A client that explicitly ruled out a representationcould still receive it.
Details
qparameter (default 1.0 if absent/malformed).q <= 0moves the type from "accepted" to "rejected".would otherwise accept it (e.g.
application/json;q=0, */*still rejectsJSON) — most-specific-wins, consistent with RFC 7231 §5.3.2 precedence.
representations by preference, it only gates on q=0 vs not.
Testing
test_accept_q0_for_the_required_type_is_not_acceptable— the exact examplefrom the issue.
test_accept_q0_for_a_specific_type_overrides_a_present_wildcard— specificrejection beats a present wildcard.
test_accept_nonzero_q_for_the_required_type_is_still_acceptable— regressionguard for nonzero q.
uv run pytest tests/server/,uv run ruff check .,uv run ruff format --check .,uv run pyrightall pass.Disclosure
I used AI (Claude) to help draft this fix and its tests; I've reviewed and
understand the change and I'm happy to answer questions about it.