@teamnetwork-nz/smtp-mcp-server 1.3.0 → 1.4.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 +4 -0
- package/dist/http-server.js +90 -0
- package/dist/index.js +229 -219
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# @teamnetwork-nz/smtp-mcp-server
|
|
2
2
|
|
|
3
|
+
## Streamable HTTP
|
|
4
|
+
|
|
5
|
+
Set `SMTP_TRANSPORT=http` to serve stateless MCP at `/mcp`. The default bind is `127.0.0.1`; remote binding requires `SMTP_HTTP_BEARER_TOKEN` and `SMTP_HTTP_ALLOWED_HOSTS`. `SMTP_HTTP_PORT` and `SMTP_HTTP_ALLOWED_ORIGINS` are optional.
|
|
6
|
+
|
|
3
7
|
An MCP server that lets AI agents send email via any SMTP relay. Recipient restrictions, sender identity, and SMTP credentials are all enforced server-side — an agent cannot override them.
|
|
4
8
|
|
|
5
9
|
## Features
|
|
@@ -0,0 +1,90 @@
|
|
|
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.validateHttpOptions = validateHttpOptions;
|
|
7
|
+
exports.createHttpServer = createHttpServer;
|
|
8
|
+
const node_crypto_1 = require("node:crypto");
|
|
9
|
+
const node_http_1 = __importDefault(require("node:http"));
|
|
10
|
+
const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
|
11
|
+
const loopback = new Set(["127.0.0.1", "::1", "localhost"]);
|
|
12
|
+
const send = (res, status, body) => {
|
|
13
|
+
res.writeHead(status, { "Content-Type": "application/json" }).end(JSON.stringify(body));
|
|
14
|
+
};
|
|
15
|
+
const error = (res, status, code, message) => send(res, status, { jsonrpc: "2.0", error: { code, message }, id: null });
|
|
16
|
+
const matches = (value, expected) => {
|
|
17
|
+
if (!value?.startsWith("Bearer "))
|
|
18
|
+
return false;
|
|
19
|
+
const supplied = Buffer.from(value.slice(7));
|
|
20
|
+
const wanted = Buffer.from(expected);
|
|
21
|
+
return supplied.length === wanted.length && (0, node_crypto_1.timingSafeEqual)(supplied, wanted);
|
|
22
|
+
};
|
|
23
|
+
const readBody = (req, limit) => new Promise((resolve, reject) => {
|
|
24
|
+
const chunks = [];
|
|
25
|
+
let size = 0;
|
|
26
|
+
let oversized = false;
|
|
27
|
+
req.on("data", (chunk) => { size += chunk.length; oversized ||= size > limit; if (!oversized)
|
|
28
|
+
chunks.push(chunk); });
|
|
29
|
+
req.on("end", () => {
|
|
30
|
+
if (oversized)
|
|
31
|
+
return reject(Object.assign(new Error("Request body too large"), { statusCode: 413 }));
|
|
32
|
+
try {
|
|
33
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
reject(Object.assign(new Error("Invalid JSON body"), { statusCode: 400 }));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
req.on("error", reject);
|
|
40
|
+
});
|
|
41
|
+
function validateHttpOptions(o) {
|
|
42
|
+
if (!Number.isInteger(o.port) || o.port < 1 || o.port > 65535)
|
|
43
|
+
throw new Error(`Invalid HTTP port: ${o.port}`);
|
|
44
|
+
if (o.maxBodyBytes !== undefined && (!Number.isInteger(o.maxBodyBytes) || o.maxBodyBytes < 1))
|
|
45
|
+
throw new Error(`Invalid HTTP body limit: ${o.maxBodyBytes}`);
|
|
46
|
+
if (!loopback.has(o.host) && !o.bearerToken)
|
|
47
|
+
throw new Error("A bearer token is required when binding beyond loopback");
|
|
48
|
+
if (!loopback.has(o.host) && !o.allowedHosts?.length)
|
|
49
|
+
throw new Error("HTTP_ALLOWED_HOSTS is required when binding beyond loopback");
|
|
50
|
+
}
|
|
51
|
+
function createHttpServer(factory, o) {
|
|
52
|
+
validateHttpOptions(o);
|
|
53
|
+
const hosts = new Set(o.allowedHosts?.map(v => v.toLowerCase()));
|
|
54
|
+
const origins = new Set(o.allowedOrigins?.map(v => v.toLowerCase()));
|
|
55
|
+
return node_http_1.default.createServer(async (req, res) => {
|
|
56
|
+
const path = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
57
|
+
if (path === "/health")
|
|
58
|
+
return send(res, 200, { status: "ok", server: o.serverName });
|
|
59
|
+
if (path !== "/mcp")
|
|
60
|
+
return void res.writeHead(404).end();
|
|
61
|
+
if (hosts.size && !hosts.has((req.headers.host ?? "").toLowerCase()))
|
|
62
|
+
return error(res, 403, -32000, "Host not allowed");
|
|
63
|
+
if (req.headers.origin && !origins.has(req.headers.origin.toLowerCase()))
|
|
64
|
+
return error(res, 403, -32000, "Origin not allowed");
|
|
65
|
+
if (o.bearerToken && !matches(req.headers.authorization, o.bearerToken)) {
|
|
66
|
+
res.setHeader("WWW-Authenticate", "Bearer");
|
|
67
|
+
return error(res, 401, -32001, "Unauthorized");
|
|
68
|
+
}
|
|
69
|
+
if (req.method !== "POST") {
|
|
70
|
+
res.setHeader("Allow", "POST");
|
|
71
|
+
return error(res, 405, -32000, "Method not allowed");
|
|
72
|
+
}
|
|
73
|
+
const server = factory();
|
|
74
|
+
const transport = new streamableHttp_js_1.StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
|
75
|
+
try {
|
|
76
|
+
const body = await readBody(req, o.maxBodyBytes ?? 1048576);
|
|
77
|
+
await server.connect(transport);
|
|
78
|
+
await transport.handleRequest(req, res, body);
|
|
79
|
+
res.once("close", () => { void transport.close(); void server.close(); });
|
|
80
|
+
}
|
|
81
|
+
catch (cause) {
|
|
82
|
+
void transport.close();
|
|
83
|
+
void server.close();
|
|
84
|
+
if (!res.headersSent) {
|
|
85
|
+
const status = cause.statusCode ?? 500;
|
|
86
|
+
error(res, status, status === 400 ? -32700 : -32603, status < 500 ? cause.message : "Internal server error");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -41,212 +41,214 @@ const config_js_1 = require("./config.js");
|
|
|
41
41
|
const restrictions_js_1 = require("./restrictions.js");
|
|
42
42
|
const email_js_1 = require("./email.js");
|
|
43
43
|
const mime_js_1 = require("./mime.js");
|
|
44
|
+
const http_server_js_1 = require("./http-server.js");
|
|
44
45
|
const transport = (0, email_js_1.createTransport)(config_js_1.config.smtp);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
isError,
|
|
110
|
-
content: [{ type: "text", text: lines.join("\n") }],
|
|
111
|
-
};
|
|
112
|
-
});
|
|
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);
|
|
46
|
+
function createConfiguredServer() {
|
|
47
|
+
const server = new mcp_js_1.McpServer({
|
|
48
|
+
name: "smtp-mcp",
|
|
49
|
+
version: "1.4.0",
|
|
50
|
+
});
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// Shared schemas
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
const attachmentSchema = zod_1.z
|
|
55
|
+
.object({
|
|
56
|
+
path: zod_1.z.string().optional().describe("Absolute path to the file on disk — server reads and encodes it"),
|
|
57
|
+
filename: zod_1.z.string().optional().describe("Override the filename shown to the recipient"),
|
|
58
|
+
content: zod_1.z.string().optional().describe("Base64-encoded file content (use path instead where possible)"),
|
|
59
|
+
content_type: zod_1.z.string().optional().describe("MIME type — auto-detected from path extension if omitted"),
|
|
60
|
+
})
|
|
61
|
+
.refine((a) => a.path || a.content, {
|
|
62
|
+
message: "Each attachment must include either 'path' (preferred) or 'content' (base64)",
|
|
63
|
+
})
|
|
64
|
+
.refine((a) => !a.content || (a.filename && a.content_type), {
|
|
65
|
+
message: "Attachments using 'content' must also include 'filename' and 'content_type'",
|
|
66
|
+
});
|
|
67
|
+
const recipientSchema = zod_1.z
|
|
68
|
+
.union([zod_1.z.string().email(), zod_1.z.array(zod_1.z.string().email()).min(1)])
|
|
69
|
+
.transform((v) => (Array.isArray(v) ? v : [v]));
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// Tool: health
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
server.registerTool("health", {
|
|
74
|
+
description: "Check that the SMTP server is reachable and the configuration is valid. " +
|
|
75
|
+
"Returns the active settings (host, port, auth mode, sender identity, restrictions) " +
|
|
76
|
+
"and the result of a live connection test.",
|
|
77
|
+
}, async () => {
|
|
78
|
+
const start = Date.now();
|
|
79
|
+
let connectionStatus;
|
|
80
|
+
let isError = false;
|
|
81
|
+
try {
|
|
82
|
+
await (0, email_js_1.verifyTransport)(transport);
|
|
83
|
+
connectionStatus = `OK (${Date.now() - start} ms)`;
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
connectionStatus = `FAILED — ${err instanceof Error ? err.message : String(err)}`;
|
|
87
|
+
isError = true;
|
|
88
|
+
}
|
|
89
|
+
const { allowedDomains, allowedAddresses } = config_js_1.config.restrictions;
|
|
90
|
+
const restrictionSummary = allowedDomains.length === 0 && allowedAddresses.length === 0
|
|
91
|
+
? "none (all addresses permitted)"
|
|
92
|
+
: [
|
|
93
|
+
allowedDomains.length
|
|
94
|
+
? `domains: ${allowedDomains.join(", ")}`
|
|
95
|
+
: null,
|
|
96
|
+
allowedAddresses.length
|
|
97
|
+
? `addresses: ${allowedAddresses.join(", ")}`
|
|
98
|
+
: null,
|
|
99
|
+
]
|
|
100
|
+
.filter(Boolean)
|
|
101
|
+
.join(" | ");
|
|
102
|
+
const lines = [
|
|
103
|
+
`connection: ${connectionStatus}`,
|
|
104
|
+
`smtp_host: ${config_js_1.config.smtp.host}:${config_js_1.config.smtp.port}`,
|
|
105
|
+
`tls: ${config_js_1.config.smtp.secure ? "implicit TLS" : config_js_1.config.smtp.ignoreTls ? "none (plain SMTP)" : "STARTTLS"}`,
|
|
106
|
+
`auth: ${config_js_1.config.smtp.user ? `authenticated as ${config_js_1.config.smtp.user}` : "unauthenticated"}`,
|
|
107
|
+
`from: "${config_js_1.config.from.name}" <${config_js_1.config.from.address}>`,
|
|
108
|
+
`restrictions: ${restrictionSummary}`,
|
|
109
|
+
];
|
|
153
110
|
return {
|
|
154
|
-
isError
|
|
155
|
-
content: [{ type: "text", text:
|
|
111
|
+
isError,
|
|
112
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
156
113
|
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
//
|
|
160
|
-
//
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
.
|
|
248
|
-
|
|
249
|
-
|
|
114
|
+
});
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// Tool: encode_file
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
server.registerTool("encode_file", {
|
|
119
|
+
description: "Read a file from disk and return it base64-encoded. " +
|
|
120
|
+
"NOTE: prefer supplying 'path' directly in the send_email attachments array — " +
|
|
121
|
+
"the server will encode the file itself without passing binary data through the agent. " +
|
|
122
|
+
"Use encode_file only when you need to inspect or transform the content before sending.",
|
|
123
|
+
inputSchema: {
|
|
124
|
+
path: zod_1.z.string().min(1).describe("Absolute or relative path to the file"),
|
|
125
|
+
content_type: zod_1.z
|
|
126
|
+
.string()
|
|
127
|
+
.optional()
|
|
128
|
+
.describe("MIME type override. If omitted the type is detected from the file extension."),
|
|
129
|
+
},
|
|
130
|
+
}, async (args) => {
|
|
131
|
+
const fs = await Promise.resolve().then(() => __importStar(require("fs")));
|
|
132
|
+
const nodePath = await Promise.resolve().then(() => __importStar(require("path")));
|
|
133
|
+
if (!fs.existsSync(args.path)) {
|
|
134
|
+
return {
|
|
135
|
+
isError: true,
|
|
136
|
+
content: [{ type: "text", text: `File not found: ${args.path}` }],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const data = fs.readFileSync(args.path);
|
|
141
|
+
const content = data.toString("base64");
|
|
142
|
+
const filename = nodePath.basename(args.path);
|
|
143
|
+
const content_type = args.content_type ?? (0, mime_js_1.mimeFromExtension)(args.path);
|
|
144
|
+
return {
|
|
145
|
+
content: [
|
|
146
|
+
{
|
|
147
|
+
type: "text",
|
|
148
|
+
text: JSON.stringify({ filename, content_type, content }),
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
155
|
+
return {
|
|
156
|
+
isError: true,
|
|
157
|
+
content: [{ type: "text", text: `Failed to read file: ${message}` }],
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
// Tool: send_email
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
server.registerTool("send_email", {
|
|
165
|
+
description: "Send an email via SMTP. Recipient addresses are validated against the server's allowed domains/addresses policy.",
|
|
166
|
+
inputSchema: {
|
|
167
|
+
to: recipientSchema.describe("Recipient address or list of addresses"),
|
|
168
|
+
subject: zod_1.z.string().min(1).describe("Email subject line"),
|
|
169
|
+
body: zod_1.z.string().min(1).describe("Email body content"),
|
|
170
|
+
body_type: zod_1.z
|
|
171
|
+
.enum(["text", "html"])
|
|
172
|
+
.default("text")
|
|
173
|
+
.describe("Body format — plain text (default) or HTML"),
|
|
174
|
+
cc: recipientSchema
|
|
175
|
+
.optional()
|
|
176
|
+
.describe("CC address or list of addresses"),
|
|
177
|
+
bcc: recipientSchema
|
|
178
|
+
.optional()
|
|
179
|
+
.describe("BCC address or list of addresses"),
|
|
180
|
+
attachments: zod_1.z
|
|
181
|
+
.array(attachmentSchema)
|
|
182
|
+
.optional()
|
|
183
|
+
.describe("Files to attach"),
|
|
184
|
+
},
|
|
185
|
+
}, async (args) => {
|
|
186
|
+
const allRecipients = [
|
|
187
|
+
...args.to,
|
|
188
|
+
...(args.cc ?? []),
|
|
189
|
+
...(args.bcc ?? []),
|
|
190
|
+
];
|
|
191
|
+
const check = (0, restrictions_js_1.checkRecipients)(allRecipients, config_js_1.config.restrictions);
|
|
192
|
+
if (!check.allowed) {
|
|
193
|
+
return {
|
|
194
|
+
isError: true,
|
|
195
|
+
content: [
|
|
196
|
+
{
|
|
197
|
+
type: "text",
|
|
198
|
+
text: `Recipient(s) not permitted by server policy: ${check.blocked.join(", ")}`,
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
const result = await (0, email_js_1.sendEmail)(transport, config_js_1.config.from, {
|
|
205
|
+
to: args.to,
|
|
206
|
+
subject: args.subject,
|
|
207
|
+
body: args.body,
|
|
208
|
+
body_type: args.body_type,
|
|
209
|
+
cc: args.cc,
|
|
210
|
+
bcc: args.bcc,
|
|
211
|
+
attachments: args.attachments,
|
|
212
|
+
});
|
|
213
|
+
return {
|
|
214
|
+
content: [
|
|
215
|
+
{
|
|
216
|
+
type: "text",
|
|
217
|
+
text: `Email sent successfully. Message ID: ${result.messageId}`,
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
224
|
+
return {
|
|
225
|
+
isError: true,
|
|
226
|
+
content: [{ type: "text", text: `Failed to send email: ${message}` }],
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// Resource: usage guide for agents
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
function buildUsageText() {
|
|
234
|
+
const { allowedDomains, allowedAddresses } = config_js_1.config.restrictions;
|
|
235
|
+
const hasRestrictions = allowedDomains.length > 0 || allowedAddresses.length > 0;
|
|
236
|
+
const restrictionSection = hasRestrictions
|
|
237
|
+
? [
|
|
238
|
+
"RECIPIENT RESTRICTIONS",
|
|
239
|
+
"An address is permitted when it satisfies at least one rule:",
|
|
240
|
+
allowedDomains.length
|
|
241
|
+
? ` Allowed domains: ${allowedDomains.join(", ")}`
|
|
242
|
+
: null,
|
|
243
|
+
allowedAddresses.length
|
|
244
|
+
? ` Allowed addresses: ${allowedAddresses.join(", ")}`
|
|
245
|
+
: null,
|
|
246
|
+
"Blocked addresses are rejected before any connection is made to the SMTP server.",
|
|
247
|
+
]
|
|
248
|
+
.filter((l) => l !== null)
|
|
249
|
+
.join("\n")
|
|
250
|
+
: "RECIPIENT RESTRICTIONS\nNone configured — all addresses are permitted.";
|
|
251
|
+
return `
|
|
250
252
|
SMTP MCP SERVER — USAGE GUIDE
|
|
251
253
|
==============================
|
|
252
254
|
|
|
@@ -335,27 +337,35 @@ TIPS
|
|
|
335
337
|
- If you are unsure of a file's MIME type, use application/octet-stream — it
|
|
336
338
|
will arrive correctly, and the recipient's mail client will identify it.
|
|
337
339
|
`.trim();
|
|
340
|
+
}
|
|
341
|
+
server.registerResource("usage", "smtp://server/usage", {
|
|
342
|
+
title: "SMTP MCP Server — Usage Guide",
|
|
343
|
+
description: "Reference guide describing this server's tools, parameters, active sender identity, and recipient restrictions.",
|
|
344
|
+
mimeType: "text/plain",
|
|
345
|
+
}, async () => ({
|
|
346
|
+
contents: [
|
|
347
|
+
{
|
|
348
|
+
uri: "smtp://server/usage",
|
|
349
|
+
text: buildUsageText(),
|
|
350
|
+
mimeType: "text/plain",
|
|
351
|
+
},
|
|
352
|
+
],
|
|
353
|
+
}));
|
|
354
|
+
return server;
|
|
338
355
|
}
|
|
339
|
-
server.registerResource("usage", "smtp://server/usage", {
|
|
340
|
-
title: "SMTP MCP Server — Usage Guide",
|
|
341
|
-
description: "Reference guide describing this server's tools, parameters, active sender identity, and recipient restrictions.",
|
|
342
|
-
mimeType: "text/plain",
|
|
343
|
-
}, async () => ({
|
|
344
|
-
contents: [
|
|
345
|
-
{
|
|
346
|
-
uri: "smtp://server/usage",
|
|
347
|
-
text: buildUsageText(),
|
|
348
|
-
mimeType: "text/plain",
|
|
349
|
-
},
|
|
350
|
-
],
|
|
351
|
-
}));
|
|
352
356
|
// ---------------------------------------------------------------------------
|
|
353
357
|
// Start
|
|
354
358
|
// ---------------------------------------------------------------------------
|
|
355
359
|
async function main() {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
360
|
+
if ((process.env.SMTP_TRANSPORT ?? "stdio") === "http") {
|
|
361
|
+
const host = process.env.SMTP_HTTP_HOST ?? "127.0.0.1";
|
|
362
|
+
const port = Number.parseInt(process.env.SMTP_HTTP_PORT ?? "3000", 10);
|
|
363
|
+
(0, http_server_js_1.createHttpServer)(createConfiguredServer, { host, port, serverName: "@teamnetwork-nz/smtp-mcp-server", bearerToken: process.env.SMTP_HTTP_BEARER_TOKEN, allowedHosts: process.env.SMTP_HTTP_ALLOWED_HOSTS?.split(",").map(v => v.trim()).filter(Boolean), allowedOrigins: process.env.SMTP_HTTP_ALLOWED_ORIGINS?.split(",").map(v => v.trim()).filter(Boolean) }).listen(port, host, () => console.error(`SMTP MCP server listening on ${host}:${port}/mcp`));
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
await createConfiguredServer().connect(new stdio_js_1.StdioServerTransport());
|
|
367
|
+
console.error("SMTP MCP server running on stdio");
|
|
368
|
+
}
|
|
359
369
|
}
|
|
360
370
|
main().catch((err) => {
|
|
361
371
|
console.error("Fatal error:", err);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teamnetwork-nz/smtp-mcp-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "MCP server for sending email via SMTP relay",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "tsc",
|
|
16
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
16
17
|
"start": "node dist/index.js",
|
|
17
18
|
"dev": "tsc --watch",
|
|
18
19
|
"prepublishOnly": "npm run build"
|