From 0d3e02f6a2677189eef84533cf4dc8a20b758913 Mon Sep 17 00:00:00 2001 From: David Soria Parra Date: Mon, 3 Feb 2025 11:33:32 +0000 Subject: [PATCH] fix: standardize resource response format Github-Issue:#141 --- src/mcp/client/session.py | 11 ++ tests/issues/test_141_resource_templates.py | 118 ++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 tests/issues/test_141_resource_templates.py diff --git a/src/mcp/client/session.py b/src/mcp/client/session.py index 27ca74d..4858ede 100644 --- a/src/mcp/client/session.py +++ b/src/mcp/client/session.py @@ -120,6 +120,17 @@ async def list_resources(self) -> types.ListResourcesResult: types.ListResourcesResult, ) + async def list_resource_templates(self) -> types.ListResourceTemplatesResult: + """Send a resources/templates/list request.""" + return await self.send_request( + types.ClientRequest( + types.ListResourceTemplatesRequest( + method="resources/templates/list", + ) + ), + types.ListResourceTemplatesResult, + ) + async def read_resource(self, uri: AnyUrl) -> types.ReadResourceResult: """Send a resources/read request.""" return await self.send_request( diff --git a/tests/issues/test_141_resource_templates.py b/tests/issues/test_141_resource_templates.py new file mode 100644 index 0000000..d6526e9 --- /dev/null +++ b/tests/issues/test_141_resource_templates.py @@ -0,0 +1,118 @@ +import pytest +from pydantic import AnyUrl + +from mcp.server.fastmcp import FastMCP +from mcp.shared.memory import ( + create_connected_server_and_client_session as client_session, +) +from mcp.types import ( + ListResourceTemplatesResult, + TextResourceContents, +) + + +@pytest.mark.anyio +async def test_resource_template_edge_cases(): + """Test server-side resource template validation""" + mcp = FastMCP("Demo") + + # Test case 1: Template with multiple parameters + @mcp.resource("resource://users/{user_id}/posts/{post_id}") + def get_user_post(user_id: str, post_id: str) -> str: + return f"Post {post_id} by user {user_id}" + + # Test case 2: Template with optional parameter (should fail) + with pytest.raises(ValueError, match="Mismatch between URI parameters"): + + @mcp.resource("resource://users/{user_id}/profile") + def get_user_profile(user_id: str, optional_param: str | None = None) -> str: + return f"Profile for user {user_id}" + + # Test case 3: Template with mismatched parameters + with pytest.raises(ValueError, match="Mismatch between URI parameters"): + + @mcp.resource("resource://users/{user_id}/profile") + def get_user_profile_mismatch(different_param: str) -> str: + return f"Profile for user {different_param}" + + # Test case 4: Template with extra function parameters + with pytest.raises(ValueError, match="Mismatch between URI parameters"): + + @mcp.resource("resource://users/{user_id}/profile") + def get_user_profile_extra(user_id: str, extra_param: str) -> str: + return f"Profile for user {user_id}" + + # Test case 5: Template with missing function parameters + with pytest.raises(ValueError, match="Mismatch between URI parameters"): + + @mcp.resource("resource://users/{user_id}/profile/{section}") + def get_user_profile_missing(user_id: str) -> str: + return f"Profile for user {user_id}" + + # Verify valid template works + result = await mcp.read_resource("resource://users/123/posts/456") + assert result.content == "Post 456 by user 123" + assert result.mime_type == "text/plain" + + # Verify invalid parameters raise error + with pytest.raises(ValueError, match="Unknown resource"): + await mcp.read_resource("resource://users/123/posts") # Missing post_id + + with pytest.raises(ValueError, match="Unknown resource"): + await mcp.read_resource( + "resource://users/123/posts/456/extra" + ) # Extra path component + + +@pytest.mark.anyio +async def test_resource_template_client_interaction(): + """Test client-side resource template interaction""" + mcp = FastMCP("Demo") + + # Register some templated resources + @mcp.resource("resource://users/{user_id}/posts/{post_id}") + def get_user_post(user_id: str, post_id: str) -> str: + return f"Post {post_id} by user {user_id}" + + @mcp.resource("resource://users/{user_id}/profile") + def get_user_profile(user_id: str) -> str: + return f"Profile for user {user_id}" + + async with client_session(mcp._mcp_server) as session: + # Initialize the session + await session.initialize() + + # List available resources + resources = await session.list_resource_templates() + assert isinstance(resources, ListResourceTemplatesResult) + assert len(resources.resourceTemplates) == 2 + + # Verify resource templates are listed correctly + templates = [r.uriTemplate for r in resources.resourceTemplates] + assert "resource://users/{user_id}/posts/{post_id}" in templates + assert "resource://users/{user_id}/profile" in templates + + # Read a resource with valid parameters + result = await session.read_resource(AnyUrl("resource://users/123/posts/456")) + contents = result.contents[0] + assert isinstance(contents, TextResourceContents) + assert contents.text == "Post 456 by user 123" + assert contents.mimeType == "text/plain" + + # Read another resource with valid parameters + result = await session.read_resource(AnyUrl("resource://users/789/profile")) + contents = result.contents[0] + assert isinstance(contents, TextResourceContents) + assert contents.text == "Profile for user 789" + assert contents.mimeType == "text/plain" + + # Verify invalid resource URIs raise appropriate errors + with pytest.raises(Exception): # Specific exception type may vary + await session.read_resource( + AnyUrl("resource://users/123/posts") + ) # Missing post_id + + with pytest.raises(Exception): # Specific exception type may vary + await session.read_resource( + AnyUrl("resource://users/123/invalid") + ) # Invalid template