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\CancelledNotificationParams;
19:
20: /**
21: * This notification can be sent by either side to indicate that it is cancelling a previously-issued request.
22: *
23: * The request SHOULD still be in-flight, but due to communication latency, it is always possible that this
24: * notification MAY arrive after the request has already finished.
25: *
26: * This notification indicates that the result will be unused, so any associated processing SHOULD cease.
27: *
28: * A client MUST NOT attempt to cancel its `initialize` request.
29: *
30: * For task cancellation, use the `tasks/cancel` request instead of this notification.
31: *
32: * @extends JsonRpcNotification<'notifications/cancelled'>
33: *
34: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#cancellednotification
35: */
36: final readonly class CancelledNotification extends JsonRpcNotification implements ClientNotification, ServerNotification
37: {
38: public function __construct(CancelledNotificationParams $params)
39: {
40: parent::__construct($params);
41: }
42:
43: #[\Override]
44: public static function getMethod(): string
45: {
46: return 'notifications/cancelled';
47: }
48:
49: #[\Override]
50: public static function fromArray(array $data): static
51: {
52: Assert::that($data)->hasOffset('params', 'missing the required "params" key.');
53: Assert::that($data['params'])
54: ->isArray('"params" must be an object, {type} given.')
55: ->isMap('"params" must be a string-keyed object.')
56: ;
57:
58: return new self(CancelledNotificationParams::fromArray($data['params']));
59: }
60:
61: #[\Override]
62: public function jsonSerialize(): array
63: {
64: $params = $this->params->jsonSerialize();
65:
66: return [
67: 'jsonrpc' => self::JSONRPC_VERSION,
68: 'method' => static::getMethod(),
69: 'params' => [] === $params ? new \stdClass() : $params,
70: ];
71: }
72: }
73: