ViaggiaTreno MCP Server
# 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
Il modo consigliato รจ `uvx` (richiede [uv](https://docs.astral.sh/uv/)), che
esegue il server in un ambiente isolato senza installarlo a mano.
**Claude Desktop** (`~/Library/Application Support/Claude/claude_desktop_config.json`
su macOS, `%APPDATA%\Claude\claude_desktop_config.json` su Windows):
```json
{
"mcpServers": {
"viaggiatreno": {
"command": "uvx",
"args": ["viaggiatreno-mcp"]
}
}
}
```
**Kiro** (`~/.kiro/settings/mcp.json`) e altri client con lo stesso formato
(Cursor, Windsurf, VS Code MCP):
```json
{
"mcpServers": {
"viaggiatreno": {
"command": "uvx",
"args": ["viaggiatreno-mcp"],
"disabled": false,
"autoApprove": []
}
}
}
```
In alternativa, se hai giร fatto `pip install viaggiatreno-mcp`, usa
`"command": "viaggiatreno-mcp"` (o `"command": "python", "args": ["-m", "viaggiatreno_mcp"]`).
Riavvia il client dopo aver salvato la configurazione.
Dettagli completi in [packages/viaggiatreno_mcp/README.md](./packages/viaggiatreno_mcp/README.md).
---
## ๐ ๏ธ 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
Il publish usa **Trusted Publishing (OIDC)** โ nessun token da gestire. Configurazione una tantum:
1. PyPI account (https://pypi.org)
2. Per **ciascun** pacchetto (`viaggiatreno`, `viaggiatreno-mcp`), aggiungi un Trusted Publisher:
owner `matteoredaelli`, repo `pyviaggiatreno`, workflow `publish-pypi.yml`, environment `pypi`
3. Crea l'environment `pypi` in GitHub (Settings โ Environments)
### 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
Scored across 21 tools
Most tools target distinct resources or actions, but station lookup overlaps: autocompleta_stazione and cerca_stazione both search stations by prefix, and autocompleta_stazione_nts adds a related NTS variant. The infomobility trio also differs mainly by detail level, so descriptions help but boundaries are not perfectly sharp.
All names use Italian lowercase snake_case, so casing and delimiters are predictable throughout. However, most are noun phrases while a few are verb-first (cerca_, autocompleta_), so the verb_noun pattern is not fully uniform.
21 tools is above the typical 3-15 sweet spot and feels heavy for an MCP surface. The breadth is partly justified by the domain (boards, stations, routes, alerts, weather, dictionary), but several endpoints could likely be consolidated.
Coverage is broad: real-time departure/arrival boards, train status, route segments, station metadata, infomobility, weather and utility endpoints. The main gap is journey planning beyond direct trains plus some overlapping station-search behavior, but core ViaggiaTreno workflows are covered.