Skip to content

Commit 8c1f757

Browse files
author
Soham Dahivalkar
committed
fix(auth): add Accept: application/json header to OAuth token requests
Token exchange and refresh requests only set Content-Type but omit the Accept header. Some OAuth providers (e.g. GitHub) return form-encoded data by default and require Accept: application/json to return JSON. Since _handle_token_response parses the body as JSON, omitting the Accept header causes parse failures with these providers. Add Accept: application/json to both _exchange_token_authorization_code and _build_refresh_token_request. Fixes #1523
1 parent 616476f commit 8c1f757

1 file changed

Lines changed: 8 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)

0 commit comments

Comments
 (0)