-
Notifications
You must be signed in to change notification settings - Fork 455
Add MCP session reconnection with Ping health check #417
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
Summary of ChangesHello @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 significantly enhances the resilience of the MCP toolset by introducing an automatic session reconnection capability. It ensures that any previously established MCP session is validated for health using 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 adds session reconnection logic to the MCP toolset, using a Ping health check to detect stale connections. The implementation in set.go correctly checks for an active session and attempts to reconnect if the ping fails. The accompanying test in set_test.go effectively validates this new reconnection behavior.
My review includes a couple of suggestions for improvement: one to make the session recovery logic more robust in set.go, and another to improve context handling in the new test case to prevent potential resource leaks.
| if err := s.session.Ping(ctx, &mcp.PingParams{}); err == nil { | ||
| return s.session, nil | ||
| } | ||
| s.session.Close() |
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.
After closing a stale session, it's good practice to set s.session to nil. This makes the error recovery more robust. If the subsequent call to s.client.Connect fails, the next invocation of getSession will not attempt to Ping and Close an already-closed session, which could lead to unexpected errors or behavior. Setting it to nil ensures a clean state for the next connection attempt.
if err := s.session.Ping(ctx, &mcp.PingParams{}); err == nil {
return s.session, nil
}
s.session.Close()
s.session = nil|
|
||
| func (rt *reconnectableTransport) Connect(ctx context.Context) (mcp.Connection, error) { | ||
| ct, st := mcp.NewInMemoryTransports() | ||
| _, err := rt.server.Connect(context.Background(), st, nil) |
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.
It's better to use the ctx from the function arguments here instead of context.Background(). This ensures that if the client-side connection attempt is cancelled, the server-side connection setup is also cancelled. This prevents potential resource leaks in the test and is a better practice for context propagation.
| _, err := rt.server.Connect(context.Background(), st, nil) | |
| _, err := rt.server.Connect(ctx, st, nil) |
|
Thanks for the PR! I'm thinking -- now we will always be making 2 network requests: ping + list/call_tool. What do you think about these approaches:
What do you think? |
d246633 to
f158d09
Compare
f158d09 to
3183b34
Compare
|
@dpasiukevich I updated the PR to only ping when there is an error. What do you think of the current approach? I noticed that not all errors are mcp.ErrConnectionClosed (for instance, the Close() call in the tests does not cause mcp.ErrConnectionClosed) so made the compromise to issue a ping when there is any error. If ping succeeds, then we will not create a new connection/session. |
This PR adds resilience to the MCP toolset by automatically attempting to reconnect when the connection to the MCP server is lost. When ListTools() or CallTool() fails, the client first pings the server to verify the connection is actually dead before attempting a reconnect.