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\Schema\Message;
15:
16: use Nexus\Mcp\Schema\Request\Request;
17:
18: /**
19: * A request that expects a response.
20: *
21: * @template T of array{
22: * jsonrpc: non-empty-string,
23: * id: int|non-empty-string,
24: * method: non-empty-string,
25: * params?: array<string, mixed>,
26: * }
27: *
28: * @extends Request<T>
29: */
30: abstract readonly class JsonRpcRequest extends Request
31: {
32: /**
33: * @var non-empty-string
34: */
35: public string $jsonrpc;
36:
37: /**
38: * @param RequestId $id A uniquely identifying ID for a request in JSON-RPC.
39: */
40: public function __construct(
41: string $jsonrpc,
42: public RequestId $id,
43: string $method,
44: ?array $params = null,
45: ) {
46: $this->validateJsonRpc($jsonrpc);
47: $this->jsonrpc = $jsonrpc;
48:
49: parent::__construct($method, $params);
50: }
51:
52: #[\Override]
53: public function toArray(): array
54: {
55: return array_filter([
56: 'jsonrpc' => $this->jsonrpc,
57: 'id' => $this->id->value,
58: 'method' => $this->method,
59: 'params' => $this->params,
60: ], static fn(mixed $value): bool => null !== $value);
61: }
62: }
63: