Skip to main content
Glama
README.md
# pyviaggiatreno

Python library + MCP server for ViaggiaTreno APIs (Italian Railways)

## ๐Ÿ“ฆ Packages

### `viaggiatreno` - Core Library โญ

Pure Python client library for ViaggiaTreno APIs. No MCP dependencies.

```bash
pip install viaggiatreno
```

**Usage**:
```python
from viaggiatreno import ViaggiaTrenoClient
import asyncio


async def main():
    client = ViaggiaTrenoClient()

    # Get station suggestions
    stations = await client.autocompleta_stazione("milano")
    print(stations)

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


asyncio.run(main())
```

### `viaggiatreno-mcp` - MCP Server

Model Context Protocol server for Claude, pi, and other AI agents.

```bash
pip install viaggiatreno-mcp
```

**Start server**:
```bash
viaggiatreno-mcp
```

Configure in Claude, pi, or other AI tools to use these tools:
- `autocompleta_stazione` - Station autocomplete
- `cerca_stazione` - Search stations
- `dettaglio_stazione` - Station details
- `partenze` - Departures board
- `arrivi` - Arrivals board
- `stato_treno` - Train status & full route
- E altri...

---

## โœจ Features

### Core Library
- ๐Ÿš„ Async/await HTTP client with `httpx`
- ๐Ÿ“Š Strongly-typed Pydantic models
- ๐Ÿ”„ Intelligent parsing (JSON, plain text, HTML fragments)
- โšก Handles ViaggiaTreno quirks (204 No Content, JS date formats, etc.)
- ๐Ÿ‡ฎ๐Ÿ‡น Full coverage of Italian railways

### MCP Server
- ๐Ÿค– AI-ready tool registration
- ๐Ÿ”— Automatic parameter resolution (derive trip details from train number)
- ๐Ÿ“ฑ Works with Claude, pi, and other MCP clients
- ๐Ÿงต Async concurrent tool execution

---

## ๐Ÿ“š Documentation

See main documentation files:
- [VIAGGIATRENO.md](./VIAGGIATRENO.md) - API reference (20+ endpoints)
- [TRENORD.md](./TRENORD.md) - GTFS Trenord structure
- [ENTITA.md](./ENTITA.md) - Data model & schema
- [GLOSSARIO.md](./GLOSSARIO.md) - Railway terminology
- [COPERTURA_GEOGRAFICA.md](./COPERTURA_GEOGRAFICA.md) - Geographic coverage

---

## ๐Ÿš€ Quick Start

### Installation

```bash
# Core library only
pip install viaggiatreno

# With MCP server
pip install viaggiatreno-mcp
```

### Using the Library

```python
from viaggiatreno import ViaggiaTrenoClient
from datetime import datetime

client = ViaggiaTrenoClient()

# Get train departures from Milano Centrale
trains = await client.partenze("S01700", datetime.now())

for train in trains:
    print(
        f"{train.numeroTreno}: {train.destinazione} at {train.compOrarioPartenza} (delay: {train.ritardo}m)"
    )
```

### Using MCP Server

1. Install: `pip install viaggiatreno-mcp`
2. Start: `viaggiatreno-mcp`
3. Configure your AI tool (Claude, pi, etc.) to connect to the MCP server

---

## ๐Ÿ› ๏ธ Development

```bash
# Clone repo
git clone https://github.com/matteoredaelli/pyviaggiatreno.git
cd pyviaggiatreno

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

# Run tests
pytest

# Run MCP server
python -m viaggiatreno_mcp.server
```

---

## ๐Ÿ“„ License

MIT

---

## ๐Ÿค Contributing

Contributions welcome! Please:
1. Fork the repo
2. Create a feature branch
3. Submit a PR

---

## ๐Ÿ“ž Support

- ๐Ÿ› [Bug reports](https://github.com/matteoredaelli/pyviaggiatreno/issues)
- ๐Ÿ’ฌ [Discussions](https://github.com/matteoredaelli/pyviaggiatreno/discussions)
- ๐Ÿ“ง Email: matteo.redaelli@gmail.com

---

## ๐Ÿ”„ GitHub Actions Workflow

La repo ha 3 workflow automatici:

### 1. **Tests & Linting** (`tests.yml`)
- โœ… Ruff linting
- โœ… Pyright type checking  
- โœ… Pytest unit tests
- ๐Ÿ”„ Trigger: Ogni commit su `main`/`dev` e PR
- ๐ŸŽฏ Purpose: Verifica codice quality

### 2. **Test Build** (`test-build.yml`)
- โœ… Build entrambi i pacchetti
- โœ… Testa installazione wheel
- ๐Ÿ”„ Trigger: Commit su `packages/` o workflow_dispatch
- ๐ŸŽฏ Purpose: Verifica che la build funzioni (senza pubblicare)

### 3. **Publish to PyPI** (`publish-pypi.yml`)
- โœ… Build entrambi i pacchetti
- โœ… Pubblica su PyPI
- โœ… Crea GitHub Release con artifacts
- ๐Ÿ”„ Trigger: Tag push (e.g. `git push origin v0.1.0`)
- ๐ŸŽฏ Purpose: Pubblica su PyPI (una sola volta)

**Vedi [RELEASE.md](./RELEASE.md) per istruzioni dettagliate.**

---

## ๐Ÿ“ฆ Publishing to PyPI

### Prerequisites
1. PyPI account (https://pypi.org)
2. Generate token from PyPI
3. Add token to GitHub Secrets: `PYPI_TOKEN`

### Release Process
```bash
# 1. Commit & push
git add .
git commit -m "feature: add X"
git push origin main

# 2. Create tag (triggers publish workflow)
git tag v0.2.0
git push origin v0.2.0

# 3. GitHub Actions automatically publishes both packages
```

Both `viaggiatreno` and `viaggiatreno-mcp` will be published to PyPI with the same version.

---

TDQS

B3.4/5.0

Scored across 19 tools

Disambiguation3/5

Most tools target distinct resources (departures vs arrivals, status vs itinerary, three infomobility detail levels), but there is real overlap: autocompleta_stazione and cerca_stazione both search stations by prefix and differ mainly in output shape, and autocompleta_stazione_nts overlaps the former. The descriptions mitigate this somewhat by explicitly noting output differences, but an agent could still easily misselect.

Naming Consistency4/5

All names are lowercase snake_case Italian, which is highly consistent. However the convention mixes verb-led forms (autocompleta_stazione, cerca_numero_treno, elenco_tratte_ferroviarie) with pure noun phrases (tabellone_partenze, stato_treno, meteo_stazioni), so the pattern is readable but not strictly uniform.

Tool Count4/5

19 tools is on the heavy side but justified by the broad domain: station lookup, boards, train status/tracking, network segments, statistics, infomobility, weather, and localization. Each tool maps to a genuinely distinct API capability, so few feel redundant.

Completeness4/5

The surface is comprehensive for a read-only public rail-data service: station search/detail/region listing, real-time arrivals and departures, train status and itinerary, network segments and occupancy, plus infomobility and weather. No obvious lifecycle gaps given it is inherently read-only, though nothing covers tickets or personal/favorites data.

Maintenance

ActivityMaintained
ResponsivenessNo issues