fix(server): treat text/* mime types case-insensitively for FileResource#2918
Open
ly-wang19 wants to merge 2 commits into
Open
fix(server): treat text/* mime types case-insensitively for FileResource#2918ly-wang19 wants to merge 2 commits into
ly-wang19 wants to merge 2 commits into
Conversation
Media types are case-insensitive (RFC 9110, section 8.3.1), and StreamableHTTPServerTransport._check_accept_headers already lowercases the Accept media types before comparing. _check_content_type did not, so a spec-valid request with a mixed/upper-case Content-Type (e.g. "Application/JSON") was rejected with 415 Unsupported Media Type. Lowercase the parsed Content-Type media type before comparing to CONTENT_TYPE_JSON, consistent with _check_accept_headers. Adds a unit test for case-insensitive matching (the _check_content_type path was previously no-cover).
FileResource.set_binary_from_mime_type decides whether to read a file as text
or bytes via `mime_type.startswith("text/")`, but media types are
case-insensitive (RFC 9110, section 8.3.1). A FileResource declared with an
upper/mixed-case text type (e.g. "Text/Markdown") was misclassified as binary
and read with read_bytes instead of read_text.
Normalize with .lower() before the prefix check, consistent with the other
media-type checks in the SDK (transport_security, client streamable_http).
Adds a regression test.
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.
Summary
FileResource.set_binary_from_mime_typedecides whether to read a file as text or bytes from itsmime_typeviamime_type.startswith("text/"). Media types are case-insensitive (RFC 9110, §8.3.1), so aFileResourcedeclared with an upper/mixed-case text type — e.g.mime_type="Text/Markdown"— is misclassified as binary and read withread_bytesinstead ofread_text, returning abytesblob to clients instead of text.Fix
Lower-case the mime type before the prefix check, consistent with the SDK's other media-type checks (
transport_security, clientstreamable_http).Test
Added
test_uppercase_text_mime_type_is_treated_as_text: aFileResource(mime_type="Text/Markdown")keepsis_binaryFalse.Verified red→green: the new test fails on
main(treated as binary) and passes with the fix.uv run pytest tests/server/mcpserver/resources/test_file_resources.py→ 8 passed;ruff check/ruff format --checkclean.