ch_search_connections
Find train connections in Switzerland between stations using real-time data. Get departure times, duration, platforms, and transfer information for planning journeys.
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 |
|---|---|---|---|
| origin | Yes | ||
| destination | Yes | ||
| limit | No | ||
| date | No | ||
| time | No | ||
| is_arrival_time | No |
Implementation Reference
- tools/ch.py:20-61 (handler)The main handler function implementing ch_search_connections. It validates station names, builds API parameters for date/time/arrival, and fetches JSON data from the Swiss transport API (transport.opendata.ch/connections). Handles TransportAPIError.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 registers the ch_search_connections function with the MCP server instance, specifying the tool name and description.@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." )
- tools/ch.py:139-144 (registration)The register_ch_tools function returns a list including the ch_search_connections tool object for further use (e.g., logging total tools).return [ ch_search_connections, ch_search_stations, ch_get_departures, ch_nearby_stations ]
- server.py:49-52 (registration)In the main server.py, register_ch_tools(mcp) is called to register the CH tools, including ch_search_connections, with the FastMCP server instance.# Register tools (UK only if keys exist and not disabled) ch_tools = register_ch_tools(mcp) be_tools = register_be_tools(mcp) no_tools = register_no_tools(mcp)
- tools/ch.py:28-38 (schema)Docstring providing detailed input schema descriptions for parameters, used for tool schema validation in MCP.""" 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) """