1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 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\Core\Schema\JsonRpc;
15:
16: use Nexus\Mcp\Core\Schema\Arrayable;
17: use Nexus\Mcp\Core\Schema\RequestId;
18: use Nexus\Mcp\Core\Schema\Result;
19:
20: /**
21: * A successful (non-error) response to a request.
22: *
23: * @template-covariant T of Result
24: *
25: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#jsonrpcresultresponse
26: */
27: final readonly class JsonRpcResultResponse implements \JsonSerializable, JsonRpcResponse
28: {
29: /**
30: * @param T $result
31: */
32: public function __construct(public RequestId $id, public Result $result)
33: {
34: }
35:
36: /**
37: * @return array{jsonrpc: '2.0', id: int|non-empty-string, result: template-type<T, Arrayable, 'T'>}
38: */
39: public function toArray(): array
40: {
41: return [
42: 'jsonrpc' => self::JSONRPC_VERSION,
43: 'id' => $this->id->id,
44: 'result' => $this->result->toArray(),
45: ];
46: }
47:
48: /**
49: * @return array{jsonrpc: '2.0', id: int|non-empty-string, result: array<string, mixed>|\stdClass}
50: */
51: #[\Override]
52: public function jsonSerialize(): array
53: {
54: return [
55: 'jsonrpc' => self::JSONRPC_VERSION,
56: 'id' => $this->id->id,
57: 'result' => $this->result->jsonSerialize(),
58: ];
59: }
60: }
61: