Skip to main content
Glama

create_beat

Add a narrative beat to a hierarchical fiction document by specifying parent sequence and summary text for structured story development.

Instructions

Create a new beat element

Args: file_path (str): Path to the HNPX document parent_id (str): ID of the parent sequence element summary (str): Beat summary text

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
parent_idYes
summaryYes

Implementation Reference

  • The main handler function that parses the HNPX document, creates a new 'beat' element under the specified parent using the _create_element helper, saves the document, and returns the new beat's ID.
    def create_beat(file_path: str, parent_id: str, summary: str) -> str:
        """Create a new beat element
    
        Args:
            file_path (str): Path to the HNPX document
            parent_id (str): ID of the parent sequence element
            summary (str): Beat summary text
        """
        tree = hnpx.parse_document(file_path)
    
        new_id = _create_element(tree, parent_id, "beat", {}, summary)
    
        hnpx.save_document(tree, file_path)
    
        return f"Created beat with id {new_id}"
  • Registers the create_beat function as a tool in the FastMCP application.
    app.tool()(tools.create_beat)
  • Shared helper function used by create_beat (and other create_* functions) to validate hierarchy, generate unique ID, create the element with attributes and summary, and return the new ID.
    def _create_element(
        tree: etree.ElementTree,
        parent_id: str,
        element_tag: str,
        attributes: dict,
        summary_text: str,
    ) -> str:
        """Generic element creation helper"""
        parent = hnpx.find_node(tree, parent_id)
        if parent is None:
            raise NodeNotFoundError(parent_id)
    
        # Check hierarchy
        valid_hierarchy = {
            "book": ["chapter"],
            "chapter": ["sequence"],
            "sequence": ["beat"],
            "beat": ["paragraph"],
        }
    
        if (
            parent.tag not in valid_hierarchy
            or element_tag not in valid_hierarchy[parent.tag]
        ):
            raise InvalidHierarchyError(parent.tag, element_tag)
    
        # Generate unique ID
        existing_ids = hnpx.get_all_ids(tree)
        new_id = hnpx.generate_unique_id(existing_ids)
        attributes["id"] = new_id
    
        # Create element
        element = etree.SubElement(parent, element_tag, **attributes)
        summary = etree.SubElement(element, "summary")
        summary.text = summary_text
    
        return new_id

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