Skip to content

fix: preserve MCP session across tool calls #2526

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

harjothkhara
Copy link

When using remote MCP servers, closing the HTTP connection after listing tools causes the server to terminate the session. This PR prevents the SDK from terminating the MCP session after the first interaction. It updates ResponseStreamManager.__exit__ and AsyncResponseStreamManager.__aexit__ so they only close the stream when no MCP tools are present. This preserves the session for subsequent tool calls.

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

  • Skip closing the stream in __exit__ and __aexit__ when any input tool has type == "mcp", ensuring MCP session persists across tool calls.

Additional context & links

Fixes #2513.
See related discussion on the GitHub MCP server issue #702.

When using remote MCP servers, closing the HTTP connection after listing tools causes the server to terminate the session. This change updates ResponseStreamManager.__exit__ and AsyncResponseStreamManager.__aexit__ so they only close the stream when no MCP tools are present. This preserves the session for subsequent tool calls.
@hayescode
Copy link

hayescode commented Aug 7, 2025

THANK YOU @harjothkhara !!! This issue has blocked up from using the GitHub MCP Server. I'm getting this issue for both Sync and Async OpenAI clients.

#2513

github/github-mcp-server#702

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 Server Session Terminates After 1st Interaction
2 participants