Skip to content

fix: relax MIME type validation to accept all RFC 2045 types#2067

Open
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666:fix/relax-mime-type-validation-1756
Open

fix: relax MIME type validation to accept all RFC 2045 types#2067
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
BabyChrist666:fix/relax-mime-type-validation-1756

Conversation

@BabyChrist666
Copy link

Summary

Closes #1756

  • Replaced the restrictive regex pattern on Resource.mime_type with a lightweight field_validator that only checks for the presence of / (type/subtype structure)
  • Aligns with the MCP spec which defines mimeType as an optional string with no format constraints, and matches the TypeScript SDK behavior (no validation)
  • Fixes rejection of valid RFC 2045 MIME types including quoted parameter values (e.g. text/plain; charset="utf-8"), wildcard types (*/*), and complex boundary parameters

What changed

src/mcp/server/mcpserver/resources/base.py

  • Removed pattern=r"^[a-zA-Z0-9]+/[a-zA-Z0-9\-+.]+(;\s*[a-zA-Z0-9\-_.]+=[a-zA-Z0-9\-_.]+)*$" from the mime_type Field
  • Added validate_mime_type field_validator that only rejects values without a / separator

Why 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: mimeType is a plain string, and validation should only catch obvious mistakes (like passing "plaintext" instead of "text/plain"). This is consistent with:

  • The TypeScript SDK (no MIME validation)
  • ResourceTemplate in this SDK (already had no pattern constraint)
  • Protocol-level types in _types.py (no validation)

Test plan

  • Added tests/issues/test_1756_mime_type_relaxed.py with 12 test cases covering:
    • Basic MIME types, quoted parameter values, vendor types, wildcards, complex parameters
    • Invalid MIME types (no /, empty string)
    • Default MIME type preservation
  • Added test_resource_mime_type_validation to existing test_resources.py
  • All existing tests pass (including test_1754_mime_type_parameters.py)
  • 100% code coverage on base.py
  • ruff + pyright clean

🤖 Generated with Claude Code

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider removing or relaxing MIME type validation in FastMCP resources

1 participant