1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 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\Core\Schema\Sampling;
15:
16: use Nexus\Mcp\Core\Schema\Arrayable;
17: use Nexus\Mcp\Core\Schema\Enum\ToolChoiceMode;
18: use Nexus\Mcp\Core\Validation\EnumValueValidator;
19:
20: /**
21: * Controls tool selection behavior for sampling requests.
22: *
23: * @implements Arrayable<array{mode?: value-of<ToolChoiceMode>}>
24: *
25: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#toolchoice
26: */
27: final readonly class ToolChoice implements Arrayable
28: {
29: public function __construct(public ?ToolChoiceMode $mode = null)
30: {
31: }
32:
33: /**
34: * @param array<string, mixed> $data
35: */
36: #[\Override]
37: public static function fromArray(array $data): static
38: {
39: $mode = null;
40:
41: if (\array_key_exists('mode', $data)) {
42: $mode = EnumValueValidator::parse(ToolChoiceMode::class, $data['mode'], '"toolChoice.mode"');
43: }
44:
45: return new self($mode);
46: }
47:
48: #[\Override]
49: public function toArray(): array
50: {
51: if (null === $this->mode) {
52: return [];
53: }
54:
55: return ['mode' => $this->mode->value];
56: }
57:
58: #[\Override]
59: public function jsonSerialize(): array|\stdClass
60: {
61: $data = $this->toArray();
62:
63: return [] === $data ? new \stdClass() : $data;
64: }
65: }
66: