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.

2.5 KB
Raw Blame
import { ADDON_IDS } from '../../addons';
import {
  McpController,
  Tool,
  TOOL_ANNOTATIONS_READONLY,
  TOOL_ANNOTATIONS_NON_IDEMPOTENT,
  ok,
  type McpContext,
} from '../../nest-mcp';
import { addonGate } from '../addons/addon-gate';
import { AddonsService } from '../addons/addons.service';
import { GoogleRouteService } from './google-route.service';
import { answeringRefusals } from './roadtrip-mcp.helpers';
import {
  idSchema,
  googleRouteImportSchema,
  googleRoutePreviewRequestSchema,
  googleRouteStopSchema,
  type GoogleRouteImport,
} from '@trek/shared';

import { z } from 'zod';

@McpController()
export class GoogleRouteMcp {
  constructor(
    private readonly routes: GoogleRouteService,
    readonly addons: AddonsService,
  ) {}
  @Tool({
    name: 'preview_google_maps_route',
    description:
      'Read ordered stops from a Google Maps directions link. No changes are saved. Review geocoded positions and unresolved stops before importing. The exact Google road geometry is not imported.',
    inputSchema: googleRoutePreviewRequestSchema.shape,
    annotations: TOOL_ANNOTATIONS_READONLY,
    access: { group: 'trips', mode: 'read' },
    when: addonGate(ADDON_IDS.ROADTRIP),
  })
  async preview({ url }: { url: string }) {
    return answeringRefusals(async () => ok(await this.routes.preview(url)));
  }

  @Tool({
    name: 'import_google_maps_route',
    description:
      'Append reviewed Google Maps stops to an existing trip day in supplied order. Creates places and visits atomically. Requires place and day editing permissions. Existing visits remain. TREK calculates the road geometry; no Google route geometry is preserved.',
    inputSchema: {
      tripId: idSchema,
      ...googleRouteImportSchema.shape,
      stops: z.array(z.strictObject(googleRouteStopSchema.shape)).min(2).max(30),
    },
    annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
    // What the tool writes, not where the link came from: this inserts up to
    // thirty places and their day assignments, which is exactly what create_place
    // and create_and_assign_place are gated on. trips:write does not imply it, and
    // a client that withheld places:write was getting an itinerary written anyway.
    access: { group: 'places', mode: 'write' },
    when: addonGate(ADDON_IDS.ROADTRIP),
  })
  async import(input: GoogleRouteImport & { tripId: number }, ctx: McpContext) {
    return answeringRefusals(async () => ok(await this.routes.import(input.tripId, ctx.userId, input)));
  }
}