- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.2k
feat(toolset): add generate_preprocessing_events method to BaseToolset #3342
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
Add new generate_preprocessing_events method to BaseToolset that allows toolsets to generate events (such as authentication requests) during the preprocessing phase before tool discovery occurs. Changes: - Add generate_preprocessing_events method to BaseToolset with default implementation - Integrate method call in base_llm_flow.py _preprocess_async method - Call generate_preprocessing_events before process_llm_request for proper timing - Add comprehensive unit tests covering method invocation and event generation - Maintain full backward compatibility with existing toolsets This provides the foundation for solving MCP Toolset OAuth2 authentication flow issues by allowing toolsets to request user authentication before attempting to discover tools that require authenticated sessions. The method has access to full ToolContext with authentication capabilities and executes at the perfect timing - after request processors but before tool discovery (get_tools).
| Summary of ChangesHello @loveyana, 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 introduces a foundational change to the ADK Python framework by adding a  Highlights
 Using Gemini Code AssistThe 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  
 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  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
 | 
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.
Code Review
This pull request introduces a generate_preprocessing_events method to BaseToolset, enabling toolsets to generate events like authentication requests before tool discovery. This is a well-designed, additive change that addresses a key architectural issue with OAuth2 flows. The implementation is clean, backward-compatible, and includes thorough unit tests for the new functionality. My review includes a couple of minor suggestions to improve code clarity and style.
| if False: # This ensures the method is an AsyncGenerator | ||
| yield # Required for AsyncGenerator type hint | ||
| return | 
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.
| def __init__(self): | ||
| super().__init__() | ||
| self.generate_preprocessing_events_called = False | ||
| self.generated_events = [] | 
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.
| parts=[types.Part(text='Mock authentication request')], | ||
| ), | ||
| ) | ||
| self.generated_events.append(auth_event) | 
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.
| Hi @loveyana, Thank you for your contribution! | 
Add new generate_preprocessing_events method to BaseToolset that allows toolsets to generate events (such as authentication requests) during the preprocessing phase before tool discovery occurs.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Problem Description:
The current ADK Python implementation has critical defects in the MCP (Model Context Protocol) Toolset OAuth2 authentication flow. MCP Toolset tool discovery occurs during the preprocessing phase using
ReadonlyContext, which lacks therequest_credential()method necessary for OAuth2 user interaction. This creates a fundamental architectural problem where authentication is required before tools can be discovered, but the authentication mechanism is not available at that stage.Core Issues:
ReadonlyContextlacks authentication capability during preprocessing phase3. Solution:
Add a new
generate_preprocessing_events()method toBaseToolsetthat allows toolsets to generate authentication events during the preprocessing phase, before tool discovery occurs. This method:ToolContextwith authentication capabilitiesTesting Plan
Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.
Unit Tests:
Added Tests:
test_preprocess_calls_toolset_generate_preprocessing_events: Verifies that_preprocess_asynccalls the new method on toolsets and processes generated eventstest_preprocess_calls_both_generate_events_and_process_request: Verifies correct call order (generate_preprocessing_events → process_llm_request)Test Coverage:
Manual End-to-End (E2E) Tests:
Testing the Infrastructure:
generate_preprocessing_eventsto generate authentication eventsFuture Testing (after MCP Toolset implementation):
Checklist
Additional context
Architecture Impact:
This change provides the foundational infrastructure needed to solve MCP Toolset OAuth2 authentication issues. The implementation follows ADK's existing patterns:
ToolContextandLlmRequestparameters asprocess_llm_requestAsyncGenerator[Event, None]pattern used throughout ADKImplementation Details:
generate_preprocessing_eventsmethod with default no-op implementationprocess_llm_requestin toolset processingFuture Work:
This PR provides the infrastructure. The next step will be implementing the actual OAuth2 authentication logic in MCP Toolset using this new capability.
Breaking Changes: None - this is a purely additive change with full backward compatibility.