File size: 4,131 Bytes
900795e 936b827 31f9688 cfae47b 31f9688 936b827 900795e 31f9688 936b827 31f9688 936b827 31f9688 936b827 cfae47b 936b827 900795e 31f9688 617e1d9 31f9688 617e1d9 31f9688 617e1d9 31f9688 617e1d9 31f9688 617e1d9 cfae47b 617e1d9 31f9688 cfae47b 31f9688 617e1d9 31f9688 cfae47b 2cc44c8 31f9688 10eebb7 31f9688 10eebb7 31f9688 cfae47b |
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 |
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=1)
app.launch(mcp_server=True)
|