|
11 | 11 | import pydantic_core |
12 | 12 | from pydantic import BaseModel, ConfigDict, Field, PydanticUserError, WithJsonSchema, create_model |
13 | 13 | from pydantic.fields import FieldInfo |
| 14 | +from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue, JsonSchemaWarningKind |
| 15 | +from pydantic_core import CoreSchema |
14 | 16 | from typing_extensions import is_typeddict |
15 | 17 | from typing_inspection.introspection import ( |
16 | 18 | UNKNOWN, |
|
21 | 23 | ) |
22 | 24 |
|
23 | 25 | from mcp.server.mcpserver.exceptions import InvalidSignature |
24 | | -from mcp.server.mcpserver.utilities._schema_generator import ExternalSchemaRefError, StrictJsonSchema |
25 | 26 | from mcp.server.mcpserver.utilities.logging import get_logger |
26 | 27 | from mcp.server.mcpserver.utilities.types import Audio, Image |
27 | 28 | from mcp.types import CallToolResult, ContentBlock, TextContent |
28 | 29 |
|
29 | 30 | logger = get_logger(__name__) |
30 | 31 |
|
31 | 32 |
|
| 33 | +class ExternalSchemaRefError(ValueError): |
| 34 | + """A tool schema contains a `$ref` that is not a same-document reference.""" |
| 35 | + |
| 36 | + |
| 37 | +class StrictJsonSchema(GenerateJsonSchema): |
| 38 | + """Render tool schemas, raising on pydantic warnings and external `$ref`s. |
| 39 | +
|
| 40 | + Warnings (e.g. a non-serializable type) become errors so they surface at tool |
| 41 | + 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 |
| 44 | + rejected for the same reason (SEP-2106). |
| 45 | +
|
| 46 | + See: https://modelcontextprotocol.io/seps/2106-json-schema-2020-12#security-implications |
| 47 | + """ |
| 48 | + |
| 49 | + def emit_warning(self, kind: JsonSchemaWarningKind, detail: str) -> None: |
| 50 | + raise ValueError(f"JSON schema warning: {kind} - {detail}") |
| 51 | + |
| 52 | + def generate(self, schema: CoreSchema, mode: Any = "validation") -> JsonSchemaValue: |
| 53 | + json_schema = super().generate(schema, mode) |
| 54 | + external = sorted(_find_external_refs(json_schema)) |
| 55 | + if external: |
| 56 | + raise ExternalSchemaRefError( |
| 57 | + f"Tool schema contains external $ref(s) that MUST NOT be dereferenced (SEP-2106): " |
| 58 | + f"{', '.join(external)}. Only same-document references (e.g. '#/$defs/Foo') are allowed." |
| 59 | + ) |
| 60 | + return json_schema |
| 61 | + |
| 62 | + |
| 63 | +def _find_external_refs(node: Any) -> set[str]: |
| 64 | + external: set[str] = set() |
| 65 | + if isinstance(node, dict): |
| 66 | + mapping = cast("dict[str, Any]", node) |
| 67 | + ref = mapping.get("$ref") |
| 68 | + if isinstance(ref, str) and not ref.startswith("#"): |
| 69 | + external.add(ref) |
| 70 | + for value in mapping.values(): |
| 71 | + external |= _find_external_refs(value) |
| 72 | + elif isinstance(node, list): |
| 73 | + for item in cast("list[Any]", node): |
| 74 | + external |= _find_external_refs(item) |
| 75 | + return external |
| 76 | + |
| 77 | + |
32 | 78 | class ArgModelBase(BaseModel): |
33 | 79 | """A model representing the arguments to a function.""" |
34 | 80 |
|
|
0 commit comments