Skip to content

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

Fix #4056: keep blank query values, add token bucket regression test (#4069)

Mukunda Rao Katta20359de9533c
diff --git a/src/fastmcp/resources/template.py b/src/fastmcp/resources/template.py
index f4e8b0be0..0f6eb5e61 100644
--- a/src/fastmcp/resources/template.py
+++ b/src/fastmcp/resources/template.py
@@ -104,7 +104,9 @@ def match_uri_template(uri: str, uri_template: str) -> dict[str, str] | None:
# Extract query parameters if present in URI and template
if query_string:
query_param_names = extract_query_params(uri_template)
- parsed_query = parse_qs(query_string)
+ # keep_blank_values=True preserves empty values (e.g. ?format=)
+ # so callers can distinguish "explicitly empty" from "missing".
+ parsed_query = parse_qs(query_string, keep_blank_values=True)
for name in query_param_names:
if name in parsed_query:
diff --git a/tests/resources/test_resource_template.py b/tests/resources/test_resource_template.py
index 2aa81a97f..174b81bea 100644
--- a/tests/resources/test_resource_template.py
+++ b/tests/resources/test_resource_template.py
@@ -850,6 +850,25 @@ class TestMalformedURITemplates:
assert result is not None
assert result["user_id"] == "alice"
+ def test_query_param_with_blank_value_is_preserved(self):
+ """Regression for #4056: blank query values (e.g. ?format=) should
+ not be silently dropped. parse_qs without keep_blank_values=True
+ elides empty values, which loses caller intent."""
+ result = match_uri_template(
+ "resource://data/42?format=", "resource://data/{id}{?format}"
+ )
+ assert result is not None
+ assert result == {"id": "42", "format": ""}
+
+ 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(
+ "resource://data/42?format=&verbose=true",
+ "resource://data/{id}{?format,verbose}",
+ )
+ assert result is not None
+ assert result == {"id": "42", "format": "", "verbose": "true"}
+
def test_from_function_rejects_hyphen_underscore_collision(self):
"""Two raw param names that normalize to the same key are rejected."""
diff --git a/tests/server/middleware/test_rate_limiting.py b/tests/server/middleware/test_rate_limiting.py
index f7d23f7af..59fb074f1 100644
--- a/tests/server/middleware/test_rate_limiting.py
+++ b/tests/server/middleware/test_rate_limiting.py
@@ -74,6 +74,33 @@ class TestTokenBucketRateLimiter:
await asyncio.sleep(0.2)
assert await limiter.consume(2) is True
+ async def test_denied_consumes_do_not_freeze_clock(self):
+ """Regression for #4056: a client that retries quickly after being
+ denied must not be able to bypass the configured refill rate.
+
+ last_refill must advance on every consume() call (success or failure).
+ If it only advanced on success, the elapsed window would be re-counted
+ on each retry, letting a client refill faster than `refill_rate`.
+ """
+ limiter = TokenBucketRateLimiter(capacity=10, refill_rate=10.0)
+
+ # Drain the bucket.
+ assert await limiter.consume(10) is True
+
+ # Hammer with denied requests over ~0.2s. With the correct
+ # implementation, last_refill advances on each call, so total
+ # accumulated tokens after 0.2s is ~2 (10/s * 0.2s).
+ for _ in range(20):
+ await limiter.consume(1)
+ await asyncio.sleep(0.01)
+
+ # We should NOT be able to consume more than the configured rate
+ # would allow over the elapsed window. Allow a small slack for
+ # timing jitter, but stay well below `capacity`.
+ assert await limiter.consume(5) is False, (
+ "denied retries should not silently accrue extra tokens"
+ )
+
class TestSlidingWindowRateLimiter:
"""Test sliding window rate limiter."""