Skip to main content
Glama
lianshuang-photo

SearchAPI MCP Server

search_google_flights_calendar

Find flight prices across date ranges using Google Flights data. Compare fares for different departure and return dates to identify cost-effective travel options.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
flight_typeYes航班类型
departure_idYes出发地ID
arrival_idYes目的地ID
outbound_dateYes出发日期
return_dateNo返程日期
outbound_date_startNo出发日期开始
outbound_date_endNo出发日期结束
return_date_startNo返程日期开始
return_date_endNo返程日期结束
glNo地理位置
hlNo语言
currencyNo货币
adultsNo成人数量
childrenNo儿童数量
travel_classNo舱位等级
stopsNo中转次数

Implementation Reference

  • src/index.js:324-380 (registration)
    Full registration of the search_google_flights_calendar tool, including input schema definition and the inline handler function that queries the google_flights_calendar engine via searchapi.io.
    server.tool(
      'search_google_flights_calendar',
      {
        flight_type: z.string().describe('航班类型'),
        departure_id: z.string().describe('出发地ID'),
        arrival_id: z.string().describe('目的地ID'),
        outbound_date: z.string().describe('出发日期'),
        return_date: z.string().optional().describe('返程日期'),
        outbound_date_start: z.string().optional().describe('出发日期开始'),
        outbound_date_end: z.string().optional().describe('出发日期结束'),
        return_date_start: z.string().optional().describe('返程日期开始'),
        return_date_end: z.string().optional().describe('返程日期结束'),
        gl: z.string().optional().describe('地理位置'),
        hl: z.string().optional().describe('语言'),
        currency: z.string().optional().describe('货币'),
        adults: z.string().optional().describe('成人数量'),
        children: z.string().optional().describe('儿童数量'),
        travel_class: z.string().optional().describe('舱位等级'),
        stops: z.string().optional().describe('中转次数')
      },
      async (args) => {
        const params = {
          engine: 'google_flights_calendar',
          flight_type: args.flight_type,
          departure_id: args.departure_id,
          arrival_id: args.arrival_id,
          outbound_date: args.outbound_date
        };
        
        // 检查航班类型,确保提供必要参数
        if (args.flight_type === 'round_trip' && !args.return_date) {
          return {
            content: [{ type: 'text', text: JSON.stringify({ error: '往返航班需要提供return_date参数' }) }],
            isError: true
          };
        } else if (args.flight_type === 'round_trip') {
          params.return_date = args.return_date;
        }
        
        // 添加可选参数
        const optionalParams = [
          'outbound_date_start', 'outbound_date_end', 'return_date_start', 'return_date_end',
          'gl', 'hl', 'currency', 'adults', 'children', 'travel_class', 'stops'
        ];
        
        for (const key of optionalParams) {
          if (args[key] !== undefined) {
            params[key] = args[key];
          }
        }
        
        const result = await makeSearchapiRequest(params);
        return {
          content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
        };
      }
    );
  • The core handler function that validates inputs, builds request parameters for the google_flights_calendar engine, calls the shared makeSearchapiRequest helper, and formats the response.
    async (args) => {
      const params = {
        engine: 'google_flights_calendar',
        flight_type: args.flight_type,
        departure_id: args.departure_id,
        arrival_id: args.arrival_id,
        outbound_date: args.outbound_date
      };
      
      // 检查航班类型,确保提供必要参数
      if (args.flight_type === 'round_trip' && !args.return_date) {
        return {
          content: [{ type: 'text', text: JSON.stringify({ error: '往返航班需要提供return_date参数' }) }],
          isError: true
        };
      } else if (args.flight_type === 'round_trip') {
        params.return_date = args.return_date;
      }
      
      // 添加可选参数
      const optionalParams = [
        'outbound_date_start', 'outbound_date_end', 'return_date_start', 'return_date_end',
        'gl', 'hl', 'currency', 'adults', 'children', 'travel_class', 'stops'
      ];
      
      for (const key of optionalParams) {
        if (args[key] !== undefined) {
          params[key] = args[key];
        }
      }
      
      const result = await makeSearchapiRequest(params);
      return {
        content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
      };
    }
  • Zod schema defining the input parameters for the search_google_flights_calendar tool.
    {
      flight_type: z.string().describe('航班类型'),
      departure_id: z.string().describe('出发地ID'),
      arrival_id: z.string().describe('目的地ID'),
      outbound_date: z.string().describe('出发日期'),
      return_date: z.string().optional().describe('返程日期'),
      outbound_date_start: z.string().optional().describe('出发日期开始'),
      outbound_date_end: z.string().optional().describe('出发日期结束'),
      return_date_start: z.string().optional().describe('返程日期开始'),
      return_date_end: z.string().optional().describe('返程日期结束'),
      gl: z.string().optional().describe('地理位置'),
      hl: z.string().optional().describe('语言'),
      currency: z.string().optional().describe('货币'),
      adults: z.string().optional().describe('成人数量'),
      children: z.string().optional().describe('儿童数量'),
      travel_class: z.string().optional().describe('舱位等级'),
      stops: z.string().optional().describe('中转次数')
  • Shared helper function used by the tool to make API requests to searchapi.io, handling API key injection and error processing.
    async function makeSearchapiRequest(params) {
      // 确保API Key被添加到参数中
      params.api_key = SEARCHAPI_API_KEY;
      
      try {
        const response = await axios.get(SEARCHAPI_URL, {
          params,
          timeout: 30000 // 30秒超时
        });
        return response.data;
      } catch (error) {
        let errorDetail = null;
        
        if (error.response) {
          try {
            errorDetail = error.response.data;
          } catch (e) {
            errorDetail = error.response.statusText;
          }
        }
        
        const errorMessage = `调用searchapi.io时出错: ${error.message}`;
        if (errorDetail) {
          return { error: `${errorMessage}, 详情: ${JSON.stringify(errorDetail)}` };
        }
        
        return { error: errorMessage };
      }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/lianshuang-photo/searchapi-mcp-nodejs'

If you have feedback or need assistance with the MCP directory API, please join our Discord server