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: * The contents of a resource, embedded into a prompt or tool call result.
21: *
22: * It is up to the client how best to render embedded resources for the benefit of the LLM and/or the user.
23: *
24: * @implements Arrayable<array{
25: * _meta?: array<string, mixed>,
26: * type: 'resource',
27: * resource: template-type<BlobResourceContents|TextResourceContents, ResourceContents, 'T'>,
28: * annotations?: template-type<Annotations, Arrayable, 'T'>,
29: * }>
30: */
31: final readonly class EmbeddedResource implements \JsonSerializable, Arrayable, ContentBlock
32: {
33: /**
34: * @var non-empty-string
35: */
36: public string $type;
37:
38: /**
39: * @param null|Annotations $annotations Optional annotations for the client.
40: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
41: * additional metadata to their interactions.
42: */
43: public function __construct(
44: public BlobResourceContents|TextResourceContents $resource,
45: public ?Annotations $annotations = null,
46: public ?array $meta = null,
47: ) {
48: $this->type = 'resource';
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: return array_filter([
55: '_meta' => $this->meta,
56: 'type' => $this->type,
57: 'resource' => $this->resource->toArray(),
58: 'annotations' => $this->annotations?->toArray(),
59: ], static fn(mixed $value): bool => null !== $value);
60: }
61:
62: /**
63: * @return template-type<self, Arrayable, 'T'>
64: */
65: #[\Override]
66: public function jsonSerialize(): array
67: {
68: return $this->toArray();
69: }
70: }
71: