| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Resources\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\Core\DTO\Icon; |
| 8 |
use WP\McpSchema\Common\Protocol\DTO\Annotations; |
| 9 |
use WP\McpSchema\Common\Protocol\Union\ContentBlockInterface; |
| 10 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 11 |
|
| 12 |
/** |
| 13 |
* A resource that the server is capable of reading, included in a prompt or tool call result. |
| 14 |
* |
| 15 |
* Note: resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests. |
| 16 |
* |
| 17 |
* @since 2025-06-18 |
| 18 |
* @last-updated 2025-11-25 (added properties: icons; modified property: annotations) |
| 19 |
* |
| 20 |
* @mcp-domain Server |
| 21 |
* @mcp-subdomain Resources |
| 22 |
* @mcp-version 2025-11-25 |
| 23 |
*/ |
| 24 |
class ResourceLink extends Resource implements ContentBlockInterface |
| 25 |
{ |
| 26 |
use ValidatesRequiredFields; |
| 27 |
|
| 28 |
public const TYPE = 'resource_link'; |
| 29 |
|
| 30 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 31 |
public const DISCRIMINATOR_VALUE = 'resource_link'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 2025-06-18 |
| 35 |
* |
| 36 |
* @var 'resource_link' |
| 37 |
*/ |
| 38 |
protected string $type; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param string $uri @since 2025-06-18 |
| 42 |
* @param string $name @since 2025-06-18 |
| 43 |
* @param string|null $description @since 2025-06-18 |
| 44 |
* @param string|null $mimeType @since 2025-06-18 |
| 45 |
* @param \WP\McpSchema\Common\Protocol\DTO\Annotations|null $annotations @since 2025-06-18 |
| 46 |
* @param int|null $size @since 2025-06-18 |
| 47 |
* @param array<string, mixed>|null $_meta @since 2025-06-18 |
| 48 |
* @param string|null $title @since 2025-06-18 |
| 49 |
* @param array<\WP\McpSchema\Common\Core\DTO\Icon>|null $icons @since 2025-11-25 |
| 50 |
*/ |
| 51 |
public function __construct( |
| 52 |
string $uri, |
| 53 |
string $name, |
| 54 |
?string $description = null, |
| 55 |
?string $mimeType = null, |
| 56 |
?Annotations $annotations = null, |
| 57 |
?int $size = null, |
| 58 |
?array $_meta = null, |
| 59 |
?string $title = null, |
| 60 |
?array $icons = null |
| 61 |
) { |
| 62 |
parent::__construct($name, $uri, $title, $description, $mimeType, $annotations, $size, $_meta, $icons); |
| 63 |
$this->type = self::TYPE; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Creates an instance from an array. |
| 68 |
* |
| 69 |
* @param array{ |
| 70 |
* uri: string, |
| 71 |
* description?: string|null, |
| 72 |
* mimeType?: string|null, |
| 73 |
* annotations?: array<string, mixed>|\WP\McpSchema\Common\Protocol\DTO\Annotations|null, |
| 74 |
* size?: int|null, |
| 75 |
* _meta?: array<string, mixed>|null, |
| 76 |
* name: string, |
| 77 |
* title?: string|null, |
| 78 |
* icons?: array<array<string, mixed>|\WP\McpSchema\Common\Core\DTO\Icon>|null, |
| 79 |
* type: 'resource_link' |
| 80 |
* } $data |
| 81 |
* @phpstan-param array<string, mixed> $data |
| 82 |
* @return self |
| 83 |
*/ |
| 84 |
public static function fromArray(array $data): self |
| 85 |
{ |
| 86 |
self::assertRequired($data, ['uri', 'name']); |
| 87 |
|
| 88 |
/** @var \WP\McpSchema\Common\Protocol\DTO\Annotations|null $annotations */ |
| 89 |
$annotations = isset($data['annotations']) |
| 90 |
? (is_array($data['annotations']) |
| 91 |
? Annotations::fromArray(self::asArray($data['annotations'])) |
| 92 |
: $data['annotations']) |
| 93 |
: null; |
| 94 |
|
| 95 |
/** @var array<\WP\McpSchema\Common\Core\DTO\Icon>|null $icons */ |
| 96 |
$icons = isset($data['icons']) |
| 97 |
? array_map( |
| 98 |
static fn($item) => is_array($item) |
| 99 |
? Icon::fromArray($item) |
| 100 |
: $item, |
| 101 |
self::asArray($data['icons']) |
| 102 |
) |
| 103 |
: null; |
| 104 |
|
| 105 |
return new self( |
| 106 |
self::asString($data['uri']), |
| 107 |
self::asString($data['name']), |
| 108 |
self::asStringOrNull($data['description'] ?? null), |
| 109 |
self::asStringOrNull($data['mimeType'] ?? null), |
| 110 |
$annotations, |
| 111 |
self::asIntOrNull($data['size'] ?? null), |
| 112 |
self::asArrayOrNull($data['_meta'] ?? null), |
| 113 |
self::asStringOrNull($data['title'] ?? null), |
| 114 |
$icons |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Converts the instance to an array. |
| 120 |
* |
| 121 |
* @return array<string, mixed> |
| 122 |
*/ |
| 123 |
public function toArray(): array |
| 124 |
{ |
| 125 |
$result = parent::toArray(); |
| 126 |
|
| 127 |
$result['type'] = $this->type; |
| 128 |
|
| 129 |
return $result; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @return 'resource_link' |
| 134 |
*/ |
| 135 |
public function getType(): string |
| 136 |
{ |
| 137 |
return $this->type; |
| 138 |
} |
| 139 |
} |
| 140 |
|