create_document
Generate a new Microsoft Word document with customizable metadata such as filename, title, and author. Ideal for managing and organizing documents efficiently.
Instructions
Create a new Word document with optional metadata.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| author | No | ||
| filename | Yes | ||
| title | No |
Implementation Reference
- Core handler function that creates a new Word document (.docx) file using python-docx library. Handles filename extension, writability checks, sets document properties (title, author), ensures required styles (headings, tables), saves the file, and returns success/error message.async def create_document(filename: str, title: Optional[str] = None, author: Optional[str] = None) -> str: """Create a new Word document with optional metadata. Args: filename: Name of the document to create (with or without .docx extension) title: Optional title for the document metadata author: Optional author for the document metadata """ filename = ensure_docx_extension(filename) # Check if file is writeable is_writeable, error_message = check_file_writeable(filename) if not is_writeable: return f"Cannot create document: {error_message}" try: doc = Document() # Set properties if provided if title: doc.core_properties.title = title if author: doc.core_properties.author = author # Ensure necessary styles exist ensure_heading_style(doc) ensure_table_style(doc) # Save the document doc.save(filename) return f"Document {filename} created successfully" except Exception as e: return f"Failed to create document: {str(e)}"
- word_document_server/main.py:440-443 (registration)MCP tool registration using FastMCP @mcp.tool() decorator. Defines the tool entrypoint with input schema inferred from parameters, docstring, and delegates execution to the core handler in document_tools.create_document.@mcp.tool() async def create_document(filename: str, title: Optional[str] = None, author: Optional[str] = None): """Create a new Word document with optional metadata.""" return await document_tools.create_document(filename, title, author)