1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Core\Schema\Notification;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcNotification;
18: use Nexus\Mcp\Core\Schema\NotificationParams;
19: use Nexus\Mcp\Core\Schema\NotificationParams\CancelledNotificationParams;
20:
21: /**
22: * This notification is sent by the client to indicate that it is cancelling a request it previously issued.
23: *
24: * On stdio, the server also sends this notification, solely to terminate a `subscriptions/listen` stream: it
25: * references the ID of the `subscriptions/listen` request that opened the stream. Servers MUST NOT use this
26: * notification to cancel any other request.
27: *
28: * The request SHOULD still be in-flight, but due to communication latency, it is always possible that this
29: * notification MAY arrive after the request has already finished.
30: *
31: * This notification indicates that the result will be unused, so any associated processing SHOULD cease.
32: *
33: * @property-read CancelledNotificationParams $params
34: *
35: * @extends JsonRpcNotification<'notifications/cancelled', array{
36: * jsonrpc: '2.0',
37: * method: 'notifications/cancelled',
38: * params: template-type<CancelledNotificationParams, NotificationParams, 'T'>,
39: * }>
40: *
41: * @see https://modelcontextprotocol.io/specification/draft/schema#cancellednotification
42: */
43: final readonly class CancelledNotification extends JsonRpcNotification implements ClientNotification, ServerNotification
44: {
45: public function __construct(CancelledNotificationParams $params)
46: {
47: parent::__construct(params: $params);
48: }
49:
50: #[\Override]
51: public static function getMethod(): string
52: {
53: return 'notifications/cancelled';
54: }
55:
56: #[\Override]
57: public static function fromArray(array $data): static
58: {
59: Assert::that($data)->hasOffset('params', 'missing the required "params" key.');
60: Assert::that($data['params'])
61: ->isArray('"params" must be an object, {type} given.')
62: ->isMap('"params" must be a string-keyed object.')
63: ;
64:
65: return new self(params: CancelledNotificationParams::fromArray($data['params']));
66: }
67:
68: #[\Override]
69: public function toArray(): array
70: {
71: return [
72: 'jsonrpc' => self::JSONRPC_VERSION,
73: 'method' => static::getMethod(),
74: 'params' => $this->params->toArray(),
75: ];
76: }
77: }
78: