Skip to content

Commit d83f184

Browse files
authored
Merge pull request #26 from sacha-development-stuff/codex/fix-oauthtoken-error-in-test-case
Fix OAuth authorization flow to use auth code exchange
2 parents 00d1b74 + 3af52da commit d83f184

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/mcp/client/auth/oauth2.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,9 @@ def _select_scopes(self, init_response: httpx.Response) -> None:
433433

434434
# Discovery and registration helpers provided by BaseOAuthProvider
435435

436-
async def _perform_authorization(self) -> httpx.Request:
437-
"""Perform the authorization flow."""
438-
auth_code, code_verifier = await self._perform_authorization_code_grant()
439-
token_request = await self._exchange_token_authorization_code(auth_code, code_verifier)
440-
return token_request
436+
async def _perform_authorization(self) -> tuple[str, str]:
437+
"""Perform the authorization flow and return authorization code data."""
438+
return await self._perform_authorization_code_grant()
441439

442440
async def _perform_authorization_code_grant(self) -> tuple[str, str]:
443441
"""Perform the authorization redirect and get auth code."""
@@ -687,14 +685,18 @@ async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.
687685
break # Non-4XX error, stop trying
688686

689687
# Step 4: Register client if needed
688+
if self.context.client_info and not self._client_info:
689+
self._client_info = self.context.client_info
690690
registration_request = self._create_registration_request(self._metadata)
691691
if registration_request:
692692
registration_response = yield registration_request
693693
await self._handle_registration_response(registration_response)
694694
self.context.client_info = self._client_info
695695

696696
# Step 5: Perform authorization and complete token exchange
697-
token_response = yield await self._perform_authorization()
697+
auth_code, code_verifier = await self._perform_authorization()
698+
token_request = await self._exchange_token_authorization_code(auth_code, code_verifier)
699+
token_response = yield token_request
698700
await self._handle_token_response(token_response)
699701
except Exception: # pragma: no cover
700702
logger.exception("OAuth flow error")
@@ -715,7 +717,9 @@ async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.
715717
self._select_scopes(response)
716718

717719
# Step 2b: Perform (re-)authorization and token exchange
718-
token_response = yield await self._perform_authorization()
720+
auth_code, code_verifier = await self._perform_authorization()
721+
token_request = await self._exchange_token_authorization_code(auth_code, code_verifier)
722+
token_response = yield token_request
719723
await self._handle_token_response(token_response)
720724
except Exception: # pragma: no cover
721725
logger.exception("OAuth flow error")

0 commit comments

Comments
 (0)