fix(auth): include PKCE code_challenge for MCP OAuth flows
Summary
Fixes #4708.
This updates the OAuth authorization flow to include PKCE parameters when requested by MCP tool credentials, so providers that require code_challenge no longer reject the initial auth handshake.
What changed
- Added explicit OAuth2 credential fields for PKCE:
code_challenge_methodcode_verifier
- In auth URI generation:
- when
code_challenge_methodis set, ADK now ensures acode_verifierexists, - passes PKCE params into
create_authorization_url(...).
- when
- In token exchange:
- passes
code_verifiertofetch_token(...)for authorization-code exchange.
- passes
- Added unit tests covering:
- PKCE parameters present in generated auth URL,
code_verifierforwarded during token exchange.
Testing plan
uv run pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py
Both test files pass locally.
Checklist
- [x] Linked issue
- [x] Focused, minimal scope
- [x] Unit tests updated for new behavior
Summary of Changes
Hello, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the OAuth authorization flow to support PKCE (Proof Key for Code Exchange), addressing an issue where certain providers rejected initial authentication handshakes due to missing code_challenge parameters. The changes enable the system to correctly generate and utilize PKCE parameters throughout the OAuth process, from authorization URI creation to token exchange, thereby improving compatibility with modern OAuth2 implementations.
Highlights
- PKCE Parameter Support: Added
code_challenge_methodandcode_verifierfields to theOAuth2Authcredential model to support PKCE (Proof Key for Code Exchange) flows. - Authorization URI Generation: Modified the
generate_auth_urifunction to automatically generate acode_verifierand include PKCE parameters (code_challenge_method,code_verifier) in the authorization URL when acode_challenge_methodis specified. - Token Exchange Enhancement: Updated the
_exchange_authorization_codefunction to pass thecode_verifierduring the OAuth2 token exchange, ensuring proper PKCE validation with identity providers. - Unit Test Coverage: Introduced new unit tests to validate the correct inclusion of PKCE parameters in generated authorization URLs and the proper forwarding of the
code_verifierduring token exchange.
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.
Changelog
- src/google/adk/auth/auth_credential.py
- Added
code_challenge_methodandcode_verifierfields toOAuth2Auth.
- Added
- src/google/adk/auth/auth_handler.py
- Imported
secretsmodule forcode_verifiergeneration. - Updated
generate_auth_urito include PKCE parameters and generatecode_verifier.
- Imported
- src/google/adk/auth/exchanger/oauth2_credential_exchanger.py
- Modified
_exchange_authorization_codeto passcode_verifierduring token exchange.
- Modified
- tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py
- Added
test_exchange_authorization_code_with_code_verifierto verify PKCE in token exchange.
- Added
- tests/unittests/auth/test_auth_handler.py
- Updated
MockOAuth2Sessionto handlecode_challenge_methodincreate_authorization_url. - Added
test_generate_auth_uri_with_pkceto verify PKCE parameters in auth URI generation.
- Updated
Activity
- The author linked issue #4708, indicating this PR is a fix for a known problem.
- The changes have a focused, minimal scope, targeting specific OAuth flow enhancements.
- Unit tests were updated to cover the new behavior, ensuring the PKCE implementation works as expected.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.
Thanks for the review, addressed in the latest push.
What changed:
generate_auth_urinow sendscode_challenge+code_challenge_methodfor PKCE auth requests.code_challengeis derived fromcode_verifier(S256uses Authlib RFC7636 helper).code_verifierremains stored on the credential and is only sent during token exchange.- Test mock was tightened to reflect real behavior and the PKCE test now asserts
code_verifieris not present in the authorization URL.
Validation run:
uv run pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py- 36 passed
The best interest way
Thanks for the update
Hi @pandego , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.
Hi @wuliang229 , can you please review this.
Rebased this branch onto the latest main and force-pushed 6da89c43.
Validation rerun:
uv run --extra test pytest tests/unittests/tools/mcp_tool/test_mcp_session_manager.py -q-> 26 passed
There were two pytest teardown runtime warnings from async mocks/exit stack after the suite completed, but the targeted MCP session manager tests passed cleanly.
Hi @pandego , This PR has merge conflicts that require changes from your end. Could you please rebase your branch with the latest main branch to address these? Once this is complete, please let us know so we can proceed with the review.
Rebased this branch onto the latest main and force-pushed ec4f66e6.
Also folded in the PKCE auth URL fix while resolving the rebase so the authorization request now sends code_challenge/code_challenge_method, keeps code_verifier for the token exchange only, and preserves the existing client_secret_post behavior during token fetch.
Validation rerun:
uv run --extra test pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py- 37 passed
Rebased this branch onto the latest main and force-pushed 0df1f741.
Validation rerun:
uv run --extra test pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py- 37 passed
This keeps the PKCE auth URL behavior intact (code_challenge / code_challenge_method on the authorization request, code_verifier only during token exchange). Ready for review again.
Hello @pandego and @rohityan we have a similar need to be able to connect to an MCP server which requires PKCE. Are there plans to continue with this change and make it available as part of a future release?
Rebased this branch onto the latest main and force-pushed 849f35bc to resolve the conflicts.
This still keeps the PKCE flow in place for MCP OAuth providers: the authorization request includes code_challenge / code_challenge_method, while code_verifier is retained for token exchange.
Validation rerun:
uv run --extra test pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py- 39 passeduv run --extra dev pyink --check src/google/adk/auth/auth_handler.py tests/unittests/auth/test_auth_handler.py- passed
Ready for review again.
Rebased this branch onto the latest main and force-pushed 90e84a07.
Validation rerun:
uv run --extra test pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py- 39 passeduv run --extra dev pyink --check src/google/adk/auth/auth_handler.py tests/unittests/auth/test_auth_handler.py- passed
Ready for review again.
Rebased this branch onto the latest main and force-pushed c9714ce5.
Validation rerun:
uv run --extra test pytest tests/unittests/auth/test_auth_handler.py tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py- 39 passeduv run --extra dev pyink --check src/google/adk/auth/auth_handler.py tests/unittests/auth/test_auth_handler.py- passed
Ready for review again.
Rebased this branch onto the current main and pushed the refreshed fork branch.
Validation:
uv run pytest tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py -q
The PR should be back in a current review state.
Thanks for your contribution! The issue this claims to address has already been fixed in https://github.com/google/adk-python/commit/e7316dc077d676b4349a8d7779ad4ad73f6b0d24