#!/usr/bin/env python3
"""
MCP tools for Markdown to PDF conversion.
"""
import os
from pathlib import Path
from server import mcp
from utils.converters import markdown_to_pdf
@mcp.tool()
def convert_markdown_to_pdf(
working_dir: str,
markdown_content: str,
output_filename: str = "output.pdf",
size: str = "l",
pdf_engine: str = "weasyprint"
) -> str:
"""
Convert Markdown content to PDF with customizable formatting options.
Args:
working_dir: Absolute path to the working directory for file operations
markdown_content: The markdown content to convert to PDF
output_filename: Name of the output PDF file relative to working_dir (default: output.pdf)
size: Output size - 's' (small/compact), 'm' (medium), 'l' (large/detailed) (default: l)
pdf_engine: PDF engine to use - weasyprint or pdflatex (default: weasyprint)
Returns:
A message indicating success or failure of the conversion
"""
try:
# Validate working directory
working_path = Path(working_dir)
if not working_path.is_absolute():
return f"Error: working_dir must be an absolute path, got: {working_dir}"
if not working_path.exists():
return f"Error: working_dir does not exist: {working_dir}"
if not working_path.is_dir():
return f"Error: working_dir is not a directory: {working_dir}"
# Ensure output filename has .pdf extension
if not output_filename.lower().endswith('.pdf'):
output_filename += '.pdf'
# Create output path relative to working directory
output_path = working_path / output_filename
output_path.parent.mkdir(parents=True, exist_ok=True)
# Convert markdown to PDF
success, message = markdown_to_pdf(
markdown_content=markdown_content,
output_path=str(output_path),
size=size,
pdf_engine=pdf_engine
)
if success:
file_size = output_path.stat().st_size if output_path.exists() else 0
return f"{message}\nFile size: {file_size:,} bytes"
else:
return f"Conversion failed: {message}"
except Exception as e:
return f"Error during markdown to PDF conversion: {str(e)}"
@mcp.tool()
def convert_markdown_file_to_pdf(
working_dir: str,
markdown_file_path: str,
output_filename: str = None,
size: str = "l",
pdf_engine: str = "weasyprint"
) -> str:
"""
Convert a Markdown file to PDF with customizable formatting options.
Args:
working_dir: Absolute path to the working directory for file operations
markdown_file_path: Path to the markdown file relative to working_dir
output_filename: Name of the output PDF file relative to working_dir (if not provided, uses same name as input with .pdf extension)
size: Output size - 's' (small/compact), 'm' (medium), 'l' (large/detailed) (default: l)
pdf_engine: PDF engine to use - weasyprint or pdflatex (default: weasyprint)
Returns:
A message indicating success or failure of the conversion
"""
try:
# Validate working directory
working_path = Path(working_dir)
if not working_path.is_absolute():
return f"Error: working_dir must be an absolute path, got: {working_dir}"
if not working_path.exists():
return f"Error: working_dir does not exist: {working_dir}"
if not working_path.is_dir():
return f"Error: working_dir is not a directory: {working_dir}"
# Create input path relative to working directory
input_path = working_path / markdown_file_path
if not input_path.exists():
return f"Error: Input file '{markdown_file_path}' does not exist at: {input_path}"
if not input_path.suffix.lower() in ['.md', '.markdown']:
return f"Error: '{markdown_file_path}' is not a Markdown file. Please provide a .md or .markdown file."
# Set output filename if not provided
if output_filename is None:
output_filename = input_path.stem + '.pdf'
elif not output_filename.lower().endswith('.pdf'):
output_filename += '.pdf'
# Create output path relative to working directory
output_path = working_path / output_filename
output_path.parent.mkdir(parents=True, exist_ok=True)
# Read markdown content
with open(input_path, 'r', encoding='utf-8') as f:
markdown_content = f.read()
# Convert to PDF
success, message = markdown_to_pdf(
markdown_content=markdown_content,
output_path=str(output_path),
size=size,
pdf_engine=pdf_engine
)
if success:
file_size = output_path.stat().st_size if output_path.exists() else 0
return f"{message}\nInput: {markdown_file_path} (at: {input_path})\nFile size: {file_size:,} bytes"
else:
return f"Conversion failed: {message}"
except Exception as e:
return f"Error during file conversion: {str(e)}"