Skip to content

fix: preserve OAuth authorization endpoint query params#2787

Open
he-yufeng wants to merge 1 commit into
modelcontextprotocol:mainfrom
he-yufeng:fix/oauth-redirect-existing-query
Open

fix: preserve OAuth authorization endpoint query params#2787
he-yufeng wants to merge 1 commit into
modelcontextprotocol:mainfrom
he-yufeng:fix/oauth-redirect-existing-query

Conversation

@he-yufeng
Copy link
Copy Markdown

Fixes #2776.

Summary

The OAuth authorization URL builder was always appending SDK parameters with a literal ?:

f"{auth_endpoint}?{urlencode(auth_params)}"

That breaks authorization servers whose metadata already includes endpoint-level query parameters. Salesforce's sandbox authorization endpoint is one example:

https://test.salesforce.com/services/oauth2/authorize?prompt=select_account

The generated URL became ...?prompt=select_account?response_type=..., so the OAuth parameters were folded into the existing prompt value instead of being sent as query parameters.

This change parses the endpoint query first, preserves existing parameters, and appends the SDK-generated OAuth parameters with normal form encoding.

Validation

python -m py_compile src\mcp\client\auth\oauth2.py tests\client\test_auth.py
$env:PYTHONPATH='C:\dev\GITHUB-clean\python-sdk\src'
python -m pytest tests\client\test_auth.py -q -k "authorization_endpoint_preserves_existing_query or auth_flow_adds_offline_access_when_as_advertises"
python -m ruff check src\mcp\client\auth\oauth2.py tests\client\test_auth.py
git diff --check upstream/main...HEAD

@he-yufeng he-yufeng force-pushed the fix/oauth-redirect-existing-query branch from 966555b to fd42072 Compare June 5, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth handler doesn't support redirect URLs with params

1 participant