modelcontextprotocol / modelcontextprotocol/python-sdk
Image/ImageContent serialization fails in stateless HTTP mode
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 24.3k
- 派生
- 4k
- 平均合并
- 1 天 1 小时
- 30 天内合并 PR
- 31
描述
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
Description
When using FastMCP with stateless_http=True (required by Claude.ai remote MCP), returning Image or ImageContent from a @mcp.tool() function results in serialization errors. Text-only tool results work fine in the same configuration.
Environment
mcpversion: 1.26.0 (also tested with>=1.8.0unpinned)- Python: 3.12
- Transport:
streamable-http - Deployment: Railway (remote, accessed by Claude.ai)
- Client: Claude.ai (requires
stateless_http=True)
Reproduction
Minimal server:
from mcp.server.fastmcp import FastMCP, Image
mcp = FastMCP(
"test",
host="0.0.0.0",
port=8000,
stateless_http=True,
json_response=True,
)
@mcp.tool()
def text_tool() -> str:
"""Works fine"""
return "hello"
@mcp.tool()
def image_tool() -> Image:
"""Fails with serialization error"""
# 1x1 red PNG
data = bytes.fromhex(
"89504e470d0a1a0a0000000d49484452000000010000000108020000009001"
"2e00000000c4948444154789c6260f8cf00000000020001e221bc330000000049454e44ae426082"
)
return Image(data=data, format="png")
if __name__ == "__main__":
mcp.run(transport="streamable-http")
What happens
Attempt 1: Image class + stateless_http=True + json_response=True
Unable to serialize unknown type: <class 'mcp.server.fastmcp.utilities.types.Image'>
Attempt 2: Image class + stateless_http=True + json_response=False
Same serialization error.
Attempt 3: ImageContent from mcp.types + stateless_http=True
{"error": "Error occurred during tool execution"}
Server returns 200 OK but client receives generic error.
Attempt 4: Image class + stateless_http=False (stateful mode)
POST /mcp HTTP/1.1" 400 Bad Request
Claude.ai rejects stateful mode entirely.
Expected behavior
Image and ImageContent should serialize correctly in stateless_http=True mode, since:
- The MCP spec defines
ImageContentas a valid tool result content type ToolResultContent.contentacceptsContentBlocklists which include image content- The official SDK docs show
@mcp.tool()returningImageas a supported pattern - The protocol's Streamable HTTP transport has no text-only restriction — it returns either
application/jsonortext/event-stream, both capable of carrying base64-encoded image data
Context
This blocks any MCP server deployed for Claude.ai from returning images via tool results. The only workaround is returning image URLs as text, which doesn't work for authenticated/signed URLs (e.g., Notion S3 hosted images that require download proxying).
Related
- Python SDK Image example: https://github.com/modelcontextprotocol/python-sdk#primitives
- MCP spec tool results: https://spec.modelcontextprotocol.io/specification/server/tools/
Example Code
Python & MCP Python SDK
MCP version: 1.26.0
Python: 3.12
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先使用无状态 HTTP 和 Image 结果开始最小化的 FastMCP 复现,然后跟踪 streamable-HTTP 传输的工具结果序列化路径。将其与正常工作的纯文本结果以及 ImageContent 尝试进行比较。当 Image 和 ImageContent 工具结果在无状态模式下能够携带 base64 图像数据成功返回时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- api, backend
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100