Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/mcp/server/mcpserver/resources/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ async def create_resource(
if isinstance(result, InputRequiredResult):
return result

if isinstance(result, Resource):
explicit = result.model_fields_set
return FunctionResource(
uri=uri, # type: ignore
name=result.name if "name" in explicit else self.name,
title=result.title if "title" in explicit else self.title,
description=result.description if "description" in explicit else self.description,
mime_type=result.mime_type if "mime_type" in explicit else self.mime_type,
icons=result.icons if "icons" in explicit else self.icons,
annotations=result.annotations if "annotations" in explicit else self.annotations,
meta=result.meta if "meta" in explicit else self.meta,
fn=result.read,
)

return FunctionResource(
uri=uri, # type: ignore
name=self.name,
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/server/mcpserver/resources/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BinaryResource(Resource):

async def read(self) -> bytes:
"""Read the binary content."""
return self.data # pragma: no cover
return self.data


class FunctionResource(Resource):
Expand Down
68 changes: 67 additions & 1 deletion tests/server/mcpserver/resources/test_resource_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from mcp.server.mcpserver import Context, MCPServer
from mcp.server.mcpserver.exceptions import ResourceError
from mcp.server.mcpserver.resources import FunctionResource, ResourceTemplate
from mcp.server.mcpserver.resources import BinaryResource, FunctionResource, ResourceTemplate, TextResource
from mcp.server.mcpserver.resources.templates import (
DEFAULT_RESOURCE_SECURITY,
ResourceSecurity,
Expand Down Expand Up @@ -460,6 +460,72 @@ def get_item(item_id: str) -> str:
assert resource.meta["category"] == "inventory"
assert resource.meta["cacheable"] is True

@pytest.mark.anyio
async def test_template_preserves_returned_resource_fields(self):
"""When a template handler returns a Resource instance, explicitly set fields on the
returned resource take precedence over the template defaults (#3874)."""
mcp = MCPServer("test")

@mcp.resource("bin://{name}")
def get_bin(name: str) -> BinaryResource:
return BinaryResource(
uri=f"bin://{name}",
name=name,
title="Binary Item",
description="Raw PNG bytes",
data=b"\x89PNG\r\n\x1a\n",
mime_type="image/png",
annotations=Annotations(priority=0.8),
meta={"source": "camera"},
)

template = mcp._resource_manager.list_templates()[0]
resource = await template.create_resource("bin://cat.png", {"name": "cat.png"}, Context())
assert isinstance(resource, FunctionResource)
assert resource.uri == "bin://cat.png"
assert resource.name == "cat.png"
assert resource.title == "Binary Item"
assert resource.description == "Raw PNG bytes"
assert resource.mime_type == "image/png"
assert resource.annotations == Annotations(priority=0.8)
assert resource.meta == {"source": "camera"}

contents = await mcp.read_resource("bin://cat.png")
assert not isinstance(contents, InputRequiredResult)
items = list(contents)
assert len(items) == 1
assert items[0].content == b"\x89PNG\r\n\x1a\n"
assert items[0].mime_type == "image/png"
assert items[0].meta == {"source": "camera"}

@pytest.mark.anyio
async def test_template_returned_resource_falls_back_to_template_defaults(self):
"""Fields not explicitly set on a returned Resource fall back to the template's metadata."""

def get_doc(slug: str) -> TextResource:
return TextResource(uri=f"doc://{slug}", text=f"# {slug}")

template = ResourceTemplate.from_function(
fn=get_doc,
uri_template="doc://{slug}",
name="doc_template",
title="Doc Title",
description="Doc Description",
mime_type="text/markdown",
annotations=Annotations(priority=0.4),
meta={"section": "guides"},
)

resource = await template.create_resource("doc://intro", {"slug": "intro"}, Context())
assert isinstance(resource, FunctionResource)
assert resource.name == "doc_template"
assert resource.title == "Doc Title"
assert resource.description == "Doc Description"
assert resource.mime_type == "text/markdown"
assert resource.annotations == Annotations(priority=0.4)
assert resource.meta == {"section": "guides"}
assert await resource.read() == "# intro"


@pytest.mark.anyio
async def test_sync_fn_runs_in_worker_thread():
Expand Down
Loading