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;
15:
16: use Nexus\Mcp\Schema\Enum\Role;
17:
18: /**
19: * Optional annotations for the client. The client can use annotations to inform how objects are used or displayed.
20: *
21: * @implements Arrayable<array{
22: * audience?: list<'assistant'|'user'>,
23: * lastModified?: non-empty-string,
24: * priority?: float,
25: * }>
26: */
27: final readonly class Annotations implements \JsonSerializable, Arrayable
28: {
29: /**
30: * @param null|list<Role> $audience Describes who the intended customer of this object or data is.
31: * @param null|\DateTimeImmutable $lastModified The moment the resource was last modified, as an ISO 8601 formatted string.
32: * @param null|float $priority Describes how important this data is for operating the server.
33: */
34: public function __construct(
35: public ?array $audience = null,
36: public ?\DateTimeImmutable $lastModified = null,
37: public ?float $priority = null,
38: ) {
39: if (isset($this->priority) && ($this->priority < 0.0 || $this->priority > 1.0)) {
40: throw new \InvalidArgumentException('$priority must be between 0.0 and 1.0.');
41: }
42: }
43:
44: #[\Override]
45: public function toArray(): array
46: {
47: return array_filter([
48: 'audience' => null === $this->audience ? null : array_map(
49: static fn(Role $role): string => $role->value,
50: $this->audience,
51: ),
52: 'lastModified' => $this->lastModified?->format('Y-m-d\TH:i:sp'),
53: 'priority' => $this->priority,
54: ], static fn(mixed $value): bool => null !== $value);
55: }
56:
57: /**
58: * @return template-type<self, Arrayable, 'T'>
59: */
60: #[\Override]
61: public function jsonSerialize(): array
62: {
63: return $this->toArray();
64: }
65: }
66: