refactor: standardize resource response format

Introduce ReadResourceContents type to properly handle MIME types in resource responses. Breaking change in FastMCP read_resource() return type.

Github-Issue:#152
This commit is contained in:
David Soria Parra
2025-01-27 20:36:10 +00:00
parent f90cf6a2a5
commit 070e8412c0
7 changed files with 155 additions and 35 deletions

View File

@@ -88,10 +88,10 @@ async def test_list_resources(mcp: FastMCP):
@pytest.mark.anyio
async def test_read_resource_dir(mcp: FastMCP):
files, mime_type = await mcp.read_resource("dir://test_dir")
assert mime_type == "text/plain"
res = await mcp.read_resource("dir://test_dir")
assert res.mime_type == "text/plain"
files = json.loads(files)
files = json.loads(res.content)
assert sorted([Path(f).name for f in files]) == [
"config.json",
@@ -102,8 +102,8 @@ 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")
assert result == "print('hello world')"
res = await mcp.read_resource("file://test_dir/example.py")
assert res.content == "print('hello world')"
@pytest.mark.anyio
@@ -119,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")
assert result == "File not found"
res = await mcp.read_resource("file://test_dir/example.py")
assert res.content == "File not found"