Skip to content

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

Open

Description

@xgbtxy

Initial Checks

Release line

2.x (current stable)

Description

UriTemplate.match() breaks match(expand(variables)) == variables when a {;var} path-style parameter expression (ifemp = True, RFC 6570 §3.2.7) is immediately followed by a literal that begins with "=" (for example "{;a}=end{+rest}" or "/api{;mode}=fast{/id}").

In src/mcp/shared/uri_template.py (_scan_prefix), the atom.ifemp check runs:

if uri.startswith(nxt.text, pos):
    result[var.name] = ""
    continue
if pos < limit and uri[pos] == "=":
    pos += 1

Checking uri.startswith(nxt.text, pos) unconditionally before uri[pos] == "=" handles the empty-value case (a == "" -> ";a=end/x"), except when a equals the stem of nxt.text (a == "end" -> expand() emits ";a=end=end/x"). At pos right after ";a", uri[pos:] is "=end=end/x", which also starts with nxt.text ("=end"). _scan_prefix prematurely treats a as "" and leaves the second "=end" to be absorbed by a downstream greedy variable {+rest} (or fails suffix matching when no greedy variable follows).

Example Code

from mcp.shared.uri_template import UriTemplate

tpl = UriTemplate.parse("{;a}=end{+rest}")
variables = {"a": "end", "rest": "/x"}

expanded = tpl.expand(variables)
matched = tpl.match(expanded)

print("expanded:", expanded)  # ';a=end=end/x'
print("matched :", matched)   # {'a': '', 'rest': '=end/x'}  (expected {'a': 'end', 'rest': '/x'})
assert matched == variables

Python & MCP Python SDK

  • mcp: 2.3.0 (also verified on main 77001c96f9 in src/mcp/shared/uri_template.py)
  • Python: 3.12.0

Activity

  1. ZYZ666-RGB commented on Nov 13, 2026

    @ZYZ666-RGB

    I'd like to take this issue and ask to be assigned.

    UriTemplate.match() currently misparses {;var} when the following literal starts with =, because the empty-value check in _scan_prefix can treat that literal as an empty path-style value. I plan to fix that check in src/mcp/shared/uri_template.py and add tests so match(expand(variables)) == variables holds for this case (including examples like {;a}=end{+rest}).

    Please assign this issue to me. Once it is assigned, I will open a PR with Fixes #3852, as described in the contributing guide (external PRs are reviewed when assigned or labeled help wanted).

  2. added
    bug
    v2
    on Nov 13, 2026
  3. shlokbam commented on Nov 14, 2026

    @shlokbam

    Confirmed on main (77001c9). One nuance: ;a=end=end/x is genuinely ambiguous for {;a}=end{+rest}, since {a: "", rest: "=end/x"} expands to the same string, so the fix is really about consistency: the suffix scan already prefers ;name=value ({;a}=end and {;a}=end{/rest} both match a="end"), only _scan_prefix takes the empty reading first.

    Approach I’d take: in _scan_prefix’s ifemp branch, when uri[pos] == "=", try the ;name=value reading first (bounded by the ; stop chars) and fall back to the empty check only if the next literal isn’t found. Prototype is ~15 lines in that one function and passes the existing 307 template tests. Happy to send it if you’d like an outside PR. (Investigated with AI assistance.)

  4. shlokbam commented on Nov 14, 2026

    @shlokbam

    Thanks @media-ctrl, good catch. The stop-char span takes "end=end" and falls back to the wrong parse, so the bounded find to the next literal is the right fix. I'll step back and leave this to @ZYZ666-RGB / you. Happy to review the PR.

  5. ZYZ666-RGB commented on Nov 14, 2026

    @ZYZ666-RGB

    Thanks @media-ctrl. I had an earlier fork branch, but it didn't use the bounded find to the next literal, so #3860 is the better fix. I'll close mine out rather than open a duplicate. Please go ahead with #3860.

  6. ZYZ666-RGB commented on Nov 15, 2026

    @ZYZ666-RGB

    Thanks for clarifying. I'm still stepping back in favor of #3860. Maintainers, could you review @media-ctrl's bounded-find fix and, if appropriate, assign this issue to them and reopen that PR? I won't open a competing PR.

  7. LeonxLJX commented on Nov 27, 2026

    @LeonxLJX

    I reproduced this on main (77001c9) and traced it to _scan_prefix in src/mcp/shared/uri_template.py.

    Root cause: for an ifemp (;) capture, the code checks uri.startswith(nxt.text, pos) before checking uri[pos] == "=". That correctly detects the empty-value form when the following literal begins with = (e.g. {;a}=end{+rest} with a="" → ;a=end/x), but it also fires when the non-empty value collides with that literal: a="end" expands to ;a=end=end/x, and at pos right after ;a, uri[pos:] (=end=end/x) starts with nxt.text (=end), so a is prematurely read as "" and the second =end leaks into the greedy {+rest}.

    Proposed fix (minimal, scoped to _scan_prefix): prefer the value form. When uri[pos] == "=", consume the = and try to locate the following literal (nxt.text) after a value within the operator's stop-char window — exactly the 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, and return None when neither matches. This keeps the existing empty-value cases (api;key=base/a/b → key="") and name-continuation rejections (api;keyZ/x → None) green, while fixing the collision so match(expand({a: "end", rest: "/x"})) round-trips. I verified the suffix / no-greedy path ({;a}=end) is unaffected, so the change is limited to the prefix scan.

    I'll open a PR with a focused regression test (RED before the fix, GREEN after) plus boundary-case assertions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions