Skip to content

Commit f7366a6

Browse files
committed
Make ExternalSchemaRefError a hard error, not a ValueError
Subclassing Exception (not ValueError) keeps it from being swallowed by the schema-generation fallback that degrades unserializable types, so the explicit re-raise is no longer needed. Drop em-dashes from the docstrings.
1 parent 9b8d0aa commit f7366a6

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@
3030
logger = get_logger(__name__)
3131

3232

33-
class ExternalSchemaRefError(ValueError):
34-
"""A tool schema contains a `$ref` that is not a same-document reference."""
33+
class ExternalSchemaRefError(Exception):
34+
"""A tool schema contains a `$ref` that is not a same-document reference.
35+
36+
Deliberately not a `ValueError`: schema generation treats a `ValueError` as
37+
an unserializable type and degrades gracefully, but an external `$ref` is a
38+
hard error that must surface at tool registration.
39+
"""
3540

3641

3742
class StrictJsonSchema(GenerateJsonSchema):
3843
"""Render tool schemas, raising on pydantic warnings and external `$ref`s.
3944
4045
Warnings (e.g. a non-serializable type) become errors so they surface at tool
4146
registration instead of silently producing a degenerate schema. External
42-
`$ref`s -- which pydantic never emits itself, but a user can inject via
43-
`Field(json_schema_extra=...)` -- are an SSRF / fetch-DoS vector and are
47+
`$ref`s, which pydantic never emits itself but a user can inject via
48+
`Field(json_schema_extra=...)`, are an SSRF / fetch-DoS vector and are
4449
rejected for the same reason (SEP-2106).
4550
4651
See: https://modelcontextprotocol.io/seps/2106-json-schema-2020-12#security-implications
@@ -433,9 +438,6 @@ def _try_create_model_and_schema(
433438
# Use StrictJsonSchema to raise exceptions instead of warnings
434439
try:
435440
schema = model.model_json_schema(schema_generator=StrictJsonSchema)
436-
except ExternalSchemaRefError:
437-
# SEP-2106: an external $ref is a hard error, not an unserializable type.
438-
raise
439441
except (
440442
PydanticUserError,
441443
TypeError,

0 commit comments

Comments
 (0)