import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; import { fetchJSON } from "../lib/api.js"; const log = (...args) => console.log("[MCP WhatsAppIntegration]", ...args); export default function registerWhatsappIntegration(server) { server.tool( "whatsapp.send-text", "Envía un mensaje de texto.", { to: z.string(), content: z.string(), }, async (params) => { log("Tool invoked", "whatsapp.send-text", params); const result = await fetchJSON("/whatsapp/sendText", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.send-image", "Envía una imagen.", { to: z.string(), file: z.string(), filename: z.string().optional(), caption: z.string().optional(), quotedMsgId: z.string().optional(), waitForId: z.boolean().optional(), hideTags: z.boolean().optional(), }, async (params) => { log("Tool invoked", "whatsapp.send-image", params); const result = await fetchJSON("/whatsapp/sendImage", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.send-file", "Envía un archivo.", { to: z.string(), file: z.string(), filename: z.string().optional(), caption: z.string().optional(), ptt: z.boolean().optional(), withoutPreview: z.boolean().optional(), viewOnce: z.boolean().optional(), hideTags: z.boolean().optional(), }, async (params) => { log("Tool invoked", "whatsapp.send-file", params); const result = await fetchJSON("/whatsapp/sendFile", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.delete-chat", "Elimina un chat.", { chatId: z.string(), }, async (params) => { log("Tool invoked", "whatsapp.delete-chat", params); const result = await fetchJSON("/whatsapp/deleteChat", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.delete-message", "Elimina un mensaje.", { chatId: z.string(), messageId: z.string(), onlyLocal: z.boolean().optional(), }, async (params) => { log("Tool invoked", "whatsapp.delete-message", params); const result = await fetchJSON("/whatsapp/deleteMessage", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.edit-message", "Edita un mensaje.", { messageId: z.string(), text: z.string(), }, async (params) => { log("Tool invoked", "whatsapp.edit-message", params); const result = await fetchJSON("/whatsapp/editMessage", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.forward-messages", "Reenvía mensajes.", { to: z.string(), messages: z.array(z.string()), skipMyMessages: z.boolean().optional(), }, async (params) => { log("Tool invoked", "whatsapp.forward-messages", params); const result = await fetchJSON("/whatsapp/forwardMessages", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.get-chat", "Obtiene info de un chat", { contactId: z.string(), }, async (params) => { log("Tool invoked", "whatsapp.get-chat", params); const result = await fetchJSON("/whatsapp/getChat", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.get-all-chats", "Obtiene todos los chats.", { withNewMessageOnly: z.boolean().optional(), }, async (params) => { log("Tool invoked", "whatsapp.get-all-chats", params); const result = await fetchJSON("/whatsapp/getAllChats", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.get-all-chat-ids", "Obtiene todos los chat IDs.", {}, async () => { log("Tool invoked", "whatsapp.get-all-chat-ids"); const result = await fetchJSON("/whatsapp/getAllChatIds", { method: "POST", body: JSON.stringify({ args: {} }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); server.tool( "whatsapp.get-all-messages", "Obtiene mensajes de un chat.", { chatId: z.string(), includeMe: z.boolean().optional(), includeNotifications: z.boolean().optional(), }, async (params) => { log("Tool invoked", "whatsapp.get-all-messages", params); const result = await fetchJSON("/whatsapp/getAllMessagesInChat", { method: "POST", body: JSON.stringify({ args: params }), }); return { content: [{ type: "text", text: JSON.stringify(result) }] }; } ); }