| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Common\Protocol\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Protocol\Union\ContentBlockInterface; |
| 9 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 10 |
|
| 11 |
/** |
| 12 |
* The contents of a resource, embedded into a prompt or tool call result. |
| 13 |
* |
| 14 |
* It is up to the client how best to render embedded resources for the benefit |
| 15 |
* of the LLM and/or the user. |
| 16 |
* |
| 17 |
* @since 2024-11-05 |
| 18 |
* @last-updated 2025-11-25 (modified property: annotations) |
| 19 |
* |
| 20 |
* @mcp-domain Common |
| 21 |
* @mcp-subdomain Protocol |
| 22 |
* @mcp-version 2025-11-25 |
| 23 |
*/ |
| 24 |
class EmbeddedResource extends AbstractDataTransferObject implements ContentBlockInterface |
| 25 |
{ |
| 26 |
use ValidatesRequiredFields; |
| 27 |
|
| 28 |
public const TYPE = 'resource'; |
| 29 |
|
| 30 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 31 |
public const DISCRIMINATOR_VALUE = 'resource'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 2024-11-05 |
| 35 |
* |
| 36 |
* @var 'resource' |
| 37 |
*/ |
| 38 |
protected string $type; |
| 39 |
|
| 40 |
/** |
| 41 |
* @since 2024-11-05 |
| 42 |
* |
| 43 |
* @var \WP\McpSchema\Common\Protocol\DTO\TextResourceContents|\WP\McpSchema\Common\Protocol\DTO\BlobResourceContents |
| 44 |
*/ |
| 45 |
protected $resource; |
| 46 |
|
| 47 |
/** |
| 48 |
* Optional annotations for the client. |
| 49 |
* |
| 50 |
* @since 2024-11-05 |
| 51 |
* |
| 52 |
* @var \WP\McpSchema\Common\Protocol\DTO\Annotations|null |
| 53 |
*/ |
| 54 |
protected ?Annotations $annotations; |
| 55 |
|
| 56 |
/** |
| 57 |
* See [General fields: `_meta`](/specification/2025-11-25/basic/index#meta) for notes on `_meta` usage. |
| 58 |
* |
| 59 |
* @since 2025-06-18 |
| 60 |
* |
| 61 |
* @var array<string, mixed>|null |
| 62 |
*/ |
| 63 |
protected ?array $_meta; |
| 64 |
|
| 65 |
/** |
| 66 |
* @param \WP\McpSchema\Common\Protocol\DTO\TextResourceContents|\WP\McpSchema\Common\Protocol\DTO\BlobResourceContents $resource @since 2024-11-05 |
| 67 |
* @param \WP\McpSchema\Common\Protocol\DTO\Annotations|null $annotations @since 2024-11-05 |
| 68 |
* @param array<string, mixed>|null $_meta @since 2025-06-18 |
| 69 |
*/ |
| 70 |
public function __construct( |
| 71 |
$resource, |
| 72 |
?Annotations $annotations = null, |
| 73 |
?array $_meta = null |
| 74 |
) { |
| 75 |
$this->type = self::TYPE; |
| 76 |
$this->resource = $resource; |
| 77 |
$this->annotations = $annotations; |
| 78 |
$this->_meta = $_meta; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Creates an instance from an array. |
| 83 |
* |
| 84 |
* @param array{ |
| 85 |
* type: 'resource', |
| 86 |
* resource: \WP\McpSchema\Common\Protocol\DTO\TextResourceContents|\WP\McpSchema\Common\Protocol\DTO\BlobResourceContents, |
| 87 |
* annotations?: array<string, mixed>|\WP\McpSchema\Common\Protocol\DTO\Annotations|null, |
| 88 |
* _meta?: array<string, mixed>|null |
| 89 |
* } $data |
| 90 |
* @phpstan-param array<string, mixed> $data |
| 91 |
* @return self |
| 92 |
*/ |
| 93 |
public static function fromArray(array $data): self |
| 94 |
{ |
| 95 |
self::assertRequired($data, ['resource']); |
| 96 |
|
| 97 |
/** @var \WP\McpSchema\Common\Protocol\DTO\TextResourceContents|\WP\McpSchema\Common\Protocol\DTO\BlobResourceContents $resource */ |
| 98 |
$resource = $data['resource']; |
| 99 |
|
| 100 |
/** @var \WP\McpSchema\Common\Protocol\DTO\Annotations|null $annotations */ |
| 101 |
$annotations = isset($data['annotations']) |
| 102 |
? (is_array($data['annotations']) |
| 103 |
? Annotations::fromArray(self::asArray($data['annotations'])) |
| 104 |
: $data['annotations']) |
| 105 |
: null; |
| 106 |
|
| 107 |
return new self( |
| 108 |
$resource, |
| 109 |
$annotations, |
| 110 |
self::asArrayOrNull($data['_meta'] ?? null) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Converts the instance to an array. |
| 116 |
* |
| 117 |
* @return array<string, mixed> |
| 118 |
*/ |
| 119 |
public function toArray(): array |
| 120 |
{ |
| 121 |
$result = []; |
| 122 |
|
| 123 |
$result['type'] = $this->type; |
| 124 |
$result['resource'] = (is_object($this->resource) && method_exists($this->resource, 'toArray')) ? $this->resource->toArray() : $this->resource; |
| 125 |
if ($this->annotations !== null) { |
| 126 |
$result['annotations'] = $this->annotations->toArray(); |
| 127 |
} |
| 128 |
if ($this->_meta !== null) { |
| 129 |
$result['_meta'] = $this->_meta; |
| 130 |
} |
| 131 |
|
| 132 |
return $result; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @return 'resource' |
| 137 |
*/ |
| 138 |
public function getType(): string |
| 139 |
{ |
| 140 |
return $this->type; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @return \WP\McpSchema\Common\Protocol\DTO\TextResourceContents|\WP\McpSchema\Common\Protocol\DTO\BlobResourceContents |
| 145 |
*/ |
| 146 |
public function getResource() |
| 147 |
{ |
| 148 |
return $this->resource; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @return \WP\McpSchema\Common\Protocol\DTO\Annotations|null |
| 153 |
*/ |
| 154 |
public function getAnnotations(): ?Annotations |
| 155 |
{ |
| 156 |
return $this->annotations; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @return array<string, mixed>|null |
| 161 |
*/ |
| 162 |
public function get_meta(): ?array |
| 163 |
{ |
| 164 |
return $this->_meta; |
| 165 |
} |
| 166 |
} |
| 167 |
|