Skip to main content

MCP Server for Swiss Federal Railways (SBB) open data via data.sbb.ch

Project description

๐Ÿš† sbb-opendata-mcp

๐Ÿ‡จ๐Ÿ‡ญ Part of the Swiss Public Data MCP Portfolio

PyPI License: MIT Python 3.11+ MCP Data Source CI

MCP server connecting AI models to Swiss Federal Railways (SBB) open data โ€“ passenger frequency, live rail disruptions, infrastructure & real-estate projects, train counts, platform data, rolling stock and station search from data.sbb.ch. No API key required.

๐Ÿ‡ฉ๐Ÿ‡ช Deutsche Version

Demo

Demo: Claude queries SBB passenger frequency


Overview

sbb-opendata-mcp gives AI assistants like Claude direct access to public SBB data โ€“ no copy-pasting or manual API calls. A question like "How many passengers passed through Zรผrich HB every day in 2024?" is answered with real measured data.

The SBB Open Data portal speaks the OpenDataSoft REST API (v2.1). This server translates it into clean Markdown and JSON for the AI model, and adds MCP structuredContent alongside the human-readable text so programmatic clients can consume the underlying records without re-parsing. The server is model-agnostic and works with any MCP-compatible client.

Anchor demo query: "Compare Zรผrich HB, Bern and Basel SBB by passenger frequency and platform capacity." โ†’ More use cases by audience โ†’


Features

  • ๐Ÿ“Š Passenger frequency โ€“ boardings/alightings by station and year (daily averages)
  • ๐Ÿšจ Live rail disruptions โ€“ traffic messages, updated every 5 minutes
  • ๐Ÿ—๏ธ Infrastructure projects โ€“ station and line construction
  • ๐Ÿข Real-estate projects โ€“ SBB property development (daily updates)
  • ๐Ÿš† Trains per segment โ€“ train counts per route (SBB, BLS, SOB โ€ฆ)
  • ๐Ÿ›ค๏ธ Platform data โ€“ length, type, area, step-free access
  • ๐Ÿšƒ Rolling stock โ€“ capacity and year built
  • ๐Ÿ” Station comparison โ€“ up to 10 stations across multiple datasets
  • ๐Ÿ” Stop search โ€“ Swiss DiDok register (all of Switzerland)
  • ๐Ÿ“ฆ Dataset catalogue โ€“ list all ~89 SBB open datasets
  • ๐Ÿ”‘ No API key โ€“ all data is public and free to use
  • โ˜๏ธ Dual transport โ€“ stdio for Claude Desktop, Streamable HTTP for cloud deployment

Prerequisites

  • Python 3.11+
  • No API key โ€” all data comes from the public data.sbb.ch portal

Install uv (recommended):

curl -LsSf https://astral.sh/uv/install.sh | sh

Installation

From PyPI:

pip install sbb-opendata-mcp

Or with uvx (no permanent installation):

uvx sbb-opendata-mcp

For local development, install from a clone in editable mode:

git clone https://github.com/malkreide/sbb-opendata-mcp.git
cd sbb-opendata-mcp
pip install -e ".[dev]"

Quickstart

# Start the server (stdio mode for Claude Desktop)
sbb-opendata-mcp

Try it immediately in Claude Desktop:

"How many people boarded at Zรผrich HB daily in 2024?" "Are there any current disruptions on the Swiss rail network?"


Configuration

Environment Variables

The server needs no configuration to run over stdio. The variables below tune the optional Streamable HTTP transport, logging and observability.

Variable Effect Default
MCP_HOST Bind host for the HTTP transport. Keep 127.0.0.1 locally; only bind 0.0.0.0 inside a controlled container/cloud environment. 127.0.0.1
MCP_PORT Port for the HTTP transport. 8000
MCP_ALLOWED_HOSTS Comma-separated host allow-list for DNS-rebinding protection (e.g. your-app.onrender.com,your-app.onrender.com:*). localhost only
MCP_ALLOWED_ORIGINS Comma-separated browser-origin allow-list (e.g. https://your-app.onrender.com). (none)
LOG_LEVEL Log verbosity (DEBUG/INFO/WARNING/โ€ฆ). INFO
LOG_FORMAT json for structured logs; anything else for human-readable text. Always written to stderr. text

๐Ÿ”’ DNS-rebinding / Origin protection is always on; localhost is allow-listed so local HTTP development works out of the box. Logs go to stderr โ€” stdout is reserved for the stdio JSON-RPC channel.

Claude Desktop Configuration

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

Config file locations:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Restart Claude Desktop โ€” the server is downloaded automatically on first use.

Other MCP Clients

Works with Cursor, Windsurf, VS Code + Continue, LibreChat, Cline and self-hosted models via mcp-proxy โ€” same configuration as above.

Cloud Deployment (Streamable HTTP)

For use via claude.ai in the browser or remote servers (e.g. Render.com). The cloud transport is Streamable HTTP (endpoint /mcp).

Docker (recommended):

# Build + run with explicit resource limits (see docker-compose.yml)
docker compose up --build
# โ†’ http://127.0.0.1:8000/mcp

The image is a multi-stage build running as a non-root user; docker-compose.yml adds read_only, no-new-privileges and memory/CPU/PID limits.

Manual / Render.com:

pip install -e .

# Bind publicly (behind a rate-limiting reverse proxy) and configure
# DNS-rebinding / Origin protection for your hostname:
export MCP_HOST=0.0.0.0
export MCP_ALLOWED_HOSTS="your-app.onrender.com,your-app.onrender.com:*"
export MCP_ALLOWED_ORIGINS="https://your-app.onrender.com"
python -m sbb_opendata_mcp.server --http --port 8000

โš ๏ธ Binding: In a network transport the server binds to 127.0.0.1 by default so a locally started server is not exposed to your whole network. Set MCP_HOST=0.0.0.0 only in a container/cloud environment where binding to all interfaces is intended (the Docker image does this for you), and place the server behind a reverse proxy that enforces rate limiting (and authentication, if the endpoint should not be public). See SECURITY.md.


Available Tools

Tool Description Data Update
sbb_get_passenger_frequency Boardings/alightings by station and year (daily avg.) Annual
sbb_get_rail_disruptions Live rail traffic messages Every 5 min.
sbb_get_infrastructure_construction_projects Infrastructure construction (stations, lines) Ongoing
sbb_get_real_estate_projects SBB real estate development projects Daily
sbb_get_trains_per_segment Train counts per route segment (SBB, BLS, SOB โ€ฆ) Annual
sbb_get_platform_data Platform data (length, type, area) Ongoing
sbb_get_rolling_stock Rolling stock (capacity, year built) Ongoing
sbb_compare_stations Compare up to 10 stations (multi-dataset) โ€“
sbb_search_stations Search stops (Swiss DiDok register, all CH) Ongoing
sbb_list_datasets List all ~89 SBB open datasets โ€“

All tools support response_format: "markdown" (human-readable) and "json" (machine-readable), plus pagination. Every tool also returns MCP structuredContent (the underlying records/metadata) alongside the rendered text.

Example Use Cases

Query Tool
"How many people boarded at Zรผrich HB daily in 2024?" sbb_get_passenger_frequency
"Are there any current disruptions on the Swiss rail network?" sbb_get_rail_disruptions
"Compare Zรผrich HB, Bern and Basel SBB" sbb_compare_stations
"Which SBB construction projects are active in Zรผrich?" sbb_get_infrastructure_construction_projects
"How many trains run yearly on the Zรผrichโ€“Winterthur route?" sbb_get_trains_per_segment
"Which stops exist in Wรคdenswil?" sbb_search_stations

โ†’ More use cases by audience


Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Claude / AI   โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚   SBB Open Data MCP       โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚       data.sbb.ch        โ”‚
โ”‚   (MCP Host)    โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚   (MCP Server)            โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚                           โ”‚     โ”‚  OpenDataSoft REST v2.1  โ”‚
                        โ”‚  10 Tools                 โ”‚     โ”‚  (public, no API key)    โ”‚
                        โ”‚  Stdio | Streamable HTTP  โ”‚     โ”‚                          โ”‚
                        โ”‚                           โ”‚     โ”‚  passagierfrequenz       โ”‚
                        โ”‚  Shared httpx client      โ”‚     โ”‚  rail-traffic-information โ”‚
                        โ”‚  (pooled, lifespan-managed)โ”‚    โ”‚  construction-projects   โ”‚
                        โ”‚  ODSQL escaping + Pydantic โ”‚     โ”‚  perron ยท rollmaterial   โ”‚
                        โ”‚  validation               โ”‚     โ”‚  zugzahlen ยท dienststellenโ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Project Structure

sbb-opendata-mcp/
โ”œโ”€โ”€ src/sbb_opendata_mcp/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ server.py                   # FastMCP server, all 10 tool definitions
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_server.py              # Unit + live API smoke tests
โ”œโ”€โ”€ audits/                         # MCP best-practice audit evidence
โ”œโ”€โ”€ docs/assets/demo.svg            # README demo asset
โ”œโ”€โ”€ .github/workflows/ci.yml        # GitHub Actions (Python 3.11/3.12/3.13)
โ”œโ”€โ”€ Dockerfile                      # Multi-stage, non-root runtime image
โ”œโ”€โ”€ docker-compose.yml              # Local run with resource limits
โ”œโ”€โ”€ claude_desktop_config.json      # Example Claude Desktop config
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ CHANGELOG.md
โ”œโ”€โ”€ CONTRIBUTING.md
โ”œโ”€โ”€ SECURITY.md
โ”œโ”€โ”€ EXAMPLES.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md                       # This file (English)
โ””โ”€โ”€ README.de.md                    # German version

Safety & Limits

  • Read-only: All 10 tools perform read-only HTTP GET requests โ€” no data is written, modified, or deleted upstream.
  • No personal data: Queries are transient and not stored. The portal returns aggregated statistics, infrastructure and operational metadata. No PII is processed or retained.
  • No API key: Data is public and free. There is no authentication and no secret to manage.
  • Injection-hardened: year/canton are regex-validated and every value interpolated into an ODSQL where clause is escaped via a central helper.
  • Data freshness: Real-time tools (disruptions) reflect the upstream source at query time; statistical datasets update annually/daily (see the tool table).
  • Terms of service: Data is published under the data.sbb.ch licence (NonCommercialAllowed-CommercialAllowed-ReferenceRequired).
  • No guarantees: This server is a community project, not affiliated with SBB. Availability depends on the upstream API.

See SECURITY.md for the full security posture.


Known Limitations

  • Passenger frequency: Updated annually; the latest full year may lag by some months.
  • Rail disruptions: Returns all current Swiss rail messages โ†’ use limit and pagination.
  • Trains per segment: Counts are yearly aggregates, not real-time.
  • Station search: Covers the full Swiss DiDok register (all operators), not just SBB.
  • No rate limiting of its own: Place a public HTTP deployment behind a rate-limiting reverse proxy.

Testing

No API key is required.

# Unit tests (no network required)
PYTHONPATH=src pytest tests/ -m "not live"

# Live API smoke tests (require network access to data.sbb.ch)
PYTHONPATH=src pytest tests/ -m live

Changelog

See CHANGELOG.md


Contributing

See CONTRIBUTING.md


License

MIT License โ€” see LICENSE


Author

Hayal Oezkan ยท github.com/malkreide


Credits & Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sbb_opendata_mcp-0.3.3.tar.gz (104.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sbb_opendata_mcp-0.3.3-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file sbb_opendata_mcp-0.3.3.tar.gz.

File metadata

  • Download URL: sbb_opendata_mcp-0.3.3.tar.gz
  • Upload date:
  • Size: 104.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sbb_opendata_mcp-0.3.3.tar.gz
Algorithm Hash digest
SHA256 ee5a8a149edfaf0051a0c6b1d2c9447d023215dd2708bc51a454f62a122b05bd
MD5 237b74923b441370b0875b01882e44e3
BLAKE2b-256 1db9f69f6f9b406e1fb4b28e1ab357c434266e607e88c10ac10b731faccc94d8

See more details on using hashes here.

File details

Details for the file sbb_opendata_mcp-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for sbb_opendata_mcp-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b49de96477aa357bcef9784eb7274dd9700bb9e45b2e20e4029be076eb7c92f6
MD5 4993bfc601e941f7318d08a0dc85434d
BLAKE2b-256 5ee4591f9416f11a3fee773f0f3ea25d0d86666016f4df844b11ce2ea7ca72e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page