1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Message\Notification;
15:
16: use Nexus\Mcp\Client\Message\Notification\ClientNotification;
17: use Nexus\Mcp\Message\Notification;
18: use Nexus\Mcp\Message\RequestId;
19: use Nexus\Mcp\Server\Message\Notification\ServerNotification;
20:
21: /**
22: * This notification can be sent by either side to indicate that it is cancelling a previously-issued request.
23: *
24: * The request SHOULD still be in-flight, but due to communication latency, it is always possible that this
25: * notification MAY arrive after the request has already finished.
26: *
27: * This notification indicates that the result will be unused, so any associated processing SHOULD cease.
28: *
29: * A client MUST NOT attempt to cancel its `initialize` request.
30: */
31: final readonly class CancelledNotification extends Notification implements ClientNotification, ServerNotification
32: {
33: /**
34: * @param non-empty-string $jsonrpc JSON-RPC version. Must be 2.0.
35: * @param RequestId $requestId The ID of the request to cancel.
36: * This MUST correspond to the ID of a request previously issued in the same direction.
37: * @param null|non-empty-string $reason An optional string describing the reason for the cancellation.
38: * This MAY be logged or presented to the user.
39: */
40: public function __construct(
41: string $jsonrpc,
42: public RequestId $requestId,
43: public ?string $reason = null,
44: ) {
45: parent::__construct($jsonrpc, 'notifications/cancelled', array_filter([
46: 'requestId' => $requestId->id,
47: 'reason' => $reason,
48: ], static fn(mixed $value): bool => null !== $value));
49: }
50: }
51: