Repository navigation
Conversation
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.
|
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:
Maintainers: reopen, remove |
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 aFunctionResourcethat hardcoded the template'smime_type(defaulttext/plain) andmeta(defaultNone), andMCPServer.read_resource()reports the wrapper's fields — so a PNG returned from a template crossed the wire asBlobResourceContentswithmime_type="text/plain"andmeta=None, losing every per-URI type and metadata.FunctionResource.read()already forwardsResourceresults, 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/metacome from the returned resource, falling back to the template's values. The wrapper'sreaddelegates to the returned resource.model_fields_setdecides which fields are the returned resource's own, so a value left at theResourcedefault (e.g.mime_type="text/plain"on a bareTextResource) does not silently override a template's declared type — the template'smime_typestill applies in that case.Non-
Resourcereturns are untouched.Verification
New
TestTemplateReturnedResourceWinsintests/server/mcpserver/resources/test_resource_template.py:BinaryResourcewithmime_type="image/png"andmetakeeps both throughcreate_resource, andread()returns the bytes (fails on main:text/plain/None);MCPServer.read_resource("bin://cat.png")now reportsimage/pngand the meta (fails on main);TextResource(onlyuri/textset) on a template declaringtext/markdownstill servestext/markdownand the template meta (passes on main — the fallback contract).test_resource_template.py: 37 passed.ruff check/ruff formatclean.