🚀 The fast, Pythonic way to build MCP servers and clients.
fix: decode URI fragments before schema reference hydration (#5561)
Hua.T4f27af8477b7
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema_type.py b/fastmcp_slim/fastmcp/utilities/json_schema_type.pyindex 0a80d2273..c8ff8f34c 100644--- a/fastmcp_slim/fastmcp/utilities/json_schema_type.py+++ b/fastmcp_slim/fastmcp/utilities/json_schema_type.py@@ -81,6 +81,7 @@ from typing import (get_type_hints,overload,)+from urllib.parse import unquotefrom uuid import UUIDfrom pydantic import (@@ -532,7 +533,8 @@ def _schema_digest(schema: Mapping[str, Any] | bool) -> tuple[str, int]:def _resolve_ref(ref: str, schemas: Mapping[str, Any]) -> Mapping[str, Any]:"""Resolve JSON Schema reference to target schema."""- path = ref.replace("#/", "").split("/")+ # Decode the URI fragment before splitting and unescaping JSON Pointer tokens.+ path = unquote(ref).removeprefix("#/").split("/")current = schemasfor part in path:part = part.replace("~1", "/").replace("~0", "~")diff --git a/tests/client/test_reference_hydration.py b/tests/client/test_reference_hydration.pynew file mode 100644index 000000000..934b387fe--- /dev/null+++ b/tests/client/test_reference_hydration.py@@ -0,0 +1,32 @@+"""Client hydration of JSON Schema URI-fragment references."""++import datetime++import pytest++from fastmcp import Client, FastMCP++[email protected]("dereference_schemas", [False, True])+async def test_client_hydrates_percent_encoded_reference(dereference_schemas: bool):+ server = FastMCP(dereference_schemas=dereference_schemas)++ @server.tool(+ output_schema={+ "type": "object",+ "properties": {"observed_at": {"$ref": "#/$defs/Observation%20time"}},+ "required": ["observed_at"],+ "$defs": {"Observation time": {"type": "string", "format": "date-time"}},+ }+ )+ def reading() -> dict:+ return {"observed_at": "2026-01-01T12:00:00Z"}++ async with Client(server) as client:+ result = await client.call_tool("reading")++ assert result.structured_content == {"observed_at": "2026-01-01T12:00:00Z"}+ assert result.data.observed_at == datetime.datetime(+ 2026, 1, 1, 12, tzinfo=datetime.UTC+ )+ assert type(result.data.observed_at) is datetime.datetimediff --git a/tests/utilities/json_schema_type/test_advanced.py b/tests/utilities/json_schema_type/test_advanced.pyindex d687c7baf..61b3e6c58 100644--- a/tests/utilities/json_schema_type/test_advanced.py+++ b/tests/utilities/json_schema_type/test_advanced.py@@ -227,7 +227,16 @@ class TestDefaultValues:@pytest.mark.parametrize("definition, token",- [("plain", "plain"), ("a/b", "a~1b"), ("a~b", "a~0b"), ("a~1b", "a~01b")],+ [+ ("plain", "plain"),+ ("a/b", "a~1b"),+ ("a~b", "a~0b"),+ ("a~1b", "a~01b"),+ ("a b", "a%20b"),+ ("a%20b", "a%2520b"),+ ("café", "caf%C3%A9"),+ ("a/b", "a%7E1b"),+ ],)def test_escaped_reference_tokens(definition: str, token: str):schema = {