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\ContentBlock;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\JsonRpc\ResourceContentsDispatcher;
18: use Nexus\Mcp\Core\Schema\Annotations;
19: use Nexus\Mcp\Core\Schema\Arrayable;
20: use Nexus\Mcp\Core\Schema\ContentBlock;
21: use Nexus\Mcp\Core\Schema\MetaObject;
22: use Nexus\Mcp\Core\Schema\Resource\BlobResourceContents;
23: use Nexus\Mcp\Core\Schema\Resource\ResourceContents;
24: use Nexus\Mcp\Core\Schema\Resource\TextResourceContents;
25:
26: /**
27: * The contents of a resource, embedded into a prompt or tool call result.
28: *
29: * It is up to the client how best to render embedded resources for the benefit of the LLM and/or the user.
30: *
31: * @implements Arrayable<array{
32: * resource: template-type<ResourceContents, Arrayable, 'T'>,
33: * type: 'resource',
34: * annotations?: template-type<Annotations, Arrayable, 'T'>,
35: * _meta?: template-type<MetaObject, Arrayable, 'T'>,
36: * }>
37: *
38: * @see https://modelcontextprotocol.io/specification/draft/schema#embeddedresource
39: */
40: final readonly class EmbeddedResource implements Arrayable, ContentBlock
41: {
42: public const string TYPE = 'resource';
43:
44: public function __construct(
45: public BlobResourceContents|TextResourceContents $resource,
46: public Annotations $annotations = new Annotations(),
47: public MetaObject $meta = new MetaObject(),
48: ) {
49: }
50:
51: #[\Override]
52: public static function fromArray(array $data): static
53: {
54: Assert::that($data)->hasOffset('type', 'embedded resource is missing the required "type" key.');
55: $type = $data['type'];
56: Assert::that($type)->isIdentical(self::TYPE, 'embedded resource "type" must be {other}, {value} given.');
57:
58: Assert::that($data)->hasOffset('resource', 'embedded resource is missing the required "resource" key.');
59: Assert::that($data['resource'])
60: ->isArray('embedded resource "resource" must be an object, {type} given.')
61: ->isMap('embedded resource "resource" must be a string-keyed object.')
62: ;
63: $resource = ResourceContentsDispatcher::fromArray($data['resource'], 'EmbeddedResource resource');
64:
65: $annotations = new Annotations();
66:
67: if (\array_key_exists('annotations', $data)) {
68: Assert::that($data['annotations'])
69: ->isArray('embedded resource "annotations" must be an object, {type} given.')
70: ->isMap('embedded resource "annotations" must be a string-keyed object.')
71: ;
72: $annotations = Annotations::fromArray($data['annotations']);
73: }
74:
75: $meta = new MetaObject();
76:
77: if (\array_key_exists('_meta', $data)) {
78: Assert::that($data['_meta'])
79: ->isArray('embedded resource "_meta" must be an object, {type} given.')
80: ->isMap('embedded resource "_meta" must be a string-keyed object.')
81: ;
82: $meta = MetaObject::fromArray($data['_meta']);
83: }
84:
85: return new self(resource: $resource, annotations: $annotations, meta: $meta);
86: }
87:
88: #[\Override]
89: public function toArray(): array
90: {
91: $data = [
92: 'resource' => $this->resource->toArray(),
93: 'type' => self::TYPE,
94: ];
95:
96: $annotations = $this->annotations->toArray();
97:
98: if ([] !== $annotations) {
99: $data['annotations'] = $annotations;
100: }
101:
102: $meta = $this->meta->toArray();
103:
104: if ([] !== $meta) {
105: $data['_meta'] = $meta;
106: }
107:
108: return $data;
109: }
110:
111: #[\Override]
112: public function jsonSerialize(): array
113: {
114: return $this->toArray();
115: }
116: }
117: