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\ResultResponse;
15:
16: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcResultResponse;
17: use Nexus\Mcp\Core\Schema\RequestId;
18: use Nexus\Mcp\Core\Schema\Result;
19: use Nexus\Mcp\Core\Schema\Result\InputRequiredResult;
20: use Nexus\Mcp\Core\Schema\Result\ReadResourceResult;
21:
22: /**
23: * A successful response from the server for a `resources/read` request.
24: *
25: * @property-read InputRequiredResult|ReadResourceResult $result
26: *
27: * @extends JsonRpcResultResponse<array{
28: * jsonrpc: '2.0',
29: * id: int|non-empty-string,
30: * result: template-type<InputRequiredResult|ReadResourceResult, Result, 'T'>,
31: * }>
32: *
33: * @see https://modelcontextprotocol.io/specification/draft/schema#readresourceresultresponse
34: */
35: final readonly class ReadResourceResultResponse extends JsonRpcResultResponse
36: {
37: public function __construct(RequestId $id, InputRequiredResult|ReadResourceResult $result)
38: {
39: parent::__construct(id: $id, result: $result);
40: }
41:
42: #[\Override]
43: public static function fromArray(array $data): static
44: {
45: $id = self::parseId($data);
46: $payload = self::parseResult($data);
47:
48: $result = self::isInputRequired($payload)
49: ? InputRequiredResult::fromArray($payload)
50: : ReadResourceResult::fromArray($payload);
51:
52: return new self(id: $id, result: $result);
53: }
54:
55: #[\Override]
56: public function toArray(): array
57: {
58: return [
59: 'jsonrpc' => self::JSONRPC_VERSION,
60: 'id' => $this->id->id,
61: 'result' => $this->result->toArray(),
62: ];
63: }
64: }
65: