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
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | MJML content or file path | |
| beautify | No | Beautify the output HTML | |
| minify | No | Minify the output HTML | |
| validationLevel | No | Validation level | soft |
| filePath | No | Treat input as file path | |
| keepComments | No | Keep comments in output | |
| fonts | No | Custom fonts configuration | |
| outputPath | No | Save compiled HTML to file path |
Implementation Reference
- index.js:296-335 (handler)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), }, ], }; } - index.js:35-44 (schema)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'], }, }, - index.js:98-114 (helper)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; } - index.js:116-131 (helper)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 }; }