@teamnetwork-nz/smtp-mcp-server 1.1.3 → 1.3.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/README.md CHANGED
@@ -82,6 +82,25 @@ For an unauthenticated internal relay, omit `SMTP_USER` and `SMTP_PASS`.
82
82
 
83
83
  ## Tools
84
84
 
85
+ ### `encode_file`
86
+
87
+ Reads a file from disk, base64-encodes it, and returns a ready-to-use attachment object. Use this before `send_email` rather than encoding files manually.
88
+
89
+ | Parameter | Type | Required | Description |
90
+ |---|---|---|---|
91
+ | `path` | string | yes | Absolute or relative path to the file |
92
+ | `content_type` | string | no | MIME type override — auto-detected from extension if omitted |
93
+
94
+ Returns JSON with `filename`, `content_type`, and `content` (base64). Pass these fields directly as an attachment entry in `send_email`.
95
+
96
+ **Recommended workflow for attachments:**
97
+ ```
98
+ send_email({ ..., attachments: [{ path: "/absolute/path/to/report.zip" }] })
99
+ ```
100
+ The server reads, encodes, and detects the MIME type automatically. Binary data never passes through the agent's context.
101
+
102
+ ---
103
+
85
104
  ### `health`
86
105
 
87
106
  No parameters. Performs a live TCP+SMTP handshake against the configured server and returns the active configuration summary.
@@ -111,13 +130,14 @@ Returns `isError: true` if the connection test fails, with the SMTP error messag
111
130
  | `bcc` | string \| string[] | no | BCC recipient(s) |
112
131
  | `attachments` | Attachment[] | no | Files to attach |
113
132
 
114
- **Attachment object:**
133
+ **Attachment object — supply `path` or `content`, not both:**
115
134
 
116
- | Field | Type | Description |
117
- |---|---|---|
118
- | `filename` | string | Name shown to recipient |
119
- | `content` | string | Base64-encoded file data |
120
- | `content_type` | string | MIME type, e.g. `application/pdf` |
135
+ | Field | Type | Required | Description |
136
+ |---|---|---|---|
137
+ | `path` | string | if no `content` | Absolute path to file — server reads and encodes it |
138
+ | `content` | string | if no `path` | Base64-encoded file data (no `data:` URI prefix) |
139
+ | `filename` | string | if using `content` | Name shown to recipient; defaults to basename of `path` |
140
+ | `content_type` | string | if using `content` | MIME type; auto-detected from `path` extension if omitted |
121
141
 
122
142
  ---
123
143
 
package/dist/email.js CHANGED
@@ -6,7 +6,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createTransport = createTransport;
7
7
  exports.verifyTransport = verifyTransport;
8
8
  exports.sendEmail = sendEmail;
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = __importDefault(require("path"));
9
11
  const nodemailer_1 = __importDefault(require("nodemailer"));
12
+ const mime_js_1 = require("./mime.js");
13
+ const DATA_URI_PREFIX = /^data:[^;,]*(;[^,]*)?,/;
14
+ const BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/;
15
+ const MIME_TYPE_PATTERN = /^[\w!#$&\-^.+]+\/[\w!#$&\-^.+]+$/;
16
+ /**
17
+ * Strips a leading data: URI scheme (if present) and any whitespace from a
18
+ * base64 payload, then validates what remains is well-formed base64.
19
+ * Node's Buffer.from(..., "base64") silently skips invalid characters rather
20
+ * than erroring, so an unstripped data URI or stray characters would
21
+ * silently corrupt the decoded attachment instead of failing loudly.
22
+ */
23
+ function normalizeAndValidateBase64(content, filename) {
24
+ const withoutDataUri = content.replace(DATA_URI_PREFIX, "");
25
+ const stripped = withoutDataUri.replace(/\s/g, "");
26
+ if (stripped.length === 0 || stripped.length % 4 !== 0 || !BASE64_PATTERN.test(stripped)) {
27
+ throw new Error(`Attachment "${filename}" does not contain valid base64 data. ` +
28
+ `Provide the raw base64-encoded file content (no data: URI prefix).`);
29
+ }
30
+ return stripped;
31
+ }
32
+ function validateContentType(contentType, filename) {
33
+ const trimmed = contentType.trim();
34
+ if (!MIME_TYPE_PATTERN.test(trimmed)) {
35
+ throw new Error(`Attachment "${filename}" has an invalid content_type "${contentType}". ` +
36
+ `Expected a MIME type in the form "type/subtype", e.g. "application/pdf".`);
37
+ }
38
+ return trimmed;
39
+ }
10
40
  function createTransport(smtpConfig) {
11
41
  return nodemailer_1.default.createTransport({
12
42
  host: smtpConfig.host,
@@ -21,6 +51,26 @@ function createTransport(smtpConfig) {
21
51
  async function verifyTransport(transport) {
22
52
  await transport.verify();
23
53
  }
54
+ function resolveAttachment(a) {
55
+ if (a.path) {
56
+ if (!fs_1.default.existsSync(a.path)) {
57
+ throw new Error(`Attachment file not found: ${a.path}`);
58
+ }
59
+ return {
60
+ filename: a.filename ?? path_1.default.basename(a.path),
61
+ content: fs_1.default.readFileSync(a.path),
62
+ contentType: a.content_type ?? (0, mime_js_1.mimeFromExtension)(a.path),
63
+ };
64
+ }
65
+ const content = a.content;
66
+ const filename = a.filename;
67
+ const content_type = a.content_type;
68
+ return {
69
+ filename,
70
+ content: Buffer.from(normalizeAndValidateBase64(content, filename), "base64"),
71
+ contentType: validateContentType(content_type, filename),
72
+ };
73
+ }
24
74
  async function sendEmail(transport, from, options) {
25
75
  const message = {
26
76
  from: `"${from.name}" <${from.address}>`,
@@ -28,11 +78,7 @@ async function sendEmail(transport, from, options) {
28
78
  subject: options.subject,
29
79
  cc: options.cc,
30
80
  bcc: options.bcc,
31
- attachments: options.attachments?.map((a) => ({
32
- filename: a.filename,
33
- content: Buffer.from(a.content, "base64"),
34
- contentType: a.content_type,
35
- })),
81
+ attachments: options.attachments?.map((a) => resolveAttachment(a)),
36
82
  };
37
83
  if (options.body_type === "html") {
38
84
  message.html = options.body;
package/dist/index.js CHANGED
@@ -1,5 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  Object.defineProperty(exports, "__esModule", { value: true });
4
37
  const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
5
38
  const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
@@ -7,18 +40,27 @@ const zod_1 = require("zod");
7
40
  const config_js_1 = require("./config.js");
8
41
  const restrictions_js_1 = require("./restrictions.js");
9
42
  const email_js_1 = require("./email.js");
43
+ const mime_js_1 = require("./mime.js");
10
44
  const transport = (0, email_js_1.createTransport)(config_js_1.config.smtp);
11
45
  const server = new mcp_js_1.McpServer({
12
46
  name: "smtp-mcp",
13
- version: "1.1.3",
47
+ version: "1.3.0",
14
48
  });
15
49
  // ---------------------------------------------------------------------------
16
50
  // Shared schemas
17
51
  // ---------------------------------------------------------------------------
18
- const attachmentSchema = zod_1.z.object({
19
- filename: zod_1.z.string().min(1),
20
- content: zod_1.z.string().min(1).describe("Base64-encoded file content"),
21
- content_type: zod_1.z.string().min(1).describe("MIME type, e.g. application/pdf"),
52
+ const attachmentSchema = zod_1.z
53
+ .object({
54
+ path: zod_1.z.string().optional().describe("Absolute path to the file on disk — server reads and encodes it"),
55
+ filename: zod_1.z.string().optional().describe("Override the filename shown to the recipient"),
56
+ content: zod_1.z.string().optional().describe("Base64-encoded file content (use path instead where possible)"),
57
+ content_type: zod_1.z.string().optional().describe("MIME type — auto-detected from path extension if omitted"),
58
+ })
59
+ .refine((a) => a.path || a.content, {
60
+ message: "Each attachment must include either 'path' (preferred) or 'content' (base64)",
61
+ })
62
+ .refine((a) => !a.content || (a.filename && a.content_type), {
63
+ message: "Attachments using 'content' must also include 'filename' and 'content_type'",
22
64
  });
23
65
  const recipientSchema = zod_1.z
24
66
  .union([zod_1.z.string().email(), zod_1.z.array(zod_1.z.string().email()).min(1)])
@@ -69,6 +111,52 @@ server.registerTool("health", {
69
111
  };
70
112
  });
71
113
  // ---------------------------------------------------------------------------
114
+ // Tool: encode_file
115
+ // ---------------------------------------------------------------------------
116
+ server.registerTool("encode_file", {
117
+ description: "Read a file from disk and return it base64-encoded. " +
118
+ "NOTE: prefer supplying 'path' directly in the send_email attachments array — " +
119
+ "the server will encode the file itself without passing binary data through the agent. " +
120
+ "Use encode_file only when you need to inspect or transform the content before sending.",
121
+ inputSchema: {
122
+ path: zod_1.z.string().min(1).describe("Absolute or relative path to the file"),
123
+ content_type: zod_1.z
124
+ .string()
125
+ .optional()
126
+ .describe("MIME type override. If omitted the type is detected from the file extension."),
127
+ },
128
+ }, async (args) => {
129
+ const fs = await Promise.resolve().then(() => __importStar(require("fs")));
130
+ const nodePath = await Promise.resolve().then(() => __importStar(require("path")));
131
+ if (!fs.existsSync(args.path)) {
132
+ return {
133
+ isError: true,
134
+ content: [{ type: "text", text: `File not found: ${args.path}` }],
135
+ };
136
+ }
137
+ try {
138
+ const data = fs.readFileSync(args.path);
139
+ const content = data.toString("base64");
140
+ const filename = nodePath.basename(args.path);
141
+ const content_type = args.content_type ?? (0, mime_js_1.mimeFromExtension)(args.path);
142
+ return {
143
+ content: [
144
+ {
145
+ type: "text",
146
+ text: JSON.stringify({ filename, content_type, content }),
147
+ },
148
+ ],
149
+ };
150
+ }
151
+ catch (err) {
152
+ const message = err instanceof Error ? err.message : String(err);
153
+ return {
154
+ isError: true,
155
+ content: [{ type: "text", text: `Failed to read file: ${message}` }],
156
+ };
157
+ }
158
+ });
159
+ // ---------------------------------------------------------------------------
72
160
  // Tool: send_email
73
161
  // ---------------------------------------------------------------------------
74
162
  server.registerTool("send_email", {
@@ -186,6 +274,17 @@ health
186
274
  Performs a live TCP+SMTP connection test and returns the active configuration.
187
275
  Call this first to confirm the server is reachable before sending.
188
276
 
277
+ encode_file
278
+ Required
279
+ path string Absolute or relative path to the file on disk.
280
+ Optional
281
+ content_type string MIME type override. Auto-detected from extension if omitted.
282
+
283
+ Reads the file and returns { filename, content_type, content } as JSON.
284
+ NOTE: for files on disk, prefer passing path directly in send_email attachments
285
+ instead — the server encodes the file itself without routing binary data through
286
+ the agent. Use encode_file only when you need to inspect the content first.
287
+
189
288
  send_email
190
289
  Required
191
290
  to string | string[] One or more recipient email addresses.
@@ -198,10 +297,32 @@ send_email
198
297
  bcc string | string[] Blind carbon copy recipients.
199
298
  attachments Attachment[] Files to attach (see schema below).
200
299
 
201
- Attachment schema
202
- filename string Name shown to the recipient (e.g. "report.pdf").
203
- content string File data encoded as base64.
204
- content_type string MIME type (e.g. "application/pdf", "image/png").
300
+ Attachment schema — provide EITHER path OR content for each attachment:
301
+
302
+ Path-based (preferred for files on disk):
303
+ path string Absolute path to the file. Server reads and encodes it.
304
+ filename string Optional. Defaults to the filename from the path.
305
+ content_type string Optional. Auto-detected from the file extension.
306
+
307
+ Content-based (for programmatically generated data):
308
+ content string Raw base64-encoded data. No data: URI prefix.
309
+ filename string Required when using content.
310
+ content_type string Required when using content.
311
+
312
+ ATTACHMENTS — RECOMMENDED APPROACH
313
+ -------------------------------------
314
+ Pass the file path directly in the attachments array and let the server handle
315
+ encoding. Binary data never passes through the agent's context:
316
+
317
+ send_email({
318
+ ...,
319
+ attachments: [{ path: "/absolute/path/to/file.zip" }]
320
+ })
321
+
322
+ Only use content-based attachments when the data is generated at runtime
323
+ (e.g. a report built in memory) rather than read from an existing file.
324
+ If you do supply raw base64 content, ensure it has no "data:<type>;base64,"
325
+ prefix — provide the raw base64 string only.
205
326
 
206
327
  TIPS
207
328
  ----
@@ -211,6 +332,8 @@ TIPS
211
332
  blocked address will reject the entire request.
212
333
  - Attachments have no size limit enforced by this server; the SMTP relay may
213
334
  impose its own limits.
335
+ - If you are unsure of a file's MIME type, use application/octet-stream — it
336
+ will arrive correctly, and the recipient's mail client will identify it.
214
337
  `.trim();
215
338
  }
216
339
  server.registerResource("usage", "smtp://server/usage", {
package/dist/mime.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mimeFromExtension = mimeFromExtension;
7
+ const path_1 = __importDefault(require("path"));
8
+ const MIME_TYPES = {
9
+ // Archives
10
+ ".zip": "application/zip",
11
+ ".gz": "application/gzip",
12
+ ".tar": "application/x-tar",
13
+ ".rar": "application/vnd.rar",
14
+ ".7z": "application/x-7z-compressed",
15
+ // Documents
16
+ ".pdf": "application/pdf",
17
+ ".doc": "application/msword",
18
+ ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
19
+ ".xls": "application/vnd.ms-excel",
20
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
21
+ ".ppt": "application/vnd.ms-powerpoint",
22
+ ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
23
+ ".odt": "application/vnd.oasis.opendocument.text",
24
+ ".ods": "application/vnd.oasis.opendocument.spreadsheet",
25
+ // Text
26
+ ".txt": "text/plain",
27
+ ".csv": "text/csv",
28
+ ".html": "text/html",
29
+ ".htm": "text/html",
30
+ ".xml": "application/xml",
31
+ ".json": "application/json",
32
+ ".md": "text/markdown",
33
+ // Images
34
+ ".png": "image/png",
35
+ ".jpg": "image/jpeg",
36
+ ".jpeg": "image/jpeg",
37
+ ".gif": "image/gif",
38
+ ".webp": "image/webp",
39
+ ".svg": "image/svg+xml",
40
+ ".ico": "image/x-icon",
41
+ // Audio / Video
42
+ ".mp3": "audio/mpeg",
43
+ ".mp4": "video/mp4",
44
+ ".wav": "audio/wav",
45
+ // Code / data
46
+ ".js": "application/javascript",
47
+ ".ts": "text/plain",
48
+ ".py": "text/plain",
49
+ ".sh": "text/plain",
50
+ };
51
+ function mimeFromExtension(filePath) {
52
+ const ext = path_1.default.extname(filePath).toLowerCase();
53
+ return MIME_TYPES[ext] ?? "application/octet-stream";
54
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnetwork-nz/smtp-mcp-server",
3
- "version": "1.1.3",
3
+ "version": "1.3.0",
4
4
  "description": "MCP server for sending email via SMTP relay",
5
5
  "main": "dist/index.js",
6
6
  "bin": {