Skip to content

fix(server): honor mime_type and meta of handler-returned resources - #3890

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

DZH166 wants to merge 1 commit into
modelcontextprotocol:mainfrom
DZH166:fix/resource-template-handler-metadata

Conversation

@DZH166

@DZH166 DZH166 commented Nov 21, 2026

Copy link
Copy Markdown

Problem

A resource-template handler may return a concrete Resource (BinaryResource, FileResource, ...) so each
matched URI picks its own content type and metadata. FunctionResource.read() already supports that shape
(if isinstance(result, Resource): return await result.read()), but ResourceTemplate.create_resource()
wraps the value in a FunctionResource built from the template's fields, so the returned resource's
mime_type and meta are dropped.

MCPServer.read_resource() derives the wire label from the wrapper, so a handler returning
BinaryResource(..., mime_type="image/png", meta={"source": "camera"}) is served as text/plain with
meta=None. Clients cannot decode the payload, and per-resource metadata never reaches them.

@mcp.resource("bin://{name}")
def get_bin(name: str) -> BinaryResource:
    return BinaryResource(uri=f"bin://{name}", name=name, data=png, mime_type="image/png", meta={"source": "camera"})

[item] = await mcp.read_resource("bin://cat.png")
item.mime_type  # 'text/plain', expected 'image/png'
item.meta       # None, expected {'source': 'camera'}

Reported as #3874.

Fix

create_resource() now takes mime_type and meta from a handler-returned Resource when that resource
declares them, and keeps the template's declarations otherwise:

  • mime_type is used when it is in the returned resource's model_fields_set, i.e. the handler set it
    explicitly. A Resource always carries the model default text/plain, so field presence — not truthiness —
    is what distinguishes "declared" from "unset"; a template that declares mime_type="text/markdown" is not
    clobbered by a handler returning a defaulted TextResource.
  • meta is used when it is not None, so an unset meta still inherits the template's.

Non-Resource returns and every other wrapper field are unchanged. Resource is imported from its defining
module (resources/base.py) rather than through the implicit re-export in resources/types.py, which pyright
rejects now that the name is used at runtime; resource_manager.py already imports it that way.

Tests

New: tests/server/mcpserver/resources/test_resource_template.py::test_handler_returned_resource_supplies_its_content_metadata
covers both directions through the public API — a handler-returned BinaryResource that declares
mime_type/meta, and a handler-returned TextResource that leaves them unset so the template's
mime_type="text/markdown" still applies.

  • pytest tests/server/mcpserver/resources/ → 98 passed, 1 skipped
  • pytest tests/server/mcpserver/ tests/server/test_read_resource.py → 652 passed, 1 skipped
  • pyright, ruff check, ruff format --check clean on both changed files

@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 21, 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 this PR doesn't link an open issue yet.

  • If you're already assigned to an issue for this, add Fixes #<n> to the description and the PR will reopen on its own.
  • If there's no issue yet, please open one instead: what you ran into, why it matters for your use case, and a minimal reproduction. That context is super important to us and is what we use to decide what to prioritise.
  • If there's an issue but you're not assigned, add Fixes #<n> anyway so they're linked, then engage on the issue itself by confirming the repro or describing the approach you'd take. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. If you are assigned, this PR reopens automatically.

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 21, 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.

1 participant