@sinch/mcp
Version:
Sinch MCP server
69 lines • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEmailHandler = exports.registerSendEmail = void 0;
const undici_1 = require("undici");
const zod_1 = require("zod");
const utils_1 = require("../../utils");
const types_1 = require("../../types");
const mailgun_service_helper_1 = require("./utils/mailgun-service-helper");
const mailgun_tools_helper_1 = require("./utils/mailgun-tools-helper");
const TOOL_KEY = 'sendEmail';
const TOOL_NAME = (0, mailgun_tools_helper_1.getToolName)(TOOL_KEY);
const registerSendEmail = (server, tags) => {
if (!(0, mailgun_tools_helper_1.shouldRegisterTool)(TOOL_KEY, tags))
return;
server.tool(TOOL_NAME, 'Send an email to a recipient with a subject and body.', {
recipient: zod_1.z.string().describe('The recipient of the email.'),
subject: zod_1.z.string().describe('The subject of the email.'),
sender: zod_1.z.string().optional().describe('The sender of the email.'),
body: zod_1.z.string().optional().describe('The body of the email. Can be text or HTML'),
template: zod_1.z.string().optional().describe('The name of a template to use to render the email body. If provided, the body will be ignored.'),
templateVariables: zod_1.z.record(zod_1.z.string()).optional().describe('Variables to use in the template.'),
domain: zod_1.z.string().optional().describe('The domain to use for sending the email. It would override the domain provided in the environment variables.')
}, exports.sendEmailHandler);
};
exports.registerSendEmail = registerSendEmail;
const sendEmailHandler = async ({ recipient, subject, sender, body, template, templateVariables, domain }) => {
const maybeCredentials = (0, mailgun_service_helper_1.getMailgunCredentials)(domain);
if ((0, utils_1.isPromptResponse)(maybeCredentials)) {
return maybeCredentials.promptResponse;
}
const credentials = maybeCredentials;
if (!sender) {
sender = process.env.MAILGUN_SENDER_ADDRESS;
if (!sender) {
return new types_1.PromptResponse('The "sender" is not provided and MAILGUN_SENDER_ADDRESS is no set in the environment variables.').promptResponse;
}
}
const form = new undici_1.FormData();
form.set('from', sender);
form.set('to', recipient);
form.set('subject', subject);
if (template) {
form.set('template', template);
if (templateVariables) {
form.set('t:variables', JSON.stringify(templateVariables));
}
}
else if (body) {
form.set('html', body);
}
else {
return new types_1.PromptResponse('The "body" is not provided and no template name is specified.').promptResponse;
}
const resp = await (0, undici_1.fetch)(`https://api.mailgun.net/v3/${credentials.domain}/messages`, {
method: 'POST',
headers: {
Authorization: 'Basic ' + Buffer.from(`api:${credentials.apiKey}`).toString('base64'),
'User-Agent': (0, utils_1.formatUserAgent)(TOOL_NAME, (0, mailgun_tools_helper_1.sha256)(credentials.apiKey)),
},
body: form
});
if (resp.status !== 200) {
return new types_1.PromptResponse(`An error occurred when trying to send the email: ${JSON.stringify(resp)} The status code is ${resp.status}: ${resp.statusText}.`).promptResponse;
}
const data = await resp.json();
return new types_1.PromptResponse(`Email sent to ${recipient} with subject "${subject}"! The message ID is ${data.id}`).promptResponse;
};
exports.sendEmailHandler = sendEmailHandler;
//# sourceMappingURL=send-email.js.map