ch_search_connections
Search train connections in Switzerland using real-time data. Input origin, destination, date, and time to get departure details, platforms, and transfers via transport.opendata.ch API.
Instructions
Search for train connections in Switzerland between two stations. Uses transport.opendata.ch API to provide real-time connection data including departure times, duration, platforms, and transfers.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | ||
| destination | Yes | ||
| is_arrival_time | No | ||
| limit | No | ||
| origin | Yes | ||
| time | No |
Implementation Reference
- tools/ch.py:20-61 (handler)The main handler function implementing the logic to search for train connections in Switzerland. It validates station names, builds query parameters for date/time/arrival, and fetches data from the transport.opendata.ch API.async def ch_search_connections( origin: str, destination: str, limit: Optional[int] = 4, date: Optional[str] = None, time: Optional[str] = None, is_arrival_time: Optional[bool] = False ) -> Dict[str, Any]: """ Search for train connections between Swiss stations. Args: origin: Departure station name (e.g., 'Zürich HB') destination: Arrival station name (e.g., 'Basel SBB') limit: Max number of connections (default: 4) date: Date in format YYYY-MM-DD time: Time in HH:MM format is_arrival_time: Whether time refers to arrival (True) or departure (False) """ origin_clean = validate_station_name(origin) destination_clean = validate_station_name(destination) params = { "from": origin_clean, "to": destination_clean, "limit": limit or 4 } if date: params["date"] = date if time: params["time"] = format_time_for_api(time) if is_arrival_time: params["isArrivalTime"] = "1" try: logger.info(f"Searching connections: {origin} → {destination}") return await fetch_json(f"{CH_BASE_URL}/connections", params) except TransportAPIError as e: logger.error(f"CH connection search failed: {e}") raise
- tools/ch.py:16-19 (registration)The @mcp.tool decorator that registers the ch_search_connections tool on the MCP instance, defining its name and description. The input schema is inferred from the function parameters.@mcp.tool( name="ch_search_connections", description="Search for train connections in Switzerland between two stations. Uses transport.opendata.ch API to provide real-time connection data including departure times, duration, platforms, and transfers." )
- server.py:50-50 (registration)Call to register_ch_tools(mcp) in the main server setup, which executes the decorators and registers the ch_search_connections tool along with other Swiss transport tools.ch_tools = register_ch_tools(mcp)