Skip to content

fix(auth): include PKCE code_challenge for MCP OAuth flows#4710

Open
pandego wants to merge 2 commits intogoogle:mainfrom
pandego:fix/4708-mcp-oauth-pkce
Open

fix(auth): include PKCE code_challenge for MCP OAuth flows#4710
pandego wants to merge 2 commits intogoogle:mainfrom
pandego:fix/4708-mcp-oauth-pkce

Conversation

@pandego
Copy link

@pandego pandego commented Mar 4, 2026

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_method
    • code_verifier
  • In auth URI generation:
    • when code_challenge_method is set, ADK now ensures a code_verifier exists,
    • passes PKCE params into create_authorization_url(...).
  • In token exchange:
    • passes code_verifier to fetch_token(...) for authorization-code exchange.
  • Added unit tests covering:
    • PKCE parameters present in generated auth URL,
    • code_verifier forwarded 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

  • Linked issue
  • Focused, minimal scope
  • Unit tests updated for new behavior

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! 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_method and code_verifier fields to the OAuth2Auth credential model to support PKCE (Proof Key for Code Exchange) flows.
  • Authorization URI Generation: Modified the generate_auth_uri function to automatically generate a code_verifier and include PKCE parameters (code_challenge_method, code_verifier) in the authorization URL when a code_challenge_method is specified.
  • Token Exchange Enhancement: Updated the _exchange_authorization_code function to pass the code_verifier during 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_verifier during 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_method and code_verifier fields to OAuth2Auth.
  • src/google/adk/auth/auth_handler.py
    • Imported secrets module for code_verifier generation.
    • Updated generate_auth_uri to include PKCE parameters and generate code_verifier.
  • src/google/adk/auth/exchanger/oauth2_credential_exchanger.py
    • Modified _exchange_authorization_code to pass code_verifier during token exchange.
  • tests/unittests/auth/exchanger/test_oauth2_credential_exchanger.py
    • Added test_exchange_authorization_code_with_code_verifier to verify PKCE in token exchange.
  • tests/unittests/auth/test_auth_handler.py
    • Updated MockOAuth2Session to handle code_challenge_method in create_authorization_url.
    • Added test_generate_auth_uri_with_pkce to verify PKCE parameters in auth URI generation.
Activity
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 👍 and 👎 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.

Footnotes

  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.

@adk-bot adk-bot added the mcp [Component] Issues about MCP support label Mar 4, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to add PKCE support for OAuth flows. While the changes to the data model and the token exchange part are correct, there is a critical issue in the authorization URI generation. The implementation incorrectly passes the code_verifier instead of the required code_challenge. Additionally, the unit tests use a mock that masks this bug, preventing it from being caught. My review includes feedback on fixing the implementation and improving the test mock.

@pandego
Copy link
Author

pandego commented Mar 4, 2026

Thanks for the review, addressed in the latest push.

What changed:

  • generate_auth_uri now sends code_challenge + code_challenge_method for PKCE auth requests.
  • code_challenge is derived from code_verifier (S256 uses Authlib RFC7636 helper).
  • code_verifier remains 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_verifier is 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

@anowardear062-svg
Copy link

The best interest way

@pandego pandego force-pushed the fix/4708-mcp-oauth-pkce branch from dcccc90 to f8514e9 Compare March 5, 2026 15:59
@anowardear062-svg
Copy link

Thanks for the update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mcp [Component] Issues about MCP support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP tool Oauth handshake error with "code_challenge required"

3 participants