|
| 1 | +import base64 |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pydantic import AnyUrl |
| 5 | + |
| 6 | +from mcp import types |
| 7 | +from mcp.server.fastmcp import FastMCP |
| 8 | +from mcp.server.lowlevel import Server |
| 9 | +from mcp.shared.memory import ( |
| 10 | + create_connected_server_and_client_session as client_session, |
| 11 | +) |
| 12 | + |
| 13 | +pytestmark = pytest.mark.anyio |
| 14 | + |
| 15 | + |
| 16 | +async def test_fastmcp_resource_mime_type(): |
| 17 | + """Test that mime_type parameter is respected for resources.""" |
| 18 | + mcp = FastMCP("test") |
| 19 | + |
| 20 | + # Create a small test image as bytes |
| 21 | + image_bytes = b"fake_image_data" |
| 22 | + base64_string = base64.b64encode(image_bytes).decode("utf-8") |
| 23 | + |
| 24 | + @mcp.resource("test://image", mime_type="image/png") |
| 25 | + def get_image_as_string() -> str: |
| 26 | + """Return a test image as base64 string.""" |
| 27 | + return base64_string |
| 28 | + |
| 29 | + @mcp.resource("test://image_bytes", mime_type="image/png") |
| 30 | + def get_image_as_bytes() -> bytes: |
| 31 | + """Return a test image as bytes.""" |
| 32 | + return image_bytes |
| 33 | + |
| 34 | + # Test that resources are listed with correct mime type |
| 35 | + async with client_session(mcp._mcp_server) as client: |
| 36 | + # List resources and verify mime types |
| 37 | + resources = await client.list_resources() |
| 38 | + assert resources.resources is not None |
| 39 | + |
| 40 | + mapping = {str(r.uri): r for r in resources.resources} |
| 41 | + |
| 42 | + # Find our resources |
| 43 | + string_resource = mapping["test://image"] |
| 44 | + bytes_resource = mapping["test://image_bytes"] |
| 45 | + |
| 46 | + # Verify mime types |
| 47 | + assert ( |
| 48 | + string_resource.mimeType == "image/png" |
| 49 | + ), "String resource mime type not respected" |
| 50 | + assert ( |
| 51 | + bytes_resource.mimeType == "image/png" |
| 52 | + ), "Bytes resource mime type not respected" |
| 53 | + |
| 54 | + # Also verify the content can be read correctly |
| 55 | + string_result = await client.read_resource(AnyUrl("test://image")) |
| 56 | + assert len(string_result.contents) == 1 |
| 57 | + assert ( |
| 58 | + getattr(string_result.contents[0], "text") == base64_string |
| 59 | + ), "Base64 string mismatch" |
| 60 | + assert ( |
| 61 | + string_result.contents[0].mimeType == "image/png" |
| 62 | + ), "String content mime type not preserved" |
| 63 | + |
| 64 | + bytes_result = await client.read_resource(AnyUrl("test://image_bytes")) |
| 65 | + assert len(bytes_result.contents) == 1 |
| 66 | + assert ( |
| 67 | + base64.b64decode(getattr(bytes_result.contents[0], "blob")) == image_bytes |
| 68 | + ), "Bytes mismatch" |
| 69 | + assert ( |
| 70 | + bytes_result.contents[0].mimeType == "image/png" |
| 71 | + ), "Bytes content mime type not preserved" |
| 72 | + |
| 73 | + |
| 74 | +async def test_lowlevel_resource_mime_type(): |
| 75 | + """Test that mime_type parameter is respected for resources.""" |
| 76 | + server = Server("test") |
| 77 | + |
| 78 | + # Create a small test image as bytes |
| 79 | + image_bytes = b"fake_image_data" |
| 80 | + base64_string = base64.b64encode(image_bytes).decode("utf-8") |
| 81 | + |
| 82 | + # Create test resources with specific mime types |
| 83 | + test_resources = [ |
| 84 | + types.Resource( |
| 85 | + uri=AnyUrl("test://image"), name="test image", mimeType="image/png" |
| 86 | + ), |
| 87 | + types.Resource( |
| 88 | + uri=AnyUrl("test://image_bytes"), |
| 89 | + name="test image bytes", |
| 90 | + mimeType="image/png", |
| 91 | + ), |
| 92 | + ] |
| 93 | + |
| 94 | + @server.list_resources() |
| 95 | + async def handle_list_resources(): |
| 96 | + return test_resources |
| 97 | + |
| 98 | + @server.read_resource() |
| 99 | + async def handle_read_resource(uri: AnyUrl): |
| 100 | + if str(uri) == "test://image": |
| 101 | + return base64_string |
| 102 | + elif str(uri) == "test://image_bytes": |
| 103 | + return image_bytes |
| 104 | + raise Exception(f"Resource not found: {uri}") |
| 105 | + |
| 106 | + # Test that resources are listed with correct mime type |
| 107 | + async with client_session(server) as client: |
| 108 | + # List resources and verify mime types |
| 109 | + resources = await client.list_resources() |
| 110 | + assert resources.resources is not None |
| 111 | + |
| 112 | + mapping = {str(r.uri): r for r in resources.resources} |
| 113 | + |
| 114 | + # Find our resources |
| 115 | + string_resource = mapping["test://image"] |
| 116 | + bytes_resource = mapping["test://image_bytes"] |
| 117 | + |
| 118 | + # Verify mime types |
| 119 | + assert ( |
| 120 | + string_resource.mimeType == "image/png" |
| 121 | + ), "String resource mime type not respected" |
| 122 | + assert ( |
| 123 | + bytes_resource.mimeType == "image/png" |
| 124 | + ), "Bytes resource mime type not respected" |
| 125 | + |
| 126 | + # Also verify the content can be read correctly |
| 127 | + string_result = await client.read_resource(AnyUrl("test://image")) |
| 128 | + assert len(string_result.contents) == 1 |
| 129 | + assert ( |
| 130 | + getattr(string_result.contents[0], "text") == base64_string |
| 131 | + ), "Base64 string mismatch" |
| 132 | + assert ( |
| 133 | + string_result.contents[0].mimeType == "image/png" |
| 134 | + ), "String content mime type not preserved" |
| 135 | + |
| 136 | + bytes_result = await client.read_resource(AnyUrl("test://image_bytes")) |
| 137 | + assert len(bytes_result.contents) == 1 |
| 138 | + assert ( |
| 139 | + base64.b64decode(getattr(bytes_result.contents[0], "blob")) == image_bytes |
| 140 | + ), "Bytes mismatch" |
| 141 | + assert ( |
| 142 | + bytes_result.contents[0].mimeType == "image/png" |
| 143 | + ), "Bytes content mime type not preserved" |
0 commit comments