Skip to main content
Glama

get_subtree

Retrieve XML representation of a node and all its descendants from HNPX documents, with configurable pruning levels to control output depth.

Instructions

Retrieve XML representation of node including all descendants, optionally pruned

Args: file_path (str): Path to the HNPX document node_id (str): ID of the node to retrieve pruning_level (str): Depth level - one of: "book", "chapter", "sequence", "beat", "full"

Returns: str: XML representation of the node and its descendants, pruned to specified depth

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
node_idYes
pruning_levelNofull

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler function that parses the HNPX document, finds the specified node, optionally prunes the subtree based on hierarchy level, and returns the XML representation.
    def get_subtree(file_path: str, node_id: str, pruning_level: str = "full") -> str:
        """Retrieve XML representation of node including all descendants, optionally pruned
    
        Args:
            file_path (str): Path to the HNPX document
            node_id (str): ID of the node to retrieve
            pruning_level (str): Depth level - one of: "book", "chapter", "sequence", "beat", "full"
    
        Returns:
            str: XML representation of the node and its descendants, pruned to specified depth
        """
        tree = hnpx.parse_document(file_path)
        node = hnpx.find_node(tree, node_id)
    
        if node is None:
            raise NodeNotFoundError(node_id)
    
        # If no pruning needed, return full subtree
        if pruning_level == "full":
            return etree.tostring(node, encoding="unicode", method="html")
    
        # Validate level parameter
        valid_levels = ["book", "chapter", "sequence", "beat", "full"]
        if pruning_level not in valid_levels:
            raise InvalidAttributeError(
                "pruning_level", pruning_level, f"Must be one of: {', '.join(valid_levels)}"
            )
    
        # Define hierarchy levels
        hierarchy = {"book": 0, "chapter": 1, "sequence": 2, "beat": 3, "full": 5}
    
        max_depth = hierarchy[pruning_level]
    
        # Create a copy of the node to avoid modifying the original
        node_copy = etree.Element(node.tag, node.attrib)
        for child in node:
            node_copy.append(etree.fromstring(etree.tostring(child, encoding="unicode")))
    
        def prune_tree(node: etree.Element, current_depth: int) -> None:
            """Recursively remove nodes beyond max_depth"""
            if current_depth >= max_depth:
                # Remove all children except summary
                children_to_remove = []
                for child in node:
                    if child.tag != "summary":
                        children_to_remove.append(child)
    
                for child in children_to_remove:
                    node.remove(child)
            else:
                # Recursively process children
                for child in list(node):
                    if child.tag in hierarchy:
                        prune_tree(child, current_depth + 1)
    
        # Start pruning from the node (determine depth based on node type)
        node_depth = hierarchy.get(node.tag, 0)
        prune_tree(node_copy, node_depth)
    
        # Return the pruned tree as XML
        return etree.tostring(node_copy, encoding="unicode", pretty_print=True)
  • Registers the get_subtree handler as a tool in the FastMCP application.
    app.tool()(tools.get_subtree)
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden. It mentions the tool retrieves XML representation with pruning, but doesn't disclose important behavioral aspects like whether this is a read-only operation (implied but not stated), error conditions, performance characteristics, or what happens with invalid node IDs. For a tool with 3 parameters and no annotation coverage, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Perfectly structured with a clear purpose statement followed by Args and Returns sections. Every sentence earns its place - the first sentence establishes core functionality, the Args section documents all parameters with their types and constraints, and the Returns section specifies output format. Zero waste.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (3 parameters, XML output, pruning logic) and having an output schema, the description is mostly complete. It explains parameters well and specifies the return format. However, for a tree traversal tool with no annotations, it could benefit from mentioning read-only nature, error handling, or performance considerations for deep trees.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description fully compensates by clearly explaining all 3 parameters: file_path (path to HNPX document), node_id (ID of node to retrieve), and pruning_level (depth level with specific enum values). The Returns section also clarifies the output. This adds substantial value beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Retrieve XML representation'), target resource ('node including all descendants'), and distinguishes from siblings like get_node (which presumably doesn't include descendants) and get_children (which might only get immediate children). The mention of 'optionally pruned' adds important scope information.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies when to use this tool (when you need a node's full subtree in XML format with pruning options) but doesn't explicitly contrast with alternatives like get_node or get_children. It provides context about what the tool does but lacks explicit 'when not to use' guidance or named alternatives.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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