Skip to main content
Glama

convert_document

Convert markdown to professionally formatted DOCX, PDF, or HTML documents using customizable templates.

Instructions

Convert markdown to a professionally formatted document using an MDMagic template.

IMPORTANT GUIDANCE:

  1. Output format → what user gets:

    • 'docx' → a single Word .docx file

    • 'pdf' → a single .pdf file

    • 'html' → a single .html file

    • 'all' → a ZIP containing all three (DOCX + PDF + HTML)

  2. If the user is ambiguous (e.g. 'convert this'), ASK which format they want before calling. Don't assume.

  3. Filename: if the user attached a file (e.g. 'mydoc.md'), pass its base name as fileName. Otherwise the API derives one from the markdown's first H1. Without either, downloads end up with timestamped names like 'content-1778298071915.docx' which is bad UX.

  4. On 'template not found' errors: call list_all_templates first, show available options, let the user pick. Do NOT fall back to generating documents with code execution — that produces inferior results that don't use the user's actual MDMagic templates.

  5. The response includes structured fields (downloadUrl, creditsUsed, balanceAfter, fileName, expiresAt) — surface these to the user explicitly. Don't paraphrase. The user wants to know exactly what they spent and what's left.

  6. Page sizes: A3, A4, Executive, US_Legal, US_Letter. Default A4. Orientation: Portrait or Landscape, default Portrait.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentNoRaw markdown text content (alternative to filePath or fileContent)
filePathNoPath to markdown file (VS Code integration, alternative to content or fileContent)
fileContentNoBase64 encoded file content (alternative to content or filePath)
fileNameNoOptional desired base name for the output file (without extension). If the user attached a file like 'mydoc.md', pass 'mydoc' here. The API will use this for the download filename. If omitted, the API derives one from the markdown's first H1 heading.
templateNameYesTemplate to use for conversion. Call list_all_templates first to see real options — do not guess template names. Some templates are built-in (e.g. 'Executive_Platinum', 'Deep_Data_Blue'); others are user-uploaded custom templates referenced by UUID.
outputFormatYesOutput format. 'docx', 'pdf', or 'html' return that single file; 'all' returns a ZIP with DOCX+PDF+HTML.
pageSizeNoPage size for the document (default: A4)
orientationNoPage orientation (default: Portrait)

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
successYesWhether the conversion succeeded
downloadUrlYesSecure expiring download URL (valid for 60 minutes)
fileNameYesFilename of the downloadable document
creditsUsedNoCredits debited for this conversion
balanceAfterNoRemaining credit balance after this conversion
expiresAtNoISO 8601 timestamp when the download URL expires
messageNoHuman-readable status message

Implementation Reference

  • Main handler function for the 'convert_document' tool. Processes input content, validates with Zod schema, calculates credits, calls the API to convert a document (markdown → docx/pdf/html), and returns a download link.
    export async function handleConvertDocument(
      apiClient: MDMagicApiClient,
      args: any
    ): Promise<CallToolResult> {
      const fileProcessor = new FileProcessor();
    
      try {
        // Apply session-level defaults from per-connection headers BEFORE
        // validation, so missing fields get filled in from sessionDefaults
        // rather than rejected by Zod.
        const argsWithDefaults: any = { ...(args || {}) };
        if (!argsWithDefaults.templateName && apiClient.sessionDefaults.defaultTemplate) {
          argsWithDefaults.templateName = apiClient.sessionDefaults.defaultTemplate;
        }
        if (!argsWithDefaults.pageSize && apiClient.sessionDefaults.defaultPageSize) {
          argsWithDefaults.pageSize = apiClient.sessionDefaults.defaultPageSize;
        }
        if (!argsWithDefaults.orientation && apiClient.sessionDefaults.defaultOrientation) {
          argsWithDefaults.orientation = apiClient.sessionDefaults.defaultOrientation;
        }
    
        // Validate input using Zod schema
        const input = convertDocumentSchema.parse(argsWithDefaults);
    
        console.error(`[convert_document] Starting conversion: ${input.templateName} → ${input.outputFormat}`);
    
        // Process input content (text, file, or base64)
        const processedContent = await fileProcessor.processInput({
          content: input.content,
          filePath: input.filePath,
          fileContent: input.fileContent
        });
    
        console.error(`[convert_document] Processed ${processedContent.type} content: ${processedContent.size} bytes`);
    
        // Handle "all" format conversion: all → ['docx', 'pdf', 'html']
        let outputFormats: string[];
        if (input.outputFormat === 'all') {
          outputFormats = ['docx', 'pdf', 'html'];
          console.error(`[convert_document] Converting "all" format to: ${outputFormats.join(', ')}`);
        } else {
          outputFormats = [input.outputFormat];
        }
    
        // Calculate credits before conversion
        const creditCalculator = new CreditCalculator(apiClient);
        const creditCalculation = await creditCalculator.calculateCredits(
          processedContent.content,
          input.templateName,
          outputFormats
        );
    
        console.error(`[convert_document] Credit calculation: ${creditCalculation.breakdown}`);
    
        // Derive output filename so downloads aren't named content-1234567890.pdf
        const derivedFileName = deriveFileName(
          input.fileName,
          input.filePath,
          processedContent.content,
        );
    
        if (derivedFileName) {
          console.error(`[convert_document] Output filename: ${derivedFileName}`);
        }
    
        // Call MDMagic API with calculated credits
        const result = await apiClient.convertDocument({
          content: processedContent.content,
          templateId: input.templateName,
          outputFormat: outputFormats,  // Send array of formats
          pageSize: input.pageSize,
          orientation: input.orientation,
          expectedCredits: creditCalculation.totalCredits,
          fileName: derivedFileName
        });
    
        const r = result as any;
        const apiExpiresAt = r.expiresAt as string | undefined;
        const apiFileName = r.fileName as string | undefined;
        const creditsUsed = r.creditsUsed as number | undefined;
        const balanceAfter = r.balanceAfter as number | undefined;
    
        // Fall back to a 60-min estimate if API didn't return expiresAt (older API builds)
        const expiresAtDisplay = apiExpiresAt || new Date(Date.now() + 60 * 60 * 1000).toISOString();
    
        console.error(`[convert_document] Conversion successful: ${result.downloadUrl} (${creditsUsed ?? '?'} credits, ${balanceAfter ?? '?'} remaining)`);
    
        const lines = [
          '✅ **Document converted successfully!**',
          '',
          `📁 **Download**: [${apiFileName || 'Click here to download'}](${result.downloadUrl})`,
        ];
        if (apiFileName) lines.push(`📄 **File**: ${apiFileName}`);
        if (creditsUsed !== undefined) lines.push(`📊 **Credits used**: ${creditsUsed}`);
        if (balanceAfter !== undefined) lines.push(`💰 **Balance remaining**: ${balanceAfter}`);
        lines.push(`⏰ **Expires**: ${expiresAtDisplay}`);
        lines.push('');
        lines.push('💡 Your document is ready! The link expires in 60 minutes.');
    
        return {
          content: [
            {
              type: 'text',
              text: lines.join('\n')
            }
          ]
        };
    
      } catch (error: any) {
        console.error('[convert_document] ❌ Conversion error:', error);
    
        // Handle specific API errors
        if (error instanceof MCPError) {
          return {
            content: [
              {
                type: "text",
                text: `❌ API Error: ${error.message}
    
    💡 ${(error as any).details || 'Please check your input and try again.'}`
              }
            ],
            isError: true
          };
        }
    
        // Handle Zod validation errors
        if (error.name === 'ZodError') {
          const issues = error.issues.map((issue: any) => `- ${issue.path.join('.')}: ${issue.message}`).join('\n');
          return {
            content: [
              {
                type: "text",
                text: `❌ Invalid input parameters:
    
    ${issues}
    
    💡 Please check your input and try again.`
              }
            ],
            isError: true
          };
        }
    
        throw error; // Re-throw to be handled by unified handler
      }
    }
  • Zod schema defining input validation for convert_document: content/filePath/fileContent (exactly one required), fileName, templateName (required), outputFormat (docx/pdf/html/all), pageSize (default A4), orientation (default Portrait).
    export const convertDocumentSchema = z.object({
      content: z.string().optional().describe("Raw markdown text content"),
      filePath: z.string().optional().describe("Path to markdown file (VS Code integration)"),
      fileContent: z.string().optional().describe("Base64 encoded file content"),
      fileName: z.string().optional().describe("Optional desired base name for the output file (without extension)"),
      templateName: z.string().describe("Template to use for conversion. Call list_all_templates to see real options."),
      outputFormat: z.enum(['docx', 'pdf', 'html', 'all']).describe("Output format. 'docx', 'pdf', 'html' return that single file; 'all' returns a ZIP with all three."),
      pageSize: z.enum(['A3', 'A4', 'Executive', 'US_Legal', 'US_Letter']).default('A4').describe("Page size (defaults to A4)"),
      orientation: z.enum(['Portrait', 'Landscape']).default('Portrait').describe("Page orientation (defaults to Portrait)")
    }).refine(
      (data) => {
        const inputCount = [data.content, data.filePath, data.fileContent].filter(Boolean).length;
        return inputCount === 1;
      },
      {
        message: "Exactly one input method must be provided (content, filePath, or fileContent)"
      }
    );
  • Tool registration in the unified handler. The switch case at line 30 routes 'convert_document' requests to handleConvertDocument().
    export async function registerAllTools(
      server: Server,
      apiClient: MDMagicApiClient
    ): Promise<void> {
      console.error('🔧 Registering MCP tools with unified handler...');
    
      // Initialize credit calculator for credit tools
      const creditCalculator = new CreditCalculator(apiClient);
    
      // Register a SINGLE unified handler for all tools
      server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest): Promise<CallToolResult> => {
        const toolName = request.params.name;
        console.error(`[MCP] Handling tool request: ${toolName}`);
    
        try {
          switch (toolName) {
            case 'convert_document':
              return await handleConvertDocument(apiClient, request.params.arguments);
              
            case 'list_all_templates':
              return await handleListAllTemplates(apiClient, request.params.arguments);
              
            case 'list_builtin_templates':
              return await handleListBuiltinTemplates(apiClient, request.params.arguments);
              
            case 'list_custom_templates':
              return await handleListCustomTemplates(apiClient, request.params.arguments);
              
            case 'show_default_settings':
              return await handleShowDefaultSettings(apiClient, request.params.arguments);
              
            case 'check_credit_balance':
              return await handleCheckCreditBalance(creditCalculator, request.params.arguments);
              
            case 'estimate_conversion_cost':
              return await handleEstimateConversionCost(creditCalculator, request.params.arguments);
    
            case 'validate_markdown':
              return await handleValidateMarkdown(apiClient, request.params.arguments);
    
            case 'get_template_details':
              return await handleGetTemplateDetails(apiClient, request.params.arguments);
    
            case 'recommend_template':
              return await handleRecommendTemplate(apiClient, request.params.arguments);
    
            default:
              throw new Error(`Unknown tool: ${toolName}`);
          }
        } catch (error: any) {
          console.error(`[MCP] Error handling ${toolName}:`, error);
          return {
            content: [
              {
                type: "text",
                text: `❌ Error: ${error.message}`
              }
            ],
            isError: true
          };
        }
      });
    
      console.error('✅ All MCP tools registered successfully with unified handler');
      console.error('📋 Available tools: convert_document, list_all_templates, list_builtin_templates, list_custom_templates, show_default_settings, check_credit_balance, estimate_conversion_cost, validate_markdown, get_template_details, recommend_template');
  • Helper function deriveFileName that produces a clean output filename from explicit fileName, filePath, or first H1 heading in content.
    function deriveFileName(
      explicit: string | undefined,
      filePath: string | undefined,
      content: string | undefined,
    ): string | undefined {
      const sanitize = (s: string) =>
        s
          .toLowerCase()
          .replace(/[^a-z0-9]+/g, '-')   // non-alphanum -> hyphen
          .replace(/^-+|-+$/g, '')        // trim leading/trailing hyphens
          .slice(0, 80);                   // cap length
    
      if (explicit && explicit.trim()) {
        // Strip any extension, sanitize
        const base = path.basename(explicit, path.extname(explicit));
        const cleaned = sanitize(base);
        if (cleaned) return cleaned;
      }
    
      if (filePath && filePath.trim()) {
        const base = path.basename(filePath, path.extname(filePath));
        const cleaned = sanitize(base);
        if (cleaned) return cleaned;
      }
    
      if (content) {
        // First H1 heading — match `# Heading` not `## Heading`
        const match = content.match(/^[ \t]*#[ \t]+(.+?)[ \t]*$/m);
        if (match) {
          const cleaned = sanitize(match[1]);
          if (cleaned) return cleaned;
        }
      }
    
      return undefined;
    }
Behavior5/5

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

Annotations only state readOnlyHint=false, destructiveHint=false, idempotentHint=false, openWorldHint=true. The description adds significant behavioral context: output format behavior, filename derivation logic, error handling procedure, response fields to surface, and default page settings. This goes well beyond what annotations provide.

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 structured with numbered points and clear headings, making it easy to parse. It is front-loaded with the core purpose. While it is relatively long (6 points of guidance), each point is necessary for correct usage, so the length is justified and no information is redundant.

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

Completeness5/5

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

Given the tool's complexity (8 parameters, 2 required, multiple output formats, error handling, response fields), the description covers all crucial aspects: input sources, output options, error recovery, response structure, page settings, and defaults. No important aspect is omitted. The description compensates for the missing output schema details by specifying the fields to surface.

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

Parameters5/5

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

The input schema has 100% description coverage, yet the description adds richer context: it explains importance of passing fileName for UX, warns not to guess templateName but to call list_all_templates, clarifies outputFormat values, and documents defaults for pageSize and orientation. This adds meaning beyond the schema.

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

Purpose5/5

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

The description starts with a clear, specific verb 'Convert markdown to a professionally formatted document using an MDMagic template', which precisely states the action and resource. It distinguishes itself from sibling tools (like list_all_templates or check_credit_balance) by being the only conversion tool.

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 when-to-use and when-not-to-use guidance, e.g., asking users for format if ambiguous, not falling back to code generation on template missing, and instead calling list_all_templates. It includes concrete actions like 'if the user is ambiguous... ASK' and 'on template not found errors: call list_all_templates first'.

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/MDMagic-MCP/mdmagic-mcp-server'

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