Files
TREK/server/src/nest/packing/packing.mcp.ts
T
Maurice 3f38621024 feat(server): let a trip owner switch trip addons for one trip
A trip can now turn a trip addon (lists, costs, documents, collab, road
trip, tours) on or off for itself, whatever the instance switch says. No
row in the new trip_addon_overrides table means the trip follows the
instance; a row is the owner's choice. Going back to the instance setting
deletes the row, and no addon data is touched either way.

- GET/PUT /api/trips/:tripId/addons[/:addonId] (every member reads, the
  owner writes) and the MCP tools get_trip_addons and set_trip_addon, on
  the shared tripAddons contract; tripAddons:changed tells the members.
- Every trip-scoped addon surface asks the trip: REST through
  TripAddonGuard, MCP through a per-call gate on the named or pinned trip,
  plugin RPC and events, the share link, the offline bundle, reminders,
  doc sync, the assistant (tools, keep_attached_file), booking costs, the
  trip summary prompt, and the cross-trip tour reads (library, Atlas
  stats, collection save).
- Routes without a trip open while the addon is on in any of the user's
  trips. Switching an addon on that the instance has off ends the members'
  MCP sessions so they reconnect with its tools.
- A copied trip takes its owner's choices along.

Behaviour changes: an anonymous call to an addon route that is off now
gets 401 instead of 404, and with an instance switch off the packing,
todo and files plugin RPC methods, the files MCP tools, todo reminder
mails, core plugin events, plugin view contributions, the share link's
lists, costs and chat sections and the offline bundle's lists follow it.
2026-11-21 13:23:09 +01:00

775 lines
29 KiB
TypeScript

import { ADDON_IDS } from '../../addons';
import { noAccess, permissionDenied, adminRequired } from '../../mcp/tools/_shared';
import {
McpController,
Tool,
ResourceTemplate,
type McpContext,
TOOL_ANNOTATIONS_READONLY,
TOOL_ANNOTATIONS_WRITE,
TOOL_ANNOTATIONS_DELETE,
TOOL_ANNOTATIONS_NON_IDEMPOTENT,
errorResult,
ok,
} from '../../nest-mcp';
import { addonGate, addonOffResource } from '../addons/addon-gate';
import { AddonsService } from '../addons/addons.service';
import { uriRowId } from '../common/row-id';
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
import { PackingWritesService } from './packing-writes.service';
import { PackingService } from './packing.service';
import {
packingApplyTemplateRequestSchema,
packingCreateBagRequestSchema,
packingCreateItemRequestSchema,
packingListItemsRequestSchema,
packingSetSharingRequestSchema,
packingUpdateBagRequestSchema,
packingUpdateItemRequestSchema,
type PackingListItemsRequest,
type PackingVisibility,
idSchema,
} from '@trek/shared';
import { z } from 'zod';
/** What `list_packing_items` filters a packing item by. */
type PackingListRow = { category?: string | null; bag_id?: number | null; checked?: number | boolean | null };
/** Legacy registrar gate: the whole packing surface rides the packing addon. */
const packingAddonOn = addonGate(ADDON_IDS.PACKING);
/**
* Packing MCP surface — ported from the legacy registrars: the seventeen
* tools from src/mcp/tools/packing.ts and the trek://trips/{tripId}/packing +
* trek://trips/{tripId}/packing/bags resources from src/mcp/resources.ts
* (identical names, descriptions, schemas, annotations and error/payload
* shapes; the one deviation is the packing:bag-deleted payload, aligned with
* the REST route). The registration-time gates map to `when` (the
* whole-registrar packing-addon early return) plus the declarative packing
* read/write access markers (the legacy `if (R)` / `if (W)` checks, resolved
* by trekMcpAccessPolicy). The two template-management tools keep their inline
* admin gate (isAdminUser), matching the REST routes.
*
* set_packing_item_sharing and list_packing_items have no legacy ancestor.
* PUT /:id/sharing had no counterpart here, so the whole three-tier sharing
* model (#858) was invisible to an assistant; and the items themselves could
* only be read inside get_trip_summary, unfiltered and without their bags.
*/
@McpController()
export class PackingMcp {
constructor(
private readonly packing: PackingService,
readonly addons: AddonsService,
private readonly guards: McpToolGuardsService,
private readonly writes: PackingWritesService,
) {}
// --- PACKING ---
@Tool({
name: 'create_packing_item',
description:
'Add an item to the packing checklist for a trip. It lands on the common list everyone shares unless visibility says otherwise; use set_packing_item_sharing to move an existing item between those tiers.',
inputSchema: {
tripId: idSchema,
name: packingCreateItemRequestSchema.shape.name.max(200),
category: z.string().max(100).optional().describe('Packing category (e.g. Clothes, Electronics)'),
bag_id: packingCreateItemRequestSchema.shape.bag_id.describe(
'Bag to pack the item into (ids come from list_packing_bags)',
),
quantity: packingCreateItemRequestSchema.shape.quantity.describe('How many to pack, clamped to 1-999'),
weight_grams: packingCreateItemRequestSchema.shape.weight_grams.describe(
'Weight in grams, which feeds the bag fill bar',
),
checked: packingCreateItemRequestSchema.shape.checked.describe('Create the item already ticked off'),
is_private: packingCreateItemRequestSchema.shape.is_private.describe(
'Keep the item to yourself; visibility says the same thing with more nuance',
),
visibility: packingCreateItemRequestSchema.shape.visibility.describe(
"Which list the item belongs to: 'common' (the group pool, the default), 'personal' (yours alone), or 'shared' (yours plus recipient_ids)",
),
recipient_ids: packingCreateItemRequestSchema.shape.recipient_ids.describe(
"For visibility 'shared': the trip members the item is brought for. Ignored otherwise, and ids outside the trip roster are dropped",
),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async createPackingItem(
{
tripId,
name,
category,
bag_id,
quantity,
weight_grams,
checked,
is_private,
visibility,
recipient_ids,
}: {
tripId: number;
name: string;
category?: string;
bag_id?: number | null;
quantity?: number;
weight_grams?: number | null;
checked?: boolean | number;
is_private?: boolean;
visibility?: PackingVisibility;
recipient_ids?: number[];
},
ctx: McpContext,
) {
const item = await this.writes.createItem(
tripId,
{
name,
category: category || 'General',
bag_id,
quantity,
weight_grams,
// checked takes a boolean or the legacy 0/1, exactly as the REST body does.
checked: checked === undefined ? undefined : !!checked,
is_private,
visibility,
recipient_ids,
},
await this.guards.tripWriter(tripId, ctx.userId),
);
return ok({ item });
}
@Tool({
name: 'toggle_packing_item',
description: 'Check or uncheck a packing item.',
inputSchema: {
tripId: idSchema,
itemId: idSchema,
checked: z.boolean(),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async togglePackingItem(
{ tripId, itemId, checked }: { tripId: number; itemId: number; checked: boolean },
ctx: McpContext,
) {
// The update use case: a toggle never changes the privacy, so this is its
// plain update to whoever may see the item (#858, #1976).
const item = await this.writes.updateItem(
tripId,
itemId,
{ checked: checked ? 1 : 0 },
['checked'],
await this.guards.tripWriter(tripId, ctx.userId),
);
return ok({ item });
}
@Tool({
name: 'delete_packing_item',
description: 'Remove an item from the packing checklist.',
inputSchema: {
tripId: idSchema,
itemId: idSchema,
},
annotations: TOOL_ANNOTATIONS_DELETE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async deletePackingItem({ tripId, itemId }: { tripId: number; itemId: number }, ctx: McpContext) {
await this.writes.deleteItem(tripId, itemId, await this.guards.tripWriter(tripId, ctx.userId));
return ok({ success: true });
}
// --- PACKING (update) ---
@Tool({
name: 'update_packing_item',
description:
'Change a packing item: rename it, recategorise it, move it into a bag, set how many are needed, count how many are already packed, record its weight, or flip it between the common list and your own. Ticking it off is toggle_packing_item; choosing who a private item is shared with is set_packing_item_sharing.',
inputSchema: {
tripId: idSchema,
itemId: idSchema,
name: packingCreateItemRequestSchema.shape.name.max(200).optional(),
category: z.string().max(100).optional(),
bag_id: packingUpdateItemRequestSchema.shape.bag_id.describe(
'Bag to pack the item into (ids come from list_packing_bags); null takes it out of its bag',
),
quantity: packingUpdateItemRequestSchema.shape.quantity.describe('How many to pack, clamped to 1-999'),
packed_quantity: packingUpdateItemRequestSchema.shape.packed_quantity.describe(
'How many of the quantity are already packed; reaching the quantity ticks the item off, 0 or null clears the count',
),
weight_grams: packingUpdateItemRequestSchema.shape.weight_grams.describe(
'Weight in grams, which feeds the bag fill bar; null clears it',
),
is_private: packingUpdateItemRequestSchema.shape.is_private.describe(
"true takes the item off the common list and onto the caller's own",
),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async updatePackingItem(
{
tripId,
itemId,
name,
category,
bag_id,
quantity,
packed_quantity,
weight_grams,
is_private,
}: {
tripId: number;
itemId: number;
name?: string;
category?: string;
bag_id?: number | null;
quantity?: number;
packed_quantity?: number | null;
weight_grams?: number | null;
is_private?: boolean;
},
ctx: McpContext,
) {
const fields = { name, category, bag_id, quantity, packed_quantity, weight_grams, is_private };
// The service reads presence from bodyKeys, so a field has to be named there
// for an explicit null to clear it rather than read as "leave it alone".
const bodyKeys = Object.keys(fields).filter((k) => fields[k as keyof typeof fields] !== undefined);
const item = await this.writes.updateItem(
tripId,
itemId,
fields,
bodyKeys,
await this.guards.tripWriter(tripId, ctx.userId),
);
return ok({ item });
}
@Tool({
name: 'set_packing_item_sharing',
description:
"Move an existing packing item between the three sharing tiers: the common list the whole trip pools into, the owner's own list, or shared with named trip members. Only the item's owner may change this. Everything else about an item is update_packing_item.",
inputSchema: {
tripId: idSchema,
itemId: idSchema,
visibility: packingSetSharingRequestSchema.shape.visibility.describe(
"'common' puts the item in the group pool, 'personal' keeps it to the owner, 'shared' covers the people in recipient_ids",
),
recipient_ids: packingSetSharingRequestSchema.shape.recipient_ids.describe(
"For 'shared': the trip members the item is brought for. Ids outside the trip roster are dropped, and any previous recipients are replaced",
),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async setPackingItemSharing(
{
tripId,
itemId,
visibility,
recipient_ids,
}: { tripId: number; itemId: number; visibility: PackingVisibility; recipient_ids?: number[] },
ctx: McpContext,
) {
// The use case sends what the REST route sends: no bag-totals ping, since
// who sees an item moves no weight.
const item = await this.writes.setSharing(
tripId,
itemId,
visibility,
recipient_ids ?? [],
await this.guards.tripWriter(tripId, ctx.userId),
);
return ok({ item });
}
// --- PACKING ADVANCED ---
@Tool({
name: 'reorder_packing_items',
description: 'Set the display order of packing items within a trip.',
inputSchema: {
tripId: idSchema,
orderedIds: z.array(idSchema).describe('Packing item IDs in desired order'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async reorderPackingItems({ tripId, orderedIds }: { tripId: number; orderedIds: number[] }, ctx: McpContext) {
await this.writes.reorder(tripId, orderedIds, await this.guards.tripWriter(tripId, ctx.userId));
return ok({ success: true });
}
@Tool({
name: 'list_packing_items',
description:
"List the packing items of a trip the user may see (another member's personal items stay hidden), with the trip's bags and who looks after each category. Narrow it to one category, one bag or what is still open; group_by says how the list is meant to be shown, by category or by bag, as the packing list groups it.",
inputSchema: {
tripId: idSchema,
category: packingListItemsRequestSchema.shape.category.describe(
'Only the items of this category, named exactly as the items carry it',
),
bag_id: packingListItemsRequestSchema.shape.bag_id.describe(
'Only the items in this bag (ids come from list_packing_bags); null for the items in no bag',
),
status: packingListItemsRequestSchema.shape.status.describe(
"'open' for what is still to pack, 'packed' for what is ticked off, 'all' (the default) for both",
),
group_by: packingListItemsRequestSchema.shape.group_by.describe(
"How the list is shown: by 'category' (the default) or by 'bag'. It filters nothing",
),
},
annotations: TOOL_ANNOTATIONS_READONLY,
when: packingAddonOn,
access: { group: 'packing', mode: 'read' },
})
async listPackingItems(
{ tripId, category, bag_id, status }: { tripId: number } & PackingListItemsRequest,
ctx: McpContext,
) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
// The viewer's own list (#858): another member's personal items never come back.
const visible = (await this.packing.listItems(tripId, ctx.userId)) as PackingListRow[];
const items = visible.filter(
(item) =>
(category === undefined || item.category === category) &&
(bag_id === undefined || (item.bag_id ?? null) === bag_id) &&
(status === 'open' ? !item.checked : status === 'packed' ? !!item.checked : true),
);
const bags = await this.packing.listBags(tripId);
const category_assignees = await this.packing.getCategoryAssignees(tripId);
return ok({ items, bags, category_assignees });
}
@Tool({
name: 'list_packing_bags',
description: 'List all packing bags for a trip.',
inputSchema: {
tripId: idSchema,
},
annotations: TOOL_ANNOTATIONS_READONLY,
when: packingAddonOn,
access: { group: 'packing', mode: 'read' },
})
async listPackingBags({ tripId }: { tripId: number }, ctx: McpContext) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
const bags = await this.packing.listBags(tripId);
return ok({ bags });
}
@Tool({
name: 'create_packing_bag',
description: 'Create a new packing bag (e.g. "Carry-on", "Checked bag").',
inputSchema: {
tripId: idSchema,
name: packingCreateBagRequestSchema.shape.name.max(100),
color: packingCreateBagRequestSchema.shape.color,
weight_limit_grams: packingCreateBagRequestSchema.shape.weight_limit_grams.describe(
'Allowance in grams the bag is measured against (the fill bar)',
),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async createPackingBag(
{
tripId,
name,
color,
weight_limit_grams,
}: { tripId: number; name: string; color?: string; weight_limit_grams?: number | null },
ctx: McpContext,
) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
if (!(await this.guards.hasTripPermission('packing_edit', tripId, ctx.userId))) return permissionDenied();
// createBag returns a bare row; hydrate with the empty members array that
// listBags and the schema always carry, so the client/AI consumer matches.
const bag = {
...((await this.packing.createBag(tripId, { name, color, weight_limit_grams })) as object),
members: [],
};
this.guards.safeBroadcast(tripId, 'packing:bag-created', { bag });
return ok({ bag });
}
@Tool({
name: 'update_packing_bag',
description:
'Rename or recolor a packing bag, give it a weight limit, or hand it to one traveller. Who else packs into it is set_bag_members.',
inputSchema: {
tripId: idSchema,
bagId: idSchema,
name: packingUpdateBagRequestSchema.shape.name,
color: packingUpdateBagRequestSchema.shape.color,
weight_limit_grams: packingUpdateBagRequestSchema.shape.weight_limit_grams.describe(
'Allowance in grams the bag is measured against (the fill bar); null lifts the limit',
),
user_id: packingUpdateBagRequestSchema.shape.user_id.describe(
'Trip member the bag belongs to; null leaves it unassigned, and an id outside the trip roster unassigns it too',
),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async updatePackingBag(
{
tripId,
bagId,
name,
color,
weight_limit_grams,
user_id,
}: {
tripId: number;
bagId: number;
name?: string;
color?: string;
weight_limit_grams?: number | null;
user_id?: number | null;
},
ctx: McpContext,
) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
if (!(await this.guards.hasTripPermission('packing_edit', tripId, ctx.userId))) return permissionDenied();
const fields: { name?: string; color?: string; weight_limit_grams?: number | null; user_id?: number | null } = {};
const bodyKeys: string[] = [];
if (name !== undefined) {
fields.name = name;
bodyKeys.push('name');
}
if (color !== undefined) {
fields.color = color;
bodyKeys.push('color');
}
// Both follow the presence protocol: an omitted key leaves the value alone,
// an explicit null clears it.
if (weight_limit_grams !== undefined) {
fields.weight_limit_grams = weight_limit_grams;
bodyKeys.push('weight_limit_grams');
}
if (user_id !== undefined) {
fields.user_id = user_id;
bodyKeys.push('user_id');
}
const updated = await this.packing.updateBag(tripId, bagId, fields, bodyKeys);
if (!updated) return errorResult('Bag not found.');
// Hydrate with the members array (matches create_packing_bag, listBags, and the schema).
const bag = (await this.packing.listBags(tripId)).find((b) => b.id === (updated as { id: number }).id) ?? {
...(updated as object),
members: [],
};
this.guards.safeBroadcast(tripId, 'packing:bag-updated', { bag });
return ok({ bag });
}
@Tool({
name: 'delete_packing_bag',
description: 'Delete a packing bag (items in the bag are unassigned, not deleted).',
inputSchema: {
tripId: idSchema,
bagId: idSchema,
},
annotations: TOOL_ANNOTATIONS_DELETE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async deletePackingBag({ tripId, bagId }: { tripId: number; bagId: number }, ctx: McpContext) {
// A bag that is not on the trip answers 'Bag not found.' and is announced to
// nobody, as on REST; it used to report success and send both events.
await this.writes.deleteBag(tripId, bagId, await this.guards.tripWriter(tripId, ctx.userId));
return ok({ success: true });
}
@Tool({
name: 'set_bag_members',
description: 'Assign trip members to a packing bag (determines who packs what bag).',
inputSchema: {
tripId: idSchema,
bagId: idSchema,
userIds: z.array(idSchema),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async setBagMembers(
{ tripId, bagId, userIds }: { tripId: number; bagId: number; userIds: number[] },
ctx: McpContext,
) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
if (!(await this.guards.hasTripPermission('packing_edit', tripId, ctx.userId))) return permissionDenied();
const members = await this.packing.setBagMembers(tripId, bagId, userIds);
if (!members) return errorResult('Bag not found.');
this.guards.safeBroadcast(tripId, 'packing:bag-members-updated', { bagId, members });
return ok({ members });
}
@Tool({
name: 'get_packing_category_assignees',
description: 'Get which trip members are assigned to each packing category.',
inputSchema: {
tripId: idSchema,
},
annotations: TOOL_ANNOTATIONS_READONLY,
when: packingAddonOn,
access: { group: 'packing', mode: 'read' },
})
async getPackingCategoryAssignees({ tripId }: { tripId: number }, ctx: McpContext) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
const assignees = await this.packing.getCategoryAssignees(tripId);
return ok({ assignees });
}
@Tool({
name: 'set_packing_category_assignees',
description: 'Assign trip members to a packing category.',
inputSchema: {
tripId: idSchema,
categoryName: z.string().min(1).max(100),
userIds: z.array(idSchema),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async setPackingCategoryAssignees(
{ tripId, categoryName, userIds }: { tripId: number; categoryName: string; userIds: number[] },
ctx: McpContext,
) {
// The tagged travellers get the same notification the REST route sends them.
const assignees = await this.writes.setCategoryAssignees(
tripId,
categoryName,
userIds,
await this.guards.tripWriter(tripId, ctx.userId),
);
return ok({ assignees });
}
@Tool({
name: 'apply_packing_template',
description:
"Apply a packing template to a trip (adds items from the template), to the common list or as the caller's own items.",
inputSchema: {
tripId: idSchema,
templateId: idSchema,
visibility: packingApplyTemplateRequestSchema
.unwrap()
.shape.visibility.describe(
"'common' (the default) adds the items to the list everyone shares, 'personal' to the caller's own list",
),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async applyPackingTemplate(
{ tripId, templateId, visibility }: { tripId: number; templateId: number; visibility?: 'common' | 'personal' },
ctx: McpContext,
) {
const items = await this.writes.applyTemplate(
tripId,
templateId,
visibility ?? 'common',
await this.guards.tripWriter(tripId, ctx.userId),
);
return ok({ items, count: items.length });
}
@Tool({
name: 'list_packing_templates',
description:
'List the reusable packing templates (id, name, item count) so one can be applied with apply_packing_template.',
inputSchema: {
tripId: idSchema,
},
annotations: TOOL_ANNOTATIONS_READONLY,
when: packingAddonOn,
access: { group: 'packing', mode: 'read' },
})
async listPackingTemplates({ tripId }: { tripId: number }, ctx: McpContext) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
return ok({ templates: await this.packing.listTemplates() });
}
@Tool({
name: 'save_packing_template',
description:
'Save the current packing list as a reusable template. Returns the new template (id, name, category/item counts). Admin only.',
inputSchema: {
tripId: idSchema,
templateName: z.string().min(1).max(100),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async savePackingTemplate({ tripId, templateName }: { tripId: number; templateName: string }, ctx: McpContext) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
if (!(await this.guards.hasTripPermission('packing_edit', tripId, ctx.userId))) return permissionDenied();
// Templates are global; the REST route restricts saving to admins. Match it.
if (!(await this.guards.isAdminUser(ctx.userId))) return adminRequired();
const template = await this.packing.saveAsTemplate(tripId, ctx.userId, templateName);
if (!template) return errorResult('Nothing to save — the packing list is empty.');
return ok({ template });
}
@Tool({
name: 'delete_packing_template',
description: 'Delete a reusable packing template. Templates are global, so deletion is admin only.',
inputSchema: {
templateId: idSchema,
},
annotations: TOOL_ANNOTATIONS_DELETE,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async deletePackingTemplate({ templateId }: { templateId: number }, ctx: McpContext) {
// Templates are global; the REST route restricts management to admins. Match it.
if (!(await this.guards.isAdminUser(ctx.userId))) return adminRequired();
const result = await this.packing.deletePackingTemplate(String(templateId));
return ok({ success: true, name: result.name });
}
@Tool({
name: 'bulk_import_packing',
description:
'Import multiple packing items at once from a list. Optionally assign each to a bag (by name — created if missing), set its weight, or pre-check it.',
inputSchema: {
tripId: idSchema,
items: z
.array(
z.strictObject({
name: z.string().min(1).max(200),
category: z.string().optional(),
quantity: idSchema.optional(),
bag: z
.string()
.max(100)
.optional()
.describe('Bag name to assign the item to; created if it does not exist'),
weight_grams: z.number().int().nonnegative().optional(),
checked: z.boolean().optional(),
}),
)
.min(1),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
when: packingAddonOn,
access: { group: 'packing', mode: 'write' },
})
async bulkImportPacking(
{
tripId,
items,
}: {
tripId: number;
items: {
name: string;
category?: string;
quantity?: number;
bag?: string;
weight_grams?: number;
checked?: boolean;
}[];
},
ctx: McpContext,
) {
if (!(await this.packing.verifyTripAccess(tripId, ctx.userId))) return noAccess();
if (!(await this.guards.hasTripPermission('packing_edit', tripId, ctx.userId))) return permissionDenied();
const created = await this.packing.bulkImport(tripId, items, ctx.userId);
for (const item of created) {
this.guards.safeBroadcast(tripId, 'packing:created', { item }, this.packing.viewersOf(item));
}
// Once for the whole import, not once per item: the ping is content-free and
// every one of them costs each connected client a listBags round trip.
this.packing.broadcastBagTotals(String(tripId));
return ok({ items: created, count: created.length });
}
@ResourceTemplate({
name: 'trip-packing',
uriTemplate: 'trek://trips/{tripId}/packing',
description: 'Packing checklist for a trip',
mimeType: 'application/json',
when: packingAddonOn,
access: { group: 'packing', mode: 'read' },
})
async tripPackingResource(uri: URL, { tripId }: { tripId: string | string[] }, ctx: McpContext) {
const id = uriRowId(tripId);
if (id === null || !(await this.packing.verifyTripAccess(id, ctx.userId))) {
return {
contents: [
{
uri: uri.href,
mimeType: 'application/json',
text: JSON.stringify({ error: 'Trip not found or access denied' }),
},
],
};
}
if (!(await this.addons.isEnabledForTrip(id, ADDON_IDS.PACKING))) return addonOffResource(uri, ADDON_IDS.PACKING);
// Hide other members' private items (#858) from the requesting user.
const items = await this.packing.listItems(id, ctx.userId);
return {
contents: [
{
uri: uri.href,
mimeType: 'application/json',
text: JSON.stringify(items, null, 2),
},
],
};
}
@ResourceTemplate({
name: 'trip-packing-bags',
uriTemplate: 'trek://trips/{tripId}/packing/bags',
description: 'All packing bags for a trip with their members',
mimeType: 'application/json',
when: packingAddonOn,
access: { group: 'packing', mode: 'read' },
})
async tripPackingBagsResource(uri: URL, { tripId }: { tripId: string | string[] }, ctx: McpContext) {
const id = uriRowId(tripId);
if (id === null || !(await this.packing.verifyTripAccess(id, ctx.userId))) {
return {
contents: [
{
uri: uri.href,
mimeType: 'application/json',
text: JSON.stringify({ error: 'Trip not found or access denied' }),
},
],
};
}
if (!(await this.addons.isEnabledForTrip(id, ADDON_IDS.PACKING))) return addonOffResource(uri, ADDON_IDS.PACKING);
const bags = await this.packing.listBags(id);
return {
contents: [
{
uri: uri.href,
mimeType: 'application/json',
text: JSON.stringify(bags, null, 2),
},
],
};
}
// The packing-list prompt moved to trips/trip-prompts.mcp.ts: it reads the
// whole-trip summary for the title, and the read model lives above this
// module (the fold that deleted trips.bridge).
}