Skip to main content
Glama

compile_mjml

Convert MJML markup to responsive HTML for email templates with options to beautify, minify, validate, and customize output.

Instructions

Compile MJML to HTML with various options

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputYesMJML content or file path
beautifyNoBeautify the output HTML
minifyNoMinify the output HTML
validationLevelNoValidation levelsoft
filePathNoTreat input as file path
keepCommentsNoKeep comments in output
fontsNoCustom fonts configuration
outputPathNoSave compiled HTML to file path

Implementation Reference

  • Main execution logic for compile_mjml tool: parses args with schema, reads input file if needed, compiles MJML to HTML using mjml library with options, handles output saving, logs results and warnings, returns structured response.
    async handleCompileMjml(args) {
      const parsed = CompileMjmlSchema.parse(args);
      log.info('Compiling MJML', { template: parsed.template });
    
      const mjmlContent = await this.readInput(parsed.input, parsed.filePath);
    
      const mjmlOptions = {
        beautify: parsed.beautify,
        minify: parsed.minify,
        validationLevel: parsed.validationLevel,
        keepComments: parsed.keepComments,
        fonts: parsed.fonts || {},
      };
    
      const result = mjml(mjmlContent, mjmlOptions);
    
      if (result.errors.length > 0) {
        log.warn('MJML compilation completed with errors', result.errors);
      } else {
        log.info('MJML compilation successful');
      }
    
      const saveResult = await this.saveOutput(result.html, parsed.outputPath);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify({
              success: true,
              html: result.html,
              errors: result.errors,
              warnings: result.warnings,
              mjmlOptions,
              saved: saveResult,
            }, null, 2),
          },
        ],
      };
    }
  • Zod validation schema defining input parameters for the compile_mjml tool, including MJML input, formatting options, validation level, file handling flags, and output path.
    const CompileMjmlSchema = z.object({
      input: z.string().describe('MJML content or file path'),
      beautify: z.boolean().optional().default(true).describe('Beautify the output HTML'),
      minify: z.boolean().optional().default(false).describe('Minify the output HTML'),
      validationLevel: z.enum(['skip', 'soft', 'strict']).optional().default('soft').describe('Validation level'),
      filePath: z.boolean().optional().default(false).describe('Treat input as file path'),
      keepComments: z.boolean().optional().default(false).describe('Keep comments in output'),
      fonts: z.record(z.string()).optional().describe('Custom fonts configuration'),
      outputPath: z.string().optional().describe('Save compiled HTML to file path'),
    });
  • index.js:137-184 (registration)
    Tool registration in ListToolsRequestHandler: defines name 'compile_mjml', description, and detailed inputSchema matching the Zod schema for MCP protocol compliance.
    {
      name: 'compile_mjml',
      description: 'Compile MJML to HTML with various options',
      inputSchema: {
        type: 'object',
        properties: {
          input: {
            type: 'string',
            description: 'MJML content or file path',
          },
          beautify: {
            type: 'boolean',
            description: 'Beautify the output HTML',
            default: true,
          },
          minify: {
            type: 'boolean',
            description: 'Minify the output HTML',
            default: false,
          },
          validationLevel: {
            type: 'string',
            enum: ['skip', 'soft', 'strict'],
            description: 'Validation level',
            default: 'soft',
          },
          filePath: {
            type: 'boolean',
            description: 'Treat input as file path',
            default: false,
          },
          keepComments: {
            type: 'boolean',
            description: 'Keep comments in output',
            default: false,
          },
          fonts: {
            type: 'object',
            description: 'Custom fonts configuration',
          },
          outputPath: {
            type: 'string',
            description: 'Save compiled HTML to file path',
          },
        },
        required: ['input'],
      },
    },
  • Helper method used by handler to read MJML input from string or file path, with error handling for file access.
    async readInput(input, isFilePath) {
      if (isFilePath) {
        try {
          const fullPath = resolve(input);
          await access(fullPath);
          const content = await readFile(fullPath, 'utf-8');
          log.debug(`Read file: ${fullPath}`);
          return content;
        } catch (error) {
          throw new McpError(
            ErrorCode.InvalidRequest,
            `Failed to read MJML file: ${error.message}`
          );
        }
      }
      return input;
    }
  • Helper method used by handler to optionally save compiled HTML to output file path, returns save status.
    async saveOutput(content, outputPath) {
      if (outputPath) {
        try {
          const fullPath = resolve(outputPath);
          await writeFile(fullPath, content, 'utf-8');
          log.info(`Output saved to: ${fullPath}`);
          return { saved: true, path: fullPath };
        } catch (error) {
          throw new McpError(
            ErrorCode.InternalError,
            `Failed to save output: ${error.message}`
          );
        }
      }
      return { saved: false };
    }

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/shaunie2fly/mjml_mcp'

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