pyviaggiatreno - a Python library and MCP server for Italian Railways

๏—ฌ 2026-10-18

pyviaggiatreno

pyviaggiatreno is a monorepo that ships two complementary packages for working with the ViaggiaTreno APIs โ€” the real-time information service of the Italian Railways (Trenitalia / RFI):

  • viaggiatreno โ€” a pure Python client library, with no MCP dependencies
  • viaggiatreno-mcp โ€” a Model Context Protocol server that exposes those APIs as tools for AI agents

The two live in separate subfolders under packages/, so you can install only the library if you are building an app, or the MCP server if you want an AI agent to query train information in natural language.

The core library: viaggiatreno

A pure Python client for the ViaggiaTreno APIs. Install it with:

pip install viaggiatreno

Features:

  • ๐Ÿš„ Async/await HTTP client built on httpx
  • ๐Ÿ“Š Strongly-typed Pydantic models
  • ๐Ÿ”„ Intelligent parsing (JSON, plain text, HTML fragments)
  • โšก Handles ViaggiaTreno quirks (204 No Content, JavaScript date formats, etc.)
  • ๐Ÿ‡ฎ๐Ÿ‡น Full coverage of the Italian railways endpoints (20+)

A quick example โ€” the departures board from Milano Centrale (station code S01700):

from viaggiatreno import ViaggiaTrenoClient
from datetime import datetime
import asyncio

async def main():
    client = ViaggiaTrenoClient()

    # Station autocomplete
    stations = await client.autocompleta_stazione("milano")
    print(stations)

    # Departures board
    departures = await client.partenze("S01700", datetime.now())
    for train in departures:
        print(f"{train.numeroTreno} โ†’ {train.destinazione} ({train.ritardo} min delay)")

asyncio.run(main())

The MCP server: viaggiatreno-mcp

An MCP server that makes those same APIs available to Claude, Kiro and any other MCP-compatible agent.

pip install viaggiatreno-mcp
viaggiatreno-mcp

The tools it exposes:

Tool Description
autocompleta_stazione Suggest stations by name prefix; returns the extended name and station code (e.g. S01700 for Milano Centrale)
cerca_stazione Search stations by prefix; returns structured registry data (id, long/short name, city)
cerca_numero_treno Find active runs for a train number; returns the identifiers needed for status/itinerary tools
dettaglio_stazione Full station details: geographic coordinates, city, region and type
codice_regione_stazione The region code associated with a station
elenco_stazioni_regione All stations of a region, with their coordinates
tabellone_partenze Real-time departures board: destination, scheduled time, delay, platform, status
tabellone_arrivi Real-time arrivals board: origin, scheduled time, delay, platform
stato_treno Live train status (current delay, last detection, running state) and the full list of stops
itinerario_treno_canvas Tree view of the train’s itinerary, stop by stop (departed / arrived / stopped)
elenco_tratte_ferroviarie Segments of the national network and whether each is occupied by a train
treni_su_tratta Trains currently running on a specific network segment
statistiche_servizio Network statistics: trains scheduled today and trains currently running
meteo_stazioni Weather forecast for the stations of a region
notizie_infomobilita Detailed real-time mobility notices (disruptions, delays, strikes, planned works)
titoli_infomobilita Synthetic titles of the mobility notices (compact overview)
ticker_infomobilita Short scrolling notices from the ViaggiaTreno info strip
dizionario_lingua Translations of the ViaggiaTreno UI strings (it, en, de, fr, …)

Beyond simply wrapping the endpoints, the server adds some AI-friendly conveniences:

  • ๐Ÿค– AI-ready tool registration
  • ๐Ÿ”— Automatic parameter resolution (e.g. derive trip details from just a train number)
  • ๐Ÿงต Async, concurrent tool execution

Registering the MCP server

The recommended way is uvx (which requires uv): it runs the server in an isolated environment with no manual install.

Claude Desktop (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):

{
  "mcpServers": {
    "viaggiatreno": {
      "command": "uvx",
      "args": ["viaggiatreno-mcp"]
    }
  }
}

Kiro (~/.kiro/settings/mcp.json) and other clients that share the same format (Cursor, Windsurf, VS Code MCP):

{
  "mcpServers": {
    "viaggiatreno": {
      "command": "uvx",
      "args": ["viaggiatreno-mcp"],
      "disabled": false,
      "autoApprove": []
    }
  }
}

If you already ran pip install viaggiatreno-mcp, you can use "command": "viaggiatreno-mcp" instead. Remember to restart the client after saving the configuration.

Development

git clone https://github.com/matteoredaelli/pyviaggiatreno.git
cd pyviaggiatreno

# Install dev dependencies
uv pip install -e ".[dev]"

# Run tests
pytest

# Run the MCP server
python -m viaggiatreno_mcp.server

Both packages are published to PyPI from the same monorepo via GitHub Actions, using Trusted Publishing (OIDC) triggered on a version tag.

Why it is useful

With viaggiatreno-mcp connected, you can ask an AI agent things like “when is the next train from Milano Centrale to Roma Termini and is it on time?” or “what’s the status of train 9611?” and get an answer pulled live from ViaggiaTreno. And if you want to build your own app or script, the viaggiatreno library gives you the same data with clean, typed Pydantic models.

The project is MIT licensed and available on GitHub.


Enter your instance's address


More posts like this