|
| 1 | +"""Test for GitHub issue #1756: Relax MIME type validation in FastMCP resources. |
| 2 | +
|
| 3 | +The previous MIME type validation used a restrictive regex pattern that rejected |
| 4 | +valid MIME types per RFC 2045. For example, quoted parameter values like |
| 5 | +'text/plain; charset="utf-8"' were rejected. |
| 6 | +
|
| 7 | +The fix replaces the regex with a lightweight validator that only checks for the |
| 8 | +minimal type/subtype structure (presence of '/'), aligning with the MCP spec |
| 9 | +which defines mimeType as an optional string with no format constraints. |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from mcp.server.mcpserver.resources.types import FunctionResource |
| 15 | + |
| 16 | + |
| 17 | +def _dummy() -> str: # pragma: no cover |
| 18 | + return "data" |
| 19 | + |
| 20 | + |
| 21 | +class TestRelaxedMimeTypeValidation: |
| 22 | + """Test that MIME type validation accepts all RFC 2045 valid types.""" |
| 23 | + |
| 24 | + def test_basic_mime_types(self): |
| 25 | + """Standard MIME types should be accepted.""" |
| 26 | + for mime in [ |
| 27 | + "text/plain", |
| 28 | + "application/json", |
| 29 | + "application/octet-stream", |
| 30 | + "image/png", |
| 31 | + "text/html", |
| 32 | + "text/csv", |
| 33 | + "application/xml", |
| 34 | + ]: |
| 35 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 36 | + assert r.mime_type == mime |
| 37 | + |
| 38 | + def test_mime_type_with_quoted_parameter_value(self): |
| 39 | + """Quoted parameter values are valid per RFC 2045 (the original issue).""" |
| 40 | + mime = 'text/plain; charset="utf-8"' |
| 41 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 42 | + assert r.mime_type == mime |
| 43 | + |
| 44 | + def test_mime_type_with_unquoted_parameter(self): |
| 45 | + """Unquoted parameter values should still work.""" |
| 46 | + mime = "text/plain; charset=utf-8" |
| 47 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 48 | + assert r.mime_type == mime |
| 49 | + |
| 50 | + def test_mime_type_with_profile_parameter(self): |
| 51 | + """Profile parameter used by MCP-UI (from issue #1754).""" |
| 52 | + mime = "text/html;profile=mcp-app" |
| 53 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 54 | + assert r.mime_type == mime |
| 55 | + |
| 56 | + def test_mime_type_with_multiple_parameters(self): |
| 57 | + """Multiple parameters should be accepted.""" |
| 58 | + mime = "text/plain; charset=utf-8; format=fixed" |
| 59 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 60 | + assert r.mime_type == mime |
| 61 | + |
| 62 | + def test_mime_type_with_vendor_type(self): |
| 63 | + """Vendor-specific MIME types (x- prefix, vnd.) should be accepted.""" |
| 64 | + for mime in [ |
| 65 | + "application/vnd.api+json", |
| 66 | + "application/x-www-form-urlencoded", |
| 67 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 68 | + ]: |
| 69 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 70 | + assert r.mime_type == mime |
| 71 | + |
| 72 | + def test_mime_type_with_suffix(self): |
| 73 | + """Structured syntax suffix types should be accepted.""" |
| 74 | + for mime in [ |
| 75 | + "application/ld+json", |
| 76 | + "application/soap+xml", |
| 77 | + "image/svg+xml", |
| 78 | + ]: |
| 79 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 80 | + assert r.mime_type == mime |
| 81 | + |
| 82 | + def test_mime_type_with_wildcard(self): |
| 83 | + """Wildcard MIME types should be accepted.""" |
| 84 | + for mime in [ |
| 85 | + "application/*", |
| 86 | + "*/*", |
| 87 | + ]: |
| 88 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 89 | + assert r.mime_type == mime |
| 90 | + |
| 91 | + def test_mime_type_with_complex_parameters(self): |
| 92 | + """Complex parameter values per RFC 2045.""" |
| 93 | + for mime in [ |
| 94 | + 'multipart/form-data; boundary="----WebKitFormBoundary"', |
| 95 | + "text/html; charset=ISO-8859-1", |
| 96 | + 'application/json; profile="https://example.com/schema"', |
| 97 | + ]: |
| 98 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type=mime) |
| 99 | + assert r.mime_type == mime |
| 100 | + |
| 101 | + def test_invalid_mime_type_no_slash(self): |
| 102 | + """MIME types without '/' should be rejected.""" |
| 103 | + with pytest.raises(ValueError, match="must contain a '/'"): |
| 104 | + FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type="plaintext") |
| 105 | + |
| 106 | + def test_invalid_mime_type_empty_string(self): |
| 107 | + """Empty string should be rejected (no '/').""" |
| 108 | + with pytest.raises(ValueError, match="must contain a '/'"): |
| 109 | + FunctionResource(uri="test://x", name="t", fn=_dummy, mime_type="") |
| 110 | + |
| 111 | + def test_default_mime_type(self): |
| 112 | + """Default MIME type should be text/plain.""" |
| 113 | + r = FunctionResource(uri="test://x", name="t", fn=_dummy) |
| 114 | + assert r.mime_type == "text/plain" |
0 commit comments