Skip to main content
Glama

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
NameRequiredDescriptionDefault
file_pathYes
node_idsYes
new_parent_idYes

Implementation Reference

  • 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}"
  • Registers the move_nodes handler as an MCP tool using FastMCP's app.tool() decorator.
    app.tool()(tools.move_nodes)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mozhaa/hnpx-sdk'

If you have feedback or need assistance with the MCP directory API, please join our Discord server