modelcontextprotocol / modelcontextprotocol/python-sdk
Simplify and harden session lifecycle with an explicit state machine
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
Description
Summary
Server and client sessions use an internal initialization state machine combined with various flags and manual AsyncExitStack management. Stateless mode complicates this further by setting an "Initialized" state early as a workaround.
This makes the lifecycle hard to reason about and contributes to subtle bugs (e.g., past issues like #756 in stateless mode).
Problems
- Scattered state management: Initialization and teardown logic are spread across methods and files.
- Special cases for stateless mode: Stateless HTTP sets the session as initialized even though no real protocol negotiation has occurred.
- Resource management risk: If initialization fails midway, transport tasks and resources may not be cleaned up reliably.
- Testing difficulty: Tests often bypass parts of the lifecycle using in-memory transports.
Proposal
-
Introduce an explicit session state machine
Represent distinct states as separate types, for example:
UninitializedSessionNegotiatingSessionActiveSessionClosedSessionStatelessSession(for per-request / ephemeral cases)
Each state exposes only the operations that are valid in that state, and transitions return the next state type.
-
Model stateless mode explicitly
- Instead of marking a stateful session as "Initialized" prematurely, model stateless HTTP as a dedicated
StatelessSessiontype with a simpler lifecycle. - Make behavior differences clear in code and docs.
- Instead of marking a stateful session as "Initialized" prematurely, model stateless HTTP as a dedicated
-
Centralize resource cleanup
- Ensure that all lifecycle paths (happy path, error, cancellation) lead through code that:
- cancels outstanding tasks,
- closes transports,
- releases resources stored in
AsyncExitStack.
- Ensure that all lifecycle paths (happy path, error, cancellation) lead through code that:
Why this matters
- Reliability: Fewer edge cases and surprise states where messages can be processed incorrectly.
- Debuggability: Easier to reason about what can happen in each state.
- Extensibility: Adding new lifecycle behavior (e.g., resuming sessions) becomes more manageable.
Acceptance criteria
- Session lifecycle is represented via explicit types or a clearly defined state machine.
- Stateless HTTP mode uses a dedicated path rather than setting "Initialized" as a hack.
- All entry/exit paths of sessions ensure proper cleanup of transports and tasks.
- Tests cover state transitions, including error cases and stateless/stat eful differences.
References
No response
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
未指定檔案或進入點;先尋找現有的工作階段初始化、無狀態 HTTP 路徑和 AsyncExitStack 清理。查看 issue #756 以及 issue 中描述的記憶體內傳輸測試。完成的標準是:具備明確的生命週期狀態、專用的無狀態路徑、在每個退出路徑上都能可靠清理,以及針對錯誤和有狀態/無狀態情況的狀態轉換測試。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- api, backend
- Issue 類型
- 重構
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 25/100