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\Extension\Apps\Schema;
15:
16: use Nexus\Mcp\Core\Schema\Arrayable;
17:
18: /**
19: * Sandbox permissions a UI resource requests from the host.
20: *
21: * @implements Arrayable<array{
22: * camera?: \stdClass,
23: * microphone?: \stdClass,
24: * geolocation?: \stdClass,
25: * clipboardWrite?: \stdClass,
26: * }>
27: *
28: * @see https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx
29: */
30: final readonly class UiResourcePermissions implements Arrayable
31: {
32: public function __construct(
33: public bool $camera = false,
34: public bool $microphone = false,
35: public bool $geolocation = false,
36: public bool $clipboardWrite = false,
37: ) {
38: }
39:
40: #[\Override]
41: public static function fromArray(array $data): static
42: {
43: return new self(
44: camera: self::parseRequested($data, 'camera'),
45: microphone: self::parseRequested($data, 'microphone'),
46: geolocation: self::parseRequested($data, 'geolocation'),
47: clipboardWrite: self::parseRequested($data, 'clipboardWrite'),
48: );
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: $data = [];
55:
56: if ($this->camera) {
57: $data['camera'] = new \stdClass();
58: }
59:
60: if ($this->microphone) {
61: $data['microphone'] = new \stdClass();
62: }
63:
64: if ($this->geolocation) {
65: $data['geolocation'] = new \stdClass();
66: }
67:
68: if ($this->clipboardWrite) {
69: $data['clipboardWrite'] = new \stdClass();
70: }
71:
72: return $data;
73: }
74:
75: #[\Override]
76: public function jsonSerialize(): array|\stdClass
77: {
78: $data = $this->toArray();
79:
80: return [] === $data ? new \stdClass() : $data;
81: }
82:
83: /**
84: * @param array<string, mixed> $data
85: * @param non-empty-string $slot
86: *
87: * @throws \InvalidArgumentException
88: */
89: private static function parseRequested(array $data, string $slot): bool
90: {
91: if (! \array_key_exists($slot, $data)) {
92: return false;
93: }
94:
95: $value = $data[$slot];
96:
97: if (! \is_array($value) && ! $value instanceof \stdClass) {
98: throw new \InvalidArgumentException(\sprintf(
99: '"_meta.ui.permissions.%s" must be an object, %s given.',
100: $slot,
101: get_debug_type($value),
102: ));
103: }
104:
105: return true;
106: }
107: }
108: