From 5d4cfec3b6c97c5187854056d98b98cb1b4fc282 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:04:18 +0100 Subject: [PATCH] fix(mail-ingest): list the mail import tools only while the import is on Every mail tool answered 'Mail import is not available' on installs that never switched the import on, which is the default, so they are now gated like the addon tools. --- .../src/nest/mail-ingest/mail-ingest.mcp.ts | 12 +++++++++ .../integration/broadcast-parity/harness.ts | 2 ++ .../mail-ingest.controller.test.ts | 27 ++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/server/src/nest/mail-ingest/mail-ingest.mcp.ts b/server/src/nest/mail-ingest/mail-ingest.mcp.ts index 39382e058..f87eea37d 100644 --- a/server/src/nest/mail-ingest/mail-ingest.mcp.ts +++ b/server/src/nest/mail-ingest/mail-ingest.mcp.ts @@ -12,6 +12,7 @@ import { import { DomainError } from '../common/domain-error'; import { MailIngestActionsService } from './mail-ingest-actions.service'; import { MailIngestInboxService } from './mail-ingest-inbox.service'; +import { MailIngestSettingsService } from './mail-ingest-settings.service'; import { idSchema, mailIngestApplyRequestSchema, @@ -24,6 +25,9 @@ import { type MailIngestPrefsUpdateRequest, } from '@trek/shared'; +/** The tools are listed only while the admin has the import switched on, as every call needs it. */ +const mailImportOn = (_ctx: McpContext, self: object) => (self as MailIngestMcp).settings.isAvailable(); + const candidateId = idSchema.describe('The inbox entry, as list_mail_inbox names it (entries[].id).'); /** @@ -37,6 +41,7 @@ export class MailIngestMcp { constructor( private readonly inbox: MailIngestInboxService, private readonly actions: MailIngestActionsService, + readonly settings: MailIngestSettingsService, ) {} @Tool({ @@ -45,6 +50,7 @@ export class MailIngestMcp { 'List the bookings read from mails the user forwarded to TREK: by default those that wait for a decision (status "open"), with why each one waits (reason), the booking as read (item), the trip it suggests and the mail it came from (subject, received_at, attachments), newest first. status "resolved" lists the decided ones; an added booking names reservation_ids and, while it can still be undone or moved, undo_until. Page with before = next_before.', inputSchema: mailIngestInboxQuerySchema.shape, annotations: TOOL_ANNOTATIONS_READONLY, + when: mailImportOn, access: { group: 'reservations', mode: 'read' }, }) async listMailInbox(query: MailIngestInboxQuery, ctx: McpContext) { @@ -57,6 +63,7 @@ export class MailIngestMcp { 'Add a waiting booking from the mail inbox to a trip: to the trip it suggests, or with trip_id to another trip the user may add bookings to. The booking is written with what the mail said (times, places, reference, cost) and the mail is attached as its document; it can be undone for seven days.', inputSchema: { candidateId, ...mailIngestApplyRequestSchema.shape }, annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT, + when: mailImportOn, access: { group: 'reservations', mode: 'write' }, }) async addMailBooking({ candidateId: id, trip_id }: { candidateId: number; trip_id?: number }, ctx: McpContext) { @@ -69,6 +76,7 @@ export class MailIngestMcp { 'Move a booking that was added from a mail into another trip (trip_id), while it can still be undone; only the person who forwarded the mail can move it. The booking is written there anew and removed from the first trip. One whose cost was settled with, or that was edited since, is refused with needs_confirmation until confirm is true; the edit is lost.', inputSchema: { candidateId, ...mailIngestMoveRequestSchema.shape }, annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT, + when: mailImportOn, access: { group: 'reservations', mode: 'write' }, }) async moveMailBooking( @@ -84,6 +92,7 @@ export class MailIngestMcp { 'Put the mail of a waiting entry into a trip (trip_id) as documents, its attachments or else its text, without a booking. Needs the Documents addon.', inputSchema: { candidateId, ...mailIngestFileRequestSchema.shape }, annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT, + when: mailImportOn, access: { group: 'reservations', mode: 'write' }, }) async fileMailBooking({ candidateId: id, trip_id }: { candidateId: number; trip_id: number }, ctx: McpContext) { @@ -95,6 +104,7 @@ export class MailIngestMcp { description: 'Set a waiting entry of the mail inbox aside without adding anything.', inputSchema: { candidateId }, annotations: TOOL_ANNOTATIONS_WRITE, + when: mailImportOn, access: { group: 'reservations', mode: 'write' }, }) async discardMailBooking({ candidateId: id }: { candidateId: number }, ctx: McpContext) { @@ -107,6 +117,7 @@ export class MailIngestMcp { 'Undo a booking that was added from a mail, within seven days: the booking, its stay, its cost and the documents it brought go. One whose cost was settled with, or that was edited since, is refused with needs_confirmation until confirm is true.', inputSchema: { candidateId, ...mailIngestUndoRequestSchema.shape }, annotations: TOOL_ANNOTATIONS_DELETE, + when: mailImportOn, access: { group: 'reservations', mode: 'write' }, }) async undoMailBooking({ candidateId: id, confirm }: { candidateId: number; confirm?: boolean }, ctx: McpContext) { @@ -120,6 +131,7 @@ export class MailIngestMcp { // The shape, registered strict like every other tool's; its "at least one" check runs below. inputSchema: mailIngestPrefsUpdateRequestSchema.shape, annotations: TOOL_ANNOTATIONS_WRITE, + when: mailImportOn, access: { group: 'reservations', mode: 'write' }, }) async updateMailImportSettings(update: MailIngestPrefsUpdateRequest, ctx: McpContext) { diff --git a/server/tests/integration/broadcast-parity/harness.ts b/server/tests/integration/broadcast-parity/harness.ts index 6eae6d9d3..41daed0c4 100644 --- a/server/tests/integration/broadcast-parity/harness.ts +++ b/server/tests/integration/broadcast-parity/harness.ts @@ -233,6 +233,8 @@ export async function openHarness(): Promise { ); // The wanderer tools show only while wanderer is allowed under Tours. await setAppSetting(orm, 'tours_wanderer_enabled', 'true'); + // The mail import tools show only while the import is on. + await setAppSetting(orm, 'mail_ingest_enabled', 'true'); // The photo search tools show only while a provider is on. await updateRows(orm, PhotoProviders, { id: { $in: ['immich', 'synologyphotos'] } }, { enabled: 1 }); diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest.controller.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest.controller.test.ts index 8f845abb2..49f66e9f9 100644 --- a/server/tests/unit/nest/mail-ingest/mail-ingest.controller.test.ts +++ b/server/tests/unit/nest/mail-ingest/mail-ingest.controller.test.ts @@ -7,12 +7,14 @@ */ import type { MailIngestCandidateRow } from '../../../../src/db/repositories/MailIngestCandidates.repository'; import type { McpContext } from '../../../../src/nest-mcp'; +import { getEntry, type ClassRef } from '../../../../src/nest-mcp/metadata'; import { DomainError } from '../../../../src/nest/common/domain-error'; import type { RateLimitService } from '../../../../src/nest/common/rate-limit.service'; import type { MailIngestActionsService } from '../../../../src/nest/mail-ingest/mail-ingest-actions.service'; import { MailIngestAdminController } from '../../../../src/nest/mail-ingest/mail-ingest-admin.controller'; import { MailIngestInboundController } from '../../../../src/nest/mail-ingest/mail-ingest-inbound.controller'; import type { MailIngestInboxService } from '../../../../src/nest/mail-ingest/mail-ingest-inbox.service'; +import type { MailIngestSettingsService } from '../../../../src/nest/mail-ingest/mail-ingest-settings.service'; import type { MailIngestStatusService } from '../../../../src/nest/mail-ingest/mail-ingest-status.service'; import { MailIngestTripController } from '../../../../src/nest/mail-ingest/mail-ingest-trip.controller'; import type { MailIngestWebhookService } from '../../../../src/nest/mail-ingest/mail-ingest-webhook.service'; @@ -55,8 +57,13 @@ const actions = { const rest = () => new MailIngestController(inbox as unknown as MailIngestInboxService, actions as unknown as MailIngestActionsService); +const settings = { isAvailable: vi.fn(async () => true) }; const mcp = () => - new MailIngestMcp(inbox as unknown as MailIngestInboxService, actions as unknown as MailIngestActionsService); + new MailIngestMcp( + inbox as unknown as MailIngestInboxService, + actions as unknown as MailIngestActionsService, + settings as unknown as MailIngestSettingsService, + ); /** The JSON a tool answered with. */ function read(result: { content: Array<{ type: string; text: string }> }): unknown { @@ -115,6 +122,24 @@ describe('the settings card and the inbox', () => { }); }); +describe('the mail tools', () => { + const tools = Object.getOwnPropertyNames(MailIngestMcp.prototype).filter( + (name) => getEntry(MailIngestMcp as unknown as ClassRef, name)?.kind === 'tool', + ); + + it('are listed only while the import is on, since every call needs it', async () => { + expect(tools).toHaveLength(7); + for (const name of tools) { + const entry = getEntry(MailIngestMcp as unknown as ClassRef, name); + const when = entry?.kind === 'tool' ? entry.options.when : undefined; + expect(when, name).toBeTypeOf('function'); + settings.isAvailable.mockResolvedValueOnce(false); + expect(await when?.(ctx, mcp()), name).toBe(false); + expect(await when?.(ctx, mcp()), name).toBe(true); + } + }); +}); + describe('acting on an entry, over REST and MCP alike', () => { it('apply and add_mail_booking: the suggested trip without trip_id, the picked one with it', async () => { expect(await rest().apply(user, '3', {})).toEqual({ candidate: view });