Roll out meta properties in attributes and more schema classes (#129)

This commit is contained in:
Kaipi Yann 2025-11-08 13:58:26 +01:00 • committed by GitHub
commit d3a07912b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,6 +24,7 @@ use Mcp\Exception\InvalidArgumentException;
* description?: string|null,
* mimeType?: string|null,
* annotations?: AnnotationsData|null,
* _meta?: array<string, mixed>
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
@ -42,11 +43,12 @@ class ResourceTemplate implements \JsonSerializable
private const URI_TEMPLATE_PATTERN = '/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/.*{[^{}]+}.*/';
/**
* @param string $uriTemplate a URI template (according to RFC 6570) that can be used to construct resource URIs
* @param string $name A human-readable name for the type of resource this template refers to. This can be used by clients to populate UI elements.
* @param string|null $description This can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a "hint" to the model.
* @param string|null $mimeType The MIME type for all resources that match this template. This should only be included if all resources matching this template have the same type.
* @param Annotations|null $annotations optional annotations for the client
* @param string $uriTemplate a URI template (according to RFC 6570) that can be used to construct resource URIs
* @param string $name A human-readable name for the type of resource this template refers to. This can be used by clients to populate UI elements.
* @param string|null $description This can be used by clients to improve the LLM's understanding of available resources. It can be thought of like a "hint" to the model.
* @param string|null $mimeType The MIME type for all resources that match this template. This should only be included if all resources matching this template have the same type.
* @param Annotations|null $annotations optional annotations for the client
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
public readonly string $uriTemplate,
@ -54,6 +56,7 @@ class ResourceTemplate implements \JsonSerializable
public readonly ?string $description = null,
public readonly ?string $mimeType = null,
public readonly ?Annotations $annotations = null,
public readonly ?array $meta = null,
) {
if (!preg_match(self::RESOURCE_NAME_PATTERN, $name)) {
throw new InvalidArgumentException('Invalid resource name: must contain only alphanumeric characters, underscores, and hyphens.');
@ -75,12 +78,17 @@ class ResourceTemplate implements \JsonSerializable
throw new InvalidArgumentException('Invalid or missing "name" in ResourceTemplate data.');
}
if (!empty($data['_meta']) && !\is_array($data['_meta'])) {
throw new InvalidArgumentException('Invalid "_meta" in ResourceTemplate data.');
}
return new self(
uriTemplate: $data['uriTemplate'],
name: $data['name'],
description: $data['description'] ?? null,
mimeType: $data['mimeType'] ?? null,
annotations: isset($data['annotations']) ? Annotations::fromArray($data['annotations']) : null
annotations: isset($data['annotations']) ? Annotations::fromArray($data['annotations']) : null,
meta: isset($data['_meta']) ? $data['_meta'] : null
);
}
@ -91,6 +99,7 @@ class ResourceTemplate implements \JsonSerializable
* description?: string,
* mimeType?: string,
* annotations?: Annotations,
* _meta?: array<string, mixed>
* }
*/
public function jsonSerialize(): array
@ -108,6 +117,9 @@ class ResourceTemplate implements \JsonSerializable
if (null !== $this->annotations) {
$data['annotations'] = $this->annotations;
}
if (null !== $this->meta) {
$data['_meta'] = $this->meta;
}
return $data;
}