Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(mcp): apply the message-id format rule on the agent tool
Yudhi Armyndharis3d301db9d77b
diff --git a/src/core/agent-tools/tools/session.tools.spec.ts b/src/core/agent-tools/tools/session.tools.spec.tsindex 84cbf657b..076881410 100644--- a/src/core/agent-tools/tools/session.tools.spec.ts+++ b/src/core/agent-tools/tools/session.tools.spec.ts@@ -1,3 +1,4 @@+import { BadRequestException } from '@nestjs/common';import { invokeTool } from '../tool-invoker';import { sessionTools } from './session.tools';import type { AnyToolDescriptor } from '../tool-descriptor';@@ -104,6 +105,27 @@ describe('sessionTools', () => {expect(sendSeen).toHaveBeenCalledWith('s1', '[email protected]', ['M1', 'M2']);});+ // The REST body rejects a whitespace-only id, and this path never sees that DTO: invokeTool parses+ // the tool's own schema and calls the service directly, so an id the schema accepts is sent as a+ // receipt key. Asserting the service was not reached is the point; a 400 that still delegated+ // would leave the defect in place.+ it('SessionMarkChatRead refuses a whitespace-only message id instead of sending it as a key', async () => {+ const sendSeen = jest.fn().mockResolvedValue(true);+ const tool = makeTools({ sendSeen } as unknown as SessionService).get('SessionMarkChatRead')!;++ const failure = await run(tool, { sessionId: 's1', chatId: '[email protected]', messageIds: [' '] }).catch(+ (e: unknown) => e,+ );+ expect(failure).toBeInstanceOf(BadRequestException);+ expect(JSON.stringify((failure as BadRequestException).getResponse())).toContain('no whitespace');+ expect(sendSeen).not.toHaveBeenCalled();++ // Control: a well-formed id still reaches the service, so the refusal above is this rule and not+ // a schema that turns everything away.+ await run(tool, { sessionId: 's1', chatId: '[email protected]', messageIds: ['3EB0C767D26B8A3F1A2B'] });+ expect(sendSeen).toHaveBeenCalledWith('s1', '[email protected]', ['3EB0C767D26B8A3F1A2B']);+ });+it('SessionMarkChatUnread maps the markUnread result to a success field', async () => {const markUnread = jest.fn().mockResolvedValue(true);const out = await run(makeTools({ markUnread } as unknown as SessionService).get('SessionMarkChatUnread')!, {diff --git a/src/core/agent-tools/tools/session.tools.ts b/src/core/agent-tools/tools/session.tools.tsindex 916d203e8..0fb75dd3a 100644--- a/src/core/agent-tools/tools/session.tools.ts+++ b/src/core/agent-tools/tools/session.tools.ts@@ -2,7 +2,11 @@ import { z } from 'zod';import { ApiKeyRole } from '../../../modules/auth/entities/api-key.entity';import type { SessionService } from '../../../modules/session/session.service';import { SessionResponseDto } from '../../../modules/session/dto/session-response.dto';-import { MARK_READ_MESSAGE_IDS_MAX } from '../../../modules/session/dto/mark-chat-read.dto';+import {+ MARK_READ_MESSAGE_IDS_MAX,+ MARK_READ_MESSAGE_ID_MESSAGE,+ MARK_READ_MESSAGE_ID_PATTERN,+} from '../../../modules/session/dto/mark-chat-read.dto';import { defineTool, type AnyToolDescriptor } from '../tool-descriptor';const sessionId = z.string().min(1).describe('Session UUID (the session id, not the name)');@@ -91,7 +95,9 @@ export function sessionTools(session: SessionService): AnyToolDescriptor[] {sessionId,chatId: z.string().describe('Chat JID (e.g. [email protected])'),messageIds: z- .array(z.string())+ // The element rule comes from the DTO rather than being restated here: the REST body+ // rejects a whitespace-only id, and this path reaches the engine without the DTO at all.+ .array(z.string().regex(MARK_READ_MESSAGE_ID_PATTERN, MARK_READ_MESSAGE_ID_MESSAGE)).nonempty().max(MARK_READ_MESSAGE_IDS_MAX).optional()diff --git a/src/modules/session/dto/mark-chat-read.dto.ts b/src/modules/session/dto/mark-chat-read.dto.tsindex d47648721..e993de6fa 100644--- a/src/modules/session/dto/mark-chat-read.dto.ts+++ b/src/modules/session/dto/mark-chat-read.dto.ts@@ -8,6 +8,16 @@ import { ArrayMaxSize, ArrayNotEmpty, IsArray, IsNotEmpty, IsOptional, IsString,*/export const MARK_READ_MESSAGE_IDS_MAX = 100;+/**+ * The shape of one message id: a single non-whitespace token. Exported for the same reason as the+ * cap above, so the agent tool holds the rule rather than restating it. Restating it is how the two+ * surfaces drifted: the tool copied the count and left `[' ']` reaching the engine as a receipt key.+ */+export const MARK_READ_MESSAGE_ID_PATTERN = /^\S{1,128}$/;++/** Rejection message for {@link MARK_READ_MESSAGE_ID_PATTERN}, shared so both surfaces answer alike. */+export const MARK_READ_MESSAGE_ID_MESSAGE = 'each messageIds entry must be a non-empty id with no whitespace';+export class MarkChatReadDto {@ApiProperty({description: "Chat ID in the active engine's native format (e.g. [email protected] on whatsapp-web.js)",@@ -46,8 +56,6 @@ export class MarkChatReadDto {@ArrayMaxSize(MARK_READ_MESSAGE_IDS_MAX)@IsString({ each: true })@IsNotEmpty({ each: true })- // Engine-neutral structural check: a message id is one non-whitespace token. @IsNotEmpty alone- // accepts ' ', which would reach sendNode as a receipt key.- @Matches(/^\S{1,128}$/, { each: true, message: 'each messageIds entry must be a non-empty id with no whitespace' })+ @Matches(MARK_READ_MESSAGE_ID_PATTERN, { each: true, message: MARK_READ_MESSAGE_ID_MESSAGE })messageIds?: string[];}