π The fast, Pythonic way to build MCP servers and clients.
fix(resources): preserve public metadata in built-in read contents (#5778)
HootRocka9ec97e0e066
diff --git a/fastmcp_slim/fastmcp/resources/types.py b/fastmcp_slim/fastmcp/resources/types.pyindex 3707ee170..992e6cde1 100644--- a/fastmcp_slim/fastmcp/resources/types.py+++ b/fastmcp_slim/fastmcp/resources/types.py@@ -107,7 +107,13 @@ class FileResource(Resource):else:content = await self._async_path.read_text(encoding=self.encoding)return ResourceResult(- contents=[ResourceContent(content=content, mime_type=self.mime_type)]+ contents=[+ ResourceContent(+ content=content,+ mime_type=self.mime_type,+ meta=public_result_meta(self.meta),+ )+ ])except Exception as e:raise ResourceError(f"Error reading file {self.path}") from e@@ -129,7 +135,11 @@ class HttpResource(Resource):_ = response.raise_for_status()return ResourceResult(contents=[- ResourceContent(content=response.text, mime_type=self.mime_type)+ ResourceContent(+ content=response.text,+ mime_type=self.mime_type,+ meta=public_result_meta(self.meta),+ )])@@ -185,7 +195,13 @@ class DirectoryResource(Resource):content = json.dumps({"files": file_list}, indent=2)return ResourceResult(- contents=[ResourceContent(content=content, mime_type=self.mime_type)]+ contents=[+ ResourceContent(+ content=content,+ mime_type=self.mime_type,+ meta=public_result_meta(self.meta),+ )+ ])except Exception as e:raise ResourceError(f"Error reading directory {self.path}") from ediff --git a/tests/resources/test_builtin_resource_meta.py b/tests/resources/test_builtin_resource_meta.pynew file mode 100644index 000000000..7248ec75a--- /dev/null+++ b/tests/resources/test_builtin_resource_meta.py@@ -0,0 +1,74 @@+"""Built-in resources carry public component metadata onto read contents."""++import base64+import copy+import json+from pathlib import Path++import pytest++from fastmcp import Client, FastMCP+from fastmcp.resources import DirectoryResource, FileResource, HttpResource+from tests.utilities.httpx2_mock import HTTPXMock++[email protected]("kind", ["file_text", "file_binary", "directory", "http"])[email protected]("metadata", ["public", "private_only", "absent"])+async def test_builtin_read_preserves_public_metadata(+ tmp_path: Path, httpx_mock: HTTPXMock, kind: str, metadata: str+) -> None:+ meta = None+ expected = None+ if metadata == "public":+ meta = {+ "ui": {"csp": {"connectDomains": ["https://example.invalid"]}},+ "fastmcp": {"custom": "keep", "_internal": {"visibility": "private"}},+ }+ expected = {+ "ui": {"csp": {"connectDomains": ["https://example.invalid"]}},+ "fastmcp": {"custom": "keep"},+ }+ elif metadata == "private_only":+ meta = {"fastmcp": {"_internal": {"visibility": "private"}}}+ original_meta = copy.deepcopy(meta)+ file = tmp_path / "page.html"+ file.write_text("<p>hello</p>", encoding="utf-8")+ uri = f"test://{kind}"+ if kind.startswith("file"):+ resource = FileResource(+ uri=uri,+ name=kind,+ path=file,+ mime_type="text/html",+ is_binary=kind == "file_binary",+ meta=meta,+ )+ elif kind == "directory":+ resource = DirectoryResource(uri=uri, name=kind, path=tmp_path, meta=meta)+ else:+ url = "https://example.com/page"+ httpx_mock.add_response(url=url, text="<p>hello</p>")+ resource = HttpResource(+ uri=uri, name=kind, url=url, mime_type="text/html", meta=meta+ )++ server = FastMCP("built-in resource metadata")+ server.add_resource(resource)+ async with Client(server) as client:+ listed = await client.list_resources()+ assert listed[0].meta == resource.get_meta()+ result = await client.read_resource_mcp(uri)++ assert len(result.contents) == 1+ content = result.contents[0]+ if kind == "directory":+ assert json.loads(content.text) == {"files": ["page.html"]}+ assert content.mime_type == "application/json"+ elif kind == "file_binary":+ assert base64.b64decode(content.blob) == b"<p>hello</p>"+ assert content.mime_type == "text/html"+ else:+ assert content.text == "<p>hello</p>"+ assert content.mime_type == "text/html"+ assert content.meta == expected+ assert resource.meta == original_meta