-
Notifications
You must be signed in to change notification settings - Fork 103
feat(server): add server lifecycle events #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EdouardCourty
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
EdouardCourty:feat/add-event-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,098
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Events | ||
|
|
||
| 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. | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| - [Setup](#setup) | ||
| - [List Change Events](#list-change-events) | ||
| - [Lifecycle Events](#lifecycle-events) | ||
| - [Tool Events](#tool-events) | ||
| - [Prompt Events](#prompt-events) | ||
| - [Resource Events](#resource-events) | ||
| - [Server Events](#server-events) | ||
| - [Use Cases](#use-cases) | ||
|
|
||
| ## Setup | ||
|
|
||
| Configure an event dispatcher when building your server: | ||
|
|
||
| ```php | ||
| use Mcp\Server; | ||
| use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
|
||
| $dispatcher = new EventDispatcher(); | ||
|
|
||
| // Register your listeners | ||
| $dispatcher->addListener(CallToolResultEvent::class, function (CallToolResultEvent $event) { | ||
| // Handle event | ||
| }); | ||
|
|
||
| $server = Server::builder() | ||
| ->setEventDispatcher($dispatcher) | ||
| ->build(); | ||
| ``` | ||
|
|
||
| ## List Change Events | ||
|
|
||
| These events are dispatched when the lists of available capabilities change: | ||
|
|
||
| | Event | Description | | ||
| |-------|-------------| | ||
| | `ToolListChangedEvent` | Dispatched when the list of available tools changes | | ||
| | `ResourceListChangedEvent` | Dispatched when the list of available resources changes | | ||
| | `ResourceTemplateListChangedEvent` | Dispatched when the list of available resource templates changes | | ||
| | `PromptListChangedEvent` | Dispatched when the list of available prompts changes | | ||
|
|
||
| These events carry no data and are used to notify clients that they should refresh their capability lists. | ||
|
|
||
| ```php | ||
| use Mcp\Event\ToolListChangedEvent; | ||
|
|
||
| $dispatcher->addListener(ToolListChangedEvent::class, function (ToolListChangedEvent $event) { | ||
| $logger->info('Tool list has changed, clients should refresh'); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Lifecycle Events | ||
|
|
||
| ### Tool Events | ||
|
|
||
| Events for the tool call lifecycle: | ||
|
|
||
| | Event | Timing | Data | | ||
| |-------|----------------------------|------| | ||
| | `CallToolRequestEvent` | Before tool execution | `request` | | ||
| | `CallToolResultEvent` | After successful execution | `request`, `result` | | ||
| | `CallToolExceptionEvent` | On uncaught exception | `request`, `throwable` | | ||
|
|
||
| ### Prompt Events | ||
|
|
||
| Events for the prompt retrieval lifecycle: | ||
|
|
||
| | Event | Timing | Data | | ||
| |-------|----------------------------|------| | ||
| | `GetPromptRequestEvent` | Before prompt execution | `request` | | ||
| | `GetPromptResultEvent` | After successful execution | `request`, `result` | | ||
| | `GetPromptExceptionEvent` | On uncaught exception | `request`, `throwable` | | ||
|
|
||
| ### Resource Events | ||
|
|
||
| Events for the resource read lifecycle: | ||
|
|
||
| | Event | Timing | Data | | ||
| |-------|-----------------------|------| | ||
| | `ReadResourceRequestEvent` | Before resource read | `request` | | ||
| | `ReadResourceResultEvent` | After successful read | `request`, `result` | | ||
| | `ReadResourceExceptionEvent` | On uncaught exception | `request`, `throwable` | | ||
|
|
||
| ## Server Events | ||
|
|
||
| | Event | Timing | Data | | ||
| |-------|--------|------| | ||
| | `InitializeRequestEvent` | When client sends initialize request | `request` (InitializeRequest) | | ||
| | `PingRequestEvent` | When client sends ping request | `request` (PingRequest) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?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\Request\InitializeRequest; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class InitializeRequestEvent | ||
| { | ||
| public function __construct( | ||
| private readonly InitializeRequest $request, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRequest(): InitializeRequest | ||
| { | ||
| return $this->request; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?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\Request\PingRequest; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class PingRequestEvent | ||
| { | ||
| public function __construct( | ||
| private readonly PingRequest $request, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRequest(): PingRequest | ||
| { | ||
| return $this->request; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?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\Prompt; | ||
|
|
||
| use Mcp\Schema\Request\GetPromptRequest; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| abstract class AbstractGetPromptEvent | ||
| { | ||
| public function __construct( | ||
| private readonly GetPromptRequest $request, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRequest(): GetPromptRequest | ||
| { | ||
| return $this->request; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?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\Prompt; | ||
|
|
||
| use Mcp\Schema\Request\GetPromptRequest; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class GetPromptExceptionEvent extends AbstractGetPromptEvent | ||
| { | ||
| public function __construct( | ||
| GetPromptRequest $request, | ||
| private readonly \Throwable $throwable, | ||
| ) { | ||
| parent::__construct($request); | ||
| } | ||
|
|
||
| public function getThrowable(): \Throwable | ||
| { | ||
| return $this->throwable; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?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\Prompt; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class GetPromptRequestEvent extends AbstractGetPromptEvent | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?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\Prompt; | ||
|
|
||
| use Mcp\Schema\Request\GetPromptRequest; | ||
| use Mcp\Schema\Result\GetPromptResult; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class GetPromptResultEvent extends AbstractGetPromptEvent | ||
| { | ||
| public function __construct( | ||
| GetPromptRequest $request, | ||
| private readonly GetPromptResult $result, | ||
| ) { | ||
| parent::__construct($request); | ||
| } | ||
|
|
||
| public function getResult(): GetPromptResult | ||
| { | ||
| return $this->result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?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\Resource; | ||
|
|
||
| use Mcp\Schema\Request\ReadResourceRequest; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| abstract class AbstractReadResourceEvent | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this class |
||
| { | ||
| public function __construct( | ||
| private readonly ReadResourceRequest $request, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRequest(): ReadResourceRequest | ||
| { | ||
| return $this->request; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?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\Resource; | ||
|
|
||
| use Mcp\Schema\Request\ReadResourceRequest; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class ReadResourceExceptionEvent extends AbstractReadResourceEvent | ||
| { | ||
| public function __construct( | ||
| ReadResourceRequest $request, | ||
| private readonly \Throwable $throwable, | ||
| ) { | ||
| parent::__construct($request); | ||
| } | ||
|
|
||
| public function getThrowable(): \Throwable | ||
| { | ||
| return $this->throwable; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?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\Resource; | ||
|
|
||
| /** | ||
| * @author Edouard Courty <edouard.courty2@gmail.com> | ||
| */ | ||
| class ReadResourceRequestEvent extends AbstractReadResourceEvent | ||
| { | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this class