Skip to content

fix(resources): keep a template-returned resource's mime_type and meta - #3875

Closed
lester076 wants to merge 1 commit into
modelcontextprotocol:mainfrom
lester076:fix/template-returned-resource-metadata
Closed

lester076 wants to merge 1 commit into
modelcontextprotocol:mainfrom
lester076:fix/template-returned-resource-metadata

Conversation

@lester076

Copy link
Copy Markdown

Fixes #3874

Problem

A resource template function may return a concrete Resource (BinaryResource, FileResource, TextResource) to describe the matched URI's content. ResourceTemplate.create_resource() wrapped it in a FunctionResource that hardcoded the template's mime_type (default text/plain) and meta (default None), and MCPServer.read_resource() reports the wrapper's fields — so a PNG returned from a template crossed the wire as BlobResourceContents with mime_type="text/plain" and meta=None, losing every per-URI type and metadata. FunctionResource.read() already forwards Resource results, so only the metadata was dropped.

What changed

When the template function returns a Resource, its fields now win over the template defaults (as proposed in the issue): name/title/description/mime_type/icons/annotations/meta come from the returned resource, falling back to the template's values. The wrapper's read delegates to the returned resource. model_fields_set decides which fields are the returned resource's own, so a value left at the Resource default (e.g. mime_type="text/plain" on a bare TextResource) does not silently override a template's declared type — the template's mime_type still applies in that case.

Non-Resource returns are untouched.

Verification

New TestTemplateReturnedResourceWins in tests/server/mcpserver/resources/test_resource_template.py:

  • a BinaryResource with mime_type="image/png" and meta keeps both through create_resource, and read() returns the bytes (fails on main: text/plain / None);
  • the issue's end-to-end shape: MCPServer.read_resource("bin://cat.png") now reports image/png and the meta (fails on main);
  • a control: a bare TextResource (only uri/text set) on a template declaring text/markdown still serves text/markdown and the template meta (passes on main — the fallback contract).

test_resource_template.py: 37 passed. ruff check/ruff format clean.

A resource template function may return a concrete Resource (e.g.
BinaryResource) to describe the matched URI's content. create_resource()
wrapped it in a FunctionResource that hardcoded the template's mime_type
and meta, and read_resource() reports the wrapper's fields — so a PNG
returned by a template crossed the wire as text/plain with meta=None,
and every per-URI mime type and metadata was lost (modelcontextprotocol#3874).

When the function returns a Resource, its explicitly set fields now win
over the template defaults (model_fields_set decides, so a value left at
the Resource default does not override a template's declared mime_type);
the wrapper's read delegates to the returned resource.
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3874.

If a maintainer assigns you to #3874, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take.

You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way.

CONTRIBUTING.md has the full reasoning, but in short:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant