Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(mcp): accept base64 media next to an unused url
Yudhi Armyndharis17455d7f0560
diff --git a/src/core/agent-tools/tools/message.tools.spec.ts b/src/core/agent-tools/tools/message.tools.spec.tsindex ecf11064f..2ae984ebf 100644--- a/src/core/agent-tools/tools/message.tools.spec.ts+++ b/src/core/agent-tools/tools/message.tools.spec.ts@@ -398,6 +398,42 @@ describe('messageTools', () => {expect(sendImage).toHaveBeenCalledWith('s1', expect.objectContaining({ url: 'http://media_server:8080/a.jpg' }));});+ describe.each([+ ['MessageSendImage', 'sendImage'],+ ['MessageSendVideo', 'sendVideo'],+ ['MessageSendAudio', 'sendAudio'],+ ['MessageSendDocument', 'sendDocument'],+ ['MessageSendSticker', 'sendSticker'],+ ])('%s url next to base64', (toolName, method) => {+ const call = (extra: Record<string, unknown>) => {+ const send = jest.fn().mockResolvedValue({ id: 'm1' });+ const tools = makeTools({ [method]: send } as unknown as MessageService);+ const result = run(tools.get(toolName)!, { sessionId: 's1', chatId: '[email protected]', ...extra });+ return { send, result };+ };++ it.each([+ ['an empty url', ''],+ ['a url that is not http(s)', 'ftp://example.com/a.bin'],+ ])('sends the base64 and ignores %s, as the REST route does', async (_label, url) => {+ const { send, result } = call({ url, base64: 'QUJD', mimetype: 'application/octet-stream' });+ await result;+ expect(send).toHaveBeenCalledWith('s1', expect.objectContaining({ url, base64: 'QUJD' }));+ });++ it.each([+ ['no base64', undefined],+ ['an empty base64', ''],+ ['a base64 that is only a data-URI prefix', 'data:image/png;base64,'],+ ])('still rejects a url that is not http(s) next to %s', async (_label, base64) => {+ const { send, result } = call({ url: 'ftp://example.com/a.bin', base64 });+ await expect(result).rejects.toMatchObject({+ response: { message: [expect.stringContaining('url: ') as unknown as string] },+ });+ expect(send).not.toHaveBeenCalled();+ });+ });+it('MessageForward delegates to forward', async () => {const forward = jest.fn().mockResolvedValue({ id: 'm2' });const tools = makeTools({ forward } as unknown as MessageService);diff --git a/src/core/agent-tools/tools/message.tools.ts b/src/core/agent-tools/tools/message.tools.tsindex bb3e013b2..e0ebb40a9 100644--- a/src/core/agent-tools/tools/message.tools.ts+++ b/src/core/agent-tools/tools/message.tools.ts@@ -1,6 +1,7 @@import { z } from 'zod';import { isMediaUrl, MEDIA_URL_MESSAGE } from '../../../common/media/media-url';import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';+import { stripBase64DataUri } from '../../../modules/message/media-cap.util';import type { MessageService } from '../../../modules/message/message.service';import {CHAT_ID_MAX_LENGTH,@@ -36,12 +37,17 @@ const quotedMessageIdSchema = z'the serialized message id, Baileys the raw key id of a message it has already stored.',);+const mediaUrl = (label: string) => z.string().optional().describe(`${label} URL (http/https)`);+/*** The same media url rule as the REST routes: both engines fetch only a string that starts with* http(s):// and decode anything else as base64, so any other value would go out as garbage bytes.+ * It is checked with the whole body, as the REST DTO does: base64 that holds data wins, so a url left+ * next to it (often an empty string from an agent filling every optional field) is neither fetched nor+ * an error.*/-const mediaUrl = (label: string) =>- z.string().refine(isMediaUrl, { error: MEDIA_URL_MESSAGE }).optional().describe(`${label} URL (http/https)`);+const hasValidMediaUrl = (input: { url?: string; base64?: string }): boolean =>+ !!stripBase64DataUri(input.base64) || input.url === undefined || isMediaUrl(input.url);/*** Mirrors the REST `mentions` field. The element rule and both caps come from the DTO rather than@@ -184,17 +190,19 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {tier: 'write',requiredRole: ApiKeyRole.OPERATOR,sessionScoped: true,- inputSchema: z.object({- sessionId,- chatId: chatIdSchema.describe('Chat JID'),- url: mediaUrl('Image'),- base64: z.string().optional().describe('Base64-encoded image data'),- mimetype: z.string().optional().describe('MIME type (required when using base64)'),- filename: z.string().max(255).optional(),- caption: z.string().max(1024).optional(),- quotedMessageId: quotedMessageIdSchema,- mentions: mentionsSchema,- }),+ inputSchema: z+ .object({+ sessionId,+ chatId: chatIdSchema.describe('Chat JID'),+ url: mediaUrl('Image'),+ base64: z.string().optional().describe('Base64-encoded image data'),+ mimetype: z.string().optional().describe('MIME type (required when using base64)'),+ filename: z.string().max(255).optional(),+ caption: z.string().max(1024).optional(),+ quotedMessageId: quotedMessageIdSchema,+ mentions: mentionsSchema,+ })+ .refine(hasValidMediaUrl, { error: MEDIA_URL_MESSAGE, path: ['url'] }),handler: input =>message.sendImage(input.sessionId, {chatId: input.chatId,@@ -214,17 +222,19 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {tier: 'write',requiredRole: ApiKeyRole.OPERATOR,sessionScoped: true,- inputSchema: z.object({- sessionId,- chatId: chatIdSchema.describe('Chat JID'),- url: mediaUrl('Video'),- base64: z.string().optional().describe('Base64-encoded video data'),- mimetype: z.string().optional().describe('MIME type (required when using base64)'),- filename: z.string().max(255).optional(),- caption: z.string().max(1024).optional(),- quotedMessageId: quotedMessageIdSchema,- mentions: mentionsSchema,- }),+ inputSchema: z+ .object({+ sessionId,+ chatId: chatIdSchema.describe('Chat JID'),+ url: mediaUrl('Video'),+ base64: z.string().optional().describe('Base64-encoded video data'),+ mimetype: z.string().optional().describe('MIME type (required when using base64)'),+ filename: z.string().max(255).optional(),+ caption: z.string().max(1024).optional(),+ quotedMessageId: quotedMessageIdSchema,+ mentions: mentionsSchema,+ })+ .refine(hasValidMediaUrl, { error: MEDIA_URL_MESSAGE, path: ['url'] }),handler: input =>message.sendVideo(input.sessionId, {chatId: input.chatId,@@ -244,18 +254,20 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {tier: 'write',requiredRole: ApiKeyRole.OPERATOR,sessionScoped: true,- inputSchema: z.object({- sessionId,- chatId: chatIdSchema.describe('Chat JID'),- url: mediaUrl('Audio'),- base64: z.string().optional().describe('Base64-encoded audio data'),- mimetype: z.string().optional().describe('MIME type (required when using base64)'),- filename: z.string().max(255).optional(),- caption: z.string().max(1024).optional(),- ptt: z.boolean().optional().describe('Send as a WhatsApp voice note (PTT)'),- quotedMessageId: quotedMessageIdSchema,- mentions: mentionsSchema,- }),+ inputSchema: z+ .object({+ sessionId,+ chatId: chatIdSchema.describe('Chat JID'),+ url: mediaUrl('Audio'),+ base64: z.string().optional().describe('Base64-encoded audio data'),+ mimetype: z.string().optional().describe('MIME type (required when using base64)'),+ filename: z.string().max(255).optional(),+ caption: z.string().max(1024).optional(),+ ptt: z.boolean().optional().describe('Send as a WhatsApp voice note (PTT)'),+ quotedMessageId: quotedMessageIdSchema,+ mentions: mentionsSchema,+ })+ .refine(hasValidMediaUrl, { error: MEDIA_URL_MESSAGE, path: ['url'] }),handler: input =>message.sendAudio(input.sessionId, {chatId: input.chatId,@@ -276,17 +288,19 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {tier: 'write',requiredRole: ApiKeyRole.OPERATOR,sessionScoped: true,- inputSchema: z.object({- sessionId,- chatId: chatIdSchema.describe('Chat JID'),- url: mediaUrl('Document'),- base64: z.string().optional().describe('Base64-encoded document data'),- mimetype: z.string().optional().describe('MIME type (required when using base64)'),- filename: z.string().max(255).optional(),- caption: z.string().max(1024).optional(),- quotedMessageId: quotedMessageIdSchema,- mentions: mentionsSchema,- }),+ inputSchema: z+ .object({+ sessionId,+ chatId: chatIdSchema.describe('Chat JID'),+ url: mediaUrl('Document'),+ base64: z.string().optional().describe('Base64-encoded document data'),+ mimetype: z.string().optional().describe('MIME type (required when using base64)'),+ filename: z.string().max(255).optional(),+ caption: z.string().max(1024).optional(),+ quotedMessageId: quotedMessageIdSchema,+ mentions: mentionsSchema,+ })+ .refine(hasValidMediaUrl, { error: MEDIA_URL_MESSAGE, path: ['url'] }),handler: input =>message.sendDocument(input.sessionId, {chatId: input.chatId,@@ -358,17 +372,19 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {tier: 'write',requiredRole: ApiKeyRole.OPERATOR,sessionScoped: true,- inputSchema: z.object({- sessionId,- chatId: chatIdSchema.describe('Chat JID'),- url: mediaUrl('Sticker'),- base64: z.string().optional().describe('Base64-encoded sticker data'),- mimetype: z.string().optional().describe('MIME type (required when using base64)'),- filename: z.string().max(255).optional(),- caption: z.string().max(1024).optional(),- quotedMessageId: quotedMessageIdSchema,- mentions: mentionsSchema,- }),+ inputSchema: z+ .object({+ sessionId,+ chatId: chatIdSchema.describe('Chat JID'),+ url: mediaUrl('Sticker'),+ base64: z.string().optional().describe('Base64-encoded sticker data'),+ mimetype: z.string().optional().describe('MIME type (required when using base64)'),+ filename: z.string().max(255).optional(),+ caption: z.string().max(1024).optional(),+ quotedMessageId: quotedMessageIdSchema,+ mentions: mentionsSchema,+ })+ .refine(hasValidMediaUrl, { error: MEDIA_URL_MESSAGE, path: ['url'] }),handler: input =>message.sendSticker(input.sessionId, {chatId: input.chatId,