move_nodes
Relocate multiple nodes to a new parent within HNPX documents to restructure hierarchical fiction writing content.
Instructions
Move multiple nodes between parents
Args: file_path (str): Path to the HNPX document node_ids (list): List of node IDs to move new_parent_id (str): ID of the new parent node
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| node_ids | Yes | ||
| new_parent_id | Yes |
Implementation Reference
- src/hnpx_sdk/tools.py:548-592 (handler)The main handler function that implements the logic for moving multiple nodes to a new parent in the HNPX document tree, including hierarchy validation, error checking, and XML manipulation using lxml.def move_nodes(file_path: str, node_ids: list, new_parent_id: str) -> str: """Move multiple nodes between parents Args: file_path (str): Path to the HNPX document node_ids (list): List of node IDs to move new_parent_id (str): ID of the new parent node """ tree = hnpx.parse_document(file_path) new_parent = hnpx.find_node(tree, new_parent_id) if new_parent is None: raise NodeNotFoundError(new_parent_id) # Check hierarchy validity for new parent valid_hierarchy = { "book": ["chapter"], "chapter": ["sequence"], "sequence": ["beat"], "beat": ["paragraph"], "paragraph": [], } nodes_moved = 0 for node_id in node_ids: node = hnpx.find_node(tree, node_id) if node is None: raise NodeNotFoundError(node_id) # Check if trying to move root if node.tag == "book": raise InvalidOperationError("move_nodes", "Cannot move book element") # Check hierarchy validity if node.tag not in valid_hierarchy[new_parent.tag]: raise InvalidHierarchyError(new_parent.tag, node.tag) old_parent = node.getparent() old_parent.remove(node) new_parent.append(node) nodes_moved += 1 hnpx.save_document(tree, file_path) return f"Moved {nodes_moved} nodes to parent {new_parent_id}"
- src/hnpx_sdk/server.py:29-29 (registration)Registers the move_nodes handler as an MCP tool using FastMCP's app.tool() decorator.app.tool()(tools.move_nodes)