diff --git a/src/mcp/client/auth/oauth2.py b/src/mcp/client/auth/oauth2.py index 72309f5775..ab2e102f08 100644 --- a/src/mcp/client/auth/oauth2.py +++ b/src/mcp/client/auth/oauth2.py @@ -402,7 +402,10 @@ async def _exchange_token_authorization_code( token_data["resource"] = self.context.get_resource_url() # RFC 8707 # Prepare authentication based on preferred method - headers = {"Content-Type": "application/x-www-form-urlencoded"} + headers = { + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json", + } token_data, headers = self.context.prepare_token_auth(token_data, headers) return httpx.Request("POST", token_url, data=token_data, headers=headers) @@ -447,7 +450,10 @@ async def _refresh_token(self) -> httpx.Request: refresh_data["resource"] = self.context.get_resource_url() # RFC 8707 # Prepare authentication based on preferred method - headers = {"Content-Type": "application/x-www-form-urlencoded"} + headers = { + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json", + } refresh_data, headers = self.context.prepare_token_auth(refresh_data, headers) return httpx.Request("POST", token_url, data=refresh_data, headers=headers) diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index bb0bce4c92..1ded65f81f 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -597,6 +597,7 @@ async def test_token_exchange_request_authorization_code(self, oauth_provider: O assert request.method == "POST" assert str(request.url) == "https://api.example.com/token" assert request.headers["Content-Type"] == "application/x-www-form-urlencoded" + assert request.headers["Accept"] == "application/json" # Check form data content = request.content.decode() @@ -623,6 +624,7 @@ async def test_refresh_token_request(self, oauth_provider: OAuthClientProvider, assert request.method == "POST" assert str(request.url) == "https://api.example.com/token" assert request.headers["Content-Type"] == "application/x-www-form-urlencoded" + assert request.headers["Accept"] == "application/json" # Check form data content = request.content.decode()