UNPKG

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

157 lines (136 loc) 4.26 kB
/** * Update task functionality */ const { callGraphAPI } = require('../utils/graph-api'); const { ensureAuthenticated } = require('../auth'); const config = require('../config'); /** * Update task handler * @param {object} args - Tool arguments * @returns {object} - MCP response */ async function handleUpdateTask(args) { try { const { taskId, listId, title, body, status, dueDateTime, reminderDateTime, importance, isReminderOn, categories } = 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; } // Build the update object (only include provided fields) const updateData = {}; if (title !== undefined) updateData.title = title; if (status !== undefined) updateData.status = status; if (importance !== undefined) updateData.importance = importance; if (isReminderOn !== undefined) updateData.isReminderOn = isReminderOn; // Handle body if (body !== undefined) { updateData.body = { content: body, contentType: 'text' }; } // Handle due date if (dueDateTime !== undefined) { if (dueDateTime === null || dueDateTime === '') { updateData.dueDateTime = null; } else { updateData.dueDateTime = { dateTime: dueDateTime, timeZone: config.DEFAULT_TIMEZONE }; } } // Handle reminder if (reminderDateTime !== undefined) { if (reminderDateTime === null || reminderDateTime === '') { updateData.reminderDateTime = null; } else { updateData.reminderDateTime = { dateTime: reminderDateTime, timeZone: config.DEFAULT_TIMEZONE }; } } // Handle categories if (categories !== undefined) { updateData.categories = Array.isArray(categories) ? categories : []; } // Ensure we have at least one field to update if (Object.keys(updateData).length === 0) { return { error: { code: -32602, message: "At least one field must be provided to update" } }; } // Build the API path const apiPath = `me/todo/lists/${targetListId}/tasks/${taskId}`; console.error(`Updating task: ${taskId}`); // Make API call const updatedTask = await callGraphAPI(accessToken, 'PATCH', apiPath, updateData); // Format dates for display const formatDateTime = (dateObj) => { if (!dateObj || !dateObj.dateTime) return 'Not set'; return new Date(dateObj.dateTime).toLocaleString(); }; return { content: [ { type: "text", text: `✅ Task updated successfully! **Title:** ${updatedTask.title} **Task ID:** ${updatedTask.id} **Status:** ${updatedTask.status} **Importance:** ${updatedTask.importance} **Due Date:** ${formatDateTime(updatedTask.dueDateTime)} **Reminder:** ${updatedTask.isReminderOn ? formatDateTime(updatedTask.reminderDateTime) : 'None'} **Categories:** ${updatedTask.categories && updatedTask.categories.length > 0 ? updatedTask.categories.join(', ') : 'None'} The task has been updated with the provided information.` } ] }; } catch (error) { console.error('Error in handleUpdateTask:', error); return { error: { code: -32603, message: `Failed to update task: ${error.message}` } }; } } module.exports = handleUpdateTask;