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;
15:
16: /**
17: * Base for the `result` payload carried by a JSON-RPC success response.
18: * Concrete subclasses define the method-specific fields. This base only
19: * threads the typed `_meta`.
20: *
21: * @implements Arrayable<array<string, mixed>>
22: *
23: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#result
24: */
25: abstract readonly class Result implements Arrayable
26: {
27: public function __construct(public MetaObject $meta = new MetaObject())
28: {
29: }
30:
31: /**
32: * @param array<string, mixed> $data
33: */
34: #[\Override]
35: abstract public static function fromArray(array $data): static;
36:
37: /**
38: * Serializes the result body. Subclasses override to merge their own fields
39: * alongside the `_meta` slice returned here.
40: */
41: #[\Override]
42: public function toArray(): array
43: {
44: $meta = $this->meta->toArray();
45:
46: return [] === $meta ? [] : ['_meta' => $meta];
47: }
48:
49: #[\Override]
50: public function jsonSerialize(): array|\stdClass
51: {
52: $data = $this->toArray();
53:
54: return [] === $data ? new \stdClass() : $data;
55: }
56: }
57: