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\Resource;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\MetaObject;
18:
19: /**
20: * Binary resource contents. The `blob` payload carries a base64-encoded
21: * representation of the binary data.
22: *
23: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#blobresourcecontents
24: */
25: final readonly class BlobResourceContents extends ResourceContents
26: {
27: public function __construct(
28: string $uri,
29: public string $blob,
30: ?string $mimeType = null,
31: MetaObject $meta = new MetaObject(),
32: ) {
33: parent::__construct($uri, $mimeType, $meta);
34: }
35:
36: /**
37: * @param array<string, mixed> $data
38: */
39: #[\Override]
40: public static function fromArray(array $data): static
41: {
42: Assert::that($data)->hasOffset('uri', 'blob resource contents missing the required "uri" key.');
43: $uri = $data['uri'];
44: Assert::that($uri)->isString('blob resource contents "uri" must be a string, {type} given.');
45:
46: Assert::that($data)->hasOffset('blob', 'blob resource contents missing the required "blob" key.');
47: $blob = $data['blob'];
48: Assert::that($blob)->isString('blob resource contents "blob" must be a string, {type} given.');
49:
50: $mimeType = $data['mimeType'] ?? null;
51: Assert::that($mimeType)->nullOr()->isString('blob resource contents "mimeType" must be a string or null, {type} given.');
52:
53: $meta = new MetaObject();
54:
55: if (\array_key_exists('_meta', $data)) {
56: Assert::that($data['_meta'])
57: ->isArray('blob resource contents "_meta" must be an object, {type} given.')
58: ->isMap('blob resource contents "_meta" must be a string-keyed object.')
59: ;
60: $meta = MetaObject::fromArray($data['_meta']);
61: }
62:
63: return new self($uri, $blob, $mimeType, $meta);
64: }
65:
66: #[\Override]
67: public function toArray(): array
68: {
69: return [...parent::toArray(), 'blob' => $this->blob];
70: }
71: }
72: