Repository navigation
Conversation
UriTemplate.match() misparsed a ';' (ifemp) capture whose non-empty
value collided with the following literal that begins with '='. In
_scan_prefix, uri.startswith(nxt.text, pos) was checked before
uri[pos] == '=', so a value equal to the stem of that literal (e.g.
{;a}=end{+rest} with a="end" -> ";a=end=end/x") was read as empty and
the duplicate "=end" leaked into the greedy capture.
Prefer the value form: when a '=' follows the name, locate the
following literal after a value within the operator's stop-char window;
only fall back to the empty-value (startswith) check when no boundary is
found. The suffix / no-greedy path was already correct and is unchanged.
Adds test_match_prefix_ifemp_value_collides_with_eq_literal covering the
collision plus boundary cases: empty value before an '=' literal, a
non-colliding value, name-continuation rejection, and the no-greedy
round-trip.
Closes modelcontextprotocol#3852
|
This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3852. If a maintainer assigns you to #3852, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take. You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way. CONTRIBUTING.md has the full reasoning, but in short:
Maintainers: reopen, remove |
Problem
UriTemplate.match()fails to round-tripmatch(expand(variables)) == variableswhen a{;var}path-style parameter (RFC 6570 §3.2.7ifemp) is immediately followed by a template literal that begins with=, and the variable's value collides with that literal. For example,UriTemplate.parse("{;a}=end{+rest}").expand({"a": "end", "rest": "/x"})yields";a=end=end/x", but.match()returns{'a': '', 'rest': '=end/x'}instead of{'a': 'end', 'rest': '/x'}. The valueais silently read as empty and the duplicated=endleaks into the greedy{+rest}capture, corrupting resource URI routing that relies onmatch(). A second shape,/api{;mode}=fast{+rest}withmode="fast", fails the same way. Reproduced onmainat77001c96; see #3852.Root cause
In
src/mcp/shared/uri_template.py,_scan_prefixdecides between the two;ifemp forms (;name=valuevs bare;namefor empty) by checkinguri.startswith(nxt.text, pos)beforeuri[pos] == "=". That ordering correctly detects the empty form when the following literal starts with=(e.g.;a=end/xwitha=""), but it also fires when the non-empty value's text starts with that literal: atposright after;a,uri[pos:]is=end=end/x, which also starts withnxt.text(=end), so the scan prematurely recordsa = ""andcontinues, never running the value scan that would locate the real boundary. The suffix / no-greedy scan (_scan_suffix) uses a different, correct algorithm and is unaffected, which is why{;a}=end(no greedy variable) already round-trips.Fix & verification
In
_scan_prefix, prefer the value form: whenuri[pos] == "=", consume the=and locate the following literal (nxt.text) after a value within the operator's stop-char window — the same scan already used for non-ifempcaptures. Only if that lookup fails (the=belongs to the following literal, or there is no=) fall back to thestartswith(nxt.text)empty-value check, returningNonewhen neither matches. This is scoped to the prefix scan;_scan_suffixis untouched. Addedtest_match_prefix_ifemp_value_collides_with_eq_literalcovering the collision and boundary conditions: an empty value before an=-literal still reads empty, a non-colliding value is unchanged, name-continuation (;aZ…) is still rejected, a=with no following literal is a non-match, and the no-greedy{;a}=endround-trips both forms. Before the fix the new test is RED ({'a': '', 'rest': '=end/x'}); after it is GREEN, the fulltests/shared/test_uri_template.pymodule (308 tests) and the resource-template consumer tests (60 tests) pass with no regressions, andruff format --check/ruff checkare clean. Before/after pytest logs are in the portfolio mirror referenced below.Closes #3852