Skip to content

Commit a7abebd

Browse files
authored
Merge pull request #30 from sacha-development-stuff/revert-28-codex/fix-failing-test-for-oauth-token-response
Revert "Improve OAuth token response handling"
2 parents 58928e1 + 6db5d60 commit a7abebd

File tree

1 file changed

+6
-38
lines changed

1 file changed

+6
-38
lines changed

src/mcp/client/auth/oauth2.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ async def _handle_oauth_metadata_response(self, response: httpx.Response) -> Non
222222
self.client_metadata.scope = " ".join(metadata.scopes_supported)
223223

224224
def _create_registration_request(self, metadata: OAuthMetadata | None = None) -> httpx.Request | None:
225-
if self._client_info or self.context.client_info:
225+
if self._client_info:
226226
return None
227227
if metadata and metadata.registration_endpoint:
228228
registration_url = str(metadata.registration_endpoint)
@@ -534,27 +534,15 @@ async def _exchange_token_authorization_code(
534534

535535
return httpx.Request("POST", token_url, data=token_data, headers=headers)
536536

537-
async def _read_response_content(self, response: httpx.Response) -> bytes:
538-
"""Read response content, handling preloaded or streaming bodies."""
539-
try:
540-
content = response.content
541-
if content:
542-
return content
543-
except RuntimeError:
544-
# Streaming response that hasn't been consumed yet - fall back to async read.
545-
pass
546-
547-
return await response.aread()
548-
549537
async def _handle_token_response(self, response: httpx.Response) -> None:
550538
"""Handle token exchange response."""
551539
if response.status_code != 200: # pragma: no cover
552-
body = await self._read_response_content(response)
540+
body = await response.aread()
553541
body = body.decode("utf-8")
554542
raise OAuthTokenError(f"Token exchange failed ({response.status_code}): {body}")
555543

556544
try:
557-
content = await self._read_response_content(response)
545+
content = await response.aread()
558546
token_response = OAuthToken.model_validate_json(content)
559547

560548
# Validate scopes
@@ -609,7 +597,7 @@ async def _handle_refresh_response(self, response: httpx.Response) -> bool: # p
609597
return False
610598

611599
try:
612-
content = await self._read_response_content(response)
600+
content = await response.aread()
613601
token_response = OAuthToken.model_validate_json(content)
614602

615603
self.context.current_tokens = token_response
@@ -626,8 +614,6 @@ async def _initialize(self) -> None: # pragma: no cover
626614
"""Load stored tokens and client info."""
627615
self.context.current_tokens = await self.context.storage.get_tokens()
628616
self.context.client_info = await self.context.storage.get_client_info()
629-
if self.context.client_info:
630-
self._client_info = self.context.client_info
631617
self._initialized = True
632618

633619
def _add_auth_header(self, request: httpx.Request) -> None:
@@ -708,16 +694,7 @@ async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.
708694
self.context.client_info = self._client_info
709695

710696
# Step 5: Perform authorization and complete token exchange
711-
auth_result = await self._perform_authorization()
712-
if isinstance(auth_result, httpx.Request):
713-
token_request = auth_result
714-
else:
715-
auth_code, code_verifier = auth_result
716-
token_request = await self._exchange_token_authorization_code(
717-
auth_code, code_verifier
718-
)
719-
720-
token_response = yield token_request
697+
token_response = yield await self._perform_authorization()
721698
await self._handle_token_response(token_response)
722699
except Exception: # pragma: no cover
723700
logger.exception("OAuth flow error")
@@ -738,16 +715,7 @@ async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.
738715
self._select_scopes(response)
739716

740717
# Step 2b: Perform (re-)authorization and token exchange
741-
auth_result = await self._perform_authorization()
742-
if isinstance(auth_result, httpx.Request):
743-
token_request = auth_result
744-
else:
745-
auth_code, code_verifier = auth_result
746-
token_request = await self._exchange_token_authorization_code(
747-
auth_code, code_verifier
748-
)
749-
750-
token_response = yield token_request
718+
token_response = yield await self._perform_authorization()
751719
await self._handle_token_response(token_response)
752720
except Exception: # pragma: no cover
753721
logger.exception("OAuth flow error")

0 commit comments

Comments
 (0)