π The fast, Pythonic way to build MCP servers and clients.
fix(resources): separate URI fragments from query values (#5132)
Harsh Kashyap0921c969ded2
diff --git a/docs/servers/resources.mdx b/docs/servers/resources.mdxindex fa0bd3a5c..06e046dce 100644--- a/docs/servers/resources.mdx+++ b/docs/servers/resources.mdx@@ -700,6 +700,8 @@ def search(category: str, tags: list[str] | None = None) -> dict:A single value still arrives as a one-element list, so the parameter's type is stable. Declaring a parameter that can *only* be a list without the `*` raises an error when the template is created. A parameter that also accepts a scalar (`str | list[str] | None`) is left alone β it still works as a plain `{?param}`.+A URI fragment (`#section`) is not part of the resource identity. FastMCP drops it before matching, so `data://42?format=json#top` yields `id="42"` and `format="json"`. A template that contains a literal `#` is matched against the full URI, fragment included.+**Query parameters vs. hidden defaults:**Query parameters expose optional configuration to clients. To hide optional parameters from clients entirely (always use defaults), simply omit them from the URI template:diff --git a/fastmcp_slim/fastmcp/resources/template.py b/fastmcp_slim/fastmcp/resources/template.pyindex f72d31d4a..a779ffc8e 100644--- a/fastmcp_slim/fastmcp/resources/template.py+++ b/fastmcp_slim/fastmcp/resources/template.py@@ -214,7 +214,11 @@ def match_uri_template(`regex` is the template's compiled path pattern when the caller alreadyholds it; otherwise it is built from `uri_template` through a bounded cache."""- # Split URI into path and query parts+ # A fragment is not part of the resource identity, so drop it before+ # splitting off the query. A template that spells out a literal `#` is+ # matched against the full URI.+ if "#" not in uri_template:+ uri = uri.partition("#")[0]uri_path, _, query_string = uri.partition("?")# Match path parametersdiff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.pyindex 6fca11cf0..e93beb1a9 100644--- a/tests/resources/test_resource_template.py+++ b/tests/resources/test_resource_template.py@@ -1010,6 +1010,48 @@ class TestMalformedURITemplates:assert result is not Noneassert result == {"id": "42", "format": ""}+ def test_fragment_does_not_prevent_match(self):+ result = match_uri_template(+ "test://items/42#section",+ "test://items/{id}",+ )+ assert result == {"id": "42"}++ def test_query_fragment_is_not_part_of_param(self):+ result = match_uri_template(+ "test://items?filter=active#section",+ "test://items{?filter}",+ )+ assert result == {"filter": "active"}++ def test_fragment_after_path_param_and_query(self):+ result = match_uri_template(+ "test://items/42?format=json#section",+ "test://items/{id}{?format}",+ )+ assert result == {"id": "42", "format": "json"}++ def test_literal_fragment_in_template_still_matches(self):+ result = match_uri_template(+ "test://items/42#details",+ "test://items/{id}#details",+ )+ assert result == {"id": "42"}++ def test_literal_fragment_in_template_rejects_other_fragment(self):+ result = match_uri_template(+ "test://items/42#other",+ "test://items/{id}#details",+ )+ assert result is None++ def test_encoded_hash_stays_in_path_param(self):+ result = match_uri_template(+ "test://items/a%23b",+ "test://items/{id}",+ )+ assert result == {"id": "a#b"}+def test_query_param_with_blank_and_present_values(self):"""Mix of blank and non-blank query values are both surfaced."""result = match_uri_template(