Skip to content

fix(mcp): reuse initialized client sessions#2548

Open
lihailong00 wants to merge 1 commit into
MoonshotAI:mainfrom
lihailong00:codex/issue-2031-mcp-reinit
Open

fix(mcp): reuse initialized client sessions#2548
lihailong00 wants to merge 1 commit into
MoonshotAI:mainfrom
lihailong00:codex/issue-2031-mcp-reinit

Conversation

@lihailong00

@lihailong00 lihailong00 commented Jul 23, 2026

Copy link
Copy Markdown

Summary

  • keep each initialized MCP client session open for the toolset lifetime
  • reuse the active session for repeated tool calls
  • close partial and successful connections through an AsyncExitStack

Reproduction and verification

A strict local stdio MCP server rejects a second initialize. Current main initialized once during discovery and again on the first tool call; the regression test failed with Server is already initialized. After the fix, two calls succeed and the initialize counter remains exactly one.

  • related MCP/toolset suite — 62 passed
  • targeted Ruff and Pyright — passed

Closes #2031


Open in Devin Review

@devin-ai-integration devin-ai-integration 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

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.

MCP stdio server fails with 'Server is already initialized' due to fastmcp keep_alive=True

1 participant