Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(mcp): accept customLinkPreview on the send-text tool
Yudhi Armyndharis8d4001577acd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e7bf96302..361f951a1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Changed-- The agent tools accept `mentions` on the text, media, template and reply sends, matching the REST routes. A tool schema is not strict, so an agent that passed the field before had it dropped without an error and the message went out untagged. Not offered on the sticker tool, whose adapters build no tag list.+- The agent tools accept `mentions` on the text, media, template and reply sends, and `customLinkPreview` on the text send, matching the REST routes. A tool schema is not strict, so an agent that passed either field before had it dropped without an error. Not offered on the sticker tool, whose adapters build no tag list.- `mentions` reaches every route whose engine can carry it: `reply`, `edit`, `send-template` and each `send-bulk` item, alongside the send routes that already had it. `send-template` also gained `linkPreview`. On `edit` the tags are re-applied rather than preserved, because an edit replaces the message content. Thanks @Magnarks for the report.- `POST /sessions/{sessionId}/chats/unread` publishes its own `MarkChatUnreadDto` rather than sharing `MarkChatReadDto`. The body is unchanged (`chatId` alone), but a generated client sees the schema under a new name.- ⚠️ **Breaking (Go, Java and typed Python callers).** `markRead` and `subscribePresence` each take their own request type rather than the shared `MarkChatRequest`, which now serves `markUnread` alone. Swap the type at both call sites; the wire body is unchanged and the JavaScript and PHP clients are unaffected. `SubscribePresenceDto` had no contract-gate coverage while one type stood for two routes.diff --git a/src/core/agent-tools/tools/message.tools.spec.ts b/src/core/agent-tools/tools/message.tools.spec.tsindex 03088480a..17e57598b 100644--- a/src/core/agent-tools/tools/message.tools.spec.ts+++ b/src/core/agent-tools/tools/message.tools.spec.ts@@ -311,6 +311,39 @@ describe('messageTools', () => {expect(sendText).not.toHaveBeenCalled();});+ it('forwards a caller-supplied link preview, and enforces the title WhatsApp requires', async () => {+ // REST declares customLinkPreview; the tool did not, so an agent's object was stripped before the+ // handler ran and the send went out with whatever preview the engine chose on its own.+ const sendText = jest.fn().mockResolvedValue({ id: 'm1' });+ const tools = makeTools({ sendText } as unknown as MessageService);++ await run(tools.get('MessageSendText')!, {+ sessionId: 's1',+ chatId: '[email protected]',+ text: 'see https://example.com/launch',+ customLinkPreview: { url: 'https://example.com/launch', title: 'We just launched' },+ });+ expect(sendText).toHaveBeenCalledWith(+ 's1',+ expect.objectContaining({+ customLinkPreview: { url: 'https://example.com/launch', title: 'We just launched' },+ }),+ );++ // Control: the required title is enforced here, not left to the DTO, because this path never+ // reaches the ValidationPipe.+ sendText.mockClear();+ await expect(+ run(tools.get('MessageSendText')!, {+ sessionId: 's1',+ chatId: '[email protected]',+ text: 'see https://example.com/launch',+ customLinkPreview: { url: 'https://example.com/launch' },+ }),+ ).rejects.toBeDefined();+ expect(sendText).not.toHaveBeenCalled();+ });+it('does not offer mentions on the sticker tool, whose adapters build no tag list', async () => {// Control for the additions above: the field is added where the layer behind it carries the value,// not everywhere. Both adapters build the sticker content without mentions, so declaring it herediff --git a/src/core/agent-tools/tools/message.tools.ts b/src/core/agent-tools/tools/message.tools.tsindex 024b218ec..5dc3a2156 100644--- a/src/core/agent-tools/tools/message.tools.ts+++ b/src/core/agent-tools/tools/message.tools.ts@@ -2,6 +2,9 @@ import { z } from 'zod';import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';import type { MessageService } from '../../../modules/message/message.service';import {+ CUSTOM_PREVIEW_DESCRIPTION_MAX_LENGTH,+ CUSTOM_PREVIEW_TITLE_MAX_LENGTH,+ CUSTOM_PREVIEW_URL_MAX_LENGTH,MENTIONS_MAX,MENTION_WID_MAX_LENGTH,MESSAGE_TEXT_MAX_LENGTH,@@ -47,6 +50,31 @@ const mentionsSchema = z'@<number> token for WhatsApp to render the tag.',);+/**+ * Mirrors the REST `customLinkPreview` field, whose caps come from the DTO for the same reason as+ * `mentionsSchema` above. Baileys only: whatsapp-web.js takes a boolean and answers 501, and the+ * field cannot be combined with `linkPreview: false`, which asks for the opposite.+ */+const customLinkPreviewSchema = z+ .object({+ url: z+ .string()+ .min(1)+ .max(CUSTOM_PREVIEW_URL_MAX_LENGTH)+ .describe('The URL as it appears in the message text; WhatsApp anchors the preview to it.'),+ title: z+ .string()+ .min(1)+ .max(CUSTOM_PREVIEW_TITLE_MAX_LENGTH)+ .describe('Required: WhatsApp renders no preview without a title.'),+ description: z.string().max(CUSTOM_PREVIEW_DESCRIPTION_MAX_LENGTH).optional(),+ })+ .optional()+ .describe(+ 'Attach a preview you supply instead of one fetched from the URL, so nothing is fetched and it ' ++ 'works for a URL the gateway cannot reach. Baileys only: whatsapp-web.js answers 501.',+ );+export function messageTools(message: MessageService): AnyToolDescriptor[] {return [defineTool({@@ -123,6 +151,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {),quotedMessageId: quotedMessageIdSchema,mentions: mentionsSchema,+ customLinkPreview: customLinkPreviewSchema,}),handler: input =>message.sendText(input.sessionId, {@@ -131,6 +160,7 @@ export function messageTools(message: MessageService): AnyToolDescriptor[] {...(input.linkPreview === undefined ? {} : { linkPreview: input.linkPreview }),quotedMessageId: input.quotedMessageId,mentions: input.mentions,+ customLinkPreview: input.customLinkPreview,}),}),defineTool({diff --git a/src/modules/message/dto/send-message.dto.ts b/src/modules/message/dto/send-message.dto.tsindex 72cd89350..e443a75fb 100644--- a/src/modules/message/dto/send-message.dto.ts+++ b/src/modules/message/dto/send-message.dto.ts@@ -29,6 +29,14 @@ export const MENTIONS_DESCRIPTION =export const MENTIONS_MAX = 1024;export const MENTION_WID_MAX_LENGTH = 64;+/**+ * Caps for a custom link preview, exported for the same reason as the mentions pair above: the+ * agent-tool schema restates nothing, and that path never reaches the ValidationPipe.+ */+export const CUSTOM_PREVIEW_URL_MAX_LENGTH = 2048;+export const CUSTOM_PREVIEW_TITLE_MAX_LENGTH = 256;+export const CUSTOM_PREVIEW_DESCRIPTION_MAX_LENGTH = 1024;+// Single source of truth for the text-body cap, shared with the agent-tool input schemas// (src/core/agent-tools/tools/message.tools.ts) so MCP and REST enforce the same limit.export const MESSAGE_TEXT_MAX_LENGTH = 4096;@@ -52,7 +60,7 @@ export class CustomLinkPreviewDto {})@IsString()@IsNotEmpty()- @MaxLength(2048)+ @MaxLength(CUSTOM_PREVIEW_URL_MAX_LENGTH)url!: string;@ApiProperty({@@ -62,13 +70,13 @@ export class CustomLinkPreviewDto {})@IsString()@IsNotEmpty()- @MaxLength(256)+ @MaxLength(CUSTOM_PREVIEW_TITLE_MAX_LENGTH)title!: string;@ApiPropertyOptional({ description: 'Preview description', example: 'Read the announcement.', maxLength: 1024 })@IsOptional()@IsString()- @MaxLength(1024)+ @MaxLength(CUSTOM_PREVIEW_DESCRIPTION_MAX_LENGTH)description?: string;}