ResourceTemplate.create_resource discards mime_type and meta when handler returns a Resource instance
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 45/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
Read src/mcp/server/mcpserver/resources/templates.py around ResourceTemplate.create_resource() and compare it with FunctionResource.read() in src/mcp/server/mcpserver/resources/types.py. The reproduction in the issue shows the reported MIME type and metadata loss; use it to check the behavior before and after a change. Done means read_resource() preserves the returned resource's MIME type and metadata. A linked pull request is already closed and unmerged, so check its discussion for context.
由索引模型根據 Issue 內容生成。
描述
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:
MCPServer.read_resource()readsresource.mime_typeandresource.metafrom the outerFunctionResource.- Binary files returned from templates are served across the wire as
BlobResourceContentswithmime_type="text/plain"andmeta=None. - 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.
- 主要語言
- Python
- 星號
- 25k
- 分支
- 4.2k
- 平均合併
- 10 分鐘
- 30 天內合併 PR
- 1
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/python-sdk 的其他 Issue
-
v2
難度 1/5 1 小時以內 新手友好度 86/100
modelcontextprotocol/python-sdk#3978 · 2 則留言 ·
維護者通常 1 天內回覆
-
Client serves cached tools after its context exits with an exception可能已有人在做 @jonxsong 於 1 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 65/100
modelcontextprotocol/python-sdk#3968 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100
modelcontextprotocol/python-sdk#3957 ·
維護者通常 1 天內回覆
-
v2
難度 1/5 1 小時以內 新手友好度 91/100
modelcontextprotocol/python-sdk#3934 · 2 則留言 ·
維護者通常 1 天內回覆
-
bug v1
難度 2/5 1-3 小時 新手友好度 79/100
modelcontextprotocol/python-sdk#3929 · 2 則留言 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/python-sdk 的全部 Issue
相似的 Issue
-
auto-reported bug
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
-
backend bug
難度 2/5 1-3 小時 新手友好度 86/100
nolte/kamerplanter#3193 ·
維護者通常 1 天內回覆
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise可能已有人在做 @abdulwaarith0 今天認領。 未關閉needs-triage
難度 2/5 1-3 小時 新手友好度 84/100
MakazhanAlpamys/Soup#3690 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆