fix: URL-decode parameters extracted from resource templates (#1864)

This commit is contained in:
Felix Weinberger 2026-01-16 13:47:21 +00:00 • committed by GitHub
commit 0622babbd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,78 @@
"""Test that URL-encoded parameters are decoded in resource templates.
Regression test for https://github.com/modelcontextprotocol/python-sdk/issues/973
"""
from mcp.server.fastmcp.resources import ResourceTemplate
def test_template_matches_decodes_space():
"""Test that %20 is decoded to space."""
def search(query: str) -> str: # pragma: no cover
return f"Results for: {query}"
template = ResourceTemplate.from_function(
fn=search,
uri_template="search://{query}",
name="search",
)
params = template.matches("search://hello%20world")
assert params is not None
assert params["query"] == "hello world"
def test_template_matches_decodes_accented_characters():
"""Test that %C3%A9 is decoded to e with accent."""
def search(query: str) -> str: # pragma: no cover
return f"Results for: {query}"
template = ResourceTemplate.from_function(
fn=search,
uri_template="search://{query}",
name="search",
)
params = template.matches("search://caf%C3%A9")
assert params is not None
assert params["query"] == "café"
def test_template_matches_decodes_complex_phrase():
"""Test complex French phrase from the original issue."""
def search(query: str) -> str: # pragma: no cover
return f"Results for: {query}"
template = ResourceTemplate.from_function(
fn=search,
uri_template="search://{query}",
name="search",
)
params = template.matches("search://stick%20correcteur%20teint%C3%A9%20anti-imperfections")
assert params is not None
assert params["query"] == "stick correcteur teinté anti-imperfections"
def test_template_matches_preserves_plus_sign():
"""Test that plus sign remains as plus (not converted to space).
In URI encoding, %20 is space. Plus-as-space is only for
application/x-www-form-urlencoded (HTML forms).
"""
def search(query: str) -> str: # pragma: no cover
return f"Results for: {query}"
template = ResourceTemplate.from_function(
fn=search,
uri_template="search://{query}",
name="search",
)
params = template.matches("search://hello+world")
assert params is not None
assert params["query"] == "hello+world"