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:
parent
f90cf6a2a5
commit
070e8412c0
2 changed files with 155 additions and 35 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -581,8 +581,8 @@ class TestContextInjection:
|
|||
|
||||
@mcp.tool()
|
||||
async def tool_with_resource(ctx: Context) -> str:
|
||||
data, mime_type = await ctx.read_resource("test://data")
|
||||
return f"Read resource: {data} with mime type {mime_type}"
|
||||
r = await ctx.read_resource("test://data")
|
||||
return f"Read resource: {r.content} with mime type {r.mime_type}"
|
||||
|
||||
async with client_session(mcp._mcp_server) as client:
|
||||
result = await client.call_tool("tool_with_resource", {})
|
||||
|
|
|
|||
Loading…
Reference in a new issue