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