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