fix: relax MIME type validation to accept all RFC 2045 types#2067
Open
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: relax MIME type validation to accept all RFC 2045 types#2067BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…ntextprotocol#1756) Replace restrictive regex pattern with lightweight validator that only checks for type/subtype structure (presence of '/'), aligning with the MCP spec which defines mimeType as an optional string with no format constraints. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Closes #1756
Resource.mime_typewith a lightweightfield_validatorthat only checks for the presence of/(type/subtype structure)mimeTypeas an optional string with no format constraints, and matches the TypeScript SDK behavior (no validation)text/plain; charset="utf-8"), wildcard types (*/*), and complex boundary parametersWhat changed
src/mcp/server/mcpserver/resources/base.pypattern=r"^[a-zA-Z0-9]+/[a-zA-Z0-9\-+.]+(;\s*[a-zA-Z0-9\-_.]+=[a-zA-Z0-9\-_.]+)*$"from themime_typeFieldvalidate_mime_typefield_validator that only rejects values without a/separatorWhy this approach
The previous regex was too restrictive for RFC 2045 compliance. Rather than trying to write a more complex regex that covers all edge cases (quoted strings, special characters, wildcards, etc.), this PR follows the MCP spec's intent:
mimeTypeis a plain string, and validation should only catch obvious mistakes (like passing"plaintext"instead of"text/plain"). This is consistent with:ResourceTemplatein this SDK (already had no pattern constraint)_types.py(no validation)Test plan
tests/issues/test_1756_mime_type_relaxed.pywith 12 test cases covering:/, empty string)test_resource_mime_type_validationto existingtest_resources.pytest_1754_mime_type_parameters.py)base.py🤖 Generated with Claude Code