Skip to content

Prevent MCP ClientSession hang #580

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

Merged
merged 6 commits into from
Apr 24, 2025
Merged

Prevent MCP ClientSession hang #580

merged 6 commits into from
Apr 24, 2025

Conversation

njbrake
Copy link
Contributor

@njbrake njbrake commented Apr 23, 2025

Per https://modelcontextprotocol.io/specification/draft/basic/lifecycle#timeouts

"Implementations SHOULD establish timeouts for all sent requests, to prevent hung connections and resource exhaustion. When the request has not received a success or error response within the timeout period, the sender SHOULD issue a cancellation notification for that request and stop waiting for a response.

SDKs and other middleware SHOULD allow these timeouts to be configured on a per-request basis."

I picked 5 seconds since that's the default for SSE

Per https://modelcontextprotocol.io/specification/draft/basic/lifecycle#timeouts

"Implementations SHOULD establish timeouts for all sent requests, to prevent hung connections and resource exhaustion. When the request has not received a success or error response within the timeout period, the sender SHOULD issue a cancellation notification for that request and stop waiting for a response.

SDKs and other middleware SHOULD allow these timeouts to be configured on a per-request basis."
@rm-openai
Copy link
Collaborator

@njbrake - thanks. Couple of quick questions:

  1. This is different than the timeout on the SSE server? Any idea why there are two different places for timeout configuration?
  2. Based on the spec - should we make this configurable? Just add client_session_timeout_seconds: int | None = 5 and use that?

@njbrake
Copy link
Contributor Author

njbrake commented Apr 24, 2025

Thanks for the quick response @rm-openai.

@njbrake - thanks. Couple of quick questions:

1. This is different than the timeout on the SSE server? Any idea why there are two different places for timeout configuration?

Based on my understanding of the MCP python sdk, yep this is different because this ClientSession applies to both SSE and Stdio sessions. The current timeout and sse_read_timeout parameters only apply to the sse_client so they're not involved with a Stdio client, resulting in a ♾️ hang if the Stdio server dies.

The parameter that I'm adding to mcp.ClientSession is passed to https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/shared/session.py#L246-L263
which looks like it's the ClientSession equivalent HTTP timeout just like https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/sse.py#L25

2. Based on the spec - should we make this configurable? Just add `client_session_timeout_seconds: int | None = 5` and use that?

Sounds good to me 👍

@njbrake
Copy link
Contributor Author

njbrake commented Apr 24, 2025

@rm-openai Ok gave a shot at implementing the timeout as you suggested, let me know what you think 🙏

@njbrake
Copy link
Contributor Author

njbrake commented Apr 24, 2025

@rm-openai ah sorry, I see some linting etc failing, one sec I'll fix it up

@rm-openai rm-openai merged commit af80e3a into openai:main Apr 24, 2025
5 checks passed
@njbrake njbrake deleted the patch-1 branch April 24, 2025 16:14
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.

2 participants