Skip to content

ResourceTemplate.create_resource discards mime_type and meta when handler returns a Resource instance #3874

Open

Description

@shivamm-gupta

Release Line

2.x (main branch)

Problem Description

When registering dynamic resource templates via @mcp.resource("data://{name}"), handler functions can return concrete Resource subclasses (such as BinaryResource or FileResource) to provide dynamic MIME types (e.g. image/png, application/pdf) and metadata for each matched URI.

In src/mcp/server/mcpserver/resources/types.py:96-97, FunctionResource.read() explicitly anticipates and supports returning a Resource:

if isinstance(result, Resource):
    return await result.read()

However, in ResourceTemplate.create_resource() (src/mcp/server/mcpserver/resources/templates.py:234-244), the returned resource is unconditionally wrapped into a new FunctionResource that hardcodes mime_type=self.mime_type (which defaults to "text/plain") and meta=self.meta (which defaults to None):

return FunctionResource(
    uri=uri,
    name=self.name,
    title=self.title,
    description=self.description,
    mime_type=self.mime_type,  # defaults to "text/plain"
    icons=self.icons,
    annotations=self.annotations,
    meta=self.meta,            # defaults to None
    fn=lambda: result,
)

As a consequence:

  1. MCPServer.read_resource() reads resource.mime_type and resource.meta from the outer FunctionResource.
  2. Binary files returned from templates are served across the wire as BlobResourceContents with mime_type="text/plain" and meta=None.
  3. The client receives binary data erroneously identified as text/plain, and all custom resource metadata is lost.

Minimal Reproduction Code

import anyio
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.resources.types import BinaryResource

mcp = MCPServer("test")

@mcp.resource("bin://{name}")
def get_bin(name: str):
    return BinaryResource(
        uri=f"bin://{name}",
        name=name,
        data=b"\x89PNG\r\n\x1a\n",
        mime_type="image/png",
        meta={"source": "camera"},
    )

async def main():
    contents = await mcp.read_resource("bin://cat.png")
    item = contents[0]
    print(f"Content: {item.content}")
    print(f"MIME Type: {item.mime_type}")
    print(f"Meta: {item.meta}")
    assert item.mime_type == "image/png", f"Expected 'image/png', got {item.mime_type!r}"
    assert item.meta == {"source": "camera"}, f"Expected meta, got {item.meta!r}"

if __name__ == "__main__":
    anyio.run(main)

Actual Output

Content: b'\x89PNG\r\n\x1a\n'
MIME Type: text/plain
Meta: None
AssertionError: Expected 'image/png', got 'text/plain'

Expected Output

read_resource() should retain mime_type="image/png" and meta={"source": "camera"} from the returned BinaryResource.

Proposed Fix

In src/mcp/server/mcpserver/resources/templates.py:create_resource:
When result is an instance of Resource, inherit its properties:

if isinstance(result, Resource):
    return FunctionResource(
        uri=uri,
        name=result.name or self.name,
        title=result.title or self.title,
        description=result.description or self.description,
        mime_type=result.mime_type or self.mime_type,
        icons=result.icons or self.icons,
        annotations=result.annotations or self.annotations,
        meta=result.meta if result.meta is not None else self.meta,
        fn=result.read,
    )

I would be happy to submit a PR with test coverage if assigned.


Assisted by AI; reviewed and verified with reproduction script.

Activity

  1. added
    bugSomething isn't working
    v1Affects the v1.x maintenance line
    v2Affects the v2 line (2.x on main)
    on Nov 19, 2026
  2. lester076 commented on Nov 19, 2026

    @lester076

    Approach I validated while reproducing this (in case it helps triage): the wrapping in create_resource is the only lossy step — FunctionResource.read() already forwards a returned Resource's content, so mime_type/meta are dropped purely at the wrapper. A fix that preserves them needs one decision: whether a field the returned resource left at its Resource default (e.g. a bare TextResource's mime_type="text/plain") should override a template's declared mime_type. Using model_fields_set to let explicitly-set fields win and falling back to the template otherwise keeps both contracts:

    • BinaryResource(..., mime_type="image/png", meta={...}) from a bin://{name} template → read_resource reports image/png + meta (the repro's assertion);
    • a TextResource(uri=..., text=...) on a template declaring text/markdown still serves text/markdown.

    I ran the repo's own template suite against both shapes: the first fails on main exactly as the repro shows (text/plain / None), the second passes before and after. The branch is on my fork (fix/template-returned-resource-metadata) with the regression tests; happy to leave it there if the team prefers to implement the fix internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingv1Affects the v1.x maintenance linev2Affects the v2 line (2.x on main)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions