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

145 lines (125 loc) 4.29 kB
/** * Update contact functionality */ const { callGraphAPI } = require('../utils/graph-api'); const { ensureAuthenticated } = require('../auth'); /** * Update contact handler * @param {object} args - Tool arguments * @returns {object} - MCP response */ async function handleUpdateContact(args) { try { const { id, displayName, givenName, surname, middleName, nickname, emailAddresses, businessPhones, homePhones, mobilePhone, companyName, jobTitle, department, officeLocation, businessAddress, homeAddress, personalNotes, birthday, categories } = args; if (!id) { return { error: { code: -32602, message: "Contact ID is required" } }; } // Ensure user is authenticated const accessToken = await ensureAuthenticated(); // Build the update object (only include provided fields) const updateData = {}; if (displayName !== undefined) updateData.displayName = displayName; if (givenName !== undefined) updateData.givenName = givenName; if (surname !== undefined) updateData.surname = surname; if (middleName !== undefined) updateData.middleName = middleName; if (nickname !== undefined) updateData.nickname = nickname; if (companyName !== undefined) updateData.companyName = companyName; if (jobTitle !== undefined) updateData.jobTitle = jobTitle; if (department !== undefined) updateData.department = department; if (officeLocation !== undefined) updateData.officeLocation = officeLocation; if (personalNotes !== undefined) updateData.personalNotes = personalNotes; if (birthday !== undefined) updateData.birthday = birthday; // Handle arrays and objects if (emailAddresses !== undefined) { updateData.emailAddresses = Array.isArray(emailAddresses) ? emailAddresses.map(email => { if (typeof email === 'string') { return { address: email, name: displayName || '' }; } return email; }) : []; } if (businessPhones !== undefined) { updateData.businessPhones = Array.isArray(businessPhones) ? businessPhones : []; } if (homePhones !== undefined) { updateData.homePhones = Array.isArray(homePhones) ? homePhones : []; } if (mobilePhone !== undefined) { updateData.mobilePhone = mobilePhone; } if (businessAddress !== undefined) { updateData.businessAddress = businessAddress; } if (homeAddress !== undefined) { updateData.homeAddress = homeAddress; } 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/contacts/${id}`; console.error(`Updating contact: ${id}`); // Make API call const updatedContact = await callGraphAPI(accessToken, 'PATCH', apiPath, updateData); return { content: [ { type: "text", text: `✅ Contact updated successfully! **Name:** ${updatedContact.displayName} **Contact ID:** ${updatedContact.id} **Company:** ${updatedContact.companyName || 'Not provided'} **Job Title:** ${updatedContact.jobTitle || 'Not provided'} **Email:** ${updatedContact.emailAddresses && updatedContact.emailAddresses.length > 0 ? updatedContact.emailAddresses[0].address : 'Not provided'} **Phone:** ${updatedContact.businessPhones && updatedContact.businessPhones.length > 0 ? updatedContact.businessPhones[0] : updatedContact.mobilePhone || 'Not provided'} The contact has been updated with the provided information.` } ] }; } catch (error) { console.error('Error in handleUpdateContact:', error); return { error: { code: -32603, message: `Failed to update contact: ${error.message}` } }; } } module.exports = handleUpdateContact;