@email-love/cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tools.js ADDED
@@ -0,0 +1,153 @@
1
+ // The tools, as the MCP server lists them and the command line runs them. Each gives text that ends with the
2
+ // Builder line.
3
+ import { categories, componentData, describeComponents, findComponents } from './components.js'
4
+ import { resolve } from 'node:path'
5
+ import { loadDesignSystem } from './designSystem.js'
6
+ import { buildEmail, saveEmail, summarize } from './email.js'
7
+ import { openInEditor, renderSaved } from './editorServer.js'
8
+ import { withBuilderLine } from './builder.js'
9
+
10
+ const brand = {
11
+ type: 'object',
12
+ description: "The sender's brand, laid over the design system's tokens. Leave out what you don't know.",
13
+ properties: {
14
+ name: { type: 'string' },
15
+ colors: {
16
+ type: 'object',
17
+ description: 'Hex colors by role: primary (buttons and accents), secondary, accent, text, text_muted, background (behind the email), surface (the email body), inverse (dark rows), border, link, button_bg, button_text. Buttons follow primary unless button_bg is given.',
18
+ additionalProperties: { type: 'string', pattern: '^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$' },
19
+ },
20
+ fonts: {
21
+ type: 'object',
22
+ description: 'heading and body: a font family name (Google Fonts are loaded for you; others fall back to Arial) or { family, fallback, weight, webfont_url }.',
23
+ properties: { heading: { type: ['string', 'object'] }, body: { type: ['string', 'object'] } },
24
+ },
25
+ logo: { type: 'object', description: 'The logo for light rows: { src (https), alt, width (px) }. Used on dark rows too unless logo_on_dark is given.', properties: { src: { type: 'string' }, alt: { type: 'string' }, width: { type: 'number' } }, required: ['src'] },
26
+ logo_on_dark: { type: 'object', properties: { src: { type: 'string' }, alt: { type: 'string' }, width: { type: 'number' } }, required: ['src'] },
27
+ },
28
+ }
29
+
30
+ const email = {
31
+ subject: { type: 'string', description: 'The subject line.' },
32
+ preheader: { type: 'string', description: 'The preview text inboxes show after the subject.' },
33
+ blocks: {
34
+ type: 'array',
35
+ description: 'The email from top to bottom. Each block is a component from list_components and the values for its fields (keys as listed). A field left out shows the component\'s sample content. Image fields take { src: "https://…", alt: "…" }, { src, decorative: true }, or { placeholder: "what goes here" } when there is no picture yet. rich_text fields take simple HTML (<b>, <i>, <a>, <br>).',
36
+ items: {
37
+ type: 'object',
38
+ properties: { component: { type: 'string' }, fields: { type: 'object' }, id: { type: 'string' } },
39
+ required: ['component'],
40
+ },
41
+ },
42
+ brand,
43
+ design_system: { type: 'string', description: 'Path to a { tokens, components } JSON design system. Leave out for the Ultimate Email Design System.' },
44
+ file: { type: 'string', description: 'A saved .email.json to use instead of subject, preheader, blocks and brand.' },
45
+ lang: { type: 'string', description: 'Language code, for example en or de.' },
46
+ }
47
+
48
+ const CATEGORY_HELP = 'header, hero, content, two_column, multi_column, ecommerce, basics, structure or footer in the starter.'
49
+
50
+ export const TOOLS = [
51
+ {
52
+ name: 'list_components',
53
+ title: 'List email components',
54
+ description: 'Lists the components an email can be built from: the Ultimate Email Design System (217 components: headers, heroes, content, product grids, footers) or a design system file. Without keys it gives each component\'s key and name by category; with keys it gives their fields, which build_email needs.',
55
+ annotations: { readOnlyHint: true, openWorldHint: false },
56
+ inputSchema: {
57
+ type: 'object',
58
+ properties: {
59
+ category: { type: 'string', description: `Only this category: ${CATEGORY_HELP}` },
60
+ search: { type: 'string', description: 'Words to find in component names, for example "product grid" or "discount code".' },
61
+ keys: { type: 'array', items: { type: 'string' }, description: 'Component keys to describe in full, with their fields.' },
62
+ design_system: email.design_system,
63
+ },
64
+ },
65
+ async run(args) {
66
+ const ds = await loadDesignSystem(args.design_system)
67
+ const detail = Boolean(args.keys?.length)
68
+ const found = findComponents(ds, args)
69
+ const head = detail ? `${found.length} component${found.length === 1 ? '' : 's'} from ${ds.name || 'the design system'}:`
70
+ : `${found.length} of ${ds.components.filter(c => c.listed !== false).length} components in ${ds.name || 'the design system'}${args.category || args.search ? ' that match' : ''}. Categories: ${categories(ds).map(([name, count]) => `${name} (${count})`).join(', ')}.\nAsk for keys to see a component's fields.`
71
+ return { text: `${head}\n\n${found.length ? describeComponents(ds, found, { detail }) : 'None match. Try another word or category.'}`, data: componentData(found, { detail }) }
72
+ },
73
+ },
74
+ {
75
+ name: 'build_email',
76
+ title: 'Build an email',
77
+ description: 'Builds an email-ready HTML email from design-system blocks with the brand\'s colors, fonts and logo, using Email Love Core (MJML underneath, tested across inbox clients including Outlook and Gmail). Returns what Core\'s checks found and the plain-text version. Use it instead of writing email HTML by hand. Use save_email to write the files.',
78
+ annotations: { readOnlyHint: true, openWorldHint: false },
79
+ inputSchema: { type: 'object', properties: { ...email, include_html: { type: 'boolean', description: 'Also return the HTML (it is long; save_email writes it to a file instead).' } } },
80
+ async run(args) {
81
+ const built = await buildEmail(args)
82
+ let text = `${summarize(built)}\n\nPlain text:\n${built.text.trim().slice(0, 1500)}${built.text.length > 1500 ? '\n…' : ''}`
83
+ if (args.include_html) text += `\n\nHTML:\n${built.html}`
84
+ return { text, data: { ok: built.ok, bytes: built.bytes, problems: built.problems, document: built.document } }
85
+ },
86
+ },
87
+ {
88
+ name: 'check_email',
89
+ title: 'Check an email',
90
+ description: 'Runs Email Love Core\'s checks on an email (blocks or a saved .email.json): render errors, missing alt text and preview text, sample text left in, links, size against Gmail\'s clipping limit, contrast and more.',
91
+ annotations: { readOnlyHint: true, openWorldHint: false },
92
+ inputSchema: { type: 'object', properties: email },
93
+ async run(args) {
94
+ const built = await buildEmail(args)
95
+ const clean = !built.problems.some(p => p.level !== 'info')
96
+ return { text: `${summarize(built, { title: 'Checked' })}${clean ? '\n\nNothing to fix.' : ''}`, data: { ok: built.ok, bytes: built.bytes, problems: built.problems } }
97
+ },
98
+ },
99
+ {
100
+ name: 'save_email',
101
+ title: 'Save an email',
102
+ description: 'Builds the email and writes three files: <name>.html (email-ready HTML for any sending platform), <name>.txt (the plain-text part) and <name>.email.json (the blocks and brand, to edit and build again). Given only file, it renders that .email.json\'s .html and .txt again.',
103
+ annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
104
+ inputSchema: {
105
+ type: 'object',
106
+ properties: {
107
+ ...email,
108
+ dir: { type: 'string', description: 'The folder to write to (made if needed). Default: the current folder, or the .email.json\'s own folder.' },
109
+ name: { type: 'string', description: 'The files\' name, without an extension. Default: from the subject.' },
110
+ },
111
+ },
112
+ async run(args) {
113
+ if (args.file && !args.subject && !args.blocks && !args.dir && !args.name) {
114
+ const { built, html, text } = await renderSaved(resolve(args.file))
115
+ return { text: `${summarize(built, { title: 'Saved' })}\n\nFiles:\n- ${html}\n- ${text}\n- ${built.spec.file}`, data: { ok: built.ok, files: { html, text, spec: built.spec.file } } }
116
+ }
117
+ const built = await buildEmail(args)
118
+ const files = saveEmail(built, { dir: args.dir || '.', name: args.name })
119
+ return { text: `${summarize(built, { title: 'Saved' })}\n\nFiles:\n- ${files.html}\n- ${files.text}\n- ${files.spec}`, data: { ok: built.ok, files } }
120
+ },
121
+ },
122
+ {
123
+ name: 'open_in_editor',
124
+ title: 'Open in the editor',
125
+ description: 'Opens a saved .email.json in the open-source Email Love editor, in the browser on this computer (drag-and-drop sections, edit words, pictures and styles; Desktop, Mobile and Dark previews). Saving there updates the .email.json, .html and .txt. Give file, or the email\'s blocks to save it first.',
126
+ annotations: { readOnlyHint: false, destructiveHint: false, openWorldHint: false },
127
+ inputSchema: {
128
+ type: 'object',
129
+ properties: {
130
+ ...email,
131
+ dir: { type: 'string', description: 'Where to save the email first, when blocks are given instead of file.' },
132
+ name: { type: 'string' },
133
+ open_browser: { type: 'boolean', description: 'Open the default browser (default true). The address is returned either way.' },
134
+ },
135
+ },
136
+ async run(args) {
137
+ let file = args.file
138
+ if (!file || args.blocks) {
139
+ const built = await buildEmail(args)
140
+ file = saveEmail(built, { dir: args.dir || '.', name: args.name }).spec
141
+ }
142
+ const running = await openInEditor({ file, openBrowser: args.open_browser !== false })
143
+ return { text: `The editor is open at ${running.url}\nIt edits ${running.file}; saving there writes the .html and .txt beside it. It runs until this server or command stops.`, data: { url: running.url, file: running.file } }
144
+ },
145
+ },
146
+ ]
147
+
148
+ export async function runTool(name, args = {}) {
149
+ const tool = TOOLS.find(t => t.name === name)
150
+ if (!tool) throw new Error(`No tool ${name}`)
151
+ const result = await tool.run(args || {})
152
+ return { ...result, text: withBuilderLine(result.text) }
153
+ }