outlook-mcp
Version:
Comprehensive MCP server for Claude to access Microsoft Outlook and Teams via Microsoft Graph API - including Email, Calendar, Contacts, Tasks, Teams, Chats, and Online Meetings
166 lines (144 loc) • 3.74 kB
JavaScript
/**
* Email reply functionality
*/
const { callGraphAPI } = require('../utils/graph-api');
const { ensureAuthenticated } = require('../auth');
/**
* Reply to email handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleReplyToEmail(args) {
try {
const {
emailId,
body,
replyAll = false,
comment = ''
} = args;
if (!emailId || !body) {
return {
error: {
code: -32602,
message: "Email ID and body are required"
}
};
}
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Build the API path
const apiPath = replyAll ?
`me/messages/${emailId}/replyAll` :
`me/messages/${emailId}/reply`;
// Build the request body
const requestBody = {
message: {
body: {
contentType: 'HTML',
content: body
}
},
comment: comment || body
};
console.error(`${replyAll ? 'Reply all' : 'Reply'} to email: ${emailId}`);
// Make API call
await callGraphAPI(accessToken, 'POST', apiPath, requestBody);
return {
content: [
{
type: "text",
text: `✅ Email ${replyAll ? 'reply all' : 'reply'} sent successfully!
**Original Email ID:** ${emailId}
**Reply Type:** ${replyAll ? 'Reply All' : 'Reply'}
**Message:** Your reply has been sent to the appropriate recipients.
The reply has been delivered and saved to your sent items.`
}
]
};
} catch (error) {
console.error('Error in handleReplyToEmail:', error);
return {
error: {
code: -32603,
message: `Failed to reply to email: ${error.message}`
}
};
}
}
/**
* Forward email handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleForwardEmail(args) {
try {
const {
emailId,
to,
cc = '',
bcc = '',
body = '',
comment = ''
} = args;
if (!emailId || !to) {
return {
error: {
code: -32602,
message: "Email ID and 'to' recipients are required"
}
};
}
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Build the API path
const apiPath = `me/messages/${emailId}/forward`;
// Parse recipients
const parseRecipients = (recipients) => {
if (!recipients) return [];
return recipients.split(',').map(email => ({
emailAddress: { address: email.trim() }
}));
};
// Build the request body
const requestBody = {
message: {
toRecipients: parseRecipients(to),
ccRecipients: parseRecipients(cc),
bccRecipients: parseRecipients(bcc),
body: {
contentType: 'HTML',
content: body
}
},
comment: comment || body
};
console.error(`Forward email: ${emailId} to: ${to}`);
// Make API call
await callGraphAPI(accessToken, 'POST', apiPath, requestBody);
return {
content: [
{
type: "text",
text: `✅ Email forwarded successfully!
**Original Email ID:** ${emailId}
**To:** ${to}
**CC:** ${cc || 'None'}
**BCC:** ${bcc || 'None'}
The forwarded email has been sent and saved to your sent items.`
}
]
};
} catch (error) {
console.error('Error in handleForwardEmail:', error);
return {
error: {
code: -32603,
message: `Failed to forward email: ${error.message}`
}
};
}
}
module.exports = {
handleReplyToEmail,
handleForwardEmail
};