| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 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: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | abstract readonly class ResultMetaObject extends MetaObject |
| 29: | { |
| 30: | public const string SERVER_INFO_KEY = 'io.modelcontextprotocol/serverInfo'; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | public function __construct(public ?Implementation $serverInfo = null, array $extras = []) |
| 36: | { |
| 37: | parent::__construct(extras: $extras); |
| 38: | } |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 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: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | protected static function splitServerInfo(array $data): array |
| 73: | { |
| 74: | $serverInfo = null; |
| 75: | |
| 76: | if (\array_key_exists(self::SERVER_INFO_KEY, $data)) { |
| 77: | Assert::that($data[self::SERVER_INFO_KEY]) |
| 78: | ->isArray(\sprintf('"_meta.%s" must be an object, {type} given.', self::SERVER_INFO_KEY)) |
| 79: | ->isMap(\sprintf('"_meta.%s" must be a string-keyed object.', self::SERVER_INFO_KEY)) |
| 80: | ; |
| 81: | $serverInfo = Implementation::fromArray($data[self::SERVER_INFO_KEY]); |
| 82: | unset($data[self::SERVER_INFO_KEY]); |
| 83: | } |
| 84: | |
| 85: | return [$serverInfo, $data]; |
| 86: | } |
| 87: | } |
| 88: | |