1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Schema\Content;
15:
16: use Nexus\Mcp\Schema\Annotations;
17: use Nexus\Mcp\Schema\Arrayable;
18:
19: /**
20: * An image provided to or from an LLM.
21: *
22: * @implements Arrayable<array{
23: * _meta?: array<string, mixed>,
24: * type: 'image',
25: * data: non-empty-string,
26: * mimeType: non-empty-string,
27: * annotations?: template-type<Annotations, Arrayable, 'T'>,
28: * }>
29: */
30: final readonly class ImageContent implements \JsonSerializable, Arrayable, ContentBlock
31: {
32: /**
33: * @var non-empty-string
34: */
35: public string $type;
36:
37: /**
38: * @param non-empty-string $data The base64-encoded image data.
39: * @param non-empty-string $mimeType The MIME type of the image. Different providers may support different image types.
40: * @param null|Annotations $annotations Optional annotations for the client.
41: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
42: * additional metadata to their interactions.
43: */
44: public function __construct(
45: public string $data,
46: public string $mimeType,
47: public ?Annotations $annotations = null,
48: public ?array $meta = null,
49: ) {
50: $this->type = 'image';
51: }
52:
53: #[\Override]
54: public function toArray(): array
55: {
56: return array_filter([
57: '_meta' => $this->meta,
58: 'type' => $this->type,
59: 'data' => $this->data,
60: 'mimeType' => $this->mimeType,
61: 'annotations' => $this->annotations?->toArray(),
62: ], static fn(mixed $value): bool => null !== $value);
63: }
64:
65: /**
66: * @return template-type<self, Arrayable, 'T'>
67: */
68: #[\Override]
69: public function jsonSerialize(): array
70: {
71: return $this->toArray();
72: }
73: }
74: