File size: 5,363 Bytes
57a889c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
import { z } from 'zod';
import { findByIata, searchAirports } from '../../services/airportService';
import { searchPlaces, getPlaceDetails, reverseGeocode, resolveGoogleMapsUrl } from '../../services/mapsService';
import { getWeather, getDetailedWeather } from '../../services/weatherService';
import {
  TOOL_ANNOTATIONS_READONLY,
  ok,
} from './_shared';
import { canRead } from '../scopes';

export function registerMapsWeatherTools(server: McpServer, userId: number, scopes: string[] | null): void {
  const canGeo     = canRead(scopes, 'geo');
  const canWeather = canRead(scopes, 'weather');

  // --- MAPS EXTRAS ---

  if (canGeo) server.registerTool(
    'get_place_details',
    {
      description: 'Fetch detailed information about a place by its Google Place ID.',
      inputSchema: {
        placeId: z.string().describe('Google Place ID'),
        lang: z.string().optional().default('en'),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ placeId, lang }) => {
      const details = await getPlaceDetails(userId, placeId, lang ?? 'en');
      if (!details) return { content: [{ type: 'text' as const, text: 'Place not found or maps service not configured.' }], isError: true };
      return ok({ details });
    }
  );

  if (canGeo) server.registerTool(
    'reverse_geocode',
    {
      description: 'Get a human-readable address for given coordinates.',
      inputSchema: {
        lat: z.number(),
        lng: z.number(),
        lang: z.string().optional().default('en'),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ lat, lng, lang }) => {
      const result = await reverseGeocode(String(lat), String(lng), lang ?? 'en');
      if (!result) return { content: [{ type: 'text' as const, text: 'Reverse geocode failed or maps service not configured.' }], isError: true };
      return ok(result);
    }
  );

  if (canGeo) server.registerTool(
    'resolve_maps_url',
    {
      description: 'Resolve a Google Maps share URL to coordinates and place name.',
      inputSchema: {
        url: z.string().describe('Google Maps share URL'),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ url }) => {
      const result = await resolveGoogleMapsUrl(url);
      if (!result) return { content: [{ type: 'text' as const, text: 'Could not resolve URL or maps service not configured.' }], isError: true };
      return ok(result);
    }
  );

  // --- WEATHER ---

  if (canWeather) server.registerTool(
    'get_weather',
    {
      description: 'Get weather forecast for a location and date.',
      inputSchema: {
        lat: z.number(),
        lng: z.number(),
        date: z.string().describe('ISO date YYYY-MM-DD'),
        lang: z.string().optional().default('en'),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ lat, lng, date, lang }) => {
      try {
        const weather = await getWeather(String(lat), String(lng), date, lang ?? 'en');
        return ok({ weather });
      } catch (err: any) {
        return { content: [{ type: 'text' as const, text: err?.message ?? 'Weather service not available.' }], isError: true };
      }
    }
  );

  if (canWeather) server.registerTool(
    'get_detailed_weather',
    {
      description: 'Get hourly/detailed weather forecast for a location and date.',
      inputSchema: {
        lat: z.number(),
        lng: z.number(),
        date: z.string().describe('ISO date YYYY-MM-DD'),
        lang: z.string().optional().default('en'),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ lat, lng, date, lang }) => {
      try {
        const weather = await getDetailedWeather(String(lat), String(lng), date, lang ?? 'en');
        return ok({ weather });
      } catch (err: any) {
        return { content: [{ type: 'text' as const, text: err?.message ?? 'Weather service not available.' }], isError: true };
      }
    }
  );

  // --- AIRPORTS ---

  if (canGeo) server.registerTool(
    'search_airports',
    {
      description: 'Search for airports by name, city, or IATA code. Returns matching airports with IATA code, name, city, country, coordinates, and timezone. Use before create_transport (flight) to get the correct IATA code and timezone for endpoints.',
      inputSchema: {
        query: z.string().min(1).max(200).describe('Airport name, city, or IATA code (e.g. "zurich", "ZRH", "charles de gaulle")'),
        limit: z.number().int().min(1).max(50).optional().default(10),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ query, limit }) => {
      const airports = searchAirports(query, limit ?? 10);
      return ok({ airports });
    }
  );

  if (canGeo) server.registerTool(
    'get_airport',
    {
      description: 'Get a single airport by its IATA code. Returns name, city, country, coordinates, and timezone.',
      inputSchema: {
        iata: z.string().length(3).toUpperCase().describe('IATA airport code (e.g. "ZRH", "AMS", "CDG")'),
      },
      annotations: TOOL_ANNOTATIONS_READONLY,
    },
    async ({ iata }) => {
      const airport = findByIata(iata);
      if (!airport) return { content: [{ type: 'text' as const, text: 'Airport not found.' }], isError: true };
      return ok({ airport });
    }
  );
}