ya funciona al 80% el agent, es una pasada

This commit is contained in:
2025-06-07 02:00:43 -06:00
parent 9c16fb55fa
commit 40dcfe00e4
6 changed files with 170 additions and 277 deletions

View File

@@ -1,209 +1,200 @@
// mcp/modules/whatsappIntegration.js
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); // Changed log prefix
const log = (...args) => console.log("[MCP WhatsAppIntegration]", ...args);
export default function registerWhatsappIntegration(server) {
// --- WhatsApp Actions ---
// Tool: whatsapp.send-text
server.tool(
"whatsapp.send-text",
"Sends a text message via WhatsApp.",
"Envía un mensaje de texto.",
{
to: z.string().describe("Recipient ID (e.g., 1234567890@c.us)"),
content: z.string().describe("Text message content"),
to: z.string(),
content: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.send-text", params);
const result = await fetchJSON("/send-text", {
const result = await fetchJSON("/whatsapp/sendText", {
method: "POST",
body: JSON.stringify(params),
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.send-image
server.tool(
"whatsapp.send-image",
"Sends an image message via WhatsApp.",
"Envía una imagen.",
{
to: z.string().describe("Recipient ID"),
path: z.string().describe("Path or URL to the image"),
caption: z.string().optional().describe("Image caption"),
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("/send-image", {
const result = await fetchJSON("/whatsapp/sendImage", {
method: "POST",
body: JSON.stringify(params),
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.send-file
server.tool(
"whatsapp.send-file",
"Sends a file message via WhatsApp.",
"Envía un archivo.",
{
to: z.string().describe("Recipient ID"),
path: z.string().describe("Path or URL to the file"),
filename: z.string().optional().describe("Name of the file"),
caption: z.string().optional().describe("File caption"),
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("/send-file", {
const result = await fetchJSON("/whatsapp/sendFile", {
method: "POST",
body: JSON.stringify(params),
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.list-chats
server.tool(
"whatsapp.list-chats",
"Retrieves a list of all chats.",
{}, // No input parameters
async () => {
log("Tool invoked", "whatsapp.list-chats");
const result = await fetchJSON("/chats");
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.get-chat-details
server.tool(
"whatsapp.get-chat-details",
"Retrieves details for a specific chat by its ID.",
{
chatId: z.string().describe("ID of the chat (e.g., 1234567890@c.us)"),
},
async (params) => {
log("Tool invoked", "whatsapp.get-chat-details", params);
const result = await fetchJSON(`/chats/${params.chatId}`);
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.get-chat-messages
server.tool(
"whatsapp.get-chat-messages",
"Retrieves messages for a specific chat.",
{
chatId: z.string().describe("ID of the chat"),
limit: z.number().optional().describe("Number of messages to retrieve"),
includeMe: z.boolean().optional().describe("Include messages sent by me"),
includeNotifications: z.boolean().optional().describe("Include notification messages"),
},
async ({ chatId, ...queryParams}) => {
log("Tool invoked", "whatsapp.get-chat-messages", { chatId, queryParams });
const qs = new URLSearchParams(
Object.entries(queryParams)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => [k, String(v)])
);
const result = await fetchJSON(`/chats/${chatId}/messages?${qs.toString()}`);
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.delete-chat",
"Deletes a chat by its ID. (Note: The underlying OpenWA endpoint for this is assumed and might need verification)",
"Elimina un chat.",
{
chatId: z.string().describe("ID of the chat to delete (e.g., 1234567890@c.us)"),
chatId: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.delete-chat", params);
const result = await fetchJSON(`/chats/${params.chatId}`, {
method: "DELETE",
});
// Consider what a successful deletion should return.
// OpenWA might return a success message or just a 200/204.
// For now, we'll return the full result.
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.create-group
server.tool(
"whatsapp.create-group",
"Creates a new WhatsApp group.",
{
groupName: z.string().describe("Name of the group"),
contactIds: z.array(z.string()).describe("Array of contact IDs to add to the group"),
},
async (params) => {
log("Tool invoked", "whatsapp.create-group", params);
const result = await fetchJSON("/groups", {
const result = await fetchJSON("/whatsapp/deleteChat", {
method: "POST",
body: JSON.stringify(params),
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.get-contact-details
server.tool(
"whatsapp.get-contact-details",
"Retrieves details for a specific contact by ID.",
"whatsapp.delete-message",
"Elimina un mensaje.",
{
contactId: z.string().describe("ID of the contact (e.g., 1234567890@c.us)"),
chatId: z.string(),
messageId: z.string(),
onlyLocal: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.get-contact-details", params);
const result = await fetchJSON(`/contacts/${params.contactId}`);
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) }] };
}
);
// Tool: whatsapp.get-blocklist
server.tool(
"whatsapp.get-blocklist",
"Retrieves the blocklist.",
{}, // No input parameters
"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-blocklist");
const result = await fetchJSON("/blocklist");
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.block-contact
server.tool(
"whatsapp.block-contact",
"Blocks a contact on WhatsApp.",
{
contactId: z.string().describe("ID of the contact to block"),
},
async (params) => {
log("Tool invoked", "whatsapp.block-contact", params);
const result = await fetchJSON("/blocklist/block", {
log("Tool invoked", "whatsapp.get-all-chat-ids");
const result = await fetchJSON("/whatsapp/getAllChatIds", {
method: "POST",
body: JSON.stringify(params),
body: JSON.stringify({ args: {} }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// Tool: whatsapp.unblock-contact
server.tool(
"whatsapp.unblock-contact",
"Unblocks a contact on WhatsApp.",
"whatsapp.get-all-messages",
"Obtiene mensajes de un chat.",
{
contactId: z.string().describe("ID of the contact to unblock"),
chatId: z.string(),
includeMe: z.boolean().optional(),
includeNotifications: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.unblock-contact", params);
const result = await fetchJSON("/blocklist/unblock", {
log("Tool invoked", "whatsapp.get-all-messages", params);
const result = await fetchJSON("/whatsapp/getAllMessagesInChat", {
method: "POST",
body: JSON.stringify(params),
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}