Skip to content

Conversation

@Nerja
Copy link

@Nerja Nerja commented Dec 8, 2025

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 #399 (comment)

@google-cla
Copy link

google-cla bot commented Dec 8, 2025

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.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Nerja, 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 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 👍 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.

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 correctly addresses an issue where a cached MCP session was being closed prematurely due to using a request-scoped context. The fix of using context.Background() to initialize the session is appropriate, as the session's lifecycle should not be tied to a single request. The newly added test, TestSessionContextCancellation, effectively verifies this fix by using a spy transport to ensure the connection context is not cancelled along with the request context. The changes look good. I have one minor suggestion for improving the thread-safety of the test helper for future robustness.

Comment on lines +351 to +354
type contextSpyTransport struct {
mcp.Transport
ctx context.Context
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The contextSpyTransport is not thread-safe. The ctx field is written in Connect and read in Context without synchronization. While this is safe in the current test because getSession is protected by a mutex and ensures Connect is called only once, it's a fragile pattern. If this test helper is reused in other tests with concurrent operations, it could lead to data races. Consider adding a mutex (sync.Mutex) to protect access to the ctx field to make this helper more robust.

@dpasiukevich
Copy link
Collaborator

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.

@Nerja
Copy link
Author

Nerja commented Dec 15, 2025

Closing in favor of #417 and based on #399 (comment)

@Nerja Nerja closed this Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants