Skip to content

Commit d6981b3

Browse files
author
xueyuan
committed
fix(auth): add Accept header to token and refresh requests
OAuthClientProvider._build_token_request and _refresh_token did not include an Accept header, so servers that default to form-encoded responses (e.g. GitHub's OAuth token endpoint) would not return JSON. This caused token exchange to fail with a parse error. Added Accept: application/json to both token exchange and refresh requests so servers know to respond with JSON.
1 parent 3eb5799 commit d6981b3

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/mcp/client/auth/oauth2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,10 @@ async def _exchange_token_authorization_code(
402402
token_data["resource"] = self.context.get_resource_url() # RFC 8707
403403

404404
# Prepare authentication based on preferred method
405-
headers = {"Content-Type": "application/x-www-form-urlencoded"}
405+
headers = {
406+
"Content-Type": "application/x-www-form-urlencoded",
407+
"Accept": "application/json",
408+
}
406409
token_data, headers = self.context.prepare_token_auth(token_data, headers)
407410

408411
return httpx.Request("POST", token_url, data=token_data, headers=headers)
@@ -447,7 +450,10 @@ async def _refresh_token(self) -> httpx.Request:
447450
refresh_data["resource"] = self.context.get_resource_url() # RFC 8707
448451

449452
# Prepare authentication based on preferred method
450-
headers = {"Content-Type": "application/x-www-form-urlencoded"}
453+
headers = {
454+
"Content-Type": "application/x-www-form-urlencoded",
455+
"Accept": "application/json",
456+
}
451457
refresh_data, headers = self.context.prepare_token_auth(refresh_data, headers)
452458

453459
return httpx.Request("POST", token_url, data=refresh_data, headers=headers)

tests/client/test_auth.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ async def test_token_exchange_request_authorization_code(self, oauth_provider: O
597597
assert request.method == "POST"
598598
assert str(request.url) == "https://api.example.com/token"
599599
assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
600+
assert request.headers["Accept"] == "application/json"
600601

601602
# Check form data
602603
content = request.content.decode()
@@ -623,6 +624,7 @@ async def test_refresh_token_request(self, oauth_provider: OAuthClientProvider,
623624
assert request.method == "POST"
624625
assert str(request.url) == "https://api.example.com/token"
625626
assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
627+
assert request.headers["Accept"] == "application/json"
626628

627629
# Check form data
628630
content = request.content.decode()

0 commit comments

Comments
 (0)