@sinch/mcp
Version:
Sinch MCP server
80 lines • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retrieveEmailInfoHandler = exports.registerRetrieveEmailInfo = void 0;
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 = 'retrieveEmailInfo';
const TOOL_NAME = (0, mailgun_tools_helper_1.getToolName)(TOOL_KEY);
const registerRetrieveEmailInfo = (server, tags) => {
if (!(0, utils_1.matchesAnyTag)(tags, mailgun_tools_helper_1.toolsConfig[TOOL_KEY].tags))
return;
server.tool(TOOL_NAME, 'Retrieve the content of an email and the events that happened thanks to its ID', {
emailId: zod_1.z.string().describe('The email ID.'),
domain: zod_1.z.string().optional().describe('The domain to use for retrieving the email. If defined, it will override the domain provided in the environment variable "MAILGUN_DOMAIN".')
}, exports.retrieveEmailInfoHandler);
};
exports.registerRetrieveEmailInfo = registerRetrieveEmailInfo;
const retrieveEmailInfoHandler = async ({ emailId, domain }) => {
const maybeCredentials = (0, mailgun_service_helper_1.getMailgunCredentials)(domain);
if ((0, utils_1.isPromptResponse)(maybeCredentials)) {
return maybeCredentials.promptResponse;
}
const credentials = maybeCredentials;
const resp = await fetch(`https://api.mailgun.net/v3/${credentials.domain}/events?message-id=${emailId}`, {
method: 'GET',
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)),
}
});
if (resp.status !== 200) {
return new types_1.PromptResponse(JSON.stringify({
success: false,
error: `An error occurred when trying to retrieve the events related to the email ID ${emailId}. The status code is ${resp.status}.`
})).promptResponse;
}
const data = await resp.json();
let storageUrl;
const events = [];
for (const event of data.items) {
const eventType = event.event;
if (eventType === 'accepted') {
storageUrl = event.storage.url;
}
events.push({
event: eventType,
timestamp: new Date(event.timestamp * 1000).toISOString()
});
}
const result = {
message_id: emailId,
events: events
};
const storedEmail = await fetch(storageUrl, {
method: 'GET',
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)),
}
});
if (storedEmail.status === 200) {
const storedEmailData = await storedEmail.json();
result.sender = storedEmailData['Sender'];
result.from = storedEmailData['From'];
result.to = storedEmailData['To'];
result.subject = storedEmailData['Subject'];
result.body_html = storedEmailData['body-html'];
}
else {
return new types_1.PromptResponse(JSON.stringify({
success: false,
error: `An error occurred when trying to retrieve the events related to the email ID ${emailId}. The status code is ${storedEmail.status}.`
})).promptResponse;
}
return new types_1.PromptResponse(JSON.stringify({ success: true, data: result })).promptResponse;
};
exports.retrieveEmailInfoHandler = retrieveEmailInfoHandler;
//# sourceMappingURL=retrieve-email-info.js.map