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
102 lines (85 loc) • 2.66 kB
JavaScript
/**
* Delete task functionality
*/
const { callGraphAPI } = require('../utils/graph-api');
const { ensureAuthenticated } = require('../auth');
/**
* Delete task handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleDeleteTask(args) {
try {
const { taskId, listId } = args;
if (!taskId) {
return {
error: {
code: -32602,
message: "taskId is required"
}
};
}
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Get the target list ID if not provided
let targetListId = listId;
if (!targetListId) {
// Get default task list
const listsResponse = await callGraphAPI(accessToken, 'GET', 'me/todo/lists', null, { '$select': 'id,displayName,isOwner' });
const defaultList = listsResponse.value.find(list => list.displayName === 'Tasks') || listsResponse.value[0];
if (!defaultList) {
return {
error: {
code: -32603,
message: "No task lists found"
}
};
}
targetListId = defaultList.id;
}
// First, get the task details for confirmation
const taskPath = `me/todo/lists/${targetListId}/tasks/${taskId}`;
let taskTitle = 'Unknown';
try {
const task = await callGraphAPI(accessToken, 'GET', taskPath, null, { '$select': 'title' });
taskTitle = task.title || 'Unknown';
} catch (error) {
// If we can't get the task, it might not exist
console.error('Could not retrieve task for deletion:', error);
}
// Build the API path
const apiPath = `me/todo/lists/${targetListId}/tasks/${taskId}`;
console.error(`Deleting task: ${taskId} (${taskTitle})`);
// Make API call to delete the task
await callGraphAPI(accessToken, 'DELETE', apiPath);
return {
content: [
{
type: "text",
text: `✅ Task deleted successfully!
**Task:** ${taskTitle}
**Task ID:** ${taskId}
The task has been permanently removed from your task list.`
}
]
};
} catch (error) {
console.error('Error in handleDeleteTask:', error);
// Handle specific error cases
if (error.message.includes('404') || error.message.includes('Not Found')) {
return {
error: {
code: -32603,
message: `Task not found: ${taskId}`
}
};
}
return {
error: {
code: -32603,
message: `Failed to delete task: ${error.message}`
}
};
}
}
module.exports = handleDeleteTask;