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.Lockto 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09d6e49ae69ae6c5b823c3ee1182a2f4806a1490da25280e60a57856bc9e0903
|
|
| MD5 |
f18fef93d5d835c2a961a8477fe743e4
|
|
| BLAKE2b-256 |
0eb4b6ec51334d83a7b7275b263d33b4a273ebddefa716e4b9eb73cceb43fd8e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a2e2381421960124ac649836a5e24c61784d18a8d0bca87ad8194c0151824d9
|
|
| MD5 |
5dc3f08df35db96961824f3de2e1488d
|
|
| BLAKE2b-256 |
cfbd74cf4851a27801b532f6850a2dfacb8c3be488267c2d7d77c7ece27c7058
|