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
77 lines (66 loc) • 2.36 kB
JavaScript
/**
* List contacts functionality
*/
const { callGraphAPI } = require('../utils/graph-api');
const { ensureAuthenticated } = require('../auth');
const config = require('../config');
/**
* List contacts handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleListContacts(args) {
try {
const { folder = 'contacts', count = config.DEFAULT_PAGE_SIZE } = args;
// Ensure user is authenticated
const accessToken = await ensureAuthenticated();
// Validate count parameter
const validCount = Math.min(Math.max(1, count), config.MAX_RESULT_COUNT);
// Build the API path
let apiPath = 'me/contacts';
if (folder && folder !== 'contacts') {
apiPath = `me/contactFolders/${folder}/contacts`;
}
// Build query parameters
const queryParams = {
'$select': config.CONTACTS_SELECT_FIELDS,
'$top': validCount,
'$orderby': 'displayName asc'
};
console.error(`Fetching ${validCount} contacts from ${folder}`);
// Make API call
const response = await callGraphAPI(accessToken, 'GET', apiPath, null, queryParams);
const contacts = response.value || [];
return {
content: [
{
type: "text",
text: `Found ${contacts.length} contacts:\n\n${contacts.map(contact => {
const name = contact.displayName || 'No name';
const company = contact.companyName ? ` (${contact.companyName})` : '';
const emails = contact.emailAddresses && contact.emailAddresses.length > 0
? contact.emailAddresses.map(e => e.address).join(', ')
: 'No email';
const phones = contact.businessPhones && contact.businessPhones.length > 0
? contact.businessPhones.join(', ')
: contact.mobilePhone || 'No phone';
return `📧 ${name}${company}
ID: ${contact.id}
Email: ${emails}
Phone: ${phones}
${contact.jobTitle ? `Title: ${contact.jobTitle}` : ''}`;
}).join('\n\n')}`
}
]
};
} catch (error) {
console.error('Error in handleListContacts:', error);
return {
error: {
code: -32603,
message: `Failed to list contacts: ${error.message}`
}
};
}
}
module.exports = handleListContacts;