-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: respect resource mime type in responses #170
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import base64 | ||
|
||
import pytest | ||
from pydantic import AnyUrl | ||
|
||
from mcp import types | ||
from mcp.server.fastmcp import FastMCP | ||
from mcp.server.lowlevel import Server | ||
from mcp.shared.memory import ( | ||
create_connected_server_and_client_session as client_session, | ||
) | ||
|
||
pytestmark = pytest.mark.anyio | ||
|
||
|
||
async def test_fastmcp_resource_mime_type(): | ||
"""Test that mime_type parameter is respected for resources.""" | ||
mcp = FastMCP("test") | ||
|
||
# Create a small test image as bytes | ||
image_bytes = b"fake_image_data" | ||
base64_string = base64.b64encode(image_bytes).decode("utf-8") | ||
|
||
@mcp.resource("test://image", mime_type="image/png") | ||
def get_image_as_string() -> str: | ||
"""Return a test image as base64 string.""" | ||
return base64_string | ||
|
||
@mcp.resource("test://image_bytes", mime_type="image/png") | ||
def get_image_as_bytes() -> bytes: | ||
"""Return a test image as bytes.""" | ||
return image_bytes | ||
|
||
# Test that resources are listed with correct mime type | ||
async with client_session(mcp._mcp_server) as client: | ||
# List resources and verify mime types | ||
resources = await client.list_resources() | ||
assert resources.resources is not None | ||
|
||
mapping = {str(r.uri): r for r in resources.resources} | ||
|
||
# Find our resources | ||
string_resource = mapping["test://image"] | ||
bytes_resource = mapping["test://image_bytes"] | ||
|
||
# Verify mime types | ||
assert ( | ||
string_resource.mimeType == "image/png" | ||
), "String resource mime type not respected" | ||
assert ( | ||
bytes_resource.mimeType == "image/png" | ||
), "Bytes resource mime type not respected" | ||
|
||
# Also verify the content can be read correctly | ||
string_result = await client.read_resource(AnyUrl("test://image")) | ||
assert len(string_result.contents) == 1 | ||
assert ( | ||
getattr(string_result.contents[0], "text") == base64_string | ||
), "Base64 string mismatch" | ||
assert ( | ||
string_result.contents[0].mimeType == "image/png" | ||
), "String content mime type not preserved" | ||
|
||
bytes_result = await client.read_resource(AnyUrl("test://image_bytes")) | ||
assert len(bytes_result.contents) == 1 | ||
assert ( | ||
base64.b64decode(getattr(bytes_result.contents[0], "blob")) == image_bytes | ||
), "Bytes mismatch" | ||
assert ( | ||
bytes_result.contents[0].mimeType == "image/png" | ||
), "Bytes content mime type not preserved" | ||
|
||
|
||
async def test_lowlevel_resource_mime_type(): | ||
"""Test that mime_type parameter is respected for resources.""" | ||
server = Server("test") | ||
|
||
# Create a small test image as bytes | ||
image_bytes = b"fake_image_data" | ||
base64_string = base64.b64encode(image_bytes).decode("utf-8") | ||
|
||
# Create test resources with specific mime types | ||
test_resources = [ | ||
types.Resource( | ||
uri=AnyUrl("test://image"), name="test image", mimeType="image/png" | ||
), | ||
types.Resource( | ||
uri=AnyUrl("test://image_bytes"), | ||
name="test image bytes", | ||
mimeType="image/png", | ||
), | ||
] | ||
|
||
@server.list_resources() | ||
async def handle_list_resources(): | ||
return test_resources | ||
|
||
@server.read_resource() | ||
async def handle_read_resource(uri: AnyUrl): | ||
if str(uri) == "test://image": | ||
return (base64_string, "image/png") | ||
elif str(uri) == "test://image_bytes": | ||
return (bytes(image_bytes), "image/png") | ||
raise Exception(f"Resource not found: {uri}") | ||
|
||
# Test that resources are listed with correct mime type | ||
async with client_session(server) as client: | ||
# List resources and verify mime types | ||
resources = await client.list_resources() | ||
assert resources.resources is not None | ||
|
||
mapping = {str(r.uri): r for r in resources.resources} | ||
|
||
# Find our resources | ||
string_resource = mapping["test://image"] | ||
bytes_resource = mapping["test://image_bytes"] | ||
|
||
# Verify mime types | ||
assert ( | ||
string_resource.mimeType == "image/png" | ||
), "String resource mime type not respected" | ||
assert ( | ||
bytes_resource.mimeType == "image/png" | ||
), "Bytes resource mime type not respected" | ||
|
||
# Also verify the content can be read correctly | ||
string_result = await client.read_resource(AnyUrl("test://image")) | ||
assert len(string_result.contents) == 1 | ||
assert ( | ||
getattr(string_result.contents[0], "text") == base64_string | ||
), "Base64 string mismatch" | ||
assert ( | ||
string_result.contents[0].mimeType == "image/png" | ||
), "String content mime type not preserved" | ||
|
||
bytes_result = await client.read_resource(AnyUrl("test://image_bytes")) | ||
assert len(bytes_result.contents) == 1 | ||
assert ( | ||
base64.b64decode(getattr(bytes_result.contents[0], "blob")) == image_bytes | ||
), "Bytes mismatch" | ||
assert ( | ||
bytes_result.contents[0].mimeType == "image/png" | ||
), "Bytes content mime type not preserved" |
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.