modelcontextprotocol / modelcontextprotocol/python-sdk

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

オープン
#1,060 コメント 4 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug P2 ready for work
主要言語
Python
スター
24.3k
フォーク
4k
平均マージ
1日 1時間
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

tests/server/fastmcp/test_server.py から始め、特に test_tool_mixed_content と mixed_content_tool_fn の再現を確認してください。mcp.server.fastmcp.utilities.types からインポートされている Image 型を読み、mcp 1.10.1 でそのタプルの戻り値アノテーションがどのように処理されるかを追跡してください。mixed-content テストが PydanticSchemaGenerationError なしで通過し、期待される画像および音声コンテンツのアサーションが維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
62/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。