Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
harjothkhara wants to merge 1 commit into openai:main
base: main
Choose a base branch
Loading
from harjothkhara:fix-mcp-session

Conversation

Copy link

@harjothkhara harjothkhara commented Aug 7, 2025

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.

hayescode, marrobi, anto2378, and Strangerxxx reacted with hooray emoji
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.
@harjothkhara harjothkhara requested a review from a team as a code owner August 7, 2025 21:08
Copy link

hayescode commented Aug 7, 2025
edited
Loading

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

Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

MCP Server Session Terminates After 1st Interaction

AltStyle によって変換されたページ (->オリジナル) /