enhanced MCP session management with mcp-session-id support#4466
enhanced MCP session management with mcp-session-id support#4466jayy-77 wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Response from ADK Triaging Agent Hello @jayy-77, thank you for creating this PR! Could you please fill out the PR template? This PR is missing a link to an issue (or a description of the change), a testing plan, and the checklist is not filled out. This information will help reviewers to review your PR more efficiently. Thanks! |
Summary of ChangesHello @jayy-77, 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 significantly improves the handling of MCP sessions by introducing support for Highlights
🧠 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
Activity
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.
Code Review
This pull request introduces support for mcp-session-id to enable stateful server sessions, which enhances the MCP toolset. However, this implementation introduces a vulnerability: the sensitive mcp-session-id is being logged and exposed through public methods, which could lead to session information leakage. To mitigate this, ensure the session ID is masked or removed from log statements and public informational methods. Additionally, there is a critical bug in the session reuse logic within MCPSessionManager that prevents the feature from working as intended, and a minor encapsulation issue in McpToolset.
| if mcp_session_id: | ||
| key_parts.append(f'mcp_id:{mcp_session_id}') | ||
|
|
||
| if merged_headers: | ||
| headers_json = json.dumps(merged_headers, sort_keys=True) | ||
| headers_hash = hashlib.md5(headers_json.encode()).hexdigest() | ||
| return f'session_{headers_hash}' | ||
| key_parts.append(f'headers:{headers_hash}') |
There was a problem hiding this comment.
To enable correct and simple session reuse logic in create_session, the constant part of the session key (derived from headers) should precede the mcp_session_id part. This allows for simple prefix matching. Please swap the order of these if blocks.
| if mcp_session_id: | |
| key_parts.append(f'mcp_id:{mcp_session_id}') | |
| if merged_headers: | |
| headers_json = json.dumps(merged_headers, sort_keys=True) | |
| headers_hash = hashlib.md5(headers_json.encode()).hexdigest() | |
| return f'session_{headers_hash}' | |
| key_parts.append(f'headers:{headers_hash}') | |
| if merged_headers: | |
| headers_json = json.dumps(merged_headers, sort_keys=True) | |
| headers_hash = hashlib.md5(headers_json.encode()).hexdigest() | |
| key_parts.append(f'headers:{headers_hash}') | |
| if mcp_session_id: | |
| key_parts.append(f'mcp_id:{mcp_session_id}') |
| async with self._session_lock: | ||
| for key, (session, exit_stack, stored_loop, mcp_id) in list(self._sessions.items()): | ||
| # Check if this session matches our connection params (ignoring mcp-session-id) | ||
| if key.startswith(temp_key.split('_mcp_id')[0]) and mcp_id: |
There was a problem hiding this comment.
This logic for finding a matching session is incorrect. temp_key.split('_mcp_id')[0] will not work as _mcp_id is not in the key string, and the original key part order makes prefix matching fail.
With the suggested change to _generate_session_key (placing the headers part first), this check can be simplified and corrected to use startswith.
| if key.startswith(temp_key.split('_mcp_id')[0]) and mcp_id: | |
| if key.startswith(temp_key + '_') and mcp_id: |
| if stored_loop is current_loop and not self._is_session_disconnected(session): | ||
| # Found a valid session with mcp-session-id, use it | ||
| existing_mcp_session_id = mcp_id | ||
| logger.debug('Reusing existing mcp-session-id: %s', existing_mcp_session_id) |
There was a problem hiding this comment.
The mcp_session_id is a sensitive session identifier used to maintain stateful sessions with the MCP server. Logging it at the DEBUG level can lead to session identifier leakage in log files, which could be exploited to interfere with active sessions.
| logger.debug('Reusing existing mcp-session-id: %s', existing_mcp_session_id) | |
| logger.debug('Reusing existing mcp-session-id') |
|
|
||
| # If we got a new mcp-session-id, update the session key and re-store | ||
| if new_mcp_session_id and new_mcp_session_id != existing_mcp_session_id: | ||
| logger.info('Received new mcp-session-id from server: %s', new_mcp_session_id) |
There was a problem hiding this comment.
The mcp_session_id is logged at the INFO level. Since INFO logs are frequently enabled in production environments, this poses a significant risk of leaking session identifiers to log management systems.
| logger.info('Received new mcp-session-id from server: %s', new_mcp_session_id) | |
| logger.info('Received new mcp-session-id from server') |
| logger.debug( | ||
| 'Captured mcp-session-id from server: %s', self._mcp_session_id | ||
| ) |
There was a problem hiding this comment.
| session_info = {} | ||
| for key, (_, _, _, mcp_id) in self._mcp_session_manager._sessions.items(): | ||
| session_info[key] = { | ||
| 'mcp_session_id': mcp_id, |
There was a problem hiding this comment.
The get_session_info method returns the raw mcp_session_id for all active sessions. As a public method intended for debugging, it should avoid exposing full session identifiers which could be leaked if the method's output is displayed in a UI or exposed via an API. Consider masking the identifier.
| 'mcp_session_id': mcp_id, | |
| 'mcp_session_id': mcp_id[:8] + '...' if mcp_id else None, |
| and mcp-session-id values. | ||
| """ | ||
| session_info = {} | ||
| for key, (_, _, _, mcp_id) in self._mcp_session_manager._sessions.items(): |
There was a problem hiding this comment.
Accessing the private member _sessions of _mcp_session_manager breaks encapsulation. This makes McpToolset dependent on the internal implementation of MCPSessionManager and more brittle to future changes. It would be better to add a public method to MCPSessionManager to provide this debugging information.
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. Or, if no issue exists, describe the change:
If applicable, please follow the issue templates to provide as much detail as
possible.
Problem:
A clear and concise description of what the problem is.
Solution:
A clear and concise description of what you want to happen and why you choose
this solution.
Testing 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:
Please include a summary of passed
pytestresults.Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any
necessary setup or configuration. Please provide logs or screenshots to help
reviewers better understand the fix.
Checklist
Additional context
Add any other context or screenshots about the feature request here.