@teamnetwork-nz/smtp-mcp-server 1.1.3 → 1.2.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,27 @@ 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
+ 1. encode_file({ path: "/path/to/report.zip" })
99
+ → { filename: "report.zip", content_type: "application/zip", content: "..." }
100
+
101
+ 2. send_email({ ..., attachments: [{ filename, content_type, content }] })
102
+ ```
103
+
104
+ ---
105
+
85
106
  ### `health`
86
107
 
87
108
  No parameters. Performs a live TCP+SMTP handshake against the configured server and returns the active configuration summary.
package/dist/email.js CHANGED
@@ -7,6 +7,33 @@ exports.createTransport = createTransport;
7
7
  exports.verifyTransport = verifyTransport;
8
8
  exports.sendEmail = sendEmail;
9
9
  const nodemailer_1 = __importDefault(require("nodemailer"));
10
+ const DATA_URI_PREFIX = /^data:[^;,]*(;[^,]*)?,/;
11
+ const BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/;
12
+ const MIME_TYPE_PATTERN = /^[\w!#$&\-^.+]+\/[\w!#$&\-^.+]+$/;
13
+ /**
14
+ * Strips a leading data: URI scheme (if present) and any whitespace from a
15
+ * base64 payload, then validates what remains is well-formed base64.
16
+ * Node's Buffer.from(..., "base64") silently skips invalid characters rather
17
+ * than erroring, so an unstripped data URI or stray characters would
18
+ * silently corrupt the decoded attachment instead of failing loudly.
19
+ */
20
+ function normalizeAndValidateBase64(content, filename) {
21
+ const withoutDataUri = content.replace(DATA_URI_PREFIX, "");
22
+ const stripped = withoutDataUri.replace(/\s/g, "");
23
+ if (stripped.length === 0 || stripped.length % 4 !== 0 || !BASE64_PATTERN.test(stripped)) {
24
+ throw new Error(`Attachment "${filename}" does not contain valid base64 data. ` +
25
+ `Provide the raw base64-encoded file content (no data: URI prefix).`);
26
+ }
27
+ return stripped;
28
+ }
29
+ function validateContentType(contentType, filename) {
30
+ const trimmed = contentType.trim();
31
+ if (!MIME_TYPE_PATTERN.test(trimmed)) {
32
+ throw new Error(`Attachment "${filename}" has an invalid content_type "${contentType}". ` +
33
+ `Expected a MIME type in the form "type/subtype", e.g. "application/pdf".`);
34
+ }
35
+ return trimmed;
36
+ }
10
37
  function createTransport(smtpConfig) {
11
38
  return nodemailer_1.default.createTransport({
12
39
  host: smtpConfig.host,
@@ -30,8 +57,8 @@ async function sendEmail(transport, from, options) {
30
57
  bcc: options.bcc,
31
58
  attachments: options.attachments?.map((a) => ({
32
59
  filename: a.filename,
33
- content: Buffer.from(a.content, "base64"),
34
- contentType: a.content_type,
60
+ content: Buffer.from(normalizeAndValidateBase64(a.content, a.filename), "base64"),
61
+ contentType: validateContentType(a.content_type, a.filename),
35
62
  })),
36
63
  };
37
64
  if (options.body_type === "html") {
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,10 +40,11 @@ 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.2.0",
14
48
  });
15
49
  // ---------------------------------------------------------------------------
16
50
  // Shared schemas
@@ -69,6 +103,51 @@ server.registerTool("health", {
69
103
  };
70
104
  });
71
105
  // ---------------------------------------------------------------------------
106
+ // Tool: encode_file
107
+ // ---------------------------------------------------------------------------
108
+ server.registerTool("encode_file", {
109
+ description: "Read a file from disk and return it base64-encoded, ready to pass directly " +
110
+ "into the send_email attachments array. Also returns the detected MIME type " +
111
+ "and filename so the agent does not need to derive them manually.",
112
+ inputSchema: {
113
+ path: zod_1.z.string().min(1).describe("Absolute or relative path to the file"),
114
+ content_type: zod_1.z
115
+ .string()
116
+ .optional()
117
+ .describe("MIME type override. If omitted the type is detected from the file extension."),
118
+ },
119
+ }, async (args) => {
120
+ const fs = await Promise.resolve().then(() => __importStar(require("fs")));
121
+ const nodePath = await Promise.resolve().then(() => __importStar(require("path")));
122
+ if (!fs.existsSync(args.path)) {
123
+ return {
124
+ isError: true,
125
+ content: [{ type: "text", text: `File not found: ${args.path}` }],
126
+ };
127
+ }
128
+ try {
129
+ const data = fs.readFileSync(args.path);
130
+ const content = data.toString("base64");
131
+ const filename = nodePath.basename(args.path);
132
+ const content_type = args.content_type ?? (0, mime_js_1.mimeFromExtension)(args.path);
133
+ return {
134
+ content: [
135
+ {
136
+ type: "text",
137
+ text: JSON.stringify({ filename, content_type, content }),
138
+ },
139
+ ],
140
+ };
141
+ }
142
+ catch (err) {
143
+ const message = err instanceof Error ? err.message : String(err);
144
+ return {
145
+ isError: true,
146
+ content: [{ type: "text", text: `Failed to read file: ${message}` }],
147
+ };
148
+ }
149
+ });
150
+ // ---------------------------------------------------------------------------
72
151
  // Tool: send_email
73
152
  // ---------------------------------------------------------------------------
74
153
  server.registerTool("send_email", {
@@ -186,6 +265,17 @@ health
186
265
  Performs a live TCP+SMTP connection test and returns the active configuration.
187
266
  Call this first to confirm the server is reachable before sending.
188
267
 
268
+ encode_file
269
+ Required
270
+ path string Absolute or relative path to the file on disk.
271
+ Optional
272
+ content_type string MIME type override. Auto-detected from extension if omitted.
273
+
274
+ Reads the file, base64-encodes it, and returns a JSON object with three fields:
275
+ { filename, content_type, content }
276
+ These can be spread directly into a send_email attachment entry.
277
+ Use this tool instead of reading and encoding files manually.
278
+
189
279
  send_email
190
280
  Required
191
281
  to string | string[] One or more recipient email addresses.
@@ -200,9 +290,23 @@ send_email
200
290
 
201
291
  Attachment schema
202
292
  filename string Name shown to the recipient (e.g. "report.pdf").
203
- content string File data encoded as base64.
293
+ content string File data encoded as base64. Use encode_file to produce this.
204
294
  content_type string MIME type (e.g. "application/pdf", "image/png").
205
295
 
296
+ ATTACHMENTS — HOW TO PREPARE FILES
297
+ ------------------------------------
298
+ Use the encode_file tool — it handles reading, encoding, and MIME detection:
299
+
300
+ 1. Call encode_file({ path: "/absolute/path/to/file.zip" })
301
+ 2. Parse the returned JSON: { filename, content_type, content }
302
+ 3. Pass those three fields as an entry in the send_email attachments array
303
+
304
+ Do NOT use a generic file-reading tool to load binary files — text-mode
305
+ readers corrupt binary content. encode_file always reads in binary mode.
306
+
307
+ If you already have base64 content from another source, ensure it is a raw
308
+ base64 string with no data URL prefix (strip any "data:<type>;base64," first).
309
+
206
310
  TIPS
207
311
  ----
208
312
  - Call health before send_email if you are unsure whether the relay is up.
@@ -211,6 +315,8 @@ TIPS
211
315
  blocked address will reject the entire request.
212
316
  - Attachments have no size limit enforced by this server; the SMTP relay may
213
317
  impose its own limits.
318
+ - If you are unsure of a file's MIME type, use application/octet-stream — it
319
+ will arrive correctly, and the recipient's mail client will identify it.
214
320
  `.trim();
215
321
  }
216
322
  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.2.0",
4
4
  "description": "MCP server for sending email via SMTP relay",
5
5
  "main": "dist/index.js",
6
6
  "bin": {