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

131 lines (113 loc) 3.29 kB
/** * Create task functionality */ const { callGraphAPI } = require('../utils/graph-api'); const { ensureAuthenticated } = require('../auth'); /** * Create task handler * @param {object} args - Tool arguments * @returns {object} - MCP response */ async function handleCreateTask(args) { try { const { title, body = '', dueDateTime, reminderDateTime, importance = 'normal', isReminderOn = false, categories = [], listId } = args; if (!title) { return { error: { code: -32602, message: "title is required" } }; } // Ensure user is authenticated const accessToken = await ensureAuthenticated(); // Get the target list ID 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 task object const taskData = { title, importance, isReminderOn, categories: Array.isArray(categories) ? categories : [] }; // Add body if provided if (body) { taskData.body = { content: body, contentType: 'text' }; } // Add due date if provided if (dueDateTime) { taskData.dueDateTime = { dateTime: dueDateTime, timeZone: config.DEFAULT_TIMEZONE }; } // Add reminder if provided if (reminderDateTime && isReminderOn) { taskData.reminderDateTime = { dateTime: reminderDateTime, timeZone: config.DEFAULT_TIMEZONE }; } // Build the API path const apiPath = `me/todo/lists/${targetListId}/tasks`; console.error(`Creating task: ${title}`); // Make API call const newTask = await callGraphAPI(accessToken, 'POST', apiPath, taskData); // 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 created successfully! **Title:** ${newTask.title} **Task ID:** ${newTask.id} **Importance:** ${newTask.importance} **Status:** ${newTask.status} **Due Date:** ${formatDateTime(newTask.dueDateTime)} **Reminder:** ${newTask.isReminderOn ? formatDateTime(newTask.reminderDateTime) : 'None'} **Categories:** ${newTask.categories && newTask.categories.length > 0 ? newTask.categories.join(', ') : 'None'} The task has been created and added to your task list.` } ] }; } catch (error) { console.error('Error in handleCreateTask:', error); return { error: { code: -32603, message: `Failed to create task: ${error.message}` } }; } } module.exports = handleCreateTask;