modelcontextprotocol / modelcontextprotocol/python-sdk
PydanticSchemaGenerationError: Unable to generate pydantic-core schema for Image type
还没有人认领这个 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
The Image type in question is imported like this: from mcp.server.fastmcp.utilities.types import Image
We are upgrading our dependency on mcp from 1.9.4 to 1.10.1.
Once on 1.10.1 our application and tests began to fail with error like this:
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema
for <class 'mcp.server.fastmcp.utilities.types.Image'>. Set `arbitrary_types_allowed=True` in
the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your
type to fully support it.
I found a test tool function among this repo's unit tests, that is pretty close to the mixed content
use case in our server. So I tweaked it to create a repro case for you:
diff --git a/tests/server/fastmcp/test_server.py b/tests/server/fastmcp/test_server.py
index c30930f..20c25af 100644
--- a/tests/server/fastmcp/test_server.py
+++ b/tests/server/fastmcp/test_server.py
@@ -194,12 +194,12 @@ def image_tool_fn(path: str) -> Image:
return Image(path)
-def mixed_content_tool_fn() -> list[ContentBlock]:
- return [
- TextContent(type="text", text="Hello"),
- ImageContent(type="image", data="abc", mimeType="image/png"),
+def mixed_content_tool_fn() -> tuple[str, Image, AudioContent]:
+ return (
+ "Hello",
+ Image(data=b"abc", format="png"),
AudioContent(type="audio", data="def", mimeType="audio/wav"),
- ]
+ )
class TestServerTools:
@@ -311,7 +311,7 @@ class TestServerTools:
assert content1.text == "Hello"
assert isinstance(content2, ImageContent)
assert content2.mimeType == "image/png"
- assert content2.data == "abc"
+ assert content2.data == "YWJj"
assert isinstance(content3, AudioContent)
assert content3.mimeType == "audio/wav"
assert content3.data == "def"
test_tool_mixed_content fails with PydanticSchemaGenerationError on 1.10.1 but succeeds on 1.9.4.
Python & MCP Python SDK
Python 3.12, mcp 1.10.1
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 tests/server/fastmcp/test_server.py 开始,尤其关注 test_tool_mixed_content 和 mixed_content_tool_fn reproduction。阅读从 mcp.server.fastmcp.utilities.types 导入的 Image 类型,并跟踪其元组返回注解在 mcp 1.10.1 下的处理方式。完成的标准是混合内容测试在不出现 PydanticSchemaGenerationError 的情况下通过,同时保留预期的图像和音频内容断言。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 62/100