Add server lifecycle events (ServerStarted / ServerStopped)
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 52/100
Línea de trabajo
Comienza en Server::run($transport), luego inspecciona Protocol::handleRequest y los puntos de entrada del transporte creados por Builder. Revisa los Events existentes y sus pruebas de dispatching, si las hay. Se considera terminado cuando ServerStartedEvent se emite una vez antes de la primera solicitud y ServerStoppedEvent se emite desde finally al salir limpiamente, ante excepciones y al cerrarse el transporte, llevando el contexto especificado.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- PHP
- Estrellas
- 1.6k
- Forks
- 173
- Merge medio
- 2 d 49 min
- PR fusionados (30 d)
- 23
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de modelcontextprotocol/php-sdk
-
[Server] Handler type uses bare Closure, hard to decorate RegistryInterface under strict PHPStan AbiertoServer
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
modelcontextprotocol/php-sdk#468 · 2 comentarios ·
-
needs confirmation needs maintainer action Server
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
modelcontextprotocol/php-sdk#398 · 1 reacción ·
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
modelcontextprotocol/php-sdk#370 ·
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
modelcontextprotocol/php-sdk#510 · 1 comentario ·
-
bug
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
modelcontextprotocol/php-sdk#504 ·
Todos los issues de modelcontextprotocol/php-sdk
Issues similares
-
Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Automattic/safe-publish#594 ·
-
HttpClient
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
symfony/symfony-docs#23092 ·
-
sync-en
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
-
sync-en
Dificultad 1/5 Menos de una hora Aptitud para principiantes 95/100
-
Перевод устарел
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100