Add idle timeout termination for StreamableHTTPServerTransport #1159
+150
−25
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
By adding an optional idle timeout termination to the StreamableHTTPServerTransport, this PR allows the stateful server to close long-idle sessions to avoid memory leak issues when the client does not terminate the session as expected
These changes comply with the specifications for Streamable HTTP transports session management in the MCP protocol.
Meanwhile, optimized the logic for transport termination and session management without affecting existing functionality, including:
finally
blockfinally
block of the run_server inhandle_stateful_request
, because that instances should be cleared even if transport terminates firstMotivation and Context
#1116 addressed the memory leak issue caused by session not terminating in stateless requests, but in stateful requests, if the client does not close the session as expected, the session on the server will never be closed, leading to a similar memory leak issue.
This PR adds an optional idle timeout parameter to StreamableHTTPServerTransport, a context manager for tracking idle status, and an
_idle_timeout_terminate
method. When the idle timeout parameter is set, an_idle_timeout_terminate
task is initiated when transportconnect
is called. Once the transport does not receive or hanle any requests within the timeout period, the transport will be automatically terminated. To ensure that correctly tracks the idle status of transport, thehandle_request
method of the transport needs to be called within the context manager.How Has This Been Tested?
I have added a test to verify that the transfer and session will exist before the idle timeout and will be terminated after the idle timeout.
Breaking Changes
Types of changes
Checklist
Additional context