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 successful (non-error) response to a request. |
18: | */ |
19: | final readonly class JsonRpcResponse extends Message |
20: | { |
21: | /** |
22: | * @param non-empty-string $jsonrpc JSON-RPC version. Must be 2.0. |
23: | */ |
24: | public function __construct( |
25: | string $jsonrpc, |
26: | public RequestId $requestId, |
27: | public Result $result, |
28: | ) { |
29: | parent::__construct($jsonrpc); |
30: | } |
31: | |
32: | /** |
33: | * @return array{ |
34: | * jsonrpc: non-empty-string, |
35: | * id: int|non-empty-string, |
36: | * result: array<string, mixed>, |
37: | * } |
38: | */ |
39: | #[\Override] |
40: | public function toArray(): array |
41: | { |
42: | return [ |
43: | 'jsonrpc' => $this->jsonrpc, |
44: | 'id' => $this->requestId->id, |
45: | 'result' => $this->result->toArray(), |
46: | ]; |
47: | } |
48: | |
49: | /** |
50: | * @return array{ |
51: | * jsonrpc: non-empty-string, |
52: | * id: int|non-empty-string, |
53: | * result: mixed, |
54: | * } |
55: | */ |
56: | #[\Override] |
57: | public function jsonSerialize(): array |
58: | { |
59: | return [ |
60: | 'jsonrpc' => $this->jsonrpc, |
61: | 'id' => $this->requestId->id, |
62: | 'result' => $this->result->jsonSerialize(), |
63: | ]; |
64: | } |
65: | } |
66: |