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\Tool\Tool;
17:
18: /**
19: * Base interface for metadata with name (identifier) and title (display name) properties.
20: *
21: * @template T of array{name: non-empty-string, title?: non-empty-string}
22: *
23: * @implements Arrayable<T>
24: */
25: abstract readonly class BaseMetadata implements \JsonSerializable, Arrayable
26: {
27: /**
28: * @param non-empty-string $name Intended for programmatic or logical use, but used as a display name in past specs or fallback.
29: * @param null|non-empty-string $title Intended for UI and end-user contexts — optimized to be human-readable and easily understood,
30: * even by those unfamiliar with domain-specific terminology. If not provided, the name should be
31: * used for display (except for Tool, where `annotations.title` should be given precedence over
32: * using `name`, if present).
33: */
34: public function __construct(
35: public string $name,
36: public ?string $title = null,
37: ) {}
38:
39: /**
40: * Gets the display name of an object with `BaseMetadata`.
41: *
42: * For tools, the precedence is `title` -> `annotations.title` -> `name`.
43: * For other objects, the precedence is `title` -> `name`.
44: *
45: * @return non-empty-string
46: */
47: final public function getDisplayName(): string
48: {
49: if (null !== $this->title) {
50: return $this->title;
51: }
52:
53: if ($this instanceof Tool && null !== $this->annotations?->title) {
54: return $this->annotations->title;
55: }
56:
57: return $this->name;
58: }
59:
60: /**
61: * @return T
62: */
63: #[\Override]
64: public function jsonSerialize(): array
65: {
66: return $this->toArray();
67: }
68: }
69: