modelcontextprotocol / modelcontextprotocol/python-sdk

Simplify and harden session lifecycle with an explicit state machine

オープン
#1,691 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

enhancement P2 v2
主要言語
Python
スター
24.3k
フォーク
4k
平均マージ
1日 1時間
マージ済み PR(30日)
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

  1. Introduce an explicit session state machine

    Represent distinct states as separate types, for example:

    • UninitializedSession
    • NegotiatingSession
    • ActiveSession
    • ClosedSession
    • StatelessSession (for per-request / ephemeral cases)

    Each state exposes only the operations that are valid in that state, and transitions return the next state type.

  2. Model stateless mode explicitly

    • Instead of marking a stateful session as "Initialized" prematurely, model stateless HTTP as a dedicated StatelessSession type with a simpler lifecycle.
    • Make behavior differences clear in code and docs.
  3. 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.

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ファイルやエントリーポイントは指定されていないため、まず既存のセッション初期化、ステートレス HTTP パス、AsyncExitStack のクリーンアップを見つけてください。issue #756 と、issue で説明されているインメモリトランスポートのテストを確認してください。完了の条件は、ライフサイクル状態が明示されていること、専用のステートレスパスがあること、すべての終了パスで確実にクリーンアップされること、そしてエラーおよび stateful/stateless のケースに対する遷移テストがあることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api, backend
issue の種類
リファクタリング
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。