Files
modelcontextprotocol--pytho…/tests/issues/test_973_url_decoding.py
T
wehub-resource-sync 49b9bb6724
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:10:27 +08:00

79 lines
2.2 KiB
Python

"""Test that URL-encoded parameters are decoded in resource templates.
Regression test for https://github.com/modelcontextprotocol/python-sdk/issues/973
"""
from mcp.server.mcpserver.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"