Skip to content

Commit 3fca8a5

Browse files
committed
Refactor the schema tool array to use normalizeSchemaProperties method
1 parent 42fe143 commit 3fca8a5

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/Schema/Tool.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,24 @@ public static function fromArray(array $data): self
8484
if (!isset($data['inputSchema']['type']) || 'object' !== $data['inputSchema']['type']) {
8585
throw new InvalidArgumentException('Tool inputSchema must be of type "object".');
8686
}
87-
if (isset($data['inputSchema']['properties']) && \is_array($data['inputSchema']['properties']) && empty($data['inputSchema']['properties'])) {
88-
$data['inputSchema']['properties'] = new \stdClass();
89-
}
87+
$inputSchema = self::normalizeSchemaProperties($data['inputSchema']);
9088

89+
$outputSchema = null;
9190
if (isset($data['outputSchema']) && \is_array($data['outputSchema'])) {
9291
if (!isset($data['outputSchema']['type']) || 'object' !== $data['outputSchema']['type']) {
9392
throw new InvalidArgumentException('Tool outputSchema must be of type "object".');
9493
}
95-
if (isset($data['outputSchema']['properties']) && \is_array($data['outputSchema']['properties']) && empty($data['outputSchema']['properties'])) {
96-
$data['outputSchema']['properties'] = new \stdClass();
97-
}
94+
$outputSchema = self::normalizeSchemaProperties($data['outputSchema']);
9895
}
9996

10097
return new self(
10198
$data['name'],
102-
$data['inputSchema'],
99+
$inputSchema,
103100
isset($data['description']) && \is_string($data['description']) ? $data['description'] : null,
104101
isset($data['annotations']) && \is_array($data['annotations']) ? ToolAnnotations::fromArray($data['annotations']) : null,
105102
isset($data['icons']) && \is_array($data['icons']) ? array_map(Icon::fromArray(...), $data['icons']) : null,
106103
isset($data['_meta']) && \is_array($data['_meta']) ? $data['_meta'] : null,
107-
isset($data['outputSchema']) && \is_array($data['outputSchema']) ? $data['outputSchema'] : null,
104+
$outputSchema,
108105
);
109106
}
110107

@@ -143,4 +140,20 @@ public function jsonSerialize(): array
143140

144141
return $data;
145142
}
143+
144+
/**
145+
* Normalize schema properties: convert an empty properties array to stdClass.
146+
*
147+
* @param array<string, mixed> $schema
148+
*
149+
* @return array<string, mixed>
150+
*/
151+
private static function normalizeSchemaProperties(array $schema): array
152+
{
153+
if (isset($schema['properties']) && \is_array($schema['properties']) && empty($schema['properties'])) {
154+
$schema['properties'] = new \stdClass();
155+
}
156+
157+
return $schema;
158+
}
146159
}

0 commit comments

Comments
 (0)