Skip to content

Commit c7af7bd

Browse files
kingpanther13claude
andcommitted
feat: add items schema for array type parameters in proxy
Add _get_array_items_schema() to enrich array-type parameters with proper JSON Schema items (e.g. list[str] -> items: {type: string}). Handles Union unwrapping for list[str] | None patterns. Needed for future proxy phases where many tools use list[str] parameters. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7906a2c commit c7af7bd

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

src/ha_mcp/tools/tool_proxy.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,25 @@ def _extract_tool_metadata(func: Any) -> tuple[str, str, dict[str, Any]]:
210210
args = getattr(hint, "__args__", ())
211211

212212
if origin is Annotated:
213-
base_type = args[0] if args else str
213+
type_to_inspect = args[0] if args else str
214214
for metadata in args[1:]:
215215
if isinstance(metadata, type(Field())):
216216
field_info = metadata
217217
if hasattr(field_info, "description") and field_info.description:
218218
param_schema["description"] = field_info.description
219219
if hasattr(field_info, "default") and field_info.default is not None:
220220
param_schema["default"] = field_info.default
221-
# Map Python types to JSON schema types
222-
param_schema["type"] = _python_type_to_json(base_type)
223221
else:
224-
param_schema["type"] = _python_type_to_json(hint)
222+
type_to_inspect = hint
223+
224+
json_type = _python_type_to_json(type_to_inspect)
225+
param_schema["type"] = json_type
226+
227+
# Enrich array types with items schema (e.g. list[str] -> items: {type: string})
228+
if json_type == "array":
229+
item_schema = _get_array_items_schema(type_to_inspect)
230+
if item_schema:
231+
param_schema["items"] = item_schema
225232

226233
properties[param_name] = param_schema
227234

@@ -236,6 +243,24 @@ def _extract_tool_metadata(func: Any) -> tuple[str, str, dict[str, Any]]:
236243
return name, description, schema
237244

238245

246+
def _get_array_items_schema(python_type: Any) -> dict[str, str] | None:
247+
"""Extract items schema for array types (e.g. list[str] -> {"type": "string"})."""
248+
effective = python_type
249+
eff_origin = getattr(effective, "__origin__", None)
250+
251+
# Unwrap Union to find the list type (e.g. list[str] | None)
252+
if eff_origin is types.UnionType:
253+
for arg in effective.__args__:
254+
if arg is not type(None) and getattr(arg, "__origin__", arg) is list:
255+
effective = arg
256+
break
257+
258+
type_args = getattr(effective, "__args__", ())
259+
if type_args:
260+
return {"type": _python_type_to_json(type_args[0])}
261+
return None
262+
263+
239264
def _python_type_to_json(python_type: Any) -> str:
240265
"""Map Python type hints to JSON Schema types."""
241266
origin = getattr(python_type, "__origin__", None)

0 commit comments

Comments
 (0)