Skip to main content
Glama

get_station_number

Retrieve station identification numbers by providing station names for Nagoya's bus system, enabling accurate transit data queries and route planning.

Instructions

Get station number for a given station name.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
station_nameYes

Implementation Reference

  • The main tool handler function that executes the logic to find a station number by name using exact match or fuzzy matching with difflib. It uses cached station names from the bus client.
    async def get_station_number(
        ctx: Context, station_name: str
    ) -> StationNumberResponse | None:
        """Get station number for a given station name."""
        client = ctx.request_context.lifespan_context.bus_client
    
        log.info("Getting station number for %s", station_name)
        station_names = await _get_station_names(client)
    
        # First try exact match
        station_number = station_names.get(station_name)
        if station_number is not None:
            return StationNumberResponse(
                success=True, station_name=station_name, station_number=station_number
            )
    
        # If no exact match, try fuzzy matching
        log.info("No exact match found for %s, trying fuzzy matching", station_name)
        closest_matches = difflib.get_close_matches(
            station_name, station_names.keys(), n=1, cutoff=0.6
        )
    
        if closest_matches:
            closest_station = closest_matches[0]
            closest_station_number = station_names[closest_station]
            log.info(
                "Found closest match: %s (station number: %s)",
                closest_station,
                closest_station_number,
            )
            return StationNumberResponse(
                success=True,
                station_name=closest_station,
                station_number=closest_station_number,
            )
    
        log.info("No fuzzy match found for %s", station_name)
        return StationNumberResponse(success=False)
  • Pydantic model defining the response schema for the get_station_number tool, including success flag and optional station details.
    class StationNumberResponse(BaseModel):
        success: bool
        station_name: str | None = None
        station_number: int | None = None
  • Registration of the get_station_number tool (and get_timetable) on the FastMCP server instance.
    mcp_server: FastMCP = FastMCP("Nagoya Bus MCP", version=version, lifespan=lifespan)
    mcp_server.tool(get_station_number)
    mcp_server.tool(get_timetable)
  • Helper function to fetch and cache station names by number from the client, used by get_station_number.
    async def _get_station_names(client: Client) -> dict[str, int]:
        global _cached_station_names  # noqa: PLW0603
        if _cached_station_names is None:
            _cached_station_names = (await client.get_station_names()).root
        return _cached_station_names
  • Helper function to fetch and cache station numbers by name (inverse of station names), shared with other tools.
    async def _get_station_numbers(client: Client) -> dict[int, str]:
        global _cached_station_numbers  # noqa: PLW0603
        if _cached_station_numbers is None:
            _cached_station_numbers = {
                num: name for name, num in (await _get_station_names(client)).items()
            }
        return _cached_station_numbers
Install Server

Other Tools

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/ymyzk/nagoya-bus-mcp'

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