create_chapter
Adds a chapter element to HNPX documents for structured fiction writing, specifying title, summary, and point-of-view character to organize narrative content.
Instructions
Create a new chapter element
Args: file_path (str): Path to the HNPX document parent_id (str): ID of the parent book element title (str): Chapter title summary (str): Chapter summary text pov (Optional[str]): Point-of-view character identifier
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| parent_id | Yes | ||
| title | Yes | ||
| summary | Yes | ||
| pov | No |
Implementation Reference
- src/hnpx_sdk/tools.py:266-288 (handler)The handler function implementing the create_chapter tool. It parses the HNPX document, creates a new chapter element under the specified parent using helper, saves the document, and returns the new chapter ID.def create_chapter( file_path: str, parent_id: str, title: str, summary: str, pov: Optional[str] = None ) -> str: """Create a new chapter element Args: file_path (str): Path to the HNPX document parent_id (str): ID of the parent book element title (str): Chapter title summary (str): Chapter summary text pov (Optional[str]): Point-of-view character identifier """ tree = hnpx.parse_document(file_path) attributes = {"title": title} if pov: attributes["pov"] = pov new_id = _create_element(tree, parent_id, "chapter", attributes, summary) hnpx.save_document(tree, file_path) return f"Created chapter with id {new_id}"
- src/hnpx_sdk/server.py:18-18 (registration)Registers the create_chapter handler as a tool in the FastMCP application.app.tool()(tools.create_chapter)
- src/hnpx_sdk/tools.py:227-263 (helper)Shared helper function for creating new elements with hierarchy validation, unique ID generation, and summary subelement. Used by create_chapter and similar tools.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