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\JsonRpc;
15:
16: use Nexus\Mcp\Core\Schema\Arrayable;
17: use Nexus\Mcp\Core\Schema\Notification;
18: use Nexus\Mcp\Core\Schema\NotificationParams;
19:
20: /**
21: * A notification which does not expect a response.
22: *
23: * @template-covariant TMethod of non-empty-string
24: *
25: * @extends Notification<TMethod>
26: * @implements Arrayable<array{
27: * jsonrpc: '2.0',
28: * method: non-empty-string,
29: * params?: template-type<NotificationParams, Arrayable, 'T'>,
30: * }>
31: *
32: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#jsonrpcnotification
33: */
34: abstract readonly class JsonRpcNotification extends Notification implements Arrayable, JsonRpcMessage
35: {
36: /**
37: * @param array<string, mixed> $data
38: */
39: #[\Override]
40: abstract public static function fromArray(array $data): static;
41:
42: #[\Override]
43: public function toArray(): array
44: {
45: $envelope = [
46: 'jsonrpc' => self::JSONRPC_VERSION,
47: 'method' => static::getMethod(),
48: ];
49:
50: $params = $this->params->toArray();
51:
52: if ([] !== $params) {
53: $envelope['params'] = $params;
54: }
55:
56: return $envelope;
57: }
58:
59: #[\Override]
60: public function jsonSerialize(): array
61: {
62: $envelope = [
63: 'jsonrpc' => self::JSONRPC_VERSION,
64: 'method' => static::getMethod(),
65: ];
66:
67: $params = $this->params->jsonSerialize();
68:
69: if ([] !== $params) {
70: $envelope['params'] = $params;
71: }
72:
73: return $envelope;
74: }
75: }
76: