Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

resources: keep every compiled template pattern cached (#5248)

nate nowack2af8a6874912
diff --git a/fastmcp_slim/fastmcp/resources/template.py b/fastmcp_slim/fastmcp/resources/template.py
index 7265a7191..82da65b0b 100644
--- a/fastmcp_slim/fastmcp/resources/template.py
+++ b/fastmcp_slim/fastmcp/resources/template.py
@@ -156,7 +156,9 @@ def _literal_pattern(text: str) -> str:
return "".join(pattern)
[email protected]_cache(maxsize=4096)
+# Unbounded: keys are server-defined templates, and a read walks every template,
+# so a capped cache would evict on every read once a server outgrew it.
def build_regex(template: str) -> re.Pattern[str] | None:
"""Build regex pattern for URI template, handling RFC 6570 syntax.
diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.py
index f5a104550..d8b423501 100644
--- a/tests/resources/test_resource_template.py
+++ b/tests/resources/test_resource_template.py
@@ -781,6 +781,27 @@ class TestMalformedURITemplates:
assert match is not None
assert match.group("user_id") == "alice"
+ def test_pattern_cache_holds_more_templates_than_4096(self, monkeypatch):
+ """A read walks every template, so a pattern must still be cached after a
+ server with thousands of templates has matched against all of them."""
+ from fastmcp.resources import template as template_module
+
+ templates = [f"many-templates://{i}/{{x}}" for i in range(5000)]
+ for uri_template in templates:
+ match_uri_template("many-templates://none", uri_template)
+
+ builds = 0
+ literal_pattern = template_module._literal_pattern
+
+ def counting(text: str) -> str:
+ nonlocal builds
+ builds += 1
+ return literal_pattern(text)
+
+ monkeypatch.setattr(template_module, "_literal_pattern", counting)
+ assert match_uri_template("many-templates://0/a", templates[0]) == {"x": "a"}
+ assert builds == 0
+
def test_matching_reuses_the_compiled_template_pattern(self, monkeypatch):
"""Reads try every template in turn, so a template's pattern is built
once, not on every match attempt."""