Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

fix(mcp): keep the " (CODE)" enum-uniqueness suffix out of flight results (#245)

Punit Arani9bab6a8614aa
diff --git a/fli/mcp/server.py b/fli/mcp/server.py
index 280e584..94a0bd0 100755
--- a/fli/mcp/server.py
+++ b/fli/mcp/server.py
@@ -35,12 +35,14 @@ from fli.core import (
)
from fli.core.parsers import ParseError
from fli.models import (
+ Airline,
Airport,
BagsFilter,
DateSearchFilters,
FlightSearchFilters,
PassengerInfo,
TripType,
+ display_name,
)
from fli.search import SearchDates, SearchFlights
from fli.search.dates import MAX_DATES_PER_SEARCH
@@ -409,15 +411,24 @@ def _flight_idents(flight: Any) -> list[str]:
return [f"{_airline_code(leg.airline)}{leg.flight_number}" for leg in _flight_legs(flight)]
+def _plain_name(value: Any) -> Any:
+ """Return an airport/airline's display name without the internal `` (CODE)`` suffix.
+
+ Legs are duck-typed here (tests and callers may pass plain strings), so
+ anything that is not an ``Airport``/``Airline`` member passes through as is.
+ """
+ return display_name(value) if isinstance(value, Airport | Airline) else value
+
+
def _serialize_flight_leg(leg: Any) -> dict[str, Any]:
"""Serialize a single flight leg to a dictionary."""
out: dict[str, Any] = {
- "departure_airport": leg.departure_airport,
- "arrival_airport": leg.arrival_airport,
+ "departure_airport": _plain_name(leg.departure_airport),
+ "arrival_airport": _plain_name(leg.arrival_airport),
"departure_time": leg.departure_datetime,
"arrival_time": leg.arrival_datetime,
"duration": leg.duration,
- "airline": leg.airline,
+ "airline": _plain_name(leg.airline),
"airline_code": _airline_code(leg.airline),
"flight_number": leg.flight_number,
}
diff --git a/tests/mcp/test_leg_display_names.py b/tests/mcp/test_leg_display_names.py
new file mode 100644
index 0000000..3a41712
--- /dev/null
+++ b/tests/mcp/test_leg_display_names.py
@@ -0,0 +1,73 @@
+"""MCP flight results must show plain airport/airline names.
+
+Names shared by several airports or airlines are stored with an internal
+`` (CODE)`` suffix so every Enum value stays unique. That suffix must not leak
+into tool responses: the code is already returned in its own field.
+"""
+
+import json
+from datetime import datetime
+from enum import Enum
+
+from fli.mcp.server import _serialize_flight_leg as _serialize_leg
+from fli.models import Airline, Airport, FlightLeg
+
+
+def _leg(airline: Airline, origin: Airport, destination: Airport) -> FlightLeg:
+ return FlightLeg(
+ airline=airline,
+ flight_number="6012",
+ departure_airport=origin,
+ arrival_airport=destination,
+ departure_datetime=datetime(2026, 12, 15, 9, 25),
+ arrival_datetime=datetime(2026, 12, 15, 11, 55),
+ duration=150,
+ )
+
+
+def _as_json(payload: dict) -> dict:
+ """Round-trip the way an MCP client receives it (Enums collapse to their value)."""
+
+ def encode(value: object) -> object:
+ return value.value if isinstance(value, Enum) else value.isoformat()
+
+ return json.loads(json.dumps(payload, default=encode))
+
+
+def test_disambiguated_names_are_shown_without_their_code_suffix():
+ """OKA/NTL/W4 all carry an internal suffix; none of it reaches the response."""
+ assert Airport.OKA.value.endswith("(OKA)") # guards the premise of this test
+ leg = _as_json(_serialize_leg(_leg(Airline.W4, Airport.OKA, Airport.NTL)))
+
+ assert leg["departure_airport"] == "Naha Airport"
+ assert leg["arrival_airport"] == "Newcastle Airport"
+ assert leg["airline"] == "Wizz Air"
+ assert leg["airline_code"] == "W4"
+
+
+def test_ordinary_names_are_unchanged():
+ """Names that were never ambiguous are returned exactly as before."""
+ leg = _as_json(_serialize_leg(_leg(Airline.BA, Airport.JFK, Airport.LHR)))
+
+ assert leg["departure_airport"] == Airport.JFK.value
+ assert leg["arrival_airport"] == Airport.LHR.value
+ assert leg["airline"] == Airline.BA.value
+
+
+def test_duck_typed_legs_with_plain_strings_pass_through():
+ """The serializer accepts leg-like objects whose names are already strings."""
+ from types import SimpleNamespace
+
+ leg = SimpleNamespace(
+ airline="Example Air",
+ flight_number="1",
+ departure_airport="Somewhere (XYZ)",
+ arrival_airport="Elsewhere",
+ departure_datetime=datetime(2026, 12, 15, 9, 25),
+ arrival_datetime=datetime(2026, 12, 15, 11, 55),
+ duration=150,
+ )
+ out = _as_json(_serialize_leg(leg))
+
+ assert out["departure_airport"] == "Somewhere (XYZ)" # untouched: not an Enum member
+ assert out["airline"] == "Example Air"