modelcontextprotocol / modelcontextprotocol/php-sdk

Add server lifecycle events (ServerStarted / ServerStopped)

Đang mở
#286 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

enhancement needs confirmation
Ngôn ngữ chính
PHP
Star
1.6k
Fork
173
Merge trung bình
2 ngày 49 phút
Pull request đã merge (30 ngày)
23

Mô tả

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 + matching ResponseEvent/ErrorEvent and deal with per-request overhead, fiber suspension, and every error path.

Proposal

Add two events in Mcp\Event:

  • ServerStartedEvent — dispatched at the top of Server::run($transport), before the first request is read. Exposes the server and transport.
  • ServerStoppedEvent — dispatched just before Server::run() returns, in a finally so 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 RequestEvent as a pseudo-start hook — works but is per-request. Adds overhead to what is logically a one-shot, and requires pairing with ResponseEvent/ErrorEvent for 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 instantiate Server directly via the Builder.
  • 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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu tại Server::run($transport), sau đó kiểm tra Protocol::handleRequest và các entry point của transport được Builder tạo ra. Xem xét các Events hiện có và các bài kiểm thử dispatching của chúng, nếu có. Được xem là hoàn tất khi ServerStartedEvent được phát một lần trước request đầu tiên và ServerStoppedEvent được phát từ finally khi thoát bình thường, khi có exception và khi transport đóng, mang theo context đã chỉ định.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
api, backend
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.