fix: use background context for MCP session and add test
The MCP session was being initialized using the request-scoped context passed to Tools(ctx). When this context was cancelled the underlying MCP connection was closed. However, the session remained cached in the mcptoolset struct, causing subsequent calls to fail.
Changed the Connect call in getSession to use context.Background().
Added TestSessionContextCancellation to verify that the context passed to connect is not cancelled.
For questions related to executing Ping as well to confirm the status of the connection, please see https://github.com/google/adk-go/issues/399#issuecomment-3643981984
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.
Summary of Changes
Hello @Nerja, 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 resolves a critical issue where the MCP session's connection was prematurely terminated due to its reliance on a request-scoped context. By switching to context.Background() for session initialization, the change ensures the session's longevity and prevents unexpected disconnections. A dedicated test case has been introduced to validate this corrected behavior.
Highlights
- MCP Session Context Fix: The MCP session initialization has been updated to use
context.Background()instead of a request-scoped context. This resolves an issue where the underlying MCP connection would close prematurely if the request-scoped context was cancelled, while the session remained cached, leading to subsequent failures. - New Test Case: A new test,
TestSessionContextCancellation, has been added to verify that the context used for connecting to the MCP session is not inadvertently cancelled, ensuring the stability of the session.
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.
I don't think there's problem with request-scoped context. Because this context is only needed to create a session. Once session object is created even if the context is cancelled it doesn't affect the session. The MCP lib doesn't store the context.
@Nerja could you verify if this assumption is correct?
If it is, then setting context.Background() won't solve the issue. As we'd need rather to handle stale cached session object.
Closing in favor of https://github.com/google/adk-go/pull/417 and based on https://github.com/google/adk-go/issues/399#issuecomment-3647187957