feat(server): add server lifecycle events (#175)

Signed-off-by: Edouard Courty <edouard.courty2@gmail.com>
This commit is contained in:
Edouard Courty 2026-02-23 22:40:43 +01:00 • committed by GitHub
commit 158e64627f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 1008 additions and 0 deletions

61
src/Event/ErrorEvent.php Normal file
View file

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Event;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Server\Session\SessionInterface;
/**
* Event dispatched when an error occurs during request processing.
*
* Listeners can modify the error before it's sent to the client.
*
* @author Edouard Courty <edouard.courty2@gmail.com>
*/
final class ErrorEvent
{
public function __construct(
private Error $error,
private readonly Request $request,
private readonly SessionInterface $session,
private readonly ?\Throwable $throwable,
) {
}
public function getError(): Error
{
return $this->error;
}
public function setError(Error $error): void
{
$this->error = $error;
}
public function getRequest(): Request
{
return $this->request;
}
public function getThrowable(): ?\Throwable
{
return $this->throwable;
}
public function getSession(): SessionInterface
{
return $this->session;
}
}

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Event;
use Mcp\Schema\JsonRpc\Notification;
use Mcp\Server\Session\SessionInterface;
/**
* Event dispatched when any notification is received from the client.
*
* Listeners can modify the notification before it's processed by handlers.
*
* @author Edouard Courty <edouard.courty2@gmail.com>
*/
final class NotificationEvent
{
public function __construct(
private Notification $notification,
private readonly SessionInterface $session,
) {
}
public function getNotification(): Notification
{
return $this->notification;
}
public function setNotification(Notification $notification): void
{
$this->notification = $notification;
}
public function getSession(): SessionInterface
{
return $this->session;
}
public function getMethod(): string
{
return $this->notification::getMethod();
}
}

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Event;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Server\Session\SessionInterface;
/**
* Event dispatched when any request is received from the client.
*
* Listeners can modify the request before it's processed by handlers.
*
* @author Edouard Courty <edouard.courty2@gmail.com>
*/
final class RequestEvent
{
public function __construct(
private Request $request,
private readonly SessionInterface $session,
) {
}
public function getRequest(): Request
{
return $this->request;
}
public function setRequest(Request $request): void
{
$this->request = $request;
}
public function getSession(): SessionInterface
{
return $this->session;
}
public function getMethod(): string
{
return $this->request::getMethod();
}
}

View file

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Event;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Server\Session\SessionInterface;
/**
* Event dispatched when a successful response is ready to be sent to the client.
*
* Listeners can modify the response before it's sent.
*
* @author Edouard Courty <edouard.courty2@gmail.com>
*/
final class ResponseEvent
{
/**
* @param Response<mixed> $response
*/
public function __construct(
private Response $response,
private readonly Request $request,
private readonly SessionInterface $session,
) {
}
/**
* @return Response<mixed>
*/
public function getResponse(): Response
{
return $this->response;
}
/**
* @param Response<mixed> $response
*/
public function setResponse(Response $response): void
{
$this->response = $response;
}
public function getRequest(): Request
{
return $this->request;
}
public function getSession(): SessionInterface
{
return $this->session;
}
public function getMethod(): string
{
return $this->request::getMethod();
}
}