Skip to content

fix(mcp): retry after missing session id errors - #1355

Merged
henrypark133 merged 1 commit into
nearai:stagingfrom
G7CNF:codex/issue-1315-mcp-session-reinit
Mar 18, 2026
Merged

henrypark133 merged 1 commit into
nearai:stagingfrom
G7CNF:codex/issue-1315-mcp-session-reinit

Conversation

@G7CNF

@G7CNF G7CNF commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Retry a single time after a recoverable MCP session-expiry 400 by forcing a fresh initialize handshake.

Fixes #1315.

@github-actions github-actions Bot added scope: tool/mcp MCP client size: L 200-499 changed lines risk: medium Business logic, config, or moderate-risk modules contributor: core 20+ merged PRs labels Mar 18, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 enhances the robustness of the MCP client by introducing an automatic retry mechanism for transient session expiry errors. It centralizes the session reinitialization logic into a dedicated function and updates the request sending process to detect and recover from specific session-related HTTP 400 errors, thereby improving the client's resilience against server-side session invalidations.

Highlights

  • Session Expiry Retry Mechanism: Implemented a retry mechanism for the MCP client to handle recoverable session expiry errors (e.g., 400 with 'missing session id'). Upon detecting such an error, the client will attempt to reinitialize the session and retry the original request once.
  • New reinitialize_session Function: Introduced a dedicated asynchronous function reinitialize_session to encapsulate the logic for terminating the current session, performing a new MCP initialize handshake, and marking the session as initialized. This function is now used both for initial client setup and for session expiry retries.
  • Error Detection Helper: Added a helper function is_session_expiry_error to programmatically identify error messages that indicate a recoverable MCP session expiry, allowing the client to react appropriately.
  • Enhanced Testing: A new mock transport (RetryMockTransport) and a comprehensive test case (test_http_session_error_triggers_reinitialize_and_retry) were added to validate the new session expiry retry logic, ensuring the client can recover from such transient errors.
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.

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. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

The pull request successfully implements a retry mechanism for MCP session expiry errors. The new reinitialize_session and is_session_expiry_error functions are well-defined and modular, enhancing code readability and maintainability. The integration into send_request correctly handles the retry logic, and the initialize function is refactored to leverage the new reinitialization logic. A comprehensive test case, test_http_session_error_triggers_reinitialize_and_retry, has been added, which effectively validates the new retry mechanism using a custom mock transport. Overall, the changes improve the robustness of the MCP client and are well-implemented.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3034c71f5a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tools/mcp/client.rs

@henrypark133 henrypark133 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review: Retry MCP requests after session expiry errors

Clean refactor + new capability. Extracts reinitialize_session() from inline code in ensure_initialized() and wires it into the send_request() retry loop for recoverable 400/missing-session-ID errors.

Positives:

  • reinitialize_session() correctly uses self.transport.send() directly (avoiding recursive retry) and properly sequences: terminate → get_or_create → handshake → mark_initialized → notification
  • is_session_expiry_error() heuristic is conservative (requires both "session" AND one of "400"/"missing session id"/"no valid session id")
  • Only triggers on attempt == 0 with session_manager.is_some() — no infinite loops, no retry on non-HTTP transports
  • RetryMockTransport test helper and test_http_session_error_triggers_reinitialize_and_retry provide good end-to-end coverage of the recovery path
  • No .unwrap() in production code

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: core 20+ merged PRs risk: medium Business logic, config, or moderate-risk modules scope: tool/mcp MCP client size: L 200-499 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(mcp): auto-reinitialize sessions on 400 'Missing session ID' errors

2 participants