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\Resource;
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:
21: /**
22: * A template description for resources available on the server.
23: *
24: * @extends BaseMetadata<array{
25: * _meta?: array<string, mixed>,
26: * uriTemplate: non-empty-string,
27: * name: non-empty-string,
28: * title?: non-empty-string,
29: * description?: non-empty-string,
30: * mimeType?: non-empty-string,
31: * annotations?: template-type<Annotations, Arrayable, 'T'>,
32: * icons?: list<template-type<Icon, Arrayable, 'T'>>,
33: * }>
34: */
35: final readonly class ResourceTemplate extends BaseMetadata
36: {
37: /**
38: * @param non-empty-string $uriTemplate A URI template (according to RFC 6570) that can be used
39: * to construct resource URIs.
40: * @param null|non-empty-string $description A description of what this template is for.
41: * @param null|non-empty-string $mimeType The MIME type for all resources that match this template. This should only
42: * be included if all resources matching this template have the same type.
43: * @param null|Annotations $annotations Optional annotations for the client.
44: * @param null|list<Icon> $icons Optional set of sized icons that the client can display in a user interface.
45: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
46: * additional metadata to their interactions.
47: */
48: public function __construct(
49: public string $uriTemplate,
50: string $name,
51: ?string $title = null,
52: public ?string $description = null,
53: public ?string $mimeType = null,
54: public ?Annotations $annotations = null,
55: public ?array $icons = null,
56: public ?array $meta = null,
57: ) {
58: ResourceValidator::assertValidUriTemplate($uriTemplate);
59:
60: parent::__construct($name, $title);
61: }
62:
63: #[\Override]
64: public function toArray(): array
65: {
66: return array_filter([
67: '_meta' => $this->meta,
68: 'uriTemplate' => $this->uriTemplate,
69: 'name' => $this->name,
70: 'title' => $this->title,
71: 'description' => $this->description,
72: 'mimeType' => $this->mimeType,
73: 'annotations' => $this->annotations?->toArray(),
74: 'icons' => null === $this->icons ? null : array_map(
75: static fn(Icon $icon): array => $icon->toArray(),
76: $this->icons,
77: ),
78: ], static fn(mixed $value): bool => null !== $value);
79: }
80: }
81: