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\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Arrayable;
18: use Nexus\Mcp\Core\Schema\MetaObject;
19:
20: /**
21: * A request from the assistant to call a tool.
22: *
23: * @implements Arrayable<array{
24: * id: non-empty-string,
25: * input: array<string, mixed>,
26: * name: non-empty-string,
27: * type: 'tool_use',
28: * _meta?: template-type<MetaObject, Arrayable, 'T'>,
29: * }>
30: *
31: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#toolusecontent
32: */
33: final readonly class ToolUseContent implements Arrayable, SamplingMessageContentBlock
34: {
35: public const string TYPE = 'tool_use';
36:
37: /**
38: * @var non-empty-string
39: */
40: public string $id;
41:
42: /**
43: * @var non-empty-string
44: */
45: public string $name;
46:
47: /**
48: * @param array<string, mixed> $input
49: */
50: public function __construct(
51: string $id,
52: string $name,
53: public array $input,
54: public MetaObject $meta = new MetaObject(),
55: ) {
56: Assert::that($id)->isNonEmptyString('"content.id" must be a non-empty string.');
57: Assert::that($name)->isNonEmptyString('"content.name" must be a non-empty string.');
58:
59: $this->id = $id;
60: $this->name = $name;
61: }
62:
63: /**
64: * @param array<string, mixed> $data
65: */
66: #[\Override]
67: public static function fromArray(array $data): static
68: {
69: Assert::that($data)->hasOffset('type', '"content" missing the required "type" key.');
70: $type = $data['type'];
71: Assert::that($type)->isIdentical(self::TYPE, '"content.type" must be {other}, {value} given.');
72:
73: Assert::that($data)->hasOffset('id', '"content" missing the required "id" key.');
74: $id = $data['id'];
75: Assert::that($id)->isString('"content.id" must be a string, {type} given.');
76:
77: Assert::that($data)->hasOffset('name', '"content" missing the required "name" key.');
78: $name = $data['name'];
79: Assert::that($name)->isString('"content.name" must be a string, {type} given.');
80:
81: Assert::that($data)->hasOffset('input', '"content" missing the required "input" key.');
82: Assert::that($data['input'])
83: ->isArray('"content.input" must be an object, {type} given.')
84: ->isMap('"content.input" must be a string-keyed object.')
85: ;
86: $input = $data['input'];
87:
88: $meta = new MetaObject();
89:
90: if (\array_key_exists('_meta', $data)) {
91: Assert::that($data['_meta'])
92: ->isArray('"content._meta" must be an object, {type} given.')
93: ->isMap('"content._meta" must be a string-keyed object.')
94: ;
95: $meta = MetaObject::fromArray($data['_meta']);
96: }
97:
98: return new self($id, $name, $input, $meta);
99: }
100:
101: #[\Override]
102: public function toArray(): array
103: {
104: $data = [
105: 'id' => $this->id,
106: 'input' => $this->input,
107: 'name' => $this->name,
108: 'type' => self::TYPE,
109: ];
110:
111: $meta = $this->meta->toArray();
112:
113: if ([] !== $meta) {
114: $data['_meta'] = $meta;
115: }
116:
117: return $data;
118: }
119:
120: #[\Override]
121: public function jsonSerialize(): array
122: {
123: $data = $this->toArray();
124:
125: if ([] === $this->input) {
126: $data['input'] = new \stdClass();
127: }
128:
129: return $data;
130: }
131: }
132: