Skip to content

Commit 278d141

Browse files
committed
Cleanup MCP resource tests.
1 parent 3d47349 commit 278d141

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

tests/test_mcp.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,20 +1550,12 @@ async def test_read_resource_template(run_context: RunContext[int]):
15501550

15511551

15521552
async def test_read_resource_not_found(mcp_server: MCPServerStdio) -> None:
1553-
"""Test that read_resource raises MCPError for non-existent resources with non-standard error codes."""
1554-
async with mcp_server:
1555-
# FastMCP uses error code 0 instead of -32002, so it should raise
1556-
with pytest.raises(MCPError, match='Unknown resource: resource://does_not_exist') as exc_info:
1557-
await mcp_server.read_resource('resource://does_not_exist')
1558-
1559-
# Verify the exception has the expected attributes
1560-
assert exc_info.value.code == 0
1561-
assert exc_info.value.message == 'Unknown resource: resource://does_not_exist'
1553+
"""Test that read_resource returns None for MCP spec error code -32002 (resource not found).
15621554
1555+
As per https://modelcontextprotocol.io/specification/2025-06-18/server/resources#error-handling
15631556
1564-
async def test_read_resource_not_found_mcp_spec(mcp_server: MCPServerStdio) -> None:
1565-
"""Test that read_resource returns None for MCP spec error code -32002 (resource not found)."""
1566-
# As per https://modelcontextprotocol.io/specification/2025-06-18/server/resources#error-handling
1557+
Note: We mock this because FastMCP uses error code 0 instead of -32002, which is non-standard.
1558+
"""
15671559
mcp_error = McpError(error=ErrorData(code=-32002, message='Resource not found'))
15681560

15691561
async with mcp_server:
@@ -1576,6 +1568,27 @@ async def test_read_resource_not_found_mcp_spec(mcp_server: MCPServerStdio) -> N
15761568
assert result is None
15771569

15781570

1571+
async def test_read_resource_error(mcp_server: MCPServerStdio) -> None:
1572+
"""Test that read_resource converts McpError to MCPError for generic errors."""
1573+
mcp_error = McpError(
1574+
error=ErrorData(code=-32603, message='Failed to read resource', data={'details': 'disk error'})
1575+
)
1576+
1577+
async with mcp_server:
1578+
with patch.object(
1579+
mcp_server._client, # pyright: ignore[reportPrivateUsage]
1580+
'read_resource',
1581+
new=AsyncMock(side_effect=mcp_error),
1582+
):
1583+
with pytest.raises(MCPError, match='Failed to read resource') as exc_info:
1584+
await mcp_server.read_resource('resource://error')
1585+
1586+
# Verify the exception has the expected attributes
1587+
assert exc_info.value.code == -32603
1588+
assert exc_info.value.message == 'Failed to read resource'
1589+
assert exc_info.value.data == {'details': 'disk error'}
1590+
1591+
15791592
async def test_read_resource_empty_contents(mcp_server: MCPServerStdio) -> None:
15801593
"""Test that read_resource returns None when server returns empty contents."""
15811594
from mcp.types import ReadResourceResult

0 commit comments

Comments
 (0)