|
| 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 logging, monitoring, validation, caching, and other custom behaviors. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Setup](#setup) |
| 8 | +- [List Change Events](#list-change-events) |
| 9 | +- [Lifecycle Events](#lifecycle-events) |
| 10 | + - [Tool Events](#tool-events) |
| 11 | + - [Prompt Events](#prompt-events) |
| 12 | + - [Resource Events](#resource-events) |
| 13 | +- [Server Events](#server-events) |
| 14 | +- [Use Cases](#use-cases) |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +Configure an event dispatcher when building your server: |
| 19 | + |
| 20 | +```php |
| 21 | +use Mcp\Server; |
| 22 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 23 | + |
| 24 | +$dispatcher = new EventDispatcher(); |
| 25 | + |
| 26 | +// Register your listeners |
| 27 | +$dispatcher->addListener(CallToolResultEvent::class, function (CallToolResultEvent $event) { |
| 28 | + // Handle event |
| 29 | +}); |
| 30 | + |
| 31 | +$server = Server::builder() |
| 32 | + ->setEventDispatcher($dispatcher) |
| 33 | + ->build(); |
| 34 | +``` |
| 35 | + |
| 36 | +## List Change Events |
| 37 | + |
| 38 | +These events are dispatched when the lists of available capabilities change: |
| 39 | + |
| 40 | +| Event | Description | |
| 41 | +|-------|-------------| |
| 42 | +| `ToolListChangedEvent` | Dispatched when the list of available tools changes | |
| 43 | +| `ResourceListChangedEvent` | Dispatched when the list of available resources changes | |
| 44 | +| `ResourceTemplateListChangedEvent` | Dispatched when the list of available resource templates changes | |
| 45 | +| `PromptListChangedEvent` | Dispatched when the list of available prompts changes | |
| 46 | + |
| 47 | +These events carry no data and are used to notify clients that they should refresh their capability lists. |
| 48 | + |
| 49 | +```php |
| 50 | +use Mcp\Event\ToolListChangedEvent; |
| 51 | + |
| 52 | +$dispatcher->addListener(ToolListChangedEvent::class, function (ToolListChangedEvent $event) { |
| 53 | + $logger->info('Tool list has changed, clients should refresh'); |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +## Lifecycle Events |
| 58 | + |
| 59 | +### Tool Events |
| 60 | + |
| 61 | +Events for the tool call lifecycle: |
| 62 | + |
| 63 | +| Event | Timing | Data | |
| 64 | +|-------|----------------------------|------| |
| 65 | +| `CallToolRequestEvent` | Before tool execution | `request` | |
| 66 | +| `CallToolResultEvent` | After successful execution | `request`, `result` | |
| 67 | +| `CallToolExceptionEvent` | On uncaught exception | `request`, `throwable` | |
| 68 | + |
| 69 | +### Prompt Events |
| 70 | + |
| 71 | +Events for the prompt retrieval lifecycle: |
| 72 | + |
| 73 | +| Event | Timing | Data | |
| 74 | +|-------|----------------------------|------| |
| 75 | +| `GetPromptRequestEvent` | Before prompt execution | `request` | |
| 76 | +| `GetPromptResultEvent` | After successful execution | `request`, `result` | |
| 77 | +| `GetPromptExceptionEvent` | On uncaught exception | `request`, `throwable` | |
| 78 | + |
| 79 | +### Resource Events |
| 80 | + |
| 81 | +Events for the resource read lifecycle: |
| 82 | + |
| 83 | +| Event | Timing | Data | |
| 84 | +|-------|-----------------------|------| |
| 85 | +| `ReadResourceRequestEvent` | Before resource read | `request` | |
| 86 | +| `ReadResourceResultEvent` | After successful read | `request`, `result` | |
| 87 | +| `ReadResourceExceptionEvent` | On uncaught exception | `request`, `throwable` | |
| 88 | + |
| 89 | +## Server Events |
| 90 | + |
| 91 | +| Event | Timing | Data | |
| 92 | +|-------|--------|------| |
| 93 | +| `InitializeRequestEvent` | When client sends initialize request | `request` (InitializeRequest) | |
| 94 | +| `PingRequestEvent` | When client sends ping request | `request` (PingRequest) | |
0 commit comments