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: /**
17: * A request that expects a response.
18: */
19: final readonly class JsonRpcRequest extends Request
20: {
21: /**
22: * @param non-empty-string $jsonrpc JSON-RPC version. Must be 2.0.
23: * @param non-empty-string $method The request method.
24: * @param null|array<string, mixed> $params Additional request parameters.
25: */
26: public function __construct(
27: string $jsonrpc,
28: public RequestId $requestId,
29: string $method,
30: ?array $params = null,
31: ) {
32: parent::__construct($jsonrpc, $method, $params);
33: }
34:
35: /**
36: * @return array{
37: * jsonrpc: non-empty-string,
38: * id: int|non-empty-string,
39: * method: non-empty-string,
40: * params?: array<string, mixed>,
41: * }
42: */
43: #[\Override]
44: public function toArray(): array
45: {
46: return array_filter([
47: 'jsonrpc' => $this->jsonrpc,
48: 'id' => $this->requestId->id,
49: 'method' => $this->method,
50: 'params' => $this->params,
51: ], static fn(mixed $value): bool => null !== $value);
52: }
53: }
54: