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\MetaObject;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Implementation;
18: use Nexus\Mcp\Core\Schema\MetaObject;
19:
20: /**
21: * Extends `MetaObject` with additional result-specific fields. All key naming rules from `MetaObject` apply.
22: *
23: * @extends MetaObject<array<array-key, mixed>>
24: *
25: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#resultmetaobject
26: */
27: abstract readonly class ResultMetaObject extends MetaObject
28: {
29: public const string SERVER_INFO_KEY = 'io.modelcontextprotocol/serverInfo';
30:
31: /**
32: * @param array<array-key, mixed> $extras
33: */
34: public function __construct(
35: public ?Implementation $serverInfo = null,
36: array $extras = [],
37: ) {
38: parent::__construct(extras: $extras);
39: }
40:
41: /**
42: * Whether a server identity is present, in the typed slot or among the extras.
43: *
44: * @internal
45: */
46: public function declaresServerInfo(): bool
47: {
48: return null !== $this->serverInfo || \array_key_exists(self::SERVER_INFO_KEY, $this->extras);
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: $out = [];
55:
56: if (null !== $this->serverInfo) {
57: $out[self::SERVER_INFO_KEY] = $this->serverInfo->toArray();
58: }
59:
60: $out += $this->extras;
61:
62: return $out;
63: }
64:
65: /**
66: * @param array<array-key, mixed> $data
67: *
68: * @return array{null|Implementation, array<array-key, mixed>}
69: */
70: protected static function splitServerInfo(array $data): array
71: {
72: $serverInfo = null;
73:
74: if (\array_key_exists(self::SERVER_INFO_KEY, $data)) {
75: Assert::that($data[self::SERVER_INFO_KEY])
76: ->isArray(\sprintf('"_meta.%s" must be an object, {type} given.', self::SERVER_INFO_KEY))
77: ->isMap(\sprintf('"_meta.%s" must be a string-keyed object.', self::SERVER_INFO_KEY))
78: ;
79: $serverInfo = Implementation::fromArray($data[self::SERVER_INFO_KEY]);
80: unset($data[self::SERVER_INFO_KEY]);
81: }
82:
83: return [$serverInfo, $data];
84: }
85: }
86: