fix(client): preserve existing query params on OAuth authorization_endpoint
Summary
- The OAuth authorization-code grant now preserves any query parameters already present on the server-advertised
authorization_endpointinstead of producing a malformed double-?URL. - User-facing impact: clients can complete OAuth against servers whose
authorization_endpointcarries query params (e.g. Salesforce's.../authorize?prompt=select_account).
Motivation
Closes #2776.
_perform_authorization_code_grant built the redirect URL with:
authorization_url = f"{auth_endpoint}?{urlencode(auth_params)}"
When the server's authorization_endpoint already has a query string, this yields an invalid URL with two ? separators. The reporter's real-world example:
- Salesforce advertises
https://test.salesforce.com/services/oauth2/authorize?prompt=select_account - The client then navigated to
...authorize?prompt=select_account?response_type=code&...— the second?is invalid, so the existingpromptparam is swallowed into the value and the request is rejected.
Root cause
Blind string concatenation of ? + encoded params, with no handling for a pre-existing query component on the endpoint.
Fix
New _build_authorization_url(auth_endpoint, auth_params) helper:
urlparsethe endpoint, merge its existing query (parse_qsl) with the flow-generatedauth_params, thenurlunparseback into a single well-formed query string.- Flow-generated params win on key conflicts (the client's
response_type,state, PKCE challenge, etc. are authoritative). None-valued params are dropped rather than serialized as the literal string"None"(preserves prior effective behavior; keeps the signature type-correct asMapping[str, str | None]).
Diff: 2 files, +103/-3.
Tests
Added TestAuthorizationEndpointWithQuery in tests/client/test_auth.py:
test_build_authorization_url_no_existing_query— baseline, single?.test_build_authorization_url_preserves_existing_query— the #2776 Salesforce case; serverpromptsurvives alongside flow params, exactly one?.test_build_authorization_url_flow_params_win_on_conflict— precedence rule.test_perform_authorization_preserves_endpoint_query— end-to-end through_perform_authorization_code_grant, asserting the captured redirect URL is well-formed.
Verification
uv run pytest tests/client/test_auth.py— 100 passed, 1 xfaileduv run pyright src/mcp/client/auth/oauth2.py— 0 errorsuv run ruff format/ruff check— clean- Did NOT change any unrelated behavior; flow param precedence and
Nonehandling preserve the previous effective output for endpoints without a query string.
CI note — rebased onto current main. The only remaining red cell (test (3.14, lowest-direct, …)) is a pre-existing, main-wide failure unrelated to this PR: tests/interaction/transports/test_stdio.py::test_tool_call_and_notification_round_trip… asserts byte-exact child stderr, but anyio==4.9 (the lowest-direct floor) has return inside a finally: in from_thread.py:119, which Python 3.14 emits as SyntaxWarning to stderr and pollutes the snapshot. This PR does not touch stdio or that test. Full root-cause + the 6 parallel fixes already in flight are consolidated in https://github.com/modelcontextprotocol/python-sdk/pull/2734#issuecomment-4628718448. All other cells (incl. the previously-flaky termination test, now run in-process via #2767) are green.
Rebased onto latest upstream `main` to resolve the merge conflict (an import collision in `tests/client/test_auth.py` — kept both upstream's `OAuthTokenError` and this PR's `_build_authorization_url` import; `oauth2.py` auto-merged cleanly). The OAuth query-param fix is unchanged.
Local verification on the rebased branch:
- `uv run pytest tests/client/test_auth.py` — 136 passed, 1 xfailed (incl. the 4 new `TestAuthorizationEndpointWithQuery` cases)
- `uv run ruff check` / `ruff format --check` — clean
- `uv run pyright src/mcp/client/auth/oauth2.py` — 0 errors
PR now shows mergeable. Ready for review.
Thanks for the careful review, @STiFLeR7 — the precedence read is exactly right.
Good catch on the duplicate-key edge case. dict(parse_qsl(...)) did collapse multi-value existing params (?scope=a&scope=b → only scope=b). Rather than just document the tradeoff, I fixed it: _build_authorization_url now builds the merged query from an ordered pair list — it keeps every existing endpoint pair (including duplicates) except keys the flow overrides, then appends the authoritative auth_params. So multi-value server params survive, and flow params still win on conflict.
Added a regression test test_build_authorization_url_preserves_multi_value_existing_query asserting scope=[a, b] survives alongside the flow's response_type, with exactly one ?.
Verification on the pushed commit:
uv run pytest tests/client/test_auth.py -k AuthorizationEndpointWithQuery— 5 passeduv run ruff check/ruff format --check— cleanuv run pyright src/mcp/client/auth/oauth2.py— 0 errors