reorder_children
Reorganize child elements in HNPX documents by specifying a new order for child IDs under a parent node to restructure narrative hierarchies.
Instructions
Reorganize the order of child elements
Args: file_path (str): Path to the HNPX document parent_id (str): ID of the parent node child_ids (list): List of child IDs in the desired order
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| parent_id | Yes | ||
| child_ids | Yes |
Implementation Reference
- src/hnpx_sdk/tools.py:447-484 (handler)The core handler function for the 'reorder_children' tool. It parses the HNPX XML document, locates the specified parent node, validates that the provided child_ids match all current non-summary children, reorders them by removing and re-appending in the new sequence, saves the updated document, and returns a confirmation message.def reorder_children(file_path: str, parent_id: str, child_ids: list) -> str: """Reorganize the order of child elements Args: file_path (str): Path to the HNPX document parent_id (str): ID of the parent node child_ids (list): List of child IDs in the desired order """ tree = hnpx.parse_document(file_path) parent = hnpx.find_node(tree, parent_id) if parent is None: raise NodeNotFoundError(parent_id) # Get current children (excluding summary) current_children = [child for child in parent if child.tag != "summary"] current_ids = [child.get("id") for child in current_children] # Validate input if set(child_ids) != set(current_ids): raise InvalidOperationError( "reorder_children", "child_ids must contain all existing child IDs" ) # Create mapping and reorder child_map = {child.get("id"): child for child in current_children} # Remove all children (except summary) for child in current_children: parent.remove(child) # Add back in new order for child_id in child_ids: parent.append(child_map[child_id]) hnpx.save_document(tree, file_path) return f"Reordered children of node {parent_id}"
- src/hnpx_sdk/server.py:30-30 (registration)Registers the reorder_children function as an MCP tool using the FastMCP app.tool() decorator.app.tool()(tools.reorder_children)