Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

feat(mcp): add set_leg_transport_mode tool

Ida Rogalska03f3c5ae3620
diff --git a/server/src/nest/assignments/assignments.mcp.ts b/server/src/nest/assignments/assignments.mcp.ts
index 989a7a727..ee549f83d 100644
--- a/server/src/nest/assignments/assignments.mcp.ts
+++ b/server/src/nest/assignments/assignments.mcp.ts
@@ -115,6 +115,35 @@ export class AssignmentsMcp {
return ok({ assignment });
}
+ @Tool({
+ name: 'set_leg_transport_mode',
+ description: 'Set the travel mode of a route leg for a place assignment. Use direction "outgoing" (default) for the common case: the leg leaving this stop toward the next. Use direction "incoming" ONLY when this stop\'s arriving leg originates from something that is not itself a place assignment (e.g. a flight/train booking arrival, or a morning hotel departure) – setting "incoming" on an ordinary place-to-place leg is stored but has no effect on route rendering, because it targets a column that is only read for non-place origins. transport_mode is a route profile key (e.g. "driving", "walking", "cycling", "transit"); null clears it so the leg inherits the day default.',
+ inputSchema: {
+ tripId: z.number().int().positive(),
+ assignmentId: z.number().int().positive(),
+ transport_mode: z.string().nullable().optional().describe('Route profile key (e.g. "driving"), or null to inherit the day default'),
+ direction: z.enum(['outgoing', 'incoming']).default('outgoing').describe('Which leg to set: "outgoing" (leaving this stop, default) or "incoming" (arriving at it)'),
+ },
+ annotations: TOOL_ANNOTATIONS_WRITE,
+ access: { group: 'places', mode: 'write' },
+ })
+ async setLegTransportMode(
+ { tripId, assignmentId, transport_mode, direction }: {
+ tripId: number; assignmentId: number; transport_mode?: string | null; direction?: 'outgoing' | 'incoming';
+ },
+ ctx: McpContext,
+ ) {
+ if (this.auth.isDemoUser(ctx.userId)) return demoDenied();
+ if (!this.assignments.verifyTripAccess(tripId, ctx.userId)) return noAccess();
+ if (!this.guards.hasTripPermission('day_edit', tripId, ctx.userId)) return permissionDenied();
+ if (!this.assignments.getAssignmentForTrip(assignmentId, tripId)) return errorResult('Assignment not found.');
+ const assignment = direction === 'incoming'
+ ? this.assignments.setIncomingLegTransportMode(assignmentId, transport_mode ?? null)
+ : this.assignments.setLegTransportMode(assignmentId, transport_mode ?? null);
+ this.guards.safeBroadcast(tripId, 'assignment:updated', { assignment });
+ return ok({ assignment });
+ }
+
@Tool({
name: 'move_assignment',
description: 'Move a place assignment to a different day.',
diff --git a/server/tests/unit/mcp/tools-leg-transport-mode.test.ts b/server/tests/unit/mcp/tools-leg-transport-mode.test.ts
new file mode 100644
index 000000000..1404fbef7
--- /dev/null
+++ b/server/tests/unit/mcp/tools-leg-transport-mode.test.ts
@@ -0,0 +1,184 @@
+/**
+ * Unit tests for MCP leg-mode tools: set_leg_transport_mode,
+ * set_day_default_transport_mode.
+ */
+import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
+
+const { testDb, dbMock } = vi.hoisted(() => {
+ const Database = require('better-sqlite3');
+ const db = new Database(':memory:');
+ db.exec('PRAGMA journal_mode = WAL');
+ db.exec('PRAGMA foreign_keys = ON');
+ db.exec('PRAGMA busy_timeout = 5000');
+ const mock = {
+ db,
+ closeDb: () => {},
+ reinitialize: () => {},
+ getPlaceWithTags: () => null,
+ canAccessTrip: (tripId: any, userId: number) =>
+ db.prepare(`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`).get(userId, tripId, userId),
+ isOwner: (tripId: any, userId: number) =>
+ !!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId),
+ };
+ return { testDb: db, dbMock: mock };
+});
+
+vi.mock('../../../src/db/database', () => dbMock);
+vi.mock('../../../src/config', () => ({
+ JWT_SECRET: 'test-jwt-secret-for-trek-testing-only',
+ ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
+ updateJwtSecret: () => {},
+}));
+
+const { broadcastMock } = vi.hoisted(() => ({ broadcastMock: vi.fn() }));
+vi.mock('../../../src/websocket', () => ({ broadcast: broadcastMock }));
+
+import { createTables } from '../../../src/db/schema';
+import { runMigrations } from '../../../src/db/migrations';
+import { resetTestDb } from '../../helpers/test-db';
+import { createUser, createTrip, createDay, createPlace, createDayAssignment } from '../../helpers/factories';
+import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
+
+beforeAll(() => {
+ createTables(testDb);
+ runMigrations(testDb);
+});
+
+beforeEach(() => {
+ resetTestDb(testDb);
+ broadcastMock.mockClear();
+ delete process.env.DEMO_MODE;
+});
+
+afterAll(() => {
+ testDb.close();
+});
+
+async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
+ const h = await createMcpHarness({ userId, withResources: false });
+ try { await fn(h); } finally { await h.cleanup(); }
+}
+
+// ---------------------------------------------------------------------------
+// set_leg_transport_mode
+// ---------------------------------------------------------------------------
+
+describe('Tool: set_leg_transport_mode', () => {
+ it('sets the outgoing leg mode (default direction)', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const place = createPlace(testDb, trip.id);
+ const assignment = createDayAssignment(testDb, day.id, place.id);
+
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip.id, assignmentId: assignment.id, transport_mode: 'cycling' },
+ });
+ const data = parseToolResult(result) as any;
+ expect(data.assignment.leg_transport_mode).toBe('cycling');
+ expect(data.assignment.incoming_leg_transport_mode ?? null).toBeNull(); // outgoing must not touch incoming (panel item 4)
+ expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'assignment:updated', expect.any(Object));
+ });
+ });
+
+ it('sets the incoming leg mode without clobbering the outgoing column (column independence)', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const place = createPlace(testDb, trip.id);
+ const assignment = createDayAssignment(testDb, day.id, place.id);
+ // Seed the outgoing column so we prove incoming does NOT touch it (panel item 4).
+ testDb.prepare('UPDATE day_assignments SET leg_transport_mode = ? WHERE id = ?').run('driving', assignment.id);
+
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip.id, assignmentId: assignment.id, transport_mode: 'transit', direction: 'incoming' },
+ });
+ const data = parseToolResult(result) as any;
+ expect(data.assignment.incoming_leg_transport_mode).toBe('transit');
+ expect(data.assignment.leg_transport_mode).toBe('driving'); // outgoing untouched
+ });
+ });
+
+ it('rejects an assignment belonging to another trip (cross-trip join)', async () => {
+ // Proves the `AND d.trip_id = ?` join in getAssignmentForTrip; a 99999 id cannot.
+ const { user } = createUser(testDb);
+ const trip1 = createTrip(testDb, user.id);
+ const trip2 = createTrip(testDb, user.id);
+ const day2 = createDay(testDb, trip2.id);
+ const place2 = createPlace(testDb, trip2.id);
+ const assignment2 = createDayAssignment(testDb, day2.id, place2.id);
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip1.id, assignmentId: assignment2.id, transport_mode: 'walking' },
+ });
+ expect(result.isError).toBe(true);
+ });
+ });
+
+ it('clears the leg mode with null (inherit day default)', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const place = createPlace(testDb, trip.id);
+ const assignment = createDayAssignment(testDb, day.id, place.id);
+ testDb.prepare('UPDATE day_assignments SET leg_transport_mode = ? WHERE id = ?').run('cycling', assignment.id);
+
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip.id, assignmentId: assignment.id, transport_mode: null },
+ });
+ const data = parseToolResult(result) as any;
+ expect(data.assignment.leg_transport_mode).toBeNull();
+ });
+ });
+
+ it('returns error when assignment not found', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip.id, assignmentId: 99999, transport_mode: 'walking' },
+ });
+ expect(result.isError).toBe(true);
+ });
+ });
+
+ it('returns access denied for non-member', async () => {
+ const { user } = createUser(testDb);
+ const { user: other } = createUser(testDb);
+ const trip = createTrip(testDb, other.id);
+ const day = createDay(testDb, trip.id);
+ const place = createPlace(testDb, trip.id);
+ const assignment = createDayAssignment(testDb, day.id, place.id);
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip.id, assignmentId: assignment.id, transport_mode: 'walking' },
+ });
+ expect(result.isError).toBe(true);
+ });
+ });
+
+ it('blocks demo user', async () => {
+ process.env.DEMO_MODE = 'true';
+ const { user } = createUser(testDb, { email: '[email protected]' });
+ const trip = createTrip(testDb, user.id);
+ const day = createDay(testDb, trip.id);
+ const place = createPlace(testDb, trip.id);
+ const assignment = createDayAssignment(testDb, day.id, place.id);
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'set_leg_transport_mode',
+ arguments: { tripId: trip.id, assignmentId: assignment.id, transport_mode: 'walking' },
+ });
+ expect(result.isError).toBe(true);
+ });
+ });
+});