-
Notifications
You must be signed in to change notification settings - Fork 1.5k
MCPToolset: Add OAuth2 Client Credentials Flow with RFC 8414 Compliant Discovery #2061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
@@ -307,6 +307,28 @@ | |||
* Added unit test coverage for local_eval_sets_manager.py ([174afb3](https://github.com/google/adk-python/commit/174afb3975bdc7e5f10c26f3eebb17d2efa0dd59)) | |||
* Extract common options for `adk web` and `adk api_server` ([01965bd](https://github.com/google/adk-python/commit/01965bdd74a9dbdb0ce91a924db8dee5961478b8)) | |||
|
|||
## [Unreleased] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CHANGELOG is only updated up on release. please put those information in the message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do!
Thank you so much for the amazing PR @jeremyschulman and I do think this feature is useful !! However such PR is too big to review, would you please kindly split it into multiple smaller PRs (group those logically highly related changes into one PR) , thank you :) |
@seanzhou1023 - Could you please provide guidance on how you would like this PR split into parts? From a Developer perspective, the feature enhancement is all part-and-parcel together. Happy to split it up, and I value your help. |
Hey @seanzhou1023 I'm working with Jeremy on this and wanted to propose this and get input for how to split.
I know we could put 3 inside 1 and 2 partially but we're trying to be mindful on sizing of the PRs. |
Never mind, I'm in the middle of review, it just takes longer to review big PRs. |
Add OAuth2 Client Credentials Flow with Automatic Discovery to ADK MCPToolset
The ADK now offers OAuth2 client credentials authentication that "just works" out of the box! 🚀
Summary
This PR adds enterprise-grade OAuth2 client credentials authentication to the ADK MCPToolset with automatic discovery capabilities. The implementation follows RFC 8414 for OAuth2 Authorization Server Metadata discovery and provides seamless authentication setup with minimal configuration.
No breaking changes - This PR is fully backwards compatible. Existing MCPToolset usage continues to work unchanged.
Key Features
🚀 Automatic OAuth Discovery by Default
🔧 RFC 8414 Compliant Discovery
.well-known/oauth-protected-resource
to find authorization server.well-known/oauth-authorization-server
for token endpoint🔐 Complete OAuth2 Client Credentials Flow
📝 Production-Ready Logging
Files Updated
🆕 New Files Created
adk-python/src/google/adk/auth/oauth2_discovery_util.py
(~180 lines)Purpose: OAuth2 discovery utilities following RFC 8414
Key Functions:
create_oauth_scheme_from_discovery()
- Main discovery entry point_query_oauth_protected_resource()
- Query .well-known/oauth-protected-resource_query_authorization_server_metadata()
- Query authorization server metadata_create_oauth2_scheme()
- Create OAuth2 scheme from discovered endpointsadk-python/src/google/adk/tools/mcp_tool/mcp_auth_discovery.py
(~85 lines)Purpose: Configuration class for OAuth discovery
Key Features:
MCPAuthDiscovery
dataclass with base_url, timeout, enabled properties📝 Enhanced Existing Files
adk-python/src/google/adk/tools/mcp_tool/mcp_toolset.py
(~100 lines added/modified)Major Changes:
MCPAuthDiscovery
from connection params_create_default_auth_discovery()
: Extracts base URLs from HTTP connections_perform_oauth_discovery()
: Performs RFC 8414 two-stage discoveryauth_discovery
parameter with automatic defaultsadk-python/src/google/adk/auth/credential_manager.py
(~50 lines added/modified)Key Enhancements:
_exchanger_registry
get_auth_credential()
: 8-step credential processing workflowadk-python/src/google/adk/auth/exchanger/oauth2_credential_exchanger.py
(~80 lines added/modified)Major Enhancements:
_exchange_client_credentials()
methodclient_secret_post
method_get_grant_type()
for flow type determinationadk-python/src/google/adk/tools/mcp_tool/mcp_tool.py
(~10 lines added/modified)Minor Enhancements:
_get_headers()
for OAuth2 tokensUsage Examples
Automatic Discovery
Override Options
Integration Flow
The implementation follows a clean integration pattern:
MCPToolset
→ Creates defaultMCPAuthDiscovery
from connection paramsoauth2_discovery_util
→ Discovers OAuth endpoints via RFC 8414credential_manager
→ Registers and usesOAuth2CredentialExchanger
oauth2_credential_exchanger
→ Performs client credentials token exchangemcp_tool
→ Uses exchanged tokens for authenticated MCP requestsDefault Behavior Logic
Auto-Enable OAuth Discovery:
http://localhost:9204/mcp/
→http://localhost:9204
(discovery base)http://server:8080/sse/
→http://server:8080
(discovery base)Discovery Process:
.well-known/oauth-protected-resource
for authorization server.well-known/oauth-authorization-server
for token endpointBenefits
auth_discovery
parameters still workTechnical Details
OAuth2 Discovery Implementation
Authentication Flow
Logging Strategy
Code Statistics
Test Coverage
🧪 Comprehensive Test Suite Added
The OAuth2 enhancement includes comprehensive test coverage across all new functionality:
New Test Files Created
test_mcp_auth_discovery.py
(~140 lines)test_mcp_toolset_oauth_discovery.py
(~380 lines)test_credential_manager_oauth2_integration.py
(~140 lines)Enhanced Existing Test Coverage
Existing comprehensive tests already cover:
test_oauth2_discovery_util.py
- OAuth2 discovery utilities (RFC 8414)test_oauth2_credential_exchanger.py
- Client credentials flow implementationtest_credential_manager.py
- Credential management workflows🎯 Test Coverage Areas
📊 Test Statistics
Dependencies
Sample Implementation
🎯 Comprehensive OAuth2 Client Credentials Sample
A complete sample implementation has been created at:
adk-python/contributing/samples/mcp_oauth2_client_credentials_agent/
Sample Features:
Sample Contents:
agent.py
- Five different OAuth2 agent configurationsmock_oauth_server.py
- Complete OAuth2 test server implementationREADME.md
- Comprehensive documentation and usage guide__init__.py
- Standard Python package structureDemonstrated Scenarios:
Mock OAuth2 Server Features:
.well-known/oauth-protected-resource
,.well-known/oauth-authorization-server
)