|
import gradio as gr |
|
import os |
|
from dotenv import load_dotenv |
|
import serpapi |
|
from typing import Optional, Dict |
|
from utils import _remove_null_params |
|
|
|
load_dotenv() |
|
|
|
|
|
def search_flights( |
|
departure_id: str, |
|
arrival_id: str, |
|
outbound_date: str, |
|
return_date: str, |
|
currency: str, |
|
) -> Dict: |
|
""" |
|
Search for flights using Google Flights API via SerpAPI. |
|
|
|
This function retrieves flight information including prices, schedules, and airport details |
|
for a given route and date range. |
|
|
|
Args: |
|
departure_id (str): The departure airport IATA code (e.g., "HYD" for Hyderabad). |
|
arrival_id (str): The arrival airport IATA code (e.g., "HKT" for Phuket). |
|
outbound_date (str): The outbound date in YYYY-MM-DD format. |
|
return_date (str): The return date in YYYY-MM-DD format. |
|
currency (str): The currency code for prices (e.g., "USD", "EUR"). |
|
|
|
Returns: |
|
Dict: A dictionary containing flight search results including: |
|
- Price insights |
|
- Best flight options |
|
- Airport information |
|
- Flight schedules |
|
""" |
|
params = { |
|
"engine": "google_flights", |
|
"departure_id": departure_id, |
|
"arrival_id": arrival_id, |
|
"outbound_date": outbound_date, |
|
"return_date": return_date, |
|
"currency": currency, |
|
"hl": "en", |
|
"api_key": os.getenv("SERP_API_KEY"), |
|
} |
|
params = _remove_null_params(params=params) |
|
result = serpapi.search(params) |
|
return result.as_dict() |
|
|
|
|
|
def search_hotels( |
|
query: str, |
|
check_in_date: str, |
|
check_out_date: str, |
|
num_adults: str, |
|
currency: str, |
|
sort_by: Optional[str] = None, |
|
min_price: Optional[str] = None, |
|
max_price: Optional[str] = None, |
|
rating: Optional[str] = None, |
|
): |
|
""" |
|
Uses Google Hotels API via SerpAPI to search for hotels and returns property information. |
|
|
|
Args: |
|
query (str): The location or hotel name to search for. |
|
check_in_date (str): The check-in date in YYYY-MM-DD format. |
|
check_out_date (str): The check-out date in YYYY-MM-DD format. |
|
num_adults (str): Number of adult guests. |
|
currency (str): The currency code for prices. |
|
sort_by (Optional[str]): Parameter is used to sort results (Available options: "3" - Lowest price, "8" - Highest rating, "13" - Most reviewed) |
|
min_price (Optional[str]): Filter for price lower limit. |
|
max_price (Optional[str]): Filter for price upper limit. |
|
rating (Optional[str]): Parameter is used for filtering the results to certain rating. (Available options: "7" - 3.5+, "8" - 4.0+, "9" - 4.5+) |
|
|
|
Returns: |
|
dict: A dictionary containing hotel properties with their details including |
|
prices, ratings, amenities, and location information. |
|
""" |
|
params = { |
|
"engine": "google_hotels", |
|
"q": query, |
|
"check_in_date": check_in_date, |
|
"check_out_date": check_out_date, |
|
"adults": num_adults, |
|
"currency": currency, |
|
"sort_by": sort_by, |
|
"min_price": min_price, |
|
"max_price": max_price, |
|
"rating": rating, |
|
"hl": "en", |
|
"api_key": os.getenv("SERP_API_KEY"), |
|
} |
|
params = _remove_null_params(params=params) |
|
result = serpapi.search(params) |
|
result_dict = result.as_dict() |
|
hotel_results = {"properties": []} |
|
if "properties" in result_dict: |
|
hotel_results["properties"] = result_dict["properties"][:5] |
|
|
|
return hotel_results |
|
|
|
|
|
with gr.Blocks(title="Travel Itinerary MCP Server") as app: |
|
gr.Markdown("# Travel Itinerary MCP Server\nThis application allows users to search for flights and hotels using the Google Flights and Google Hotels APIs via SerpAPI.") |
|
with gr.Tab("Search Flights"): |
|
gr.Interface( |
|
fn=search_flights, |
|
inputs=["text"] * 5, |
|
outputs=gr.JSON(), |
|
) |
|
with gr.Tab("Search Hotels"): |
|
gr.Interface( |
|
fn=search_hotels, |
|
inputs=["text"] * 9, |
|
outputs=gr.JSON(), |
|
) |
|
|
|
app.queue(max_size=3) |
|
app.launch(mcp_server=True) |
|
|