Skip to main content

Real-time NYC subway arrivals CLI and MCP server

Project description

MTA API v3

Real-time NYC subway arrivals API with async concurrent feed fetching.

Key Differences from v2

  • Concurrent fetching: All 8 MTA feeds fetched in parallel using asyncio.to_thread()
  • Async cache: Uses asyncio.Lock to prevent concurrent refresh storms
  • Lazy refresh: Cache updates on first request after expiry

Performance

  • Sequential fetch (v2): ~2-4 seconds
  • Concurrent fetch (v3): ~500ms-1s

Quick Start

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --reload

API Endpoints

Same as v2 - see /docs for OpenAPI documentation.

Docker

docker build -t mta-api-v3 .
docker run -p 8000:8000 mta-api-v3

Deploy to Fly.io

Fly.io is recommended for v3 since it supports persistent servers with the async caching model.

First-time setup

# Install flyctl
curl -L https://fly.io/install.sh | sh

# Login
fly auth login

# Launch app (creates fly.toml)
cd mta-api-v3
fly launch --name mta-api-v3 --region ewr --no-deploy

# Deploy
fly deploy

fly.toml

If you need to create it manually:

app = "mta-api-v3"
primary_region = "ewr"  # Newark, close to MTA servers

[build]
  dockerfile = "Dockerfile"

[http_service]
  internal_port = 8000
  force_https = true

[[http_service.checks]]
  path = "/"
  interval = "30s"
  timeout = "5s"

Useful commands

fly status              # Check app status
fly logs                # View logs
fly ssh console         # SSH into the container
fly scale count 2       # Scale to 2 instances
fly secrets set KEY=val # Set environment variables

CLI

Query subway data from the command line. Output is JSON by default.

# Setup
source venv/bin/activate

# List all stations
python -m mta.cli stations

# Search stations by name
python -m mta.cli stations -q "Times Sq"

# Get station by ID (with arrivals)
python -m mta.cli stations 127

# Get multiple stations
python -m mta.cli stations 127,A32

# Search by location
python -m mta.cli stations --lat 40.7580 --lon -73.9855

# List all routes
python -m mta.cli routes

# Get stations on a route
python -m mta.cli routes A

# Find next trains from a station
python -m mta.cli find "Times Sq"

# Find specific route from a station
python -m mta.cli find "Times Sq" -r A

# Human-readable output (--pretty goes before the command)
python -m mta.cli --pretty stations 127

MCP Server

Run as a Model Context Protocol server for AI assistants:

python -m mta.mcp

Available Tools

Tool Description
get_stations Search stations by name or location
get_station Get station(s) by ID with arrivals
get_routes List all active subway routes
get_route Get all stations on a route
find_train Find next trains from a station

Claude Desktop Configuration

Add to ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "mta-subway": {
      "command": "python",
      "args": ["-m", "mta.mcp"],
      "cwd": "/path/to/mta-api-v3"
    }
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mta_subway-1.0.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mta_subway-1.0.0-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

File details

Details for the file mta_subway-1.0.0.tar.gz.

File metadata

  • Download URL: mta_subway-1.0.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for mta_subway-1.0.0.tar.gz
Algorithm Hash digest
SHA256 09d6e49ae69ae6c5b823c3ee1182a2f4806a1490da25280e60a57856bc9e0903
MD5 f18fef93d5d835c2a961a8477fe743e4
BLAKE2b-256 0eb4b6ec51334d83a7b7275b263d33b4a273ebddefa716e4b9eb73cceb43fd8e

See more details on using hashes here.

File details

Details for the file mta_subway-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: mta_subway-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for mta_subway-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a2e2381421960124ac649836a5e24c61784d18a8d0bca87ad8194c0151824d9
MD5 5dc3f08df35db96961824f3de2e1488d
BLAKE2b-256 cfbd74cf4851a27801b532f6850a2dfacb8c3be488267c2d7d77c7ece27c7058

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page