Skip to main content
Glama

convert_document

Convert documents between formats like PDF, DOCX, HTML, and Markdown while preserving original formatting and styles.

Instructions

Convert documents between formats with enhanced style preservation. Output directory is controlled by OUTPUT_DIR environment variable. All output files will be automatically saved to the directory specified by OUTPUT_DIR with auto-generated names. ⚠️ IMPORTANT: For Markdown to HTML conversion with style preservation, use 'convert_markdown_to_html' tool instead for better results with themes and styling. For creating Word documents from content, use 'create_word_document' tool to avoid conversion loops.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputPathYesInput file path
targetFormatNoTarget format: pdf, html, docx, markdown, md, txt (optional, auto-detected from input if not specified)
preserveFormattingNoPreserve formatting
useInternalPlaywrightNoUse built-in Playwright for PDF conversion

Implementation Reference

  • The core handler function for the 'convert_document' tool. It takes document content and conversion options, applies default styling and watermark for PDF, and dispatches to specific format converters (md, html, pdf, docx).
    async convertDocument(
      content: DocumentContent,
      options: ConversionOptions
    ): Promise<DocumentConversionResult> {
      try {
        const styling = { ...this.defaultStyling, ...options.styling };
        const outputPath =
          options.outputPath ?? this.generateOutputPath(content.title ?? 'document', options.format);
    
        // 为PDF格式设置默认水印配置 - 企业微信风格(最下层)
        if (options.format === 'pdf' && !options.watermark) {
          options.watermark = {
            enabled: true,
            text: 'doc-ops-mcp',
            opacity: 0.015, // 极低透明度(1.5%)
            fontSize: 8,    // 超小字体(8px)
            rotation: -20,  // 轻微旋转
            spacing: {
              x: 600,  // 合适的水平间距
              y: 500   // 合适的垂直间距
            }
          };
        }
    
        switch (options.format) {
          case 'md':
            return await this.convertToMarkdown(content, outputPath);
          case 'html':
            return await this.convertToHTML(content, outputPath, styling);
          case 'pdf':
            return await this.convertToPDF(content, outputPath, styling, options.watermark);
          case 'docx':
            return await this.convertToDocx(content, outputPath, styling);
          default:
            return {
              success: false,
              error: `不支持的格式: ${options.format}`,
              format: options.format,
            };
        }
      } catch (error: any) {
        return {
          success: false,
          error: `转换失败: ${error.message}`,
          format: options.format,
        };
      }
    }
  • TypeScript interfaces defining the input (DocumentContent, ConversionOptions) and output (DocumentConversionResult) schemas for the convertDocument function.
    interface DocumentContent {
      title?: string;
      content: string;
      author?: string;
      description?: string;
      metadata?: {
        [key: string]: string;
      };
    }
    
    interface ConversionOptions {
      outputPath?: string;
      format: 'md' | 'pdf' | 'docx' | 'html';
      styling?: {
        fontSize?: number;
        fontFamily?: string;
        lineHeight?: number;
        margins?: {
          top?: number;
          bottom?: number;
          left?: number;
          right?: number;
        };
        colors?: {
          primary?: string;
          secondary?: string;
          text?: string;
        };
      };
      // PDF水印配置
      watermark?: {
        enabled?: boolean; // 是否启用水印,PDF格式默认为true
        text?: string; // 水印文字,默认为'doc-ops-mcp'
        imagePath?: string; // 水印图片路径,如果提供则优先使用图片
        opacity?: number; // 透明度,0-1之间,默认0.1
        fontSize?: number; // 文字水印字体大小,默认48
        rotation?: number; // 旋转角度,默认-45度
        spacing?: {
          x?: number; // 水印间距X,默认200
          y?: number; // 水印间距Y,默认150
        };
      };
    }
    
    interface DocumentConversionResult {
      success: boolean;
      outputPath?: string;
      error?: string;
      fileSize?: number;
      format: string;
    }
  • Helper method in ConversionPlanner that determines which tool to use for conversions, frequently returning 'convert_document' as the default or mapped tool.
    private getToolForConversion(fromFormat: string, toFormat: string): string {
      // 特殊转换工具映射
      const specialMappings: Record<string, Record<string, string>> = {
        docx: {
          pdf: 'convert_docx_to_pdf',
          html: 'convert_document',
        },
        markdown: {
          html: 'convert_markdown_to_html',
          docx: 'convert_markdown_to_docx',
        },
        html: {
          markdown: 'convert_html_to_markdown',
        },
      };
    
      if (specialMappings[fromFormat]?.[toFormat]) {
        return specialMappings[fromFormat][toFormat];
      }
    
      // 默认使用通用转换工具
      return 'convert_document';
    }
  • Helper method providing detailed descriptions for the 'convert_document' tool based on input/output formats, used in planning.
    private getConvertDocumentDescription(fromFormat: string, toFormat: string): string {
      if (fromFormat === 'html' && toFormat === 'docx') {
        return '将HTML文档转换为DOCX格式,保留样式、结构和格式,生成完整的Word文档';
      }
      if (fromFormat === 'html' && toFormat === 'markdown') {
        return '将HTML文档转换为Markdown格式,保留文档结构和基本格式';
      }
      if (fromFormat === 'docx' && toFormat === 'markdown') {
        return '将DOCX文档转换为Markdown格式,使用优化转换器提取文本内容和结构,保留标题、段落、列表等格式';
      }
      if (fromFormat === 'docx' && toFormat === 'html') {
        return '将DOCX文档转换为HTML格式,使用OOXML解析器保留样式、格式和结构,生成完整的HTML文档';
      }
      return `将${fromFormat.toUpperCase()}格式转换为${toFormat.toUpperCase()}格式,保持文档内容和基本结构`;
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden and adds valuable behavioral context: it discloses that output directory is controlled by OUTPUT_DIR environment variable, files are auto-saved with auto-generated names, and warns about conversion loops for Word documents. This goes beyond basic functionality to describe operational behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with three focused sentences: core functionality, operational behavior, and usage guidance. Each sentence earns its place, though the warning emoji and formatting could be slightly more concise.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a conversion tool with 4 parameters, 100% schema coverage, but no annotations or output schema, the description provides good completeness: it explains the core purpose, behavioral context (output handling), and clear usage guidelines. The main gap is lack of information about return values or error handling.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all 4 parameters thoroughly. The description doesn't add any parameter-specific information beyond what's in the schema, maintaining the baseline score of 3 for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool converts documents between formats with enhanced style preservation, providing a specific verb (convert) and resource (documents). It distinguishes from some siblings by mentioning specific alternatives, though not all sibling tools are addressed.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use alternative tools instead: 'convert_markdown_to_html' for Markdown to HTML with themes/styling and 'create_word_document' for creating Word documents from content. This clearly defines when-not-to-use scenarios with named alternatives.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Tele-AI/doc-ops-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server