-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conversation
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.
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.
kunal10542
commented
Aug 22, 2025
I am facing the same issue with a .net core mcp server. see below. can this fix be applied quickly
{
"model": "gpt-5",
"stream": false,
"tool_choice": "auto",
"parallel_tool_calls": false,
"background":false,
"input": [
{ "role": "user", "content": "Run the test
tool and return ONLY the raw result." }
],
"tools": [
{
"type": "mcp",
"server_label": "gptflex-server",
"server_url": "https://www.xxx.io/mcp",
"require_approval": "never",
"allowed_tools": ["test"]
}
]
}
"content": [
{
"type": "output_text",
"annotations": [],
"logprobs": [],
"text": "{"code": 32600, "message": "Session terminated"}"
}
],
"role": "assistant"
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__
andAsyncResponseStreamManager.__aexit__
so they only close the stream when no MCP tools are present. This preserves the session for subsequent tool calls.Changes being requested
__exit__
and__aexit__
when any input tool hastype == "mcp"
, ensuring MCP session persists across tool calls.Additional context & links
Fixes #2513.
See related discussion on the GitHub MCP server issue #702.