Title: Add idle timeout termination for StreamableHTTPServerTransport by hopeful0 · Pull Request #1159 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Add idle timeout termination for StreamableHTTPServerTransport by hopeful0 · Pull Request #1159 · modelcontextprotocol/python-sdk
X Title: Add idle timeout termination for StreamableHTTPServerTransport by hopeful0 · Pull Request #1159 · modelcontextprotocol/python-sdk
Description: 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: Moved the transport termination after handle stateless requests to the finally block Called the terminate function directly after the transport connect finished Removed the condition of the transport is not terminated in the finally block of the run_server in handle_stateful_request, because that instances should be cleared even if transport terminates first The manager returns 404 instead of 400 when it receives a non-existent session ID, because this might be due to the server actively terminating the transport Motivation 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 transport connect 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, the handle_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 Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/1159
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:49c9dae4-5a2a-9e71-da65-cb5efa588630 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 987C:183565:21E5FC:2B5D38:6A5AB6B0 |
| html-safe-nonce | 8f95038968c2af621c2b4c8293917936b2a2209a9ec52d40e6a837fe2f15285e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODdDOjE4MzU2NToyMUU1RkM6MkI1RDM4OjZBNUFCNkIwIiwidmlzaXRvcl9pZCI6IjM1NDI0MjU3MjgyOTcxMjE0NTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 15b5282444c7da616e54e57996090ac059c47db6c937394e21588b76b6ea94a9 |
| hovercard-subject-tag | pull_request:2671246502 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/1159/files |
| twitter:image | https://avatars.githubusercontent.com/u/13890191?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/13890191?s=400&v=4 |
| og:image:alt | 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 ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2702febe50e53f9152dda552dc2049a5eb182778b2655abd5b8619e82e899b4a |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c0211a077f9b056c5e115f996d9049616d85c3b5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width