|
| 1 | +"""Test for Github issue #1756: Consider removing or relaxing MIME type validation in FastMCP resources. |
| 2 | +
|
| 3 | +The validation regex for FastMCP Resource's mime_type field is too strict and does not allow valid MIME types. |
| 4 | +Ex: parameter values with quotes strings and valid token characters (e.g. !, #, *, +, etc.) were rejected. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +from pydantic import AnyUrl, ValidationError |
| 9 | + |
| 10 | +from mcp.server.fastmcp import FastMCP |
| 11 | +from mcp.shared.memory import ( |
| 12 | + create_connected_server_and_client_session as client_session, |
| 13 | +) |
| 14 | + |
| 15 | +pytestmark = pytest.mark.anyio |
| 16 | + |
| 17 | + |
| 18 | +# Exhaustive list of valid mime types formats. |
| 19 | +# https://www.iana.org/assignments/media-types/media-types.xhtml |
| 20 | +def _test_data_mime_type_with_valid_rfc2045_formats(): |
| 21 | + """Test data for valid mime types with rfc2045 formats.""" |
| 22 | + return [ |
| 23 | + # Standard types |
| 24 | + ("application/json", "Simple application type"), |
| 25 | + ("text/html", "Simple text type"), |
| 26 | + ("image/png", "Simple image type"), |
| 27 | + ("audio/mpeg", "Simple audio type"), |
| 28 | + ("video/mp4", "Simple video type"), |
| 29 | + ("font/woff2", "Simple font type"), |
| 30 | + ("model/gltf+json", "Model type"), |
| 31 | + # Vendor specific (vnd) |
| 32 | + ("application/vnd.api+json", "Vendor specific JSON api"), |
| 33 | + ("application/vnd.ms-excel", "Vendor specific Excel"), |
| 34 | + ("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Complex vendor type"), |
| 35 | + # Parameters |
| 36 | + ('text/plain; charset="utf-8"', "MIME type with quotes in parameter value"), |
| 37 | + ('text/plain; charset="utf!8"', "MIME type with exclamation mark in parameter value"), |
| 38 | + ('text/plain; charset="utf*8"', "MIME type with asterisk in parameter value"), |
| 39 | + ('text/plain; charset="utf#8"', "MIME type with hash in parameter value"), |
| 40 | + ('text/plain; charset="utf+8"', "MIME type with plus in parameter value"), |
| 41 | + ("text/plain; charset=utf-8; format=flowed", "Multiple parameters"), |
| 42 | + ("multipart/form-data; boundary=---1234", "Multipart with boundary"), |
| 43 | + # Special characters in subtype |
| 44 | + ("image/svg+xml", "Subtype with plus"), |
| 45 | + # Parmeter issues. |
| 46 | + ("text/plain; charset=utf 8", "Unquoted space in parameter"), |
| 47 | + ('text/plain; charset="utf-8', "Unbalanced quotes"), |
| 48 | + ("text/plain; charset", "Parameter missing value"), |
| 49 | + ] |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("mime_type, description", _test_data_mime_type_with_valid_rfc2045_formats()) |
| 53 | +async def test_mime_type_with_valid_rfc2045_formats(mime_type: str, description: str): |
| 54 | + """Test that MIME type with valid RFC 2045 token characters are accepted.""" |
| 55 | + mcp = FastMCP("test") |
| 56 | + |
| 57 | + @mcp.resource("ui://widget", mime_type=mime_type) |
| 58 | + def widget() -> str: |
| 59 | + raise NotImplementedError() |
| 60 | + |
| 61 | + resources = await mcp.list_resources() |
| 62 | + assert len(resources) == 1 |
| 63 | + assert resources[0].mimeType == mime_type |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("mime_type, description", _test_data_mime_type_with_valid_rfc2045_formats()) |
| 67 | +async def test_mime_type_preserved_in_read_resource(mime_type: str, description: str): |
| 68 | + """Test that MIME type with parameters is preserved when reading resource.""" |
| 69 | + mcp = FastMCP("test") |
| 70 | + |
| 71 | + @mcp.resource("ui://my-widget", mime_type=mime_type) |
| 72 | + def my_widget() -> str: |
| 73 | + return "<html><body>Hello MCP-UI</body></html>" |
| 74 | + |
| 75 | + async with client_session(mcp._mcp_server) as client: |
| 76 | + # Read the resource |
| 77 | + result = await client.read_resource(AnyUrl("ui://my-widget")) |
| 78 | + assert len(result.contents) == 1 |
| 79 | + assert result.contents[0].mimeType == mime_type |
| 80 | + |
| 81 | + |
| 82 | +def _test_data_mime_type_with_invalid_rfc2045_formats(): |
| 83 | + """Test data for invalid mime types with rfc2045 formats.""" |
| 84 | + return [ |
| 85 | + ("charset=utf-8", "MIME type with no main and subtype but only parameters."), |
| 86 | + ("text", "Missing subtype"), |
| 87 | + ("text/", "Empty subtype"), |
| 88 | + ("/html", "Missing type"), |
| 89 | + (" ", "Whitespace"), |
| 90 | + # --- Structural --- |
| 91 | + ("text//plain", "Double slash"), |
| 92 | + ("application/json/", "Trailing slash"), |
| 93 | + ("text / plain", "Spaces around primary slash"), |
| 94 | + # --- Illegal Characters --- |
| 95 | + ("image/jp@g", "Illegal character in subtype"), |
| 96 | + ("text(comment)/plain", "Comments inside type name"), |
| 97 | + # --- Parameter Issues --- |
| 98 | + ("text/plain; =utf-8", "Parameter missing key"), |
| 99 | + ("text/plain charset=utf-8", "Missing semicolon separator"), |
| 100 | + # --- Encoding/Non-ASCII --- |
| 101 | + ("text/plâin", "Non-ASCII character in subtype"), |
| 102 | + ] |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("mime_type, description", _test_data_mime_type_with_invalid_rfc2045_formats()) |
| 106 | +async def test_mime_type_with_invalid_rfc2045_formats(mime_type: str, description: str): |
| 107 | + """Test that MIME type with invalid RFC 2045 token characters are rejected.""" |
| 108 | + mcp = FastMCP("test") |
| 109 | + |
| 110 | + with pytest.raises(ValidationError): |
| 111 | + |
| 112 | + @mcp.resource("ui://widget", mime_type=mime_type) |
| 113 | + def widget() -> str: |
| 114 | + raise NotImplementedError() |
0 commit comments