Create Complete HTML Template
create_html_templateCreate an email template with HTML content, subject line, and version in one step, supporting Handlebars variables and optional plain text and test data.
Instructions
Create a new template with HTML content in one step - perfect for AI-generated designs
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| template_name | Yes | Name of the template | |
| version_name | Yes | Name for the initial version | |
| subject | Yes | Email subject line (supports Handlebars like {{firstName}}) | |
| html_content | Yes | Complete HTML email template (supports Handlebars) | |
| plain_content | No | Plain text version (will auto-generate if not provided) | |
| test_data | No | JSON string with test data for preview (e.g., '{"firstName":"John","company":"Acme"}') |
Implementation Reference
- src/tools/templates.ts:304-399 (handler)Handler function that executes the create_html_template tool logic. Creates a SendGrid dynamic template and version with HTML content in one step. Handles template creation, version creation with subject/html/plain_content, test data parsing, and returns detailed success response with template/version IDs.
handler: async ({ template_name, version_name, subject, html_content, plain_content, test_data }: { template_name: string; version_name: string; subject: string; html_content: string; plain_content?: string; test_data?: string; }): Promise<ToolResult> => { const readOnlyCheck = checkReadOnlyMode(); if (readOnlyCheck.blocked) { return { content: [{ type: "text", text: readOnlyCheck.message! }] }; } try { // Step 1: Create the template const template = await makeRequest("https://api.sendgrid.com/v3/templates", { method: "POST", body: JSON.stringify({ name: template_name, generation: "dynamic" }), }); const template_id = template.id; // Step 2: Create the version with HTML content const versionData: any = { name: version_name, subject: subject, html_content: html_content, active: 1, generate_plain_content: !plain_content, // Auto-generate if no plain content provided }; if (plain_content) { versionData.plain_content = plain_content; } if (test_data) { try { versionData.test_data = JSON.parse(test_data); } catch (error) { // Delete the template we just created since version failed await makeRequest(`https://api.sendgrid.com/v3/templates/${template_id}`, { method: "DELETE", }); return { content: [{ type: "text", text: "Error: test_data must be valid JSON. Template creation cancelled." }] }; } } const version = await makeRequest(`https://api.sendgrid.com/v3/templates/${template_id}/versions`, { method: "POST", body: JSON.stringify(versionData), }); return { content: [{ type: "text", text: `✅ HTML Template created successfully! 📧 Template Details: - Template ID: ${template_id} - Template Name: ${template_name} - Version ID: ${version.id} - Version Name: ${version_name} - Subject: ${subject} - Status: Active and ready to use 🚀 Usage: You can now send emails using this template with the Mail API: - Use template_id: "${template_id}" - Include dynamic_template_data with your Handlebars variables 🔗 Quick Actions: - View in SendGrid UI: https://mc.sendgrid.com/dynamic-templates/${template_id} - Test the template with sample data in the SendGrid editor ${JSON.stringify({ template, version }, null, 2)}` }] }; } catch (error: any) { return { content: [{ type: "text", text: `❌ Error creating template: ${error.message || 'Unknown error occurred'}` }] }; } }, - src/tools/templates.ts:291-302 (schema)Input schema definitions for create_html_template using Zod. Defines required fields: template_name, version_name, subject, html_content. Optional fields: plain_content, test_data.
create_html_template: { config: { title: "Create Complete HTML Template", description: "Create a new template with HTML content in one step - perfect for AI-generated designs", inputSchema: { template_name: z.string().describe("Name of the template"), version_name: z.string().describe("Name for the initial version"), subject: z.string().describe("Email subject line (supports Handlebars like {{firstName}})"), html_content: z.string().describe("Complete HTML email template (supports Handlebars)"), plain_content: z.string().optional().describe("Plain text version (will auto-generate if not provided)"), test_data: z.string().optional().describe("JSON string with test data for preview (e.g., '{\"firstName\":\"John\",\"company\":\"Acme\"}')") }, - src/index.ts:17-21 (registration)Registration of all tools (including create_html_template) with the MCP server. Iterates over allTools and calls server.registerTool for each.
server.registerResource(uri, uri, resource.config, resource.handler); } // Register all tools for (const [name, tool] of Object.entries(allTools)) { - src/tools/index.ts:9-17 (registration)Consolidation of all tool definitions into a single allTools object, including spread of templateTools which contains create_html_template.
export const allTools = { ...automationTools, ...campaignTools, ...contactTools, ...mailTools, ...miscTools, ...statsTools, ...templateTools, };