Skip to content

fix: UriTemplate.match() misparses {;var} when following literal begins with '=' - #3937

Closed
LeonxLJX wants to merge 1 commit into
modelcontextprotocol:mainfrom
LeonxLJX:fix/uri-template-ifemp-eq-literal
Closed

LeonxLJX wants to merge 1 commit into
modelcontextprotocol:mainfrom
LeonxLJX:fix/uri-template-ifemp-eq-literal

Conversation

@LeonxLJX

Copy link
Copy Markdown

Problem

UriTemplate.match() fails to round-trip match(expand(variables)) == variables when a {;var} path-style parameter (RFC 6570 §3.2.7 ifemp) 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 value a is silently read as empty and the duplicated =end leaks into the greedy {+rest} capture, corrupting resource URI routing that relies on match(). A second shape, /api{;mode}=fast{+rest} with mode="fast", fails the same way. Reproduced on main at 77001c96; see #3852.

Root cause

In src/mcp/shared/uri_template.py, _scan_prefix decides between the two ; ifemp forms (;name=value vs bare ;name for empty) by checking uri.startswith(nxt.text, pos) before uri[pos] == "=". That ordering correctly detects the empty form when the following literal starts with = (e.g. ;a=end/x with a=""), but it also fires when the non-empty value's text starts with that literal: at pos right after ;a, uri[pos:] is =end=end/x, which also starts with nxt.text (=end), so the scan prematurely records a = "" and continues, 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: when uri[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-ifemp captures. Only if that lookup fails (the = belongs to the following literal, or there is no =) fall back to the startswith(nxt.text) empty-value check, returning None when neither matches. This is scoped to the prefix scan; _scan_suffix is untouched. Added test_match_prefix_ifemp_value_collides_with_eq_literal covering 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}=end round-trips both forms. Before the fix the new test is RED ({'a': '', 'rest': '=end/x'}); after it is GREEN, the full tests/shared/test_uri_template.py module (308 tests) and the resource-template consumer tests (60 tests) pass with no regressions, and ruff format --check / ruff check are clean. Before/after pytest logs are in the portfolio mirror referenced below.

Closes #3852

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
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

@github-actions github-actions Bot closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UriTemplate.match() misparses {;var} when following literal begins with '='

1 participant