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: use Nexus\Mcp\Schema\BaseMetadata;
19: use Nexus\Mcp\Schema\Icon;
20: use Nexus\Mcp\Schema\Resource\ResourceValidator;
21:
22: /**
23: * A resource that the server is capable of reading, included in a prompt or tool call result.
24: *
25: * Note: resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests.
26: *
27: * @extends BaseMetadata<array{
28: * _meta?: array<string, mixed>,
29: * type: 'resource_link',
30: * uri: non-empty-string,
31: * name: non-empty-string,
32: * title?: non-empty-string,
33: * description?: non-empty-string,
34: * mimeType?: non-empty-string,
35: * size?: int<0, max>,
36: * annotations?: template-type<Annotations, Arrayable, 'T'>,
37: * icons?: list<template-type<Icon, Arrayable, 'T'>>,
38: * }>
39: */
40: final readonly class ResourceLink extends BaseMetadata implements ContentBlock
41: {
42: /**
43: * @var non-empty-string
44: */
45: public string $uri;
46:
47: /**
48: * @var non-empty-string
49: */
50: public string $type;
51:
52: /**
53: * @param non-empty-string $uri The URI of this resource.
54: * @param null|non-empty-string $description A description of what this resource represents.
55: * @param null|non-empty-string $mimeType The MIME type of this resource, if known.
56: * @param null|int<0, max> $size The size of the raw resource content, in bytes
57: * (i.e., before base64 encoding or any tokenization), if known.
58: * @param null|Annotations $annotations Optional annotations for the client.
59: * @param null|list<Icon> $icons Optional set of sized icons that the client can display in a user interface.
60: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
61: * additional metadata to their interactions.
62: */
63: public function __construct(
64: string $uri,
65: string $name,
66: ?string $title = null,
67: public ?string $description = null,
68: public ?string $mimeType = null,
69: public ?int $size = null,
70: public ?Annotations $annotations = null,
71: public ?array $icons = null,
72: public ?array $meta = null,
73: ) {
74: ResourceValidator::assertValidUri($uri);
75:
76: $this->uri = $uri;
77: $this->type = 'resource_link';
78:
79: parent::__construct($name, $title);
80: }
81:
82: #[\Override]
83: public function toArray(): array
84: {
85: return array_filter([
86: '_meta' => $this->meta,
87: 'type' => $this->type,
88: 'uri' => $this->uri,
89: 'name' => $this->name,
90: 'title' => $this->title,
91: 'description' => $this->description,
92: 'mimeType' => $this->mimeType,
93: 'size' => $this->size,
94: 'annotations' => $this->annotations?->toArray(),
95: 'icons' => null === $this->icons ? null : array_map(
96: static fn(Icon $icon): array => $icon->toArray(),
97: $this->icons,
98: ),
99: ], static fn(mixed $value): bool => null !== $value);
100: }
101: }
102: