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\Schema\Annotations;
18: use Nexus\Mcp\Core\Schema\Arrayable;
19: use Nexus\Mcp\Core\Schema\ContentBlock;
20: use Nexus\Mcp\Core\Schema\MetaObject;
21: use Nexus\Mcp\Core\Schema\MetaObject\PayloadMetaObject;
22:
23: /**
24: * An image provided to or from an LLM.
25: *
26: * @implements Arrayable<array{
27: * data: non-empty-string,
28: * mimeType: non-empty-string,
29: * type: 'image',
30: * annotations?: template-type<Annotations, Arrayable, 'T'>,
31: * _meta?: template-type<PayloadMetaObject, MetaObject, 'T'>,
32: * }>
33: *
34: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#imagecontent
35: */
36: final readonly class ImageContent implements Arrayable, ContentBlock
37: {
38: public const string TYPE = 'image';
39:
40: /**
41: * @param non-empty-string $data
42: * @param non-empty-string $mimeType
43: */
44: public function __construct(
45: public string $data,
46: public string $mimeType,
47: public Annotations $annotations = new Annotations(),
48: public PayloadMetaObject $meta = new PayloadMetaObject(),
49: ) {
50: Assert::that($data)->isNonEmptyString('image content "data" must be a non-empty string.');
51: Assert::that($mimeType)->isNonEmptyString('image content "mimeType" must be a non-empty string.');
52: }
53:
54: #[\Override]
55: public static function fromArray(array $data): static
56: {
57: Assert::that($data)->hasOffset('type', 'image content is missing the required "type" key.');
58: $type = $data['type'];
59: Assert::that($type)->isIdentical(self::TYPE, 'image content "type" must be {other}, {value} given.');
60:
61: Assert::that($data)->hasOffset('data', 'image content is missing the required "data" key.');
62: $payload = $data['data'];
63: Assert::that($payload)->isNonEmptyString('image content "data" must be a non-empty string, {type} given.');
64:
65: Assert::that($data)->hasOffset('mimeType', 'image content is missing the required "mimeType" key.');
66: $mimeType = $data['mimeType'];
67: Assert::that($mimeType)->isNonEmptyString('image content "mimeType" must be a non-empty string, {type} given.');
68:
69: $annotations = new Annotations();
70:
71: if (\array_key_exists('annotations', $data)) {
72: Assert::that($data['annotations'])
73: ->isArray('image content "annotations" must be an object, {type} given.')
74: ->isMap('image content "annotations" must be a string-keyed object.')
75: ;
76: $annotations = Annotations::fromArray($data['annotations']);
77: }
78:
79: $meta = new PayloadMetaObject();
80:
81: if (\array_key_exists('_meta', $data)) {
82: Assert::that($data['_meta'])
83: ->isArray('image content "_meta" must be an object, {type} given.')
84: ->not()->isNonEmptyList('image content "_meta" must be a string-keyed object.')
85: ;
86: $meta = PayloadMetaObject::fromArray($data['_meta']);
87: }
88:
89: return new self(data: $payload, mimeType: $mimeType, annotations: $annotations, meta: $meta);
90: }
91:
92: #[\Override]
93: public function toArray(): array
94: {
95: $data = [
96: 'data' => $this->data,
97: 'mimeType' => $this->mimeType,
98: 'type' => self::TYPE,
99: ];
100:
101: $annotations = $this->annotations->toArray();
102:
103: if ([] !== $annotations) {
104: $data['annotations'] = $annotations;
105: }
106:
107: $meta = $this->meta->toArray();
108:
109: if ([] !== $meta) {
110: $data['_meta'] = $meta;
111: }
112:
113: return $data;
114: }
115:
116: #[\Override]
117: public function jsonSerialize(): array
118: {
119: return $this->toArray();
120: }
121: }
122: