Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

fix(cli,mcp): handle multi-city flight result serialization

Punit Arani110ff6b1f648
diff --git a/fli/cli/utils.py b/fli/cli/utils.py
index 9658989..011af30 100644
--- a/fli/cli/utils.py
+++ b/fli/cli/utils.py
@@ -201,9 +201,15 @@ def _serialize_flight_segment_result(flight: Any, *, include_price: bool = False
def serialize_flight_result(flight_data: Any) -> dict[str, Any]:
- """Serialize a flight result or round-trip pair for JSON output."""
- if isinstance(flight_data, tuple):
- outbound, return_flight = flight_data
+ """Serialize a flight result or round-trip/multi-city tuple for JSON output."""
+ if not isinstance(flight_data, tuple):
+ return _serialize_flight_segment_result(flight_data, include_price=True)
+
+ segments = list(flight_data)
+
+ if len(segments) == 2:
+ # Round-trip: Google Flights returns the full RT price on the outbound leg.
+ outbound, return_flight = segments
return {
"price": outbound.price,
"currency": outbound.currency or DEFAULT_CURRENCY,
@@ -213,7 +219,15 @@ def serialize_flight_result(flight_data: Any) -> dict[str, Any]:
"return": _serialize_flight_segment_result(return_flight),
}
- return _serialize_flight_segment_result(flight_data, include_price=True)
+ # Multi-city (3+ legs): combined price is on the final leg.
+ price_segment = segments[-1]
+ return {
+ "price": price_segment.price,
+ "currency": price_segment.currency or DEFAULT_CURRENCY,
+ "duration": sum(s.duration for s in segments),
+ "stops": sum(s.stops for s in segments),
+ "segments": [_serialize_flight_segment_result(s) for s in segments],
+ }
def serialize_date_result(date_result: Any, trip_type: TripType) -> dict[str, Any]:
diff --git a/fli/mcp/server.py b/fli/mcp/server.py
index fea62c0..6496cd6 100755
--- a/fli/mcp/server.py
+++ b/fli/mcp/server.py
@@ -307,11 +307,20 @@ def _serialize_flight_leg(leg: Any) -> dict[str, Any]:
def _serialize_flight_result(flight: Any, is_round_trip: bool = False) -> dict[str, Any]:
- """Serialize a flight result (or round-trip pair) to a dictionary."""
- if is_round_trip and isinstance(flight, tuple):
- outbound, return_flight = flight
+ """Serialize a flight result (or round-trip/multi-city tuple) to a dictionary."""
+ if not isinstance(flight, tuple):
+ return {
+ "price": flight.price,
+ "currency": flight.currency or CONFIG.default_currency,
+ "legs": [_serialize_flight_leg(leg) for leg in flight.legs],
+ }
+
+ segments = list(flight)
+
+ if len(segments) == 2 and is_round_trip:
+ # Google Flights returns the full round-trip price on the outbound leg
+ outbound, return_flight = segments
return {
- # Google Flights returns the full round-trip price on the outbound leg
"price": outbound.price,
"currency": outbound.currency or CONFIG.default_currency,
"legs": [
@@ -319,12 +328,18 @@ def _serialize_flight_result(flight: Any, is_round_trip: bool = False) -> dict[s
*[_serialize_flight_leg(leg) for leg in return_flight.legs],
],
}
- else:
- return {
- "price": flight.price,
- "currency": flight.currency or CONFIG.default_currency,
- "legs": [_serialize_flight_leg(leg) for leg in flight.legs],
- }
+
+ # Multi-city (3+ legs): combined price is on the final leg.
+ price_segment = segments[-1]
+ return {
+ "price": price_segment.price,
+ "currency": price_segment.currency or CONFIG.default_currency,
+ "legs": [
+ serialized_leg
+ for segment in segments
+ for serialized_leg in [_serialize_flight_leg(leg) for leg in segment.legs]
+ ],
+ }
def _serialize_date_result(date_result: Any) -> dict[str, Any]:
diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py
index 1f2685e..aeda70f 100644
--- a/tests/cli/test_utils.py
+++ b/tests/cli/test_utils.py
@@ -348,6 +348,25 @@ def test_serialize_flight_result_round_trip():
assert payload["return"]["legs"][0]["flight_number"] == "DL200"
+def test_serialize_flight_result_multi_city():
+ """Multi-city (3+ legs) JSON serialization should not crash."""
+ leg1 = _make_flight_result(price=0.0, flight_number="AA100")
+ leg2 = _make_flight_result(price=0.0, flight_number="DL200")
+ leg3 = _make_flight_result(price=800.0, flight_number="UA300")
+
+ payload = serialize_flight_result((leg1, leg2, leg3))
+
+ # Price comes from the final leg for multi-city
+ assert payload["price"] == 800.0
+ assert payload["currency"] == "USD"
+ assert payload["duration"] == 900 # 300 * 3
+ assert payload["stops"] == 0
+ assert len(payload["segments"]) == 3
+ assert payload["segments"][0]["legs"][0]["flight_number"] == "AA100"
+ assert payload["segments"][1]["legs"][0]["flight_number"] == "DL200"
+ assert payload["segments"][2]["legs"][0]["flight_number"] == "UA300"
+
+
def test_serialize_date_result_round_trip():
"""Date serialization should include the return date for round-trip searches."""
result = DatePrice(
diff --git a/tests/mcp/test_mcp_server_fixes.py b/tests/mcp/test_mcp_server_fixes.py
index a258649..06bbd3b 100644
--- a/tests/mcp/test_mcp_server_fixes.py
+++ b/tests/mcp/test_mcp_server_fixes.py
@@ -130,6 +130,28 @@ class TestSerializeFlightResult:
result = _serialize_flight_result(flight, is_round_trip=True)
assert result["price"] == 500.0
+ def test_multi_city_three_legs(self):
+ """Multi-city (3-leg) tuple should serialize without crashing."""
+ leg1 = _make_flight(price=0.0, legs=[_make_leg("JFK", "LAX")])
+ leg2 = _make_flight(price=0.0, legs=[_make_leg("LAX", "ORD")])
+ leg3 = _make_flight(price=750.0, legs=[_make_leg("ORD", "JFK")])
+
+ result = _serialize_flight_result((leg1, leg2, leg3), is_round_trip=False)
+
+ assert result["price"] == 750.0
+ assert len(result["legs"]) == 3
+
+ def test_multi_city_not_treated_as_round_trip(self):
+ """A 3-leg tuple must not be treated as round-trip even if flag is True."""
+ leg1 = _make_flight(price=0.0, legs=[_make_leg("JFK", "LAX")])
+ leg2 = _make_flight(price=0.0, legs=[_make_leg("LAX", "ORD")])
+ leg3 = _make_flight(price=900.0, legs=[_make_leg("ORD", "JFK")])
+
+ result = _serialize_flight_result((leg1, leg2, leg3), is_round_trip=True)
+
+ assert result["price"] == 900.0
+ assert len(result["legs"]) == 3
+
def test_uses_flight_currency_when_available(self):
"""Serialization should emit the per-result returned currency."""
flight = _make_flight(price=275.0)