create_document
Generate a new Word document with custom filename, title, and author metadata. Simplifies document creation for structured and standardized workflows.
Instructions
Create a new Word document with optional metadata.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| author | No | ||
| filename | Yes | ||
| title | No |
Input Schema (JSON Schema)
{
"properties": {
"author": {
"default": null,
"title": "Author",
"type": "string"
},
"filename": {
"title": "Filename",
"type": "string"
},
"title": {
"default": null,
"title": "Title",
"type": "string"
}
},
"required": [
"filename"
],
"type": "object"
}
Implementation Reference
- Core handler function that implements the logic to create a new Word document (.docx) using python-docx library. Handles filename extension, writability check, sets title/author metadata, ensures required styles (headings, tables), saves the document, 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:94-98 (registration)FastMCP tool registration for 'create_document' using @mcp.tool() decorator. This is the entry point handler that delegates execution to the core implementation in document_tools.create_document.@mcp.tool() def create_document(filename: str, title: str = None, author: str = None): """Create a new Word document with optional metadata.""" return document_tools.create_document(filename, title, author)