Skip to main content
Glama
GTFS Realtime MCP Server

GTFS Realtime MCP Server

by bribroder
README.md
# GTFS Realtime MCP Server

A Model Context Protocol (MCP) server written in Python that allows Large Language Models to query GTFS Static and Realtime transit data, complete with an intelligent in-memory transit routing integration for location-aware transit lookups.

This MCP server provides intelligent tools to list transit stations by line, check upcoming train arrivals (filtering for trains heading toward a destination), and read service alerts affecting specific lines or stations.

## Features

- **Standard MCP Interface**: Exposes tools natively to Claude or other MCP-compatible clients.
- **GTFS Static**: Parses GTFS static zips into memory on startup (handling `stops`, `routes`, `trips`, `stop_times`) to resolve human-readable station names to GTFS IDs.
- **GTFS Realtime**: Uses `gtfs-realtime-bindings` to parse Protocol Buffer feeds for Trip Updates and Alerts.
- **In-Memory Connection Scan Algorithm (CSA)**: Takes GPS coordinates (origin/destination), resolves them to GTFS stops via Haversine distance, and queries an embedded Connection Scan Algorithm routing engine directly against the GTFS timetables to identify the optimal path and filter realtime train data correctly based on line/direction constraints.
- **Multiple Transport Modes**: Supports standard MCP `stdio` mode (for local LLM clients like Claude Desktop), `sse` (HTTP Server-Sent Events) mode, and `streamable-http` mode.

## Available Tools

The server exposes four primary tools to the LLM:

1. `list_stations_by_line(line_name)`: Returns all station names for a given rail line (e.g., 'Red', 'Blue').
2. `get_next_train(...)`: Given origin and destination station names *or* GPS coordinates (latitude/longitude), it resolves the best stations (falling back to Haversine distance), runs an optimal transit search, and returns the next 5 upcoming train arrivals. It automatically filters out trains that are not heading in the direction of the destination.
3. `plan_trip(...)`: Plan a scheduled transit trip in the future. Pass origin and destination station names along with a natural-language date/time (like "tomorrow at 8am"). It parses GTFS regular schedules (`calendar.txt`) and holiday exceptions (`calendar_dates.txt`) to route and output the top 3 optimal multi-leg itineraries using either "depart_at" or "arrive_by" logic.
4. `check_service_alerts(...)`: Takes a rail line name or station name and checks the GTFS Realtime Alerts feed to see if any service disruptions apply.

## Prerequisites

- Python 3.12+
- [`uv`](https://docs.astral.sh/uv/) (Recommended for dependency management)
- A GTFS Realtime transit provider (API keys and endpoints)

## Installation

1. Clone the repository:
   ```bash
   git clone <repository-url>
   cd <repository-dir>
   ```

2. Sync dependencies using `uv`:
   ```bash
   uv sync
   ```

## Configuration

The server requires a `config.yaml` file in the root directory to know where to fetch GTFS data from.

1. Copy the provided template:
   ```bash
   cp config.yaml.template config.yaml
   ```

2. Edit `config.yaml` to include your provider's specific endpoints and API key. For example, for WMATA:
   ```yaml
   gtfs_api_key: "YOUR_WMATA_API_KEY"
   gtfs_api_url: "https://api.wmata.com/gtfs"
   gtfs_static_url: "https://api.wmata.com/gtfs/rail-gtfs-static.zip"
   gtfs_vp_url: "https://api.wmata.com/gtfs/rail-gtfsrt-vehiclepositions.pb"
   gtfs_tu_url: "https://api.wmata.com/gtfs/rail-gtfsrt-tripupdates.pb"
   gtfs_alerts_url: "https://api.wmata.com/gtfs/rail-gtfsrt-alerts.pb"
   ```

*Note: The server uses the HTTP header `api_key: <YOUR_KEY>` when making requests. If your provider requires a different header format (like `Authorization: Bearer <KEY>`), you may need to adjust `src/gtfs_mcp/static_data.py` and `src/gtfs_mcp/realtime.py` accordingly.*

## Usage

You can run the server directly using `uv run`.

### STDIO Mode (Local MCP Clients)
This is the standard mode for desktop clients like Claude Desktop.
```bash
uv run gtfs-mcp --transport stdio
```

To configure this for Claude Desktop, add the following to your `claude_desktop_config.json`:
```json
{
  "mcpServers": {
    "gtfs-mcp": {
      "command": "uv",
      "args": [
        "run",
        "gtfs-mcp",
        "--transport",
        "stdio"
      ],
      "cwd": "/path/to/this/repository"
    }
  }
}
```

### SSE / Streamable HTTP Mode (HTTP / Remote)
Run the server exposing HTTP Server-Sent Events or Streamable HTTP on a specific port:
```bash
uv run gtfs-mcp --transport sse --host 0.0.0.0 --port 8000
# OR
uv run gtfs-mcp --transport streamable-http --host 0.0.0.0 --port 8000
```

TDQS

A3.6/5.0

Scored across 4 tools

Disambiguation4/5

The four tools have mostly distinct purposes: station listing, real-time next-train lookup, future trip planning, and service-alert checking. get_next_train and plan_trip could be confused at first, but the descriptions clearly separate real-time versus scheduled future planning.

Naming Consistency5/5

All tool names use consistent snake_case verb_noun-style naming: list_stations_by_line, get_next_train, plan_trip, and check_service_alerts. The pattern is predictable and readable throughout.

Tool Count4/5

Four tools is a focused, reasonable set for a GTFS realtime transit server. It is slightly thin but not inappropriate, since each tool maps to a clear rider-facing capability.

Completeness4/5

The surface covers core transit workflows: discovering stations by line, checking next trains, planning future trips, and checking service alerts. Minor gaps remain, such as station search/autocomplete or richer alert details, but agents can work around them.

Maintenance

ActivityInactive
ResponsivenessNo recent community issues