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;
15:
16: use Nexus\Mcp\Arrayable;
17:
18: /**
19: * A JSON-RPC message.
20: */
21: abstract readonly class Message implements \JsonSerializable, Arrayable
22: {
23: final public const string JSON_RPC_VERSION = '2.0';
24:
25: /**
26: * @var non-empty-string
27: */
28: public string $jsonrpc;
29:
30: public function __construct(string $jsonrpc)
31: {
32: if ('' === $jsonrpc) {
33: throw new \InvalidArgumentException('JSON-RPC version cannot be empty.');
34: }
35:
36: if (self::JSON_RPC_VERSION !== $jsonrpc) {
37: throw new \InvalidArgumentException('Invalid JSON-RPC version.');
38: }
39:
40: $this->jsonrpc = $jsonrpc;
41: }
42:
43: /**
44: * @return array<string, mixed>
45: */
46: #[\Override]
47: public function jsonSerialize(): array
48: {
49: return $this->toArray();
50: }
51: }
52: