Skip to main content
Glama
README.md
# GTFS MCP

An MCP server for querying public transit info from GTFS schedules and GTFS-RT realtime feeds. It works with any GTFS-compatible transit system, and includes a ready-to-use config for NYC Subway (MTA).

Ask things like *"When's the next L from Bedford Av?"*, *"Any service alerts on the G?"*, or *"What stops does the 7 train serve?"*.

**Service alerts example**

![Asking Claude about G train service alerts at Nassau Av](readme_img/example_alerts.png)

**Arrivals example**

![Asking Claude about L train arrival times from Bedford Av](readme_img/example_arrivals.png)

## Quickstart

Requires Node 22+, or use the [Docker image](#running-with-docker).

```bash
git clone https://github.com/jdamcd/gtfs-mcp.git && cd gtfs-mcp
npm install
npm run build
```

Add to your Claude Desktop config (`claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "gtfs": {
      "command": "node",
      "args": ["/absolute/path/to/gtfs-mcp/dist/index.js"],
      "env": {
        "GTFS_MCP_CONFIG": "/absolute/path/to/gtfs-mcp/config.mta.json"
      }
    }
  }
}
```

Restart Claude Desktop and ask an NYC transit question. The schedule downloads when the server first starts (~15s for MTA).

## Running with Docker

Images for amd64 and arm64 are published to GitHub Container Registry for each release:

```bash
docker pull ghcr.io/jdamcd/gtfs-mcp
```

Mount a config file at `/config/config.json` and a volume at `/data`. This keeps schedules across restarts and takes the place of the config's `data_dir`.

### Run locally with stdio transport

```bash
docker run -i --rm \
  -v $(pwd)/config.mta.json:/config/config.json:ro \
  -v gtfs-data:/data \
  ghcr.io/jdamcd/gtfs-mcp
```

To use it from Claude Desktop, put this command in `claude_desktop_config.json` with an absolute path to the config file.

### Run on your network with Streamable HTTP transport

On the host machine:

```bash
docker run -d --restart unless-stopped \
  --name gtfs-mcp \
  -p 3000:3000 \
  -e GTFS_MCP_TRANSPORT=http \
  -e MCP_ALLOWED_HOSTS=myhost.local:3000 \
  -v $(pwd)/config.mta.json:/config/config.json:ro \
  -v gtfs-data:/data \
  ghcr.io/jdamcd/gtfs-mcp
```

Clients connect to `http://myhost.local:3000/mcp`. Set `MCP_ALLOWED_HOSTS` to the host and port they use, since requests addressed to anything else are rejected. There's no authentication, so anyone who can reach the port can use the server.

## Adding other transit systems

The server reads a JSON config defining one or more transit systems, pointed to by `GTFS_MCP_CONFIG`. Find feeds for your local agency in the [Mobility Database](https://mobilitydatabase.org/).

```json
{
  "systems": [
    {
      "id": "mbta",
      "name": "MBTA Boston",
      "schedule_url": "https://cdn.mbta.com/MBTA_GTFS.zip",
      "timezone": "America/New_York",
      "realtime": {
        "trip_updates": ["https://cdn.mbta.com/realtime/TripUpdates.pb"],
        "vehicle_positions": ["https://cdn.mbta.com/realtime/VehiclePositions.pb"],
        "alerts": ["https://cdn.mbta.com/realtime/Alerts.pb"]
      },
      "auth": null
    }
  ],
  "data_dir": "~/.gtfs-mcp/data",
  "schedule_refresh_hours": 24,
  "pre_warm_database": "setup"
}
```

`pre_warm_database` controls when schedules are downloaded:

- `"setup"` (default): missing schedules at setup
- `"proactive"`: at setup plus a regular check against `schedule_refresh_hours`
- `"off"`: only when queried

A schedule older than `schedule_refresh_hours` refreshes in the background on its next query in every mode.

Each realtime feed type accepts multiple URLs (e.g. MTA splits trip updates across 8 feeds). Set any feed type to `[]` if the system doesn't provide it.

`timezone` is the agency's IANA timezone (e.g. `"Europe/London"`), so times are right whatever the server's timezone.

### Authenticated feeds

For feeds that need an API key, `auth` names the environment variable to read it from, and sends it as a header or a query parameter:

```json
{
  "auth": {
    "type": "header",
    "header_name": "X-Api-Key",
    "key_env": "MY_API_KEY"
  }
}
```

For a query parameter, use `"type": "query_param"` with `param_name` in place of `header_name`. Set the variable in the Claude Desktop `env` block. With Docker, also forward it with `-e MY_API_KEY`.

## Tools

| Tool | Description | Parameters |
|------|-------------|------------|
| `list_systems` | List all configured transit systems | — |
| `search_stops` | Search stops by name | `system`, `query`, `limit?` |
| `find_nearby_stops` | Stops near a coordinate, ordered by distance | `system`, `lat`, `lon`, `radius_m?`, `limit?` |
| `get_stop` | Stop details and routes serving it | `system`, `stop_id` |
| `get_arrivals` | Upcoming arrivals, realtime where the feed has them and scheduled beyond | `system`, `stop_id`, `route_id?`, `limit?` |
| `list_routes` | Routes in a system, with optional name filter and pagination | `system`, `query?`, `route_type?`, `limit?`, `offset?` |
| `get_route` | Route details with ordered stops in each direction | `system`, `route_id`, `direction_id?` |
| `get_alerts` | Active service alerts | `system`, `route_id?`, `stop_id?`, `include_inactive?` |
| `get_vehicles` | Live vehicle positions | `system`, `route_id?` |
| `get_trip` | Trip stop sequence with realtime delays | `system`, `trip_id` |
| `get_feed_health` | Realtime feed diagnostics: per-URL fetch results, entity counts, oldest header age | `system` |

`system` is a system `id` from your config, such as `"mta-subway"`.

## Resources and prompts

- `gtfs://systems` (resource): an overview of the configured systems and their realtime feeds, to attach as context.
- `transit-status` (prompt): a summary of active alerts, optionally for one `system`. In Claude Desktop it's in the "+" menu.

## Development

```bash
npm test
```

### Evals

LLM evals use [promptfoo](https://promptfoo.dev/) to verify that a model selects the correct tools for natural-language transit queries. Each case runs against Claude Haiku 5.5, Sonnet 5.5 and Opus 5.5. Requires `ANTHROPIC_API_KEY` and `GTFS_MCP_CONFIG` in `.env` or the environment.

```bash
npm run build
npm run eval:smoke    # one pass on Haiku only
npm run eval          # all 3 models, 3 repeats each
npm run eval:summary  # rebuild the report for the last full run
npm run eval:view     # web UI to inspect individual responses
```

Each run writes an HTML report beside its results in `evals/results/` and prints a link (includes confidence interval, problem cases, how many cases exercise each tool, tokens, and estimated cost).

### MCP inspector

Test tools interactively without an LLM client:

```bash
npx @modelcontextprotocol/inspector -e GTFS_MCP_CONFIG=./config.mta.json node dist/index.js
```

### Invariant testing

`scripts/campaign.ts` exercises the full tool surface against real GTFS feeds across the systems in `config.testing.json`, checks the responses for invariant violations, and writes per-call JSON under `campaign-results/`.

> ⚠️ This downloads static GTFS data for every configured system into `./data-testing/` — up to ~10GB.

```bash
GTFS_MCP_CONFIG=./config.testing.json npx tsx scripts/campaign.ts --smoke              # quick pass across all systems
GTFS_MCP_CONFIG=./config.testing.json npx tsx scripts/campaign.ts --deep mbta,vbb      # full phases on selected systems
```