generate_document
Generate documents in PDF, DOCX, or ODT format from HTML or Markdown content. Optionally apply templates, replace fields, and overlay a diagonal watermark.
Instructions
Generate a document with optional template, fields, and watermark.
Args: html_content: HTML content for the document body. markdown_content: Markdown content (alternative to HTML). template_name: Name of a pre-uploaded template (ODT/DOCX). fields: Template field replacements (key-value pairs). watermark: Diagonal watermark text overlay. output_format: Output format – PDF, DOCX, or ODT (default: PDF).
Returns: Base64-encoded document bytes.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| html_content | No | ||
| markdown_content | No | ||
| template_name | No | ||
| fields | No | ||
| watermark | No | ||
| output_format | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- mcp/src/docgen_mcp/server.py:83-122 (handler)The @mcp.tool() decorated handler function that generates documents from HTML, Markdown, templates, fields, and watermarks, returning a base64-encoded document.
@mcp.tool() def generate_document( html_content: str | None = None, markdown_content: str | None = None, template_name: str | None = None, fields: dict[str, str] | None = None, watermark: str | None = None, output_format: str = "PDF", ) -> str: """Generate a document with optional template, fields, and watermark. Args: html_content: HTML content for the document body. markdown_content: Markdown content (alternative to HTML). template_name: Name of a pre-uploaded template (ODT/DOCX). fields: Template field replacements (key-value pairs). watermark: Diagonal watermark text overlay. output_format: Output format – PDF, DOCX, or ODT (default: PDF). Returns: Base64-encoded document bytes. """ dg = _get_client() from docgen.models import OutputFormat builder = dg.document() if html_content: builder.html(html_content) if markdown_content: builder.markdown(markdown_content) if template_name: builder.template(template_name) if fields: builder.fields(fields) if watermark: builder.watermark(watermark) builder.output_format(OutputFormat(output_format)) result = builder.generate() return base64.b64encode(result).decode() - mcp/src/docgen_mcp/server.py:83-83 (registration)The tool is registered as an MCP tool via the @mcp.tool() decorator on the generate_document function. The mcp instance (FastMCP('DocGen')) is created at line 25.
@mcp.tool() - mcp/src/docgen_mcp/server.py:28-47 (helper)The _get_client() helper function used by generate_document to lazily initialize and return the DocGen client.
def _get_client(): """Lazy-initialise the DocGen client.""" global _client if _client is None: # Import here so the module can be imported without the SDK installed # (useful for schema introspection) from docgen import DocGen api_key = os.environ.get("DOCGEN_API_KEY", "") if not api_key: raise RuntimeError( "DOCGEN_API_KEY environment variable is required. " "Set it to your DocGen API key before starting the server." ) base_url = os.environ.get("DOCGEN_BASE_URL") kwargs: dict[str, Any] = {"api_key": api_key} if base_url: kwargs["base_url"] = base_url _client = DocGen(**kwargs) return _client