@sinch/mcp
Version:
Sinch MCP server
69 lines • 3.72 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, mailgun_tools_helper_1.shouldRegisterTool)(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(`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 eventSummary = [];
for (const event of data.items) {
const eventType = event.event;
if (eventType === 'accepted') {
storageUrl = event.storage.url;
}
eventSummary.push(`Event: ${eventType}, Timestamp: ${new Date(event.timestamp * 1000).toISOString()}`);
}
let result = `Summary of events to be presented into an table in chronological order: ${eventSummary}.`;
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 (resp.status !== 200) {
// Don't fail the tool if the email content cannot be retrieved, just inform the user
result += `\nBut an error occurred when trying to retrieve the events related to the email ID ${emailId}. The status code is ${resp.status}.`;
}
else {
const storedEmailData = await storedEmail.json();
result += `\nSender: ${storedEmailData['Sender']}.`;
result += `\nFrom: ${storedEmailData['From']}.`;
result += `\nTo: ${storedEmailData['To']}.`;
result += `\nSubject: ${storedEmailData['Subject']}.`;
result += `\nEmail content: ${storedEmailData['body-html']}.`;
}
return new types_1.PromptResponse(result).promptResponse;
};
exports.retrieveEmailInfoHandler = retrieveEmailInfoHandler;
//# sourceMappingURL=retrieve-email-info.js.map