rail-mcp
by dalarso
README.md
# rail-mcp
An [MCP](https://modelcontextprotocol.io) server for European trains: plan journeys, read live departure
boards and follow a train's run, with delays, cancellations and platform changes where operators publish
them. It works with Claude (as a custom connector or in Claude Code) and any other MCP client.
All data comes from [Transitous](https://transitous.org), a free, open, community-run journey planner
built on operators' open data. Coverage includes all of Germany (DB and regional trains), the Netherlands
(NS), Switzerland (SBB), France (SNCF, TGV, Eurostar), Italy (Trenitalia) and most of the rest of Europe,
cross-border journeys included.
Fares and network-wide disruptions come from operators' own APIs where you have a key: so far NS (the
Netherlands), see [Operator extras](#operator-extras). Otherwise there are **no fares or bookings**:
European rail ticketing isn't open data. Results name the operator, so you can book on its own site.
## Tools
| Tool | What it does |
|---|---|
| `plan_journey` | Journeys from A to B: times, changes, trains, platforms, live delays and cancellations. Departure or arrival time, a via station, max changes, bikes, trains only or all modes, paging to later or earlier journeys. |
| `departures` | A station's departure or arrival board, with live delays, platform changes, cancellations and service alerts. Optionally long-distance trains only. |
| `train_details` | Every stop of one train run (from a `trip_id` in a journey or on a board), with live times and platforms. |
| `find_stations` | Stations matching a name, with ids for the other tools. |
| `fares` | With an NS key: NS ticket prices within the Netherlands, per adult, single and return, first and second class, with the 20% and 40% subscription discounts, and the children's Railrunner fare. |
| `disruptions` | With an NS key: NS's current disruptions, major incidents and engineering works, for the whole network or one station. |
Places are station names (local spellings work best: `Köln Hbf`, `Amsterdam Centraal`, `Zürich HB`,
`Paris Gare de Lyon`, `Milano Centrale`), station ids from `find_stations`, or `latitude,longitude`.
Times are each station's local time with its UTC offset. A time sent without an offset is read as the
station's local time.
Some things the answers handle that raw timetable data doesn't:
- Big stations are often split across feeds (Paris Gare de Lyon is one stop for RER and metro, another
for TGVs), so boards take in every stop within 150 m. A train that two feeds both list is shown once,
even when each gives its own end of the line (NS's ICE 143 to Hengelo, DB's to Hannover Hbf): the other
is kept in `to_also_given_as`.
- A cross-border train that two operators both list (EC Zürich–Milano: SBB to Chiasso, Trenitalia after)
comes back from the planner as two legs. The second is marked `same_train_as_previous_leg`, so the
traveller isn't told to change trains.
- A delay is given only when there's live data (`live: true`). Without it, times are the timetable's.
## Using Transitous fairly
Transitous is run by volunteers, free for open-source, non-commercial use. This server follows its
[usage policy](https://transitous.org/api/):
- Every request sends `User-Agent: rail-mcp/<version> (+<contact>)`. Set `RAIL_MCP_CONTACT` to your own
URL or e-mail address when you run your own copy.
- Journey searches are capped at 300 a day by default (`RAIL_MCP_PLANS_PER_DAY`, at most 700). Transitous
asks to hear from clients above 720 a day.
- Station look-ups are cached for six hours.
- Every answer carries Transitous's attribution, and the instructions ask the model to credit it.
Data: Transitous and [its sources](https://transitous.org/sources/); map data © OpenStreetMap
contributors. If you use it a lot, consider [donating to Transitous](https://transitous.org/donate/).
## Operator extras
Each needs your own key and is off without it: its tools aren't even listed.
**NS (the Netherlands)** adds `fares` and `disruptions`. The key is free:
1. Make an account at the [NS API portal](https://apiportal.ns.nl) (choose *Externe bezoeker*).
2. Subscribe to the **Ns-App** product.
3. Copy the subscription's primary key from your profile into `NS_API_KEY`.
How it uses NS's API:
- Fares come from NS's journey planner, whose trips carry their whole fare table. NS's price endpoint
answered `500` to every request in November 2026, so the planner is used instead.
- The children's Railrunner fare comes from NS's older price list.
- Disruptions come in English where NS has a translation. Some advice texts are Dutch only.
- Data: NS (Nederlandse Spoorwegen).
Requests to operators' own APIs carry an ordinary desktop browser's headers (`identity.py`); Transitous
is the exception, as its policy asks.
## Running it
Python 3.12 or newer.
### In Claude Code (stdio)
```bash
claude mcp add trains -e RAIL_MCP_CONTACT=https://example.com/you -- uvx --from git+https://github.com/dalarso/rail-mcp rail-mcp --stdio
```
### As a remote connector (HTTP)
`rail-mcp` serves `POST /mcp` (Streamable HTTP with plain JSON answers, no sessions, both the 2026-07-28
and 2025-era protocol) and `GET /health`.
Without authentication it listens on loopback only. To publish it, put it behind a Cloudflare Access
application with Managed OAuth (so claude.ai can sign in) and set the three `ACCESS_*` variables. The server then checks Access's signed JWT
(`Cf-Access-Jwt-Assertion`) on every request: signature against the team's keys, the app's AUD, the
issuer, the time and the allowed e-mail. It also refuses any request carrying an `Origin` header, and
with `RAIL_MCP_HOST` set it answers that host name only.
## Configuration
| Variable | Default | Meaning |
|---|---|---|
| `RAIL_MCP_CONTACT` | this repository's URL | Contact in the User-Agent sent to Transitous: your URL or e-mail. |
| `RAIL_MCP_PLANS_PER_DAY` | `300` | Journey searches allowed in any 24 hours (1 to 700). |
| `RAIL_MCP_LANGUAGE` | `en` | Preferred language for alerts and names, where feeds have translations. |
| `RAIL_MCP_LISTEN`, `RAIL_MCP_PORT` | `127.0.0.1`, `8080` | Where the HTTP server listens. Not loopback requires Access. |
| `RAIL_MCP_HOST` | none | The public host name. When set, `/mcp` answers it only. |
| `ACCESS_TEAM_DOMAIN`, `ACCESS_AUD`, `ACCESS_EMAIL` | none | Cloudflare Access team (`<team>.cloudflareaccess.com`), the app's AUD tag, and the one e-mail allowed. All three or none. |
| `TRANSITOUS_URL` | `https://api.transitous.org` | The MOTIS 2 API to use (a self-hosted MOTIS works too). |
| `NS_API_KEY` | none | Your NS API portal key (Ns-App product). Turns on `fares` and `disruptions` for the Netherlands. |
## Development
```bash
PYTHONPATH=src python -m unittest discover -s tests
```
The tests need only `aiohttp`, `cryptography` and `tzdata`, and never call Transitous.
## License
MIT. The data belongs to its publishers. See Transitous's sources for each feed's licence.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues