|
| 1 | +# Events |
| 2 | + |
| 3 | +The MCP SDK provides a PSR-14 compatible event system that allows you to hook into the server's lifecycle. Events enable request/response modification, and other user-defined behaviors. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Setup](#setup) |
| 8 | +- [Protocol Events](#protocol-events) |
| 9 | + - [RequestEvent](#requestevent) |
| 10 | + - [ResponseEvent](#responseevent) |
| 11 | + - [ErrorEvent](#errorevent) |
| 12 | + - [NotificationEvent](#notificationevent) |
| 13 | +- [List Change Events](#list-change-events) |
| 14 | + |
| 15 | +## Setup |
| 16 | + |
| 17 | +Configure an event dispatcher when building your server: |
| 18 | + |
| 19 | +```php |
| 20 | +use Mcp\Server; |
| 21 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 22 | + |
| 23 | +$dispatcher = new EventDispatcher(); |
| 24 | + |
| 25 | +// Register your listeners |
| 26 | +$dispatcher->addListener(RequestEvent::class, function (RequestEvent $event) { |
| 27 | + // Handle any incoming request |
| 28 | + if ($event->getMethod() === 'tools/call') { |
| 29 | + // Handle tool call requests specifically |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +$server = Server::builder() |
| 34 | + ->setEventDispatcher($dispatcher) |
| 35 | + ->build(); |
| 36 | +``` |
| 37 | + |
| 38 | +## Protocol Events |
| 39 | + |
| 40 | +The SDK dispatches 4 broad event types at the protocol level, allowing you to observe and modify all server operations: |
| 41 | + |
| 42 | +### RequestEvent |
| 43 | + |
| 44 | +**Dispatched**: When any request is received from the client, before it's processed by handlers. |
| 45 | + |
| 46 | +**Properties**: |
| 47 | +- `getRequest(): Request` - The incoming request |
| 48 | +- `setRequest(Request $request): void` - Modify the request before processing |
| 49 | +- `getSession(): SessionInterface` - The current session |
| 50 | +- `getMethod(): string` - Convenience method to get the request method |
| 51 | + |
| 52 | +### ResponseEvent |
| 53 | + |
| 54 | +**Dispatched**: When a successful response is ready to be sent to the client, after handler execution. |
| 55 | + |
| 56 | +**Properties**: |
| 57 | +- `getResponse(): Response` - The response being sent |
| 58 | +- `setResponse(Response $response): void` - Modify the response before sending |
| 59 | +- `getRequest(): Request` - The original request |
| 60 | +- `getSession(): SessionInterface` - The current session |
| 61 | +- `getMethod(): string` - Convenience method to get the request method |
| 62 | + |
| 63 | +### ErrorEvent |
| 64 | + |
| 65 | +**Dispatched**: When an error occurs during request processing. |
| 66 | + |
| 67 | +**Properties**: |
| 68 | +- `getError(): Error` - The error being sent |
| 69 | +- `setError(Error $error): void` - Modify the error before sending |
| 70 | +- `getRequest(): Request` - The original request (null for parse errors) |
| 71 | +- `getThrowable(): ?\Throwable` - The exception that caused the error (if any) |
| 72 | +- `getSession(): SessionInterface` - The current session |
| 73 | + |
| 74 | +### NotificationEvent |
| 75 | + |
| 76 | +**Dispatched**: When a notification is received from the client, before it's processed by handlers. |
| 77 | + |
| 78 | +**Properties**: |
| 79 | +- `getNotification(): Notification` - The incoming notification |
| 80 | +- `setNotification(Notification $notification): void` - Modify the notification before processing |
| 81 | +- `getSession(): SessionInterface` - The current session |
| 82 | +- `getMethod(): string` - Convenience method to get the notification method |
| 83 | + |
| 84 | +## List Change Events |
| 85 | + |
| 86 | +These events are dispatched when the lists of available capabilities change: |
| 87 | + |
| 88 | +| Event | Description | |
| 89 | +|------------------------------------|------------------------------------------------------------------| |
| 90 | +| `ToolListChangedEvent` | Dispatched when the list of available tools changes | |
| 91 | +| `ResourceListChangedEvent` | Dispatched when the list of available resources changes | |
| 92 | +| `ResourceTemplateListChangedEvent` | Dispatched when the list of available resource templates changes | |
| 93 | +| `PromptListChangedEvent` | Dispatched when the list of available prompts changes | |
| 94 | + |
| 95 | +These events carry no data and are used to notify clients that they should refresh their capability lists. |
| 96 | + |
| 97 | +```php |
| 98 | +use Mcp\Event\ToolListChangedEvent; |
| 99 | + |
| 100 | +$dispatcher->addListener(ToolListChangedEvent::class, function (ToolListChangedEvent $event) { |
| 101 | + $logger->info('Tool list has changed, clients should refresh'); |
| 102 | +}); |
| 103 | +``` |
0 commit comments