<?xml version="1.0" encoding="utf-8" standalone="yes"?><?xml-stylesheet href="/feed_style.xsl" type="text/xsl"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="https://www.rssboard.org/media-rss">
  <channel>
    <title>trains on MR70</title>
    <link>https://www.mr70.eu/tags/trains/</link>
    <description>Recent content in trains on MR70</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>MR70 - [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).</copyright>
    <lastBuildDate>Sun, 18 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://www.mr70.eu/tags/trains/index.xml" rel="self" type="application/rss+xml" /><icon>https://www.mr70.eu/logo.svg</icon>
    
    
    <item>
      <title>pyviaggiatreno - a Python library and MCP server for Italian Railways</title>
      <link>https://www.mr70.eu/posts/pyviaggiatreno-python-library-mcp-server/</link>
      <pubDate>Sun, 18 Oct 2026 00:00:00 +0000</pubDate>
      
      <guid>https://www.mr70.eu/posts/pyviaggiatreno-python-library-mcp-server/</guid>
      <description><![CDATA[<h2 id="pyviaggiatreno">pyviaggiatreno</h2>
<p><a href="https://github.com/matteoredaelli/pyviaggiatreno">pyviaggiatreno</a> is a monorepo that ships <strong>two complementary packages</strong> for working with the <a href="http://www.viaggiatreno.it">ViaggiaTreno</a> APIs — the real-time information service of the Italian Railways (Trenitalia / RFI):</p>
<ul>
<li><code>viaggiatreno</code> — a pure Python client library, with no MCP dependencies</li>
<li><code>viaggiatreno-mcp</code> — a <a href="https://modelcontextprotocol.io">Model Context Protocol</a> server that exposes those APIs as tools for AI agents</li>
</ul>
<p>The two live in separate subfolders under <code>packages/</code>, 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.</p>
<h2 id="the-core-library-viaggiatreno">The core library: <code>viaggiatreno</code></h2>
<p>A pure Python client for the ViaggiaTreno APIs. Install it with:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>pip install viaggiatreno
</span></span></code></pre></div><p>Features:</p>
<ul>
<li>🚄 Async/await HTTP client built on <code>httpx</code></li>
<li>📊 Strongly-typed Pydantic models</li>
<li>🔄 Intelligent parsing (JSON, plain text, HTML fragments)</li>
<li>⚡ Handles ViaggiaTreno quirks (204 No Content, JavaScript date formats, etc.)</li>
<li>🇮🇹 Full coverage of the Italian railways endpoints (20+)</li>
</ul>
<p>A quick example — the departures board from Milano Centrale (station code <code>S01700</code>):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#ff79c6">from</span> viaggiatreno <span style="color:#ff79c6">import</span> ViaggiaTrenoClient
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">from</span> datetime <span style="color:#ff79c6">import</span> datetime
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">import</span> asyncio
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#ff79c6">async</span> <span style="color:#ff79c6">def</span> <span style="color:#50fa7b">main</span>():
</span></span><span style="display:flex;"><span>    client <span style="color:#ff79c6">=</span> ViaggiaTrenoClient()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#6272a4"># Station autocomplete</span>
</span></span><span style="display:flex;"><span>    stations <span style="color:#ff79c6">=</span> <span style="color:#ff79c6">await</span> client<span style="color:#ff79c6">.</span>autocompleta_stazione(<span style="color:#f1fa8c">&#34;milano&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#8be9fd;font-style:italic">print</span>(stations)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#6272a4"># Departures board</span>
</span></span><span style="display:flex;"><span>    departures <span style="color:#ff79c6">=</span> <span style="color:#ff79c6">await</span> client<span style="color:#ff79c6">.</span>partenze(<span style="color:#f1fa8c">&#34;S01700&#34;</span>, datetime<span style="color:#ff79c6">.</span>now())
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">for</span> train <span style="color:#ff79c6">in</span> departures:
</span></span><span style="display:flex;"><span>        <span style="color:#8be9fd;font-style:italic">print</span>(<span style="color:#f1fa8c">f</span><span style="color:#f1fa8c">&#34;</span><span style="color:#f1fa8c">{</span>train<span style="color:#ff79c6">.</span>numeroTreno<span style="color:#f1fa8c">}</span><span style="color:#f1fa8c"> → </span><span style="color:#f1fa8c">{</span>train<span style="color:#ff79c6">.</span>destinazione<span style="color:#f1fa8c">}</span><span style="color:#f1fa8c"> (</span><span style="color:#f1fa8c">{</span>train<span style="color:#ff79c6">.</span>ritardo<span style="color:#f1fa8c">}</span><span style="color:#f1fa8c"> min delay)&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>asyncio<span style="color:#ff79c6">.</span>run(main())
</span></span></code></pre></div><h2 id="the-mcp-server-viaggiatreno-mcp">The MCP server: <code>viaggiatreno-mcp</code></h2>
<p>An MCP server that makes those same APIs available to Claude, Kiro and any other MCP-compatible agent.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>pip install viaggiatreno-mcp
</span></span><span style="display:flex;"><span>viaggiatreno-mcp
</span></span></code></pre></div><p>The tools it exposes:</p>
<table>
<thead>
<tr>
<th>Tool</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>autocompleta_stazione</code></td>
<td>Suggest stations by name prefix; returns the extended name and station code (e.g. <code>S01700</code> for Milano Centrale)</td>
</tr>
<tr>
<td><code>cerca_stazione</code></td>
<td>Search stations by prefix; returns structured registry data (id, long/short name, city)</td>
</tr>
<tr>
<td><code>cerca_numero_treno</code></td>
<td>Find active runs for a train number; returns the identifiers needed for status/itinerary tools</td>
</tr>
<tr>
<td><code>dettaglio_stazione</code></td>
<td>Full station details: geographic coordinates, city, region and type</td>
</tr>
<tr>
<td><code>codice_regione_stazione</code></td>
<td>The region code associated with a station</td>
</tr>
<tr>
<td><code>elenco_stazioni_regione</code></td>
<td>All stations of a region, with their coordinates</td>
</tr>
<tr>
<td><code>tabellone_partenze</code></td>
<td>Real-time departures board: destination, scheduled time, delay, platform, status</td>
</tr>
<tr>
<td><code>tabellone_arrivi</code></td>
<td>Real-time arrivals board: origin, scheduled time, delay, platform</td>
</tr>
<tr>
<td><code>stato_treno</code></td>
<td>Live train status (current delay, last detection, running state) and the full list of stops</td>
</tr>
<tr>
<td><code>itinerario_treno_canvas</code></td>
<td>Tree view of the train&rsquo;s itinerary, stop by stop (departed / arrived / stopped)</td>
</tr>
<tr>
<td><code>elenco_tratte_ferroviarie</code></td>
<td>Segments of the national network and whether each is occupied by a train</td>
</tr>
<tr>
<td><code>treni_su_tratta</code></td>
<td>Trains currently running on a specific network segment</td>
</tr>
<tr>
<td><code>statistiche_servizio</code></td>
<td>Network statistics: trains scheduled today and trains currently running</td>
</tr>
<tr>
<td><code>meteo_stazioni</code></td>
<td>Weather forecast for the stations of a region</td>
</tr>
<tr>
<td><code>notizie_infomobilita</code></td>
<td>Detailed real-time mobility notices (disruptions, delays, strikes, planned works)</td>
</tr>
<tr>
<td><code>titoli_infomobilita</code></td>
<td>Synthetic titles of the mobility notices (compact overview)</td>
</tr>
<tr>
<td><code>ticker_infomobilita</code></td>
<td>Short scrolling notices from the ViaggiaTreno info strip</td>
</tr>
<tr>
<td><code>dizionario_lingua</code></td>
<td>Translations of the ViaggiaTreno UI strings (it, en, de, fr, &hellip;)</td>
</tr>
</tbody>
</table>
<p>Beyond simply wrapping the endpoints, the server adds some AI-friendly conveniences:</p>
<ul>
<li>🤖 AI-ready tool registration</li>
<li>🔗 Automatic parameter resolution (e.g. derive trip details from just a train number)</li>
<li>🧵 Async, concurrent tool execution</li>
</ul>
<h2 id="registering-the-mcp-server">Registering the MCP server</h2>
<p>The recommended way is <code>uvx</code> (which requires <a href="https://docs.astral.sh/uv/">uv</a>): it runs the server in an isolated environment with no manual install.</p>
<p><strong>Claude Desktop</strong> (<code>~/Library/Application Support/Claude/claude_desktop_config.json</code> on macOS):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>  <span style="color:#ff79c6">&#34;mcpServers&#34;</span>: {
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">&#34;viaggiatreno&#34;</span>: {
</span></span><span style="display:flex;"><span>      <span style="color:#ff79c6">&#34;command&#34;</span>: <span style="color:#f1fa8c">&#34;uvx&#34;</span>,
</span></span><span style="display:flex;"><span>      <span style="color:#ff79c6">&#34;args&#34;</span>: [<span style="color:#f1fa8c">&#34;viaggiatreno-mcp&#34;</span>]
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><strong>Kiro</strong> (<code>~/.kiro/settings/mcp.json</code>) and other clients that share the same format (Cursor, Windsurf, VS Code MCP):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>  <span style="color:#ff79c6">&#34;mcpServers&#34;</span>: {
</span></span><span style="display:flex;"><span>    <span style="color:#ff79c6">&#34;viaggiatreno&#34;</span>: {
</span></span><span style="display:flex;"><span>      <span style="color:#ff79c6">&#34;command&#34;</span>: <span style="color:#f1fa8c">&#34;uvx&#34;</span>,
</span></span><span style="display:flex;"><span>      <span style="color:#ff79c6">&#34;args&#34;</span>: [<span style="color:#f1fa8c">&#34;viaggiatreno-mcp&#34;</span>],
</span></span><span style="display:flex;"><span>      <span style="color:#ff79c6">&#34;disabled&#34;</span>: <span style="color:#ff79c6">false</span>,
</span></span><span style="display:flex;"><span>      <span style="color:#ff79c6">&#34;autoApprove&#34;</span>: []
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>If you already ran <code>pip install viaggiatreno-mcp</code>, you can use <code>&quot;command&quot;: &quot;viaggiatreno-mcp&quot;</code> instead. Remember to restart the client after saving the configuration.</p>
<h2 id="development">Development</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git clone https://github.com/matteoredaelli/pyviaggiatreno.git
</span></span><span style="display:flex;"><span><span style="color:#8be9fd;font-style:italic">cd</span> pyviaggiatreno
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#6272a4"># Install dev dependencies</span>
</span></span><span style="display:flex;"><span>uv pip install -e <span style="color:#f1fa8c">&#34;.[dev]&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#6272a4"># Run tests</span>
</span></span><span style="display:flex;"><span>pytest
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#6272a4"># Run the MCP server</span>
</span></span><span style="display:flex;"><span>python -m viaggiatreno_mcp.server
</span></span></code></pre></div><p>Both packages are published to PyPI from the same monorepo via GitHub Actions, using Trusted Publishing (OIDC) triggered on a version tag.</p>
<h2 id="why-it-is-useful">Why it is useful</h2>
<p>With <code>viaggiatreno-mcp</code> connected, you can ask an AI agent things like <em>&ldquo;when is the next train from Milano Centrale to Roma Termini and is it on time?&rdquo;</em> or <em>&ldquo;what&rsquo;s the status of train 9611?&rdquo;</em> and get an answer pulled live from ViaggiaTreno. And if you want to build your own app or script, the <code>viaggiatreno</code> library gives you the same data with clean, typed Pydantic models.</p>
<p>The project is MIT licensed and available on <a href="https://github.com/matteoredaelli/pyviaggiatreno">GitHub</a>.</p>
]]></description>
      
    </item>
    
    
  </channel>
</rss>
