modelcontextprotocol / modelcontextprotocol/python-sdk

PydanticSchemaGenerationError: Unable to generate pydantic-core schema for Image type

未關閉
#1,060 4 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

bug P2 ready for work
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
31

描述

Initial Checks
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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 tests/server/fastmcp/test_server.py 開始,特別關注 test_tool_mixed_content 和 mixed_content_tool_fn reproduction。閱讀從 mcp.server.fastmcp.utilities.types 匯入的 Image 型別,並追蹤其 tuple 回傳註解在 mcp 1.10.1 下的處理方式。完成的標準是混合內容測試在不出現 PydanticSchemaGenerationError 的情況下通過,同時保留預期的影像和音訊內容斷言。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
冷清
描述清晰度
基本清楚
新手友好度
62/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。