/**
* Utility tools for MCP WPPConnect Server.
* Handles contacts and chats retrieval.
*/
import { GetContactsSchema, GetContactsInput, GetChatsSchema, GetChatsInput } from '../schemas/validations.js';
import { sessionManager } from '../utils/sessionManager.js';
import { logger } from '../utils/logger.js';
import { ToolResponse } from '../types/index.js';
/**
* Get all contacts for the authenticated session.
*/
export const getContactsTool = {
name: 'get_contacts',
description: 'Get the list of all contacts for the authenticated WhatsApp session.',
inputSchema: GetContactsSchema,
execute: async (input: GetContactsInput): Promise<ToolResponse> => {
try {
logger.info('getContactsTool', `Getting contacts for session ${input.sessionId}`);
const client = sessionManager.getClient(input.sessionId);
if (!client) {
return {
ok: false,
error: 'Session not found or not authenticated.',
};
}
const contacts = await client.getAllChats(); // Using getAllChats as getContacts doesn't exist
logger.info('getContactsTool', `Retrieved ${contacts?.length || 0} contacts`);
return {
ok: true,
data: {
contacts: contacts || [],
count: contacts?.length || 0,
},
};
} catch (error) {
logger.error('getContactsTool', error);
return {
ok: false,
error: `Failed to get contacts: ${String(error)}`,
};
}
},
};
/**
* Get all chats for the authenticated session.
*/
export const getChatsTool = {
name: 'get_chats',
description: 'Get the list of all chats (conversations) for the authenticated WhatsApp session.',
inputSchema: GetChatsSchema,
execute: async (input: GetChatsInput): Promise<ToolResponse> => {
try {
logger.info('getChatsTool', `Getting chats for session ${input.sessionId}`);
const client = sessionManager.getClient(input.sessionId);
if (!client) {
return {
ok: false,
error: 'Session not found or not authenticated.',
};
}
const chats = await client.getAllChats();
logger.info('getChatsTool', `Retrieved ${chats?.length || 0} chats`);
return {
ok: true,
data: {
chats: chats || [],
count: chats?.length || 0,
},
};
} catch (error) {
logger.error('getChatsTool', error);
return {
ok: false,
error: `Failed to get chats: ${String(error)}`,
};
}
},
};
/**
* Array of all utility tools.
*/
export const utilityTools = [getContactsTool, getChatsTool];