modelcontextprotocol / modelcontextprotocol/python-sdk

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

Offen
#1,060 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug P2 ready for work
Vorherrschende Sprache
Python
Sterne
24.3k
Forks
4k
Ø Merge
1 T. 1 Std.
Gemergte PRs (30 T.)
31

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit tests/server/fastmcp/test_server.py, insbesondere mit test_tool_mixed_content und der Reproduktion mixed_content_tool_fn. Lies den aus mcp.server.fastmcp.utilities.types importierten Image-Typ und verfolge, wie seine Tupel-Rückgabeannotation unter mcp 1.10.1 verarbeitet wird. Die Aufgabe ist erledigt, wenn der Mixed-Content-Test ohne PydanticSchemaGenerationError besteht und dabei die erwarteten Assertions für Bild- und Audioinhalte erhalten bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
62/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.