add_endnote_to_document
Insert endnotes into specific paragraphs of a Word document by specifying the filename, paragraph index, and endnote text.
Instructions
Add an endnote to a specific paragraph in a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endnote_text | Yes | ||
| filename | Yes | ||
| paragraph_index | Yes |
Implementation Reference
- Core implementation of the add_endnote_to_document tool. Loads the Word document using python-docx, validates the paragraph index, inserts a superscript '†' reference at the end of the specified paragraph, ensures an 'Endnotes:' section exists (adding it with a page break if necessary), appends the endnote text there, and saves the document. Includes input validation and error handling.async def add_endnote_to_document(filename: str, paragraph_index: int, endnote_text: str) -> str: """Add an endnote to a specific paragraph in a Word document. Args: filename: Path to the Word document paragraph_index: Index of the paragraph to add endnote to (0-based) endnote_text: Text content of the endnote """ filename = ensure_docx_extension(filename) # Ensure paragraph_index is an integer try: paragraph_index = int(paragraph_index) except (ValueError, TypeError): return "Invalid parameter: paragraph_index must be an integer" if not os.path.exists(filename): return f"Document {filename} does not exist" # Check if file is writeable is_writeable, error_message = check_file_writeable(filename) if not is_writeable: return f"Cannot modify document: {error_message}. Consider creating a copy first." try: doc = Document(filename) # Validate paragraph index if paragraph_index < 0 or paragraph_index >= len(doc.paragraphs): return f"Invalid paragraph index. Document has {len(doc.paragraphs)} paragraphs (0-{len(doc.paragraphs)-1})." paragraph = doc.paragraphs[paragraph_index] # Add endnote reference last_run = paragraph.add_run() last_run.text = "†" # Unicode dagger symbol common for endnotes last_run.font.superscript = True # Check if endnotes section exists, if not create it endnotes_heading_found = False for para in doc.paragraphs: if para.text == "Endnotes:" or para.text == "ENDNOTES": endnotes_heading_found = True break if not endnotes_heading_found: # Add a page break before endnotes section doc.add_page_break() doc.add_heading("Endnotes:", level=1) # Add the endnote text endnote_para = doc.add_paragraph("† " + endnote_text) endnote_para.style = "Endnote Text" if "Endnote Text" in doc.styles else "Normal" doc.save(filename) return f"Endnote added to paragraph {paragraph_index} in {filename}" except Exception as e: return f"Failed to add endnote: {str(e)}"
- word_document_server/main.py:323-326 (registration)MCP tool registration using the @mcp.tool() decorator. This synchronous wrapper function delegates execution to the async handler in footnote_tools.py, defining the tool's interface with type hints and docstring that serve as input schema.@mcp.tool() def add_endnote_to_document(filename: str, paragraph_index: int, endnote_text: str): """Add an endnote to a specific paragraph in a Word document.""" return footnote_tools.add_endnote_to_document(filename, paragraph_index, endnote_text)
- Re-exports add_endnote_to_document from footnote_tools.py, facilitating its import in main.py as part of the tools package.from word_document_server.tools.footnote_tools import ( add_footnote_to_document, add_endnote_to_document, convert_footnotes_to_endnotes_in_document, customize_footnote_style )