match() round-trip guarantee vs. the documented lenient decode: which one wins?
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
調査の方向性
Start in src/mcp/shared/uri_template.py at _scan_prefix and trace how match() captures bounded and reserved-expansion variables before comparing expand(match(uri)) with the input. Reproduce cases A and B from the issue, then read tests/shared/test_uri_template.py and extend _VALUE_CHARS with =% to expose the failures through the property test. Done means both examples re-expand identically and the property test passes, with Case A's decoding contract either changed or explicitly documented.
索引モデルが issue の本文から書いたものです。
説明
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
2.x (current stable) — reproduced on main at 77001c96f9, src/mcp/shared/uri_template.py
Description
match() can return a value set that is not a pre-image of the URI it just matched: expand(match(uri)) != uri. That breaks the contract the module docstring states and that test_match_inverts_expand_for_every_parseable_template asserts ("Exact equality with the original values is not required … as long as it re-expands identically").
Two shapes, same root cause family (captured values are percent-decoded, but re-expanding the decoded value does not always reproduce the original text).
A. Reserved expansion passing an existing percent-escape through. Two lines, no literals involved:
t = UriTemplate.parse("{+v}")
uri = t.expand({"v": "%33"}) # '%33'
t.match(uri) # {'v': '3'} <- decoded
t.expand(t.match(uri)) # '3' <- not the URI, so not a pre-image
{+v} leaves an existing pct-triplet untouched on expansion, so decoding it on match cannot be inverted by re-expansion.
B. An encoded separator inside a bounded variable, followed by a literal and a multi-segment variable. This is the shape that shows up in practice (a token or query value containing =, then a literal, then {+…}):
t = UriTemplate.parse("{a}x{+b}")
uri = t.expand({"a": "x=1", "b": ""}) # 'x%3D1x'
t.match(uri) # {'a': '', 'b': '=1x'}
t.expand(t.match(uri)) # 'x=1x' <- differs
Expected: {'a': 'x=1', 'b': ''}, which re-expands to x%3D1x.
Trace: in _scan_prefix, the ifemp path is not taken here, so the bounded capture does uri.find(nxt.text, pos, …) — find("x") hits the value's own first character at pos, so a is captured as "", the literal x consumes it, and the greedy {+b} takes %3D1 and unquotes it to =1. + re-emits = as =, not %3D, so the parse diverges from the URI.
Also affected, same mechanism: {a}end{+b} with a="end=end" (end%3Dendend → {'a': '', 'b': '=endend'}) and {?a}end{+b}.
Not affected (so this needs a multi-segment variable after the literal): {a}end{b} (correct), {a}end{/b} (correct), {a}end{?b} (correct), {a}end (correct, no greedy variable), and a plain non-encoded value like {"a": "xy"} (correct).
This is distinct from #3852: there the returned parse is a genuine pre-image (both readings expand identically, as discussed in that thread). Here the returned dict does not expand back to the URI at all.
Why the suite does not catch it
tests/shared/test_uri_template.py draws property values from a "literal-disjoint alphabet":
_VALUE_CHARS = string.ascii_lowercase + string.digits
so no generated value ever contains % or =. Extending that one line to ... + "=%" makes test_match_inverts_expand_for_every_parseable_template fail on the first accepted template, e.g.:
'{+v0}{&v1}~{;v2}{?v3}': match('%33&v1=%25f~;v2=erau?v3=l')
-> {'v0': '3', 'v2': 'erau', 'v1': '%f', 'v3': 'l'}
which re-expands to '3&v1=%25f~;v2=erau?v3=l' != input
Measured locally at 77001c9: 307 passed unchanged; with "=%" added to _VALUE_CHARS, that property test fails immediately.
Possible direction
- Case B is the cheap one: after the lazy capture, check
expand(candidate) == uriand, when it fails, retry at the next occurrence of the following literal before giving up. That leaves the documented lazy ordering intact for the cases that already work. - Case A looks like a contract decision: either stop decoding pct-escapes that the operator would re-emit differently, or extend the docstring so reserved-operator values containing pct-triplets are explicitly named as outside the round-trip guarantee (it currently names only the operator's separator).
- Independently of the fix,
_VALUE_CHARSincluding"=%"would keep both covered, since the property test already encodes the invariant that is being violated.
Python & MCP Python SDK
mcp:2.3.0, andmainat77001c96f9- Python:
3.12.12
(I investigated and drafted this with AI assistance; every claim above was verified by running the code on 77001c9 rather than taken from the model. Happy to carry the fix if an outside PR is welcome for this one.)
- 主要言語
- Python
- スター
- 25k
- フォーク
- 4.2k
- 平均マージ
- 10分
- マージ済み PR(30日)
- 1
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/python-sdk のほかの issue
-
v2
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
modelcontextprotocol/python-sdk#3978 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Client serves cached tools after its context exits with an exception対応中かも @jonxsong が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
modelcontextprotocol/python-sdk#3968 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
modelcontextprotocol/python-sdk#3957 ·
メンテナーはふだん 1 日以内に返信
-
v2
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
modelcontextprotocol/python-sdk#3934 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
bug v1
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
modelcontextprotocol/python-sdk#3929 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/python-sdk の issue をすべて見る
似ている issue
-
auto-reported bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
stix-atlas.json: the same relationship id is used for several different mitigates relationshipsオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
backend bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
nolte/kamerplanter#3193 ·
メンテナーはふだん 1 日以内に返信
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise対応中かも @abdulwaarith0 が今日担当しました。 オープンneeds-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
MakazhanAlpamys/Soup#3690 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信