- Handle empty 2xx Graph API responses without JSON parse errors - Fix list-rules handler import (handleListRules named export) - Add timezone-aware display formatting using MS_TIMEZONE / DEFAULT_TIMEZONE - Return created event ID in create-event success message - Change default timezone from Windows name to IANA (America/New_York)
102 lines
3.2 KiB
JavaScript
102 lines
3.2 KiB
JavaScript
const { formatDateTime } = require('../utils/time-formatter');
|
|
|
|
/**
|
|
* List emails functionality
|
|
*/
|
|
const config = require('../config');
|
|
const { callGraphAPI } = require('../utils/graph-api');
|
|
const { ensureAuthenticated } = require('../auth');
|
|
const { buildODataFilter, buildDateFilter } = require('../utils/odata-helpers');
|
|
const { resolveFolderPath } = require('./folder-utils');
|
|
|
|
/**
|
|
* List emails handler
|
|
* @param {object} args - Tool arguments
|
|
* @returns {object} - MCP response
|
|
*/
|
|
async function handleListEmails(args) {
|
|
const folder = args.folder || "inbox";
|
|
const count = Math.min(parseInt(args.count, 10) || 10, config.MAX_RESULT_COUNT);
|
|
|
|
try {
|
|
// Get access token
|
|
const accessToken = await ensureAuthenticated();
|
|
|
|
// Resolve folder path using the proper folder utilities
|
|
const endpoint = await resolveFolderPath(accessToken, folder);
|
|
|
|
// Add query parameters
|
|
const queryParams = {
|
|
$top: count,
|
|
$orderby: 'receivedDateTime desc',
|
|
$select: config.EMAIL_SELECT_FIELDS
|
|
};
|
|
|
|
// Add date filtering if specified
|
|
const dateConditions = buildDateFilter(args.dateFrom, args.dateTo, args.dateRange);
|
|
if (dateConditions.length > 0) {
|
|
queryParams.$filter = buildODataFilter(dateConditions);
|
|
}
|
|
|
|
// Make API call
|
|
const response = await callGraphAPI(accessToken, 'GET', endpoint, null, queryParams);
|
|
|
|
if (!response.value || response.value.length === 0) {
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: `No emails found in ${folder}.`
|
|
}]
|
|
};
|
|
}
|
|
|
|
// Format results
|
|
const emailList = response.value.map((email, index) => {
|
|
const sender = email.from ? email.from.emailAddress : { name: 'Unknown', address: 'unknown' };
|
|
const date = formatDateTime(email.receivedDateTime);
|
|
const readStatus = email.isRead ? '' : '[UNREAD] ';
|
|
const convLine = email.conversationId ? `ConversationID: ${email.conversationId}\n` : '';
|
|
|
|
return `${index + 1}. ${readStatus}${date} - From: ${sender.name} (${sender.address})\nSubject: ${email.subject}\nID: ${email.id}\n${convLine}`;
|
|
}).join("\n");
|
|
|
|
// Build result message with date filter info
|
|
let resultMessage = `Found ${response.value.length} emails in ${folder}`;
|
|
|
|
if (args.dateRange) {
|
|
resultMessage += ` (${args.dateRange})`;
|
|
} else if (args.dateFrom || args.dateTo) {
|
|
const dateInfo = [];
|
|
if (args.dateFrom) dateInfo.push(`from: ${args.dateFrom}`);
|
|
if (args.dateTo) dateInfo.push(`to: ${args.dateTo}`);
|
|
resultMessage += ` (${dateInfo.join(', ')})`;
|
|
}
|
|
|
|
resultMessage += `:\n\n${emailList}`;
|
|
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: resultMessage
|
|
}]
|
|
};
|
|
} catch (error) {
|
|
if (error.message === 'Authentication required') {
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: "Authentication required. Please use the 'authenticate' tool first."
|
|
}]
|
|
};
|
|
}
|
|
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: `Error listing emails: ${error.message}`
|
|
}]
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = handleListEmails;
|