Skip to main content
Glama
kennyckk

KMB Bus MCP Server

get_next_bus

Find the next arrival time for a specific KMB bus route at a designated stop in Hong Kong by providing the route number and stop name.

Instructions

Get the next arrival time for a specified bus route at a stop.

Args:
    route: The bus route number (e.g., "1A", "6", "960")
    stop_name: The name of the bus stop

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
routeYes
stop_nameYes

Implementation Reference

  • The handler function decorated with @mcp.tool(), implementing the core logic for retrieving and formatting the next bus arrival times for a given route and stop name. Includes input schema via type hints and docstring Args.
    @mcp.tool()
    async def get_next_bus(route: str, stop_name: str) -> str:
        """Get the next arrival time for a specified bus route at a stop.
        
        Args:
            route: The bus route number (e.g., "1A", "6", "960")
            stop_name: The name of the bus stop
        """
        # Find the stop ID by name
        stops = await find_stops_by_name(stop_name)
        
        if not stops:
            return f"Could not find any stops matching '{stop_name}'"
        
        results = []
        for stop in stops:
            stop_id = stop["stop"]
            stop_name_en = stop["name_en"]
            
            # Get ETA data
            eta_data = await get_eta(stop_id, route)
    
            
            if not eta_data:
                results.append(f"No arrival data available for route {route} at stop '{stop_name_en}' ({stop_id})")
                continue
            
            # Filter ETAs for the specified route
            route_etas = [eta for eta in eta_data if eta["route"] == route]
            
            if not route_etas:
                results.append(f"No scheduled arrivals for route {route} at stop '{stop_name_en}' ({stop_id})")
                continue
            
            # Format ETA information
            stop_results = [f"Arrivals for route {route} at '{stop_name_en}' ({stop_id}):"]
            
            for eta in route_etas:
                eta_time = eta.get("eta", None)
                if eta_time :
                    eta_time = eta_time.split("+")[0].replace("T", " ")  # Format the timestamp
                
                dest = eta.get("dest_tc", "") or eta.get("dest_en", "Unknown destination")
                remark = eta.get("rmk_tc", "") or eta.get("rmk_en", "")
                
                if remark:
                    stop_results.append(f"- {eta_time} to {dest} ({remark})")
                else:
                    stop_results.append(f"- {eta_time} to {dest}")
            
            results.append("\n".join(stop_results))
        
        return "\n\n".join(results)

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/kennyckk/mcp_hkbus'

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