Files
worldmonitor/scripts/generate-country-bboxes.cjs
Elie Habib 380b495be8 feat(mcp): live airspace + maritime tools; fix OAuth consent UI (#2442)
* fix(oauth): fix CSS arrow bullets + add MCP branding to consent page

- CSS content:'\2192' (not HTML entity which doesn't work in CSS)
- Rename logo/title to "WorldMonitor MCP" on both consent and error pages
- Inject real news headlines into get_country_brief to prevent hallucination
  Fetches list-feed-digest (4s budget), passes top-15 headlines as ?context=
  to get-country-intel-brief; brief timeout reduced to 24s to stay under Edge ceiling

* feat(mcp): add get_airspace + get_maritime_activity live query tools

New tools answer real-time positional questions via existing bbox RPCs:
- get_airspace: civilian ADS-B (OpenSky) + military flights over any country
  parallel-fetches track-aircraft + list-military-flights, capped at 100 each
- get_maritime_activity: AIS density zones + disruptions for a country's waters
  calls get-vessel-snapshot with country bbox

Country → bounding box resolved via shared/country-bboxes.json (167 entries,
generated from public/data/countries.geojson by scripts/generate-country-bboxes.cjs).
Both API calls use 8s AbortSignal.timeout; get_airspace uses Promise.allSettled
so one failure doesn't block the other.

* docs: fix markdown lint in airspace/maritime plan (blank lines around lists)

* fix(oauth): use literal → in CSS content (\2192 is invalid JS octal in ESM)

* fix(hooks): extend bundle check to api/oauth/ subdirectory (was api/*.js, now uses find)

* fix(mcp): address P1 review findings from PR 2442

- JSON import: add 'with { type: json }' so node --test works without tsx loader
- get_airspace: surface upstream failures; partial outage => partial:true+warnings,
  total outage => throw (prevents misleading zero-aircraft response)
- pre-push hook: add #!/usr/bin/env bash shebang (was no shebang, ran as /bin/sh
  on Linux CI/contributors; process substitution + [[ ]] require bash)

* fix(mcp): replace JSON import attribute with TS module for Vercel compat

Vercel's esbuild bundler does not support `with { type: 'json' }` import
attributes, causing builds to fail with "Expected ';' but found 'with'".

Fix: generate shared/country-bboxes.ts (typed TS module) alongside the
existing JSON file. The TS import has no attributes and bundles cleanly
with all esbuild versions.

Also extend the pre-push bundle check to include api/*.ts root-level files
so this class of error is caught locally before push.

* fix(mcp): reduce get_country_brief timing budget to 24 s (6 s Edge margin)

Digest pre-fetch: 4 s → 2 s (cached endpoint, silent fallback on miss)
Brief call: 24 s → 22 s
Total worst-case: 24 s vs Vercel Edge 30 s hard kill — was 28 s (2 s margin)

* test(mcp): add coverage for get_airspace and get_maritime_activity

9 new tests:
- get_airspace: happy path, unknown code, partial failure (mil down),
  total failure (-32603), type=civilian skips military fetch
- get_maritime_activity: happy path, unknown code, API failure (-32603),
  empty snapshot handled gracefully

Also fixes import to use .ts extension so Node --test resolver finds the
country-bboxes module (tsx resolves .ts directly; .js alias only works
under moduleResolution:bundler at typecheck time)

* fix(mcp): use .js + .d.ts for country-bboxes — Vercel rejects .ts imports

Vercel edge bundler refuses .ts extension imports even from .ts edge
functions. Plain .js is the only safe runtime import for edge functions.

Pattern: generate shared/country-bboxes.js (pure ESM, no TS syntax) +
shared/country-bboxes.d.ts (type declaration). TypeScript uses the .d.ts
for tuple types at check time; Vercel and Node --test load the .js at
runtime. The previous .ts module is removed.

* test(mcp): update tool count to 26 (main added search_flights + search_flight_prices_by_date)
2026-03-28 23:59:47 +04:00

66 lines
2.7 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const root = path.resolve(__dirname, '..');
const geojson = JSON.parse(fs.readFileSync(path.join(root, 'public', 'data', 'countries.geojson'), 'utf8'));
function coordsFromGeom(geom) {
if (!geom) return [];
if (geom.type === 'Polygon') return geom.coordinates.flat(1);
if (geom.type === 'MultiPolygon') return geom.coordinates.flat(2);
return [];
}
const result = {};
for (const f of geojson.features) {
const iso2 = f.properties['ISO3166-1-Alpha-2'];
// Skip entries with non-standard ISO codes (e.g. "-99" for disputed/unassigned territories)
if (!iso2 || !f.geometry || !/^[A-Z]{2}$/.test(iso2)) continue;
const coords = coordsFromGeom(f.geometry);
if (!coords.length) continue;
let minLat = Infinity, maxLat = -Infinity, minLon = Infinity, maxLon = -Infinity;
for (const [lon, lat] of coords) {
if (lat < minLat) minLat = lat;
if (lat > maxLat) maxLat = lat;
if (lon < minLon) minLon = lon;
if (lon > maxLon) maxLon = lon;
}
// [sw_lat, sw_lon, ne_lat, ne_lon] — 2dp precision keeps file compact
result[iso2] = [+(minLat.toFixed(2)), +(minLon.toFixed(2)), +(maxLat.toFixed(2)), +(maxLon.toFixed(2))];
}
// Sort keys for stable diffs
const sorted = Object.fromEntries(Object.entries(result).sort(([a], [b]) => a.localeCompare(b)));
const out = path.join(root, 'shared', 'country-bboxes.json');
fs.writeFileSync(out, JSON.stringify(sorted, null, 2) + '\n');
console.log(`Wrote ${Object.keys(sorted).length} entries to ${out}`);
// Generate a plain .js ESM module — no TS syntax so Vercel edge bundler accepts it.
// Paired with a .d.ts declaration so api/mcp.ts gets proper tuple types at typecheck.
// Import in api/mcp.ts as '../shared/country-bboxes.js'.
const jsLines = [
'// Auto-generated by scripts/generate-country-bboxes.cjs — do not edit manually',
'// To regenerate: node scripts/generate-country-bboxes.cjs',
'const COUNTRY_BBOXES = {',
...Object.entries(sorted).map(([k, v]) => ` ${JSON.stringify(k)}: [${v.join(', ')}],`),
'};',
'export default COUNTRY_BBOXES;',
'',
];
const jsOut = path.join(root, 'shared', 'country-bboxes.js');
fs.writeFileSync(jsOut, jsLines.join('\n'));
console.log(`Wrote JS module to ${jsOut}`);
// Companion .d.ts so TypeScript knows the shape when api/mcp.ts imports the .js
const dtsLines = [
'// Auto-generated by scripts/generate-country-bboxes.cjs — do not edit manually',
'declare const COUNTRY_BBOXES: Record<string, [number, number, number, number]>;',
'export default COUNTRY_BBOXES;',
'',
];
const dtsOut = path.join(root, 'shared', 'country-bboxes.d.ts');
fs.writeFileSync(dtsOut, dtsLines.join('\n'));
console.log(`Wrote .d.ts declaration to ${dtsOut}`);