Skip to content

Commit 5cbd259

Browse files
authored
fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat) (#2434)
1 parent 2dfb51a commit 5cbd259

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/mcp/server/mcpserver/utilities/func_metadata.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import anyio
1010
import anyio.to_thread
1111
import pydantic_core
12-
from pydantic import BaseModel, ConfigDict, Field, WithJsonSchema, create_model
12+
from pydantic import BaseModel, ConfigDict, Field, PydanticUserError, WithJsonSchema, create_model
1313
from pydantic.fields import FieldInfo
1414
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaWarningKind
1515
from typing_extensions import is_typeddict
@@ -402,9 +402,16 @@ def _try_create_model_and_schema(
402402
# Use StrictJsonSchema to raise exceptions instead of warnings
403403
try:
404404
schema = model.model_json_schema(schema_generator=StrictJsonSchema)
405-
except (TypeError, ValueError, pydantic_core.SchemaError, pydantic_core.ValidationError) as e:
405+
except (
406+
PydanticUserError,
407+
TypeError,
408+
ValueError,
409+
pydantic_core.SchemaError,
410+
pydantic_core.ValidationError,
411+
) as e:
406412
# These are expected errors when a type can't be converted to a Pydantic schema
407-
# TypeError: When Pydantic can't handle the type
413+
# PydanticUserError: When Pydantic can't handle the type (e.g. PydanticInvalidForJsonSchema);
414+
# subclasses TypeError on pydantic <2.13 and RuntimeError on pydantic >=2.13
408415
# ValueError: When there are issues with the type definition (including our custom warnings)
409416
# SchemaError: When Pydantic can't build a schema
410417
# ValidationError: When validation fails

0 commit comments

Comments
 (0)