Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Propagate component meta to list[ResourceContent] reads (#5843)

ε“ˆεŸΊη±³f01957f7e048
diff --git a/fastmcp_slim/fastmcp/resources/base.py b/fastmcp_slim/fastmcp/resources/base.py
index db4a78294..7dbf49328 100644
--- a/fastmcp_slim/fastmcp/resources/base.py
+++ b/fastmcp_slim/fastmcp/resources/base.py
@@ -255,7 +255,8 @@ def convert_raw_to_resource_result(
raw_value: The value returned by the user's function.
mime_type: The component's declared MIME type, forwarded to content items.
meta: Component-level meta (e.g. `ui` metadata for MCP Apps CSP/permissions)
- propagated to each content item.
+ propagated to each content item. For `list[ResourceContent]` returns
+ it is merged into every item, with item-level keys taking precedence.
"""
if isinstance(raw_value, ResourceResult):
return raw_value
@@ -276,18 +277,33 @@ def convert_raw_to_resource_result(
[ResourceContent(raw_value, mime_type=mime_type, meta=meta)]
)
+ # A non-empty list of ResourceContent carries multiple content items.
+ # Attach the component's public meta to each item, with item-level keys
+ # taking precedence over component keys where an item sets its own value
+ # (item meta could otherwise be lost or shadow the component's). Items are
+ # copied so the caller's ResourceContent objects are not mutated.
+ # ResourceResult(raw_value) still validates the whole list, preserving the
+ # TypeError for non-ResourceContent members.
+ if (
+ isinstance(raw_value, list)
+ and raw_value
+ and isinstance(raw_value[0], ResourceContent)
+ ):
+ result = ResourceResult(raw_value)
+ if meta is not None:
+ result.contents = [
+ item.model_copy(update={"meta": {**meta, **(item.meta or {})}})
+ for item in result.contents
+ ]
+ return result
+
# For JSON-native types (dict, list, tuple, int, float, bool, None),
# serialize and wrap in ResourceContent with the component's meta,
# matching the str/bytes path above so CSP/permissions propagate.
- # Exclude list[ResourceContent] which should go through ResourceResult
- # normalization below.
+ # An empty list is JSON-native and serializes to "[]" as before.
if (
isinstance(raw_value, dict | list | tuple | int | float | bool)
or raw_value is None
- ) and not (
- isinstance(raw_value, list)
- and raw_value
- and isinstance(raw_value[0], ResourceContent)
):
return ResourceResult(
[
diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.py
index 347165a87..6f1cf90c3 100644
--- a/tests/resources/test_resource_template.py
+++ b/tests/resources/test_resource_template.py
@@ -9,7 +9,7 @@ from mcp.types import TextResourceContents
from pydantic import BaseModel, Field
from fastmcp import Client, Context, FastMCP
-from fastmcp.resources import ResourceTemplate
+from fastmcp.resources import ResourceContent, ResourceTemplate
from fastmcp.resources.function_resource import FunctionResource
from fastmcp.resources.template import (
build_regex,
@@ -1771,3 +1771,23 @@ class TestNonExplodedListQueryParams:
[contents] = await client.read_resource(uri)
assert isinstance(contents, TextResourceContents)
assert contents.text == expected
+
+
+class TestTemplateComponentMetaPropagation:
+ """A template's component meta must reach list[ResourceContent] reads."""
+
+ async def test_list_resource_content_return_preserves_template_meta(self):
+ mcp = FastMCP()
+ component_meta = {"ui": {"csp": "default-src none"}, "team": "infra"}
+
+ @mcp.resource("data://multi/{id}", meta=component_meta)
+ def multi(id: str) -> list[ResourceContent]:
+ return [ResourceContent("a"), ResourceContent("b", meta={"own": 1})]
+
+ async with Client(mcp) as client:
+ result = await client.read_resource_mcp("data://multi/1")
+
+ assert [content.meta for content in result.contents] == [
+ component_meta,
+ {**component_meta, "own": 1},
+ ]
diff --git a/tests/resources/test_resources.py b/tests/resources/test_resources.py
index bd5af842c..fbc3b88dd 100644
--- a/tests/resources/test_resources.py
+++ b/tests/resources/test_resources.py
@@ -371,3 +371,72 @@ class TestResourceMetaPropagation:
result = await client.read_resource_mcp("test://json-meta")
assert len(result.contents) == 1
assert result.contents[0].meta == {"csp": "default-src 'none'"}
+
+ async def test_list_resource_content_return_preserves_component_meta(self):
+ """A list[ResourceContent] return must carry component meta on every item."""
+ mcp = FastMCP()
+ component_meta = {"ui": {"csp": "default-src none"}, "team": "infra"}
+
+ @mcp.resource("test://list-meta", meta=component_meta)
+ def list_resource() -> list[ResourceContent]:
+ return [ResourceContent("a"), ResourceContent("b")]
+
+ async with Client(mcp) as client:
+ result = await client.read_resource_mcp("test://list-meta")
+
+ assert len(result.contents) == 2
+ assert [content.meta for content in result.contents] == [
+ component_meta,
+ component_meta,
+ ]
+
+ async def test_list_resource_content_item_meta_overrides_component_meta(self):
+ """Item-level keys win over component keys; other component keys merge in."""
+ mcp = FastMCP()
+ component_meta = {"ui": {"csp": "default-src none"}, "team": "infra"}
+
+ @mcp.resource("test://list-meta-override", meta=component_meta)
+ def list_resource() -> list[ResourceContent]:
+ return [
+ ResourceContent("a", meta={"team": "app", "own": 1}),
+ ResourceContent("b"),
+ ]
+
+ async with Client(mcp) as client:
+ result = await client.read_resource_mcp("test://list-meta-override")
+
+ assert [content.meta for content in result.contents] == [
+ {"ui": {"csp": "default-src none"}, "team": "app", "own": 1},
+ {"ui": {"csp": "default-src none"}, "team": "infra"},
+ ]
+
+ async def test_str_return_still_preserves_component_meta(self):
+ """Control: the str path keeps attaching component meta."""
+ mcp = FastMCP()
+
+ @mcp.resource("test://str-meta", meta={"csp": "default-src 'none'"})
+ def str_resource() -> str:
+ return "hello"
+
+ async with Client(mcp) as client:
+ result = await client.read_resource_mcp("test://str-meta")
+
+ assert len(result.contents) == 1
+ assert result.contents[0].text == "hello"
+ assert result.contents[0].meta == {"csp": "default-src 'none'"}
+
+ async def test_empty_list_return_is_unchanged(self):
+ """Control: an empty list still serializes to a single JSON item."""
+ mcp = FastMCP()
+
+ @mcp.resource("test://empty-list", meta={"csp": "default-src 'none'"})
+ def empty_list() -> list[ResourceContent]:
+ return []
+
+ async with Client(mcp) as client:
+ result = await client.read_resource_mcp("test://empty-list")
+
+ assert len(result.contents) == 1
+ assert result.contents[0].text == "[]"
+ assert result.contents[0].mime_type == "text/plain"
+ assert result.contents[0].meta == {"csp": "default-src 'none'"}