fastmcp/tests/resources
nate nowack 80bc24ed33
resources: keep each template's compiled pattern on the template (#5253)
* resources: keep each template's compiled pattern on the template

#5248 made the shared build_regex cache unbounded on the premise that
its keys were server-defined. They are not: a proxy caches the template
strings its backend sends, so a backend whose templates change grew the
cache for the life of the process (25000 entries after 1000 list-and-read
cycles, ~1.5MB per read with long non-ASCII literals).

A ResourceTemplate now keeps its own compiled pattern, keyed on its
uri_template so a model_copy with a new template rebuilds it, and the
pattern is freed with the template. The shared cache is bounded again
and only serves direct match_uri_template callers. The pattern is read
from the private-attribute dict directly because pydantic's
private-attribute access costs ~0.3us on a path every read walks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KfHgVhbYEhBCC5eSeqGiuG

* resources: share a wrapped template's pattern and honor _list_query_params overrides

Review of the previous commit found that mounts wrap every template on
every list, so each wrapper compiled its pattern again; above 4096
templates the bounded shared cache thrashed on reads that fall back to
walking the list. A wrapper now takes the source template's compiled
pattern.

Reading _list_query_params straight from the private dict also bypassed
a model_copy update of it and a subclass property, so it is read as an
attribute again, only when the URI carries a query.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KfHgVhbYEhBCC5eSeqGiuG

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 09:30:09 -05:00
..
__init__.py restore 1.x code 2025-04-09 11:54:42 -04:00
test_file_resources.py Add encoding parameter to FileResource (#3580) 2026-03-22 09:55:16 -04:00
test_function_resources.py Migrate to MCP Python SDK v2 (#4437) 2026-07-06 17:36:45 -04:00
test_resource_security.py Make the unit suite fast: in-process HTTP tests, no real sleeps, parallel Windows CI (#4554) 2026-07-20 10:51:14 -04:00
test_resource_template.py resources: keep each template's compiled pattern on the template (#5253) 2026-09-24 09:30:09 -05:00
test_resource_template_meta.py Migrate to MCP Python SDK v2 (#4437) 2026-07-06 17:36:45 -04:00
test_resource_template_query_params.py Raise ValueError for invalid boolean query params in resource templates (#3424) (#3434) 2026-03-07 11:40:29 -05:00
test_resources.py Move to the stable MCP Python SDK 2.0.0 (#4655) 2026-07-28 16:31:49 -04:00
test_standalone_decorator.py Migrate to MCP Python SDK v2 (#4437) 2026-07-06 17:36:45 -04:00