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
225 lines (190 loc) • 5.53 kB
JavaScript
/**
* Email categories functionality
*/
const { callGraphAPI } = require('../utils/graph-api');
const { ensureAuthenticated } = require('../auth');
/**
* Set email categories handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleSetEmailCategories(args) {
try {
const { emailId, categories } = args;
if (!emailId || !categories) {
return {
error: {
code: -32602,
message: "Email ID and categories are required"
}
};
}
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Build the API path
const apiPath = `me/messages/${emailId}`;
// Build the update data
const updateData = {
categories: Array.isArray(categories) ? categories : [categories]
};
console.error(`Setting categories for email: ${emailId}`);
// Make API call
const updatedEmail = await callGraphAPI(accessToken, 'PATCH', apiPath, updateData);
return {
content: [
{
type: "text",
text: `✅ Email categories updated successfully!
**Email ID:** ${emailId}
**Subject:** ${updatedEmail.subject}
**Categories:** ${updatedEmail.categories && updatedEmail.categories.length > 0 ? updatedEmail.categories.join(', ') : 'None'}
The email has been categorized as specified.`
}
]
};
} catch (error) {
console.error('Error in handleSetEmailCategories:', error);
return {
error: {
code: -32603,
message: `Failed to set email categories: ${error.message}`
}
};
}
}
/**
* Set email importance handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleSetEmailImportance(args) {
try {
const { emailId, importance } = args;
if (!emailId || !importance) {
return {
error: {
code: -32602,
message: "Email ID and importance are required"
}
};
}
// Validate importance level
const validImportance = ['low', 'normal', 'high'];
if (!validImportance.includes(importance)) {
return {
error: {
code: -32602,
message: `Invalid importance level. Must be one of: ${validImportance.join(', ')}`
}
};
}
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Build the API path
const apiPath = `me/messages/${emailId}`;
// Build the update data
const updateData = {
importance: importance
};
console.error(`Setting importance for email: ${emailId} to: ${importance}`);
// Make API call
const updatedEmail = await callGraphAPI(accessToken, 'PATCH', apiPath, updateData);
return {
content: [
{
type: "text",
text: `✅ Email importance updated successfully!
**Email ID:** ${emailId}
**Subject:** ${updatedEmail.subject}
**Importance:** ${updatedEmail.importance}
The email importance has been set to ${importance}.`
}
]
};
} catch (error) {
console.error('Error in handleSetEmailImportance:', error);
return {
error: {
code: -32603,
message: `Failed to set email importance: ${error.message}`
}
};
}
}
/**
* Flag email handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleFlagEmail(args) {
try {
const { emailId, flagStatus = 'flagged', dueDateTime } = args;
if (!emailId) {
return {
error: {
code: -32602,
message: "Email ID is required"
}
};
}
// Validate flag status
const validStatus = ['notFlagged', 'flagged', 'complete'];
if (!validStatus.includes(flagStatus)) {
return {
error: {
code: -32602,
message: `Invalid flag status. Must be one of: ${validStatus.join(', ')}`
}
};
}
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Build the API path
const apiPath = `me/messages/${emailId}`;
// Build the update data
const updateData = {
flag: {
flagStatus: flagStatus
}
};
// Add due date if provided
if (dueDateTime && flagStatus === 'flagged') {
updateData.flag.dueDateTime = {
dateTime: dueDateTime,
timeZone: 'UTC'
};
}
console.error(`Setting flag for email: ${emailId} to: ${flagStatus}`);
// Make API call
const updatedEmail = await callGraphAPI(accessToken, 'PATCH', apiPath, updateData);
const flagInfo = updatedEmail.flag || {};
const dueDate = flagInfo.dueDateTime ?
new Date(flagInfo.dueDateTime.dateTime).toLocaleString() : 'None';
return {
content: [
{
type: "text",
text: `✅ Email flag updated successfully!
**Email ID:** ${emailId}
**Subject:** ${updatedEmail.subject}
**Flag Status:** ${flagInfo.flagStatus || 'Not flagged'}
**Due Date:** ${dueDate}
The email flag has been set to ${flagStatus}.`
}
]
};
} catch (error) {
console.error('Error in handleFlagEmail:', error);
return {
error: {
code: -32603,
message: `Failed to flag email: ${error.message}`
}
};
}
}
module.exports = {
handleSetEmailCategories,
handleSetEmailImportance,
handleFlagEmail
};