π The fast, Pythonic way to build MCP servers and clients.
Forward the resources/read result meta through proxies (#5500)
Asem ahmed09e1a7e08da0
diff --git a/fastmcp_slim/fastmcp/server/providers/proxy.py b/fastmcp_slim/fastmcp/server/providers/proxy.pyindex b52d32bd0..33de8797c 100644--- a/fastmcp_slim/fastmcp/server/providers/proxy.py+++ b/fastmcp_slim/fastmcp/server/providers/proxy.py@@ -213,10 +213,7 @@ def _session_request_meta(async def _relay_read_resource(client: Client, uri: str, ctx: Context | None-) -> (- list[mcp_types.TextResourceContents | mcp_types.BlobResourceContents]- | mcp_types.InputRequiredResult-):+) -> mcp_types.ReadResourceResult | mcp_types.InputRequiredResult:"""Read a backend resource, surfacing a guard ask rather than driving it.Mirrors `ProxyTool.run`: on a modern backend the low-level session is used@@ -228,8 +225,8 @@ async def _relay_read_resource("""meta = _forwardable_request_meta(ctx)if client.protocol_version not in MODERN_PROTOCOL_VERSIONS:- return await client.read_resource(uri, meta=meta)- result = await client._await_with_session_monitoring(+ return await client.read_resource_mcp(uri, meta=meta)+ return await client._await_with_session_monitoring(client.session.read_resource(uri,meta=_session_request_meta(meta),@@ -238,9 +235,6 @@ async def _relay_read_resource(allow_input_required=True,))- if isinstance(result, mcp_types.InputRequiredResult):- return result- return list(result.contents)def _stash_proxy_request_context(client: Client, ctx: Context) -> None:@@ -552,14 +546,14 @@ class ProxyResource(Resource):result = await _relay_read_resource(client, backend_uri, ctx)if isinstance(result, mcp_types.InputRequiredResult):return InputRequiredResourceResult(result)- if not result:+ if not result.contents:raise ResourceError(f"Remote server returned empty content for {backend_uri}")# Process all items in the result list, not just the first onecontents: list[ResourceContent] = []- for item in result:+ for item in result.contents:if isinstance(item, TextResourceContents):contents.append(ResourceContent(@@ -579,7 +573,9 @@ class ProxyResource(Resource):else:raise ResourceError(f"Unsupported content type: {type(item)}")- return ResourceResult(contents=contents)+ return ResourceResult(+ contents=contents, meta=_forwardable_server_meta(result.meta) or None+ )def get_span_attributes(self) -> dict[str, Any]:return super().get_span_attributes() | {@@ -670,14 +666,14 @@ class ProxyTemplate(ResourceTemplate):_cached_content=InputRequiredResourceResult(result),)- if not result:+ if not result.contents:raise ResourceError(f"Remote server returned empty content for {parameterized_uri}")# Process all items in the result list, not just the first onecontents: list[ResourceContent] = []- for item in result:+ for item in result.contents:if isinstance(item, TextResourceContents):contents.append(ResourceContent(@@ -697,7 +693,9 @@ class ProxyTemplate(ResourceTemplate):else:raise ResourceError(f"Unsupported content type: {type(item)}")- cached_content = ResourceResult(contents=contents)+ cached_content = ResourceResult(+ contents=contents, meta=_forwardable_server_meta(result.meta) or None+ )return ProxyResource(client_factory=self._client_factory,@@ -705,7 +703,7 @@ class ProxyTemplate(ResourceTemplate):name=self.name,title=self.title,description=self.description,- mime_type=result[+ mime_type=result.contents[0].mime_type, # Use first item's mimeType for backward compatibilityicons=self.icons,diff --git a/tests/server/providers/proxy/test_proxy_server.py b/tests/server/providers/proxy/test_proxy_server.pyindex 2a66cc412..4906280d4 100644--- a/tests/server/providers/proxy/test_proxy_server.py+++ b/tests/server/providers/proxy/test_proxy_server.py@@ -599,6 +599,16 @@ class TestResources:assert original_result[2].text == "# Markdown\nContent"assert original_result[2].mime_type == "text/markdown"+ @pytest.mark.parametrize("mode", ["legacy", "auto"])+ async def test_proxy_forwards_read_result_meta(self, fastmcp_server, mode):+ """The `_meta` of the resources/read result, not just of each content item."""+ proxy = create_proxy(fastmcp_server)+ async with Client(proxy, mode=mode) as client:+ result = await client.read_resource_mcp("data://multi")++ assert result.meta is not None+ assert result.meta["count"] == 3+async def test_read_resource_returns_none_if_not_found(self, proxy_server):with pytest.raises(MCPError, match="Resource not found: 'resource://nonexistent'"@@ -712,6 +722,16 @@ class TestResourceTemplates:assert original_result[1].text == '{"id": "test123", "status": "active"}'assert original_result[1].mime_type == "application/json"+ @pytest.mark.parametrize("mode", ["legacy", "auto"])+ async def test_proxy_template_forwards_read_result_meta(self, fastmcp_server, mode):+ """The `_meta` of the resources/read result, not just of each content item."""+ proxy = create_proxy(fastmcp_server)+ async with Client(proxy, mode=mode) as client:+ result = await client.read_resource_mcp("data://multi/test123")++ assert result.meta is not None+ assert result.meta["id"] == "test123"+async def test_proxy_can_overwrite_proxied_resource_template(self, proxy_server):"""Test that a resource template defined on the proxy can overwrite the proxied template with the same URI template.