@sinch/mcp
Version:
Sinch MCP server
144 lines • 5.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyticsMetricsHandler = exports.registerAnalyticsMetrics = void 0;
const zod_1 = require("zod");
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 utils_1 = require("../../utils");
const metricsTypes = [
// Counts
'accepted_incoming_count',
'accepted_outgoing_count',
'accepted_count',
'delivered_smtp_count',
'delivered_http_count',
'delivered_optimized_count',
'delivered_count',
'stored_count',
'processed_count',
'sent_count',
'opened_count',
'clicked_count',
'unique_opened_count',
'unique_clicked_count',
'unsubscribed_count',
'complained_count',
'failed_count',
'temporary_failed_count',
'permanent_failed_count',
'temporary_failed_esp_block_count',
'permanent_failed_esp_block_count',
'rate_limit_count',
'webhook_count',
'permanent_failed_optimized_count',
'permanent_failed_old_count',
'bounced_count',
'hard_bounces_count',
'soft_bounces_count',
'delayed_bounce_count',
'suppressed_bounces_count',
'suppressed_unsubscribed_count',
'suppressed_complaints_count',
'delivered_first_attempt_count',
'delayed_first_attempt_count',
'delivered_subsequent_count',
'delivered_two_plus_attempts_count',
// Rates
'bounced_rate',
'clicked_rate',
'complained_rate',
'delayed_rate',
'delivered_rate',
'fail_rate',
'opened_rate',
'permanent_fail_rate',
'temporary_fail_rate',
'unique_clicked_rate',
'unique_opened_rate',
'unsubscribed_rate'
];
const AnalyticsMetricsInput = {
domain: zod_1.z.string().optional().describe('(Optional) The Mailgun domain to fetch metrics for.'),
metrics: zod_1.z.array(zod_1.z.enum(metricsTypes)).optional().describe('(Optional) The specific metrics to receive the stats for. If not provided, all metrics will be returned.'),
beginSearchPeriod: zod_1.z.string().optional().describe('(Optional) The beginning of the search time range in RFC-2822 format (e.g., Mon, 02 Jun 2025 00:00:00 +0100).'),
endSearchPeriod: zod_1.z.string().optional().describe('(Optional) The end of the search time range in RFC-2822 format (e.g., Mon, 09 Jun 2025 00:00:00 +0100).'),
};
const TOOL_KEY = 'analyticsMetrics';
const TOOL_NAME = (0, mailgun_tools_helper_1.getToolName)(TOOL_KEY);
const registerAnalyticsMetrics = (server, tags) => {
if (!(0, utils_1.matchesAnyTag)(tags, mailgun_tools_helper_1.toolsConfig[TOOL_KEY].tags))
return;
server.tool(TOOL_NAME, 'Get email analytics metrics from Mailgun for an account. All parameters are optional. You can filter by domain, metrics type and specify a time range. By default, it will return all metrics for all your domains for the last 7 days.', AnalyticsMetricsInput, exports.analyticsMetricsHandler);
};
exports.registerAnalyticsMetrics = registerAnalyticsMetrics;
const analyticsMetricsHandler = async ({ domain, metrics, beginSearchPeriod, endSearchPeriod }) => {
const maybeApiKey = (0, mailgun_service_helper_1.getMailgunApiKey)();
if (typeof maybeApiKey !== 'string') {
return maybeApiKey.promptResponse;
}
const mailgunApiKey = maybeApiKey;
const body = {};
if (domain)
body.domain = {
AND: [
{
attribute: 'domain',
comparator: '=',
values: [
{
label: domain,
value: domain
}
]
}
]
};
if (metrics && metrics.length > 0) {
body.metrics = metrics;
}
else {
body.metrics = metricsTypes;
}
if (beginSearchPeriod)
body.begin = beginSearchPeriod;
if (endSearchPeriod)
body.end = endSearchPeriod;
body.include_aggregates = true;
const url = `https://api.mailgun.net/v1/analytics/metrics`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from(`api:${mailgunApiKey}`).toString('base64'),
'Content-Type': 'application/json',
'User-Agent': (0, utils_1.formatUserAgent)(TOOL_NAME, (0, mailgun_tools_helper_1.sha256)(mailgunApiKey)),
},
body: JSON.stringify(body)
});
if (!response.ok) {
return new types_1.PromptResponse(JSON.stringify({
success: false,
error: `Mailgun API error: ${response.status} ${response.statusText}`
})).promptResponse;
}
let responseData;
try {
responseData = await response.json();
}
catch (error) {
return new types_1.PromptResponse(JSON.stringify({
success: false,
error: error instanceof Error ? error.message : String(error)
})).promptResponse;
}
return new types_1.PromptResponse(JSON.stringify({
success: true,
metrics: responseData.aggregates.metrics,
period: {
begin: responseData.start,
end: responseData.end
}
})).promptResponse;
};
exports.analyticsMetricsHandler = analyticsMetricsHandler;
//# sourceMappingURL=analytics-metrics.js.map