msexchange-mcp
Version:
MCP server for Microsoft Exchange/Outlook email operations via Microsoft Graph API
103 lines • 3.95 kB
JavaScript
import { MailService as OriginalMailService } from '../mail/mailService.js';
import accountsConfig from '../config/accountsLoader.js';
/**
* Wrapper for the original MailService to provide a consistent interface
* and type conversions for the unified search system
*/
export class MailService {
static instance;
mailServices = new Map();
constructor() { }
static getInstance() {
if (!MailService.instance) {
MailService.instance = new MailService();
}
return MailService.instance;
}
normalizeAccountId(accountId) {
// Handle various account formats
const accounts = accountsConfig.accounts;
// Check if it's already a valid account ID
if (accounts.some((acc) => acc.id === accountId)) {
return accountId;
}
// Check if it's an email address that maps to an account
const accountByEmail = accounts.find((acc) => acc.email === accountId);
if (accountByEmail) {
return accountByEmail.id;
}
// Default fallback
if (accountId === 'default' && accounts.length > 0) {
return accounts[0].id;
}
return accountId;
}
getOriginalService(accountId) {
const normalizedAccountId = this.normalizeAccountId(accountId);
if (!this.mailServices.has(normalizedAccountId)) {
// Look up the email from account configuration
const accounts = accountsConfig.accounts;
const account = accounts.find((acc) => acc.id === normalizedAccountId);
if (!account) {
throw new Error(`Account '${accountId}' not found. Available accounts: ${accounts.map((a) => a.id).join(', ')}`);
}
this.mailServices.set(normalizedAccountId, new OriginalMailService(account.email));
}
return this.mailServices.get(normalizedAccountId);
}
/**
* Query messages using Microsoft Graph API
*/
async queryMessages(accountId, options) {
const service = this.getOriginalService(accountId);
// Convert select string to array if needed
const selectArray = options.select
? options.select.split(',').map(s => s.trim())
: undefined;
const emails = await service.queryEmails({
search: options.search,
filter: options.filter,
top: options.top,
skip: options.skip,
orderBy: options.orderby,
select: selectArray,
});
// Convert Email type to EmailMessage type
return emails.map(email => this.convertToEmailMessage(email));
}
/**
* Get a single message by ID
*/
async getMessage(accountId, messageId) {
const service = this.getOriginalService(accountId);
const email = await service.getEmailById(messageId);
return this.convertToEmailMessage(email);
}
/**
* Convert from the original Email type to our EmailMessage type
*/
convertToEmailMessage(email) {
return {
id: email.id,
subject: email.subject,
from: email.from,
toRecipients: email.toRecipients,
ccRecipients: email.ccRecipients,
bccRecipients: email.bccRecipients,
receivedDateTime: email.receivedDateTime,
sentDateTime: email.sentDateTime,
bodyPreview: email.bodyPreview,
body: email.body,
isRead: email.isRead,
isDraft: email.isDraft,
hasAttachments: email.hasAttachments,
attachments: email.attachments,
importance: email.importance,
categories: email.categories,
conversationId: email.conversationId,
parentFolderId: email.parentFolderId,
internetMessageHeaders: email.internetMessageHeaders,
};
}
}
//# sourceMappingURL=mail.js.map