fix: respect resource mime type in responses

The server was ignoring mime types set on resources, defaulting to text/plain
for strings and application/octet-stream for bytes. Now properly preserves
the specified mime type in both FastMCP and low-level server implementations.

Note that this is breaks backwards compatibility as it changes the return
values of read_resource() on FastMCP. It is BC compatible on lowlevel since
it only extends the callback.

Github-Issue: #152
Reported-by: eiseleMichael
This commit is contained in:
David Soria Parra
2025-01-24 14:20:42 +00:00
parent f10c2e3f1f
commit 8ff4b5e9d3
6 changed files with 82 additions and 40 deletions

View File

@@ -88,7 +88,9 @@ async def test_list_resources(mcp: FastMCP):
@pytest.mark.anyio
async def test_read_resource_dir(mcp: FastMCP):
files = await mcp.read_resource("dir://test_dir")
files, mime_type = await mcp.read_resource("dir://test_dir")
assert mime_type == "text/plain"
files = json.loads(files)
assert sorted([Path(f).name for f in files]) == [
@@ -100,7 +102,7 @@ async def test_read_resource_dir(mcp: FastMCP):
@pytest.mark.anyio
async def test_read_resource_file(mcp: FastMCP):
result = await mcp.read_resource("file://test_dir/example.py")
result, _ = await mcp.read_resource("file://test_dir/example.py")
assert result == "print('hello world')"
@@ -117,5 +119,5 @@ async def test_delete_file_and_check_resources(mcp: FastMCP, test_dir: Path):
await mcp.call_tool(
"delete_file", arguments=dict(path=str(test_dir / "example.py"))
)
result = await mcp.read_resource("file://test_dir/example.py")
result, _ = await mcp.read_resource("file://test_dir/example.py")
assert result == "File not found"