Repository navigation
Conversation
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.
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A resource-template handler may return a concrete
Resource(BinaryResource,FileResource, ...) so eachmatched URI picks its own content type and metadata.
FunctionResource.read()already supports that shape(
if isinstance(result, Resource): return await result.read()), butResourceTemplate.create_resource()wraps the value in a
FunctionResourcebuilt from the template's fields, so the returned resource'smime_typeandmetaare dropped.MCPServer.read_resource()derives the wire label from the wrapper, so a handler returningBinaryResource(..., mime_type="image/png", meta={"source": "camera"})is served astext/plainwithmeta=None. Clients cannot decode the payload, and per-resource metadata never reaches them.Reported as #3874.
Fix
create_resource()now takesmime_typeandmetafrom a handler-returnedResourcewhen that resourcedeclares them, and keeps the template's declarations otherwise:
mime_typeis used when it is in the returned resource'smodel_fields_set, i.e. the handler set itexplicitly. A
Resourcealways carries the model defaulttext/plain, so field presence — not truthiness —is what distinguishes "declared" from "unset"; a template that declares
mime_type="text/markdown"is notclobbered by a handler returning a defaulted
TextResource.metais used when it is notNone, so an unsetmetastill inherits the template's.Non-
Resourcereturns and every other wrapper field are unchanged.Resourceis imported from its definingmodule (
resources/base.py) rather than through the implicit re-export inresources/types.py, which pyrightrejects now that the name is used at runtime;
resource_manager.pyalready imports it that way.Tests
New:
tests/server/mcpserver/resources/test_resource_template.py::test_handler_returned_resource_supplies_its_content_metadatacovers both directions through the public API — a handler-returned
BinaryResourcethat declaresmime_type/meta, and a handler-returnedTextResourcethat leaves them unset so the template'smime_type="text/markdown"still applies.pytest tests/server/mcpserver/resources/→ 98 passed, 1 skippedpytest tests/server/mcpserver/ tests/server/test_read_resource.py→ 652 passed, 1 skippedpyright,ruff check,ruff format --checkclean on both changed files