/**
* Community tools for MCP WPPConnect Server.
* Handles community participant management (get, promote/demote).
*/
import { GetCommunityParticipantsSchema, GetCommunityParticipantsInput, PromoteCommunityParticipantSchema, PromoteCommunityParticipantInput, DemoteCommunityParticipantSchema, DemoteCommunityParticipantInput } from '../schemas/validations.js';
import { sessionManager } from '../utils/sessionManager.js';
import { logger } from '../utils/logger.js';
import { ToolResponse } from '../types/index.js';
/**
* Get participants in a community.
*/
export const getCommunityParticipantsTool = {
name: 'get_community_participants',
description: 'Get the list of participants in a WhatsApp community.',
inputSchema: GetCommunityParticipantsSchema,
execute: async (input: GetCommunityParticipantsInput): Promise<ToolResponse> => {
try {
logger.info('getCommunityParticipantsTool', `Getting participants for community ${input.communityId}`);
const client = sessionManager.getClient(input.sessionId);
if (!client) {
return {
ok: false,
error: 'Session not found or not authenticated.',
};
}
const participants = await client.getCommunityParticipants(input.communityId);
logger.info('getCommunityParticipantsTool', `Retrieved ${participants?.length || 0} participants`);
return {
ok: true,
data: {
communityId: input.communityId,
participants: participants || [],
count: participants?.length || 0,
},
};
} catch (error) {
logger.error('getCommunityParticipantsTool', error);
return {
ok: false,
error: `Failed to get community participants: ${String(error)}`,
};
}
},
};
/**
* Promote a participant to admin in a community.
*/
export const promoteCommunityParticipantTool = {
name: 'promote_community_participant',
description: 'Promote a participant to admin status in a WhatsApp community.',
inputSchema: PromoteCommunityParticipantSchema,
execute: async (input: PromoteCommunityParticipantInput): Promise<ToolResponse> => {
try {
logger.info('promoteCommunityParticipantTool', `Promoting ${input.participantId} in community ${input.communityId}`);
const client = sessionManager.getClient(input.sessionId);
if (!client) {
return {
ok: false,
error: 'Session not found or not authenticated.',
};
}
await client.promoteCommunityParticipant(input.communityId, input.participantId);
logger.info('promoteCommunityParticipantTool', `Participant ${input.participantId} promoted successfully`);
return {
ok: true,
data: {
communityId: input.communityId,
participantId: input.participantId,
message: 'Participant promoted to admin successfully.',
},
};
} catch (error) {
logger.error('promoteCommunityParticipantTool', error);
return {
ok: false,
error: `Failed to promote participant: ${String(error)}`,
};
}
},
};
/**
* Demote a participant from admin in a community.
*/
export const demoteCommunityParticipantTool = {
name: 'demote_community_participant',
description: 'Demote a participant from admin status in a WhatsApp community.',
inputSchema: DemoteCommunityParticipantSchema,
execute: async (input: DemoteCommunityParticipantInput): Promise<ToolResponse> => {
try {
logger.info('demoteCommunityParticipantTool', `Demoting ${input.participantId} in community ${input.communityId}`);
const client = sessionManager.getClient(input.sessionId);
if (!client) {
return {
ok: false,
error: 'Session not found or not authenticated.',
};
}
await client.demoteCommunityParticipant(input.communityId, input.participantId);
logger.info('demoteCommunityParticipantTool', `Participant ${input.participantId} demoted successfully`);
return {
ok: true,
data: {
communityId: input.communityId,
participantId: input.participantId,
message: 'Participant demoted from admin successfully.',
},
};
} catch (error) {
logger.error('demoteCommunityParticipantTool', error);
return {
ok: false,
error: `Failed to demote participant: ${String(error)}`,
};
}
},
};
/**
* Array of all community tools.
*/
export const communityTools = [getCommunityParticipantsTool, promoteCommunityParticipantTool, demoteCommunityParticipantTool];