create_html_template
Generate email templates with HTML content and Handlebars support for SendGrid campaigns. Create templates with subject lines, plain text versions, and test data in a single operation.
Instructions
Create a new template with HTML content in one step - perfect for AI-generated designs
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| html_content | Yes | Complete HTML email template (supports Handlebars) | |
| plain_content | No | Plain text version (will auto-generate if not provided) | |
| subject | Yes | Email subject line (supports Handlebars like {{firstName}}) | |
| template_name | Yes | Name of the template | |
| test_data | No | JSON string with test data for preview (e.g., '{"firstName":"John","company":"Acme"}') | |
| version_name | Yes | Name for the initial version |
Input Schema (JSON Schema)
{
"properties": {
"html_content": {
"description": "Complete HTML email template (supports Handlebars)",
"type": "string"
},
"plain_content": {
"description": "Plain text version (will auto-generate if not provided)",
"type": "string"
},
"subject": {
"description": "Email subject line (supports Handlebars like {{firstName}})",
"type": "string"
},
"template_name": {
"description": "Name of the template",
"type": "string"
},
"test_data": {
"description": "JSON string with test data for preview (e.g., '{\"firstName\":\"John\",\"company\":\"Acme\"}')",
"type": "string"
},
"version_name": {
"description": "Name for the initial version",
"type": "string"
}
},
"required": [
"template_name",
"version_name",
"subject",
"html_content"
],
"type": "object"
}
Implementation Reference
- src/tools/templates.ts:304-399 (handler)The handler function that implements the core logic of the create_html_template tool. It sequentially creates a new dynamic template and then an active version with the provided HTML, subject, and optional plain text/test data using SendGrid's API. Includes read-only mode check, error handling with cleanup, and detailed success response.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:295-302 (schema)Zod-based input schema defining parameters for the create_html_template tool: required template_name, version_name, subject, html_content; optional plain_content and test_data.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/tools/index.ts:16-16 (registration)Registration of templateTools (including create_html_template) into the aggregated allTools export used for MCP tool exposure....templateTools,
- src/tools/index.ts:7-7 (registration)Import of templateTools from templates.ts where create_html_template is defined.import { templateTools } from "./templates.js";
- src/tools/templates.ts:292-303 (schema)Full tool config including title, description, and input schema for 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\"}')") }, },