Add server lifecycle events (ServerStarted / ServerStopped)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Server::run($transport) から始め、次に Protocol::handleRequest と Builder によって作成された transport のエントリーポイントを確認します。既存の Events と、それらの dispatching のテストがあれば確認します。ServerStartedEvent が最初のリクエストの前に一度だけ発火し、ServerStoppedEvent が正常終了時、例外発生時、transport のクローズ時に finally から発火し、指定されたコンテキストを保持することが完了条件です。
索引モデルが issue の本文から書いたものです。
説明
Problem
The SDK currently dispatches only request-scoped events (RequestEvent, ResponseEvent, ErrorEvent, list-changed events). There is no hook that fires once per Server::run() invocation.
Consumers that need one-time setup/teardown per server boot must either:
- Duplicate the logic in every transport's entry point (HTTP controller, CLI command, custom transports), or
- Subscribe to
RequestEvent+ matchingResponseEvent/ErrorEventand deal with per-request overhead, fiber suspension, and every error path.
Proposal
Add two events in Mcp\Event:
ServerStartedEvent— dispatched at the top ofServer::run($transport), before the first request is read. Exposes the server and transport.ServerStoppedEvent— dispatched just beforeServer::run()returns, in afinallyso it fires on clean exit, thrown exceptions, and transport close. Exposes the exit code and any captured throwable.
Use cases
- Identity / impersonation: switch the current user of a host framework (Drupal, Symfony Security) once when the server boots. This is our concrete motivation — without a lifecycle hook we either patch every transport or pay the cost of switching on every
RequestEvent. - Metrics / observability: increment
mcp_server_started_total, start a run-duration timer, emit a "server up" log line including registered tool/prompt/resource counts. - Resource management: warm caches, acquire leases, open long-lived connections at start; release them at stop.
Alternatives considered
- Use
RequestEventas a pseudo-start hook — works but is per-request. Adds overhead to what is logically a one-shot, and requires pairing withResponseEvent/ErrorEventfor cleanup. Fiber suspension (Protocol::handleRequest, early return on$fiber->isSuspended()) means the terminal event can be delayed or skipped from the subscriber's perspective. - Subclassing
Server— not portable; transports and framework integrations instantiateServerdirectly via theBuilder. - Transport-level hooks — would require a change in every transport implementation (Stdio, StreamableHttp, any custom transport) rather than in one place in
Server::run().
Backward compatibility
Purely additive. Subscribers that don't care about the new events are unaffected. No public API changes to existing events or handlers.
- 主要言語
- PHP
- スター
- 1.7k
- フォーク
- 206
- 平均マージ
- 1日 17時間
- マージ済み PR(30日)
- 102
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/php-sdk のほかの issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 85/100
modelcontextprotocol/php-sdk#746 ·
メンテナーはふだん 1 日以内に返信
-
[Server] Handler type uses bare Closure, hard to decorate RegistryInterface under strict PHPStanオープンServer
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
modelcontextprotocol/php-sdk#468 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement P3
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
modelcontextprotocol/php-sdk#733 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
[Server] Handle notifications/cancelled on the handshake era対応中かも @ump45nose が 6 日前に担当しました。 オープンhelp wanted improves spec compliance Server
難易度 4/5 3〜5日 初心者へのやさしさ 57/100
modelcontextprotocol/php-sdk#732 ·
メンテナーはふだん 1 日以内に返信
-
[Client][Server] Ship extension classes for the two authorization extensions対応中かも @chr-hertel が 7 日前に担当しました。 オープンauth Client Server
modelcontextprotocol/php-sdk#721 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/php-sdk の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
TYPO3-Solr/ext-solr#4850 ·
メンテナーはふだん 2 日以内に返信
-
Hex viewer drops ASCII '0' after a complete row対応中かも @palashranka が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 3 日以内に返信
-
[Sync EN] stream: document the errors of the stream_context_*() and stream filter functions (#6091)オープンsync-en
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
[Sync EN] stream: document the errors of the stream_context_*() and stream filter functions (#6091)オープンsync-en
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 1 日以内に返信
-
Перевод устарел
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信