| 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\Tool; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Arrayable; |
| 17: | use Nexus\Mcp\Schema\BaseMetadata; |
| 18: | use Nexus\Mcp\Schema\Icon; |
| 19: | use Nexus\Mcp\Schema\Tool\ToolSchema\ToolSchema; |
| 20: | |
| 21: | /** |
| 22: | * Definition for a tool the client can call. |
| 23: | * |
| 24: | * @extends BaseMetadata<array{ |
| 25: | * _meta?: array<string, mixed>, |
| 26: | * name: non-empty-string, |
| 27: | * inputSchema: array<string, mixed>, |
| 28: | * title?: non-empty-string, |
| 29: | * description?: non-empty-string, |
| 30: | * outputSchema?: array<string, mixed>, |
| 31: | * icons?: list<template-type<Icon, Arrayable, 'T'>>, |
| 32: | * annotations?: template-type<ToolAnnotations, Arrayable, 'T'>, |
| 33: | * }> |
| 34: | */ |
| 35: | final readonly class Tool extends BaseMetadata |
| 36: | { |
| 37: | /** |
| 38: | * @template T |
| 39: | * @template U of array<string, mixed> |
| 40: | * |
| 41: | * @param null|non-empty-string $description A human-readable description of the tool. |
| 42: | * @param null|list<Icon> $icons Optional set of sized icons that the client can display in a user interface. |
| 43: | * @param null|ToolAnnotations $annotations Optional additional tool information. Display name precedence order is: |
| 44: | * `title`, `annotations.title`, then `name`. |
| 45: | * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach |
| 46: | * additional metadata to their interactions. |
| 47: | * @param ToolSchema<covariant T, covariant U> $inputSchema A JSON Schema object defining the expected parameters for the tool. |
| 48: | * @param null|ToolSchema<covariant T, covariant U> $outputSchema An optional JSON Schema object defining the structure of the tool's |
| 49: | * output returned in the structuredContent field of a `CallToolResult`. |
| 50: | */ |
| 51: | public function __construct( |
| 52: | string $name, |
| 53: | public ToolSchema $inputSchema, |
| 54: | ?string $title = null, |
| 55: | public ?string $description = null, |
| 56: | public ?ToolSchema $outputSchema = null, |
| 57: | public ?array $icons = null, |
| 58: | public ?ToolAnnotations $annotations = null, |
| 59: | public ?array $meta = null, |
| 60: | ) { |
| 61: | parent::__construct($name, $title); |
| 62: | } |
| 63: | |
| 64: | #[\Override] |
| 65: | public function toArray(): array |
| 66: | { |
| 67: | return array_filter([ |
| 68: | '_meta' => $this->meta, |
| 69: | 'name' => $this->name, |
| 70: | 'inputSchema' => $this->inputSchema->toArray(), |
| 71: | 'title' => $this->title, |
| 72: | 'description' => $this->description, |
| 73: | 'outputSchema' => $this->outputSchema?->toArray(), |
| 74: | 'icons' => null === $this->icons ? null : array_map( |
| 75: | static fn(Icon $icon): array => $icon->toArray(), |
| 76: | $this->icons, |
| 77: | ), |
| 78: | 'annotations' => $this->annotations?->toArray(), |
| 79: | ], static fn(mixed $value): bool => null !== $value); |
| 80: | } |
| 81: | } |
| 82: |