Skip to main content

MCP Server for Canton Zurich legislation (ZH-Lex) โ€” full-text search, article extraction, and education law tools

Project description

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

โš–๏ธ openlex-mcp

Version License: MIT Python 3.11+ MCP No Auth Required

MCP Server for Canton Zurich legislation (ZH-Lex) โ€” full-text search, article extraction, and education law tools for ~970 cantonal laws

๐Ÿ‡ฉ๐Ÿ‡ช Deutsche Version

Demo: Claude searches Zurich education law via MCP tool call


Overview

openlex-mcp provides AI-native access to the entire legal collection of Canton Zurich (Zรผrcher Gesetzessammlung). It combines full-text data from HuggingFace with live metadata from the official zh.ch website, storing everything in a local SQLite database with FTS5 full-text indexing for sub-50ms search performance.

Source Data Access
HuggingFace 974 ZH laws โ€” full text (PDF extracts) Cached locally as SQLite + FTS5
zh.ch ZH-Lex Current metadata, PDF links, validity status Live HTTP requests

Built for the Schulamt (school department) of the City of Zurich, but covers all areas of cantonal law โ€” from tax law to building regulations.

Anchor demo query: "What does the Volksschulgesetz say about parental involvement? Show me Art. 55 VSG and find all articles that mention 'Elternrat'."


Features

  • โš–๏ธ 8 tools covering search, retrieval, article extraction, and cache management
  • ๐Ÿ” FTS5 full-text search across ~970 cantonal laws with BM25 ranking
  • ๐Ÿ“‘ Article extraction โ€” parse individual articles (Art. / ยง) with paragraph detection
  • ๐Ÿซ Education law shortcuts โ€” specialized search for LS 412.x series (Volksschulgesetz, Lehrpersonalverordnung, etc.)
  • ๐ŸŒ Live metadata from zh.ch for current validity status and PDF links
  • ๐Ÿ’พ Hybrid architecture โ€” cached full-text (HuggingFace) + live metadata (zh.ch)
  • ๐Ÿ”“ No API key required โ€” all data under open licenses (CC-BY-SA 4.0)
  • โ˜๏ธ Dual transport โ€” stdio (Claude Desktop) + Streamable HTTP (cloud)

Development Phase

Current phase: Phase 1 โ€” Read-Only. All tools are read-only (readOnlyHint: true); no writes to external systems. See ROADMAP.md for the phase plan and transition gates before any write or multi-agent capability is added.


Prerequisites

  • Python 3.11+
  • uv (recommended) or pip
  • Internet connection (for initial data download and live metadata)

Installation

# Clone the repository
git clone https://github.com/malkreide/openlex-mcp.git
cd openlex-mcp

# Install
pip install -e .
# or with uv:
uv pip install -e .

Quickstart

# stdio (for Claude Desktop)
python -m openlex_mcp.server

# Streamable HTTP โ€” binds to 127.0.0.1:8000 by default (localhost only)
python -m openlex_mcp.server --http --port 8000

Network binding

By default the HTTP transport binds to 127.0.0.1 (localhost only). The host and port are configurable via the MCP_HOST / MCP_PORT environment variables (or the --host / --port CLI flags, which take precedence).

Never bind to 0.0.0.0 outside a container โ€” it exposes the server to your local network (NeighborJack risk). For containerized/cloud deployments set MCP_HOST=0.0.0.0 explicitly; when that happens outside a detected container the server logs a warning.

Try it immediately in Claude Desktop:

"What is the Volksschulgesetz (VSG)?" "Find all Zurich laws about data protection" "Show me Art. 1 of the Volksschulgesetz" "Which education laws mention 'Schulleitung'?"


Configuration

Claude Desktop

Edit ~/Library/Application Support/Claude/claude_desktop_config.json (macOS) or %APPDATA%\Claude\claude_desktop_config.json (Windows):

{
  "mcpServers": {
    "openlex": {
      "command": "python",
      "args": ["-m", "openlex_mcp.server"]
    }
  }
}

Or with the installed entry point:

{
  "mcpServers": {
    "openlex": {
      "command": "openlex-mcp"
    }
  }
}

Config file locations:

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

Cloud Deployment (SSE for browser access)

For use via claude.ai in the browser (e.g. on managed workstations without local software):

Render.com (recommended):

  1. Push/fork the repository to GitHub
  2. On render.com: New Web Service โ†’ connect GitHub repo
  3. Set start command: python -m openlex_mcp.server --http --port 8000
  4. Set environment variable MCP_HOST=0.0.0.0 so the container is reachable (the code default is 127.0.0.1; Render sets the RENDER env var, so no NeighborJack warning is logged)
  5. Set MCP_CORS_ORIGINS=https://claude.ai so the browser can read the Mcp-Session-Id header (comma-separated list; no wildcard โ€” defaults to empty, i.e. no cross-origin access)
  6. In claude.ai under Settings โ†’ MCP Servers, add: https://your-app.onrender.com/sse

๐Ÿ’ก "stdio for the developer laptop, SSE for the browser."


Available Tools

Search & Browse

Tool Description
openlex__zhlaw_search_laws Full-text search across all ~970 ZH laws (FTS5 + BM25 ranking)
openlex__zhlaw_get_law Retrieve a law by LS number (e.g. 412.100) or abbreviation (e.g. VSG)
openlex__zhlaw_list_laws List and filter laws by legal area prefix
openlex__zhlaw_find_education_laws Specialized search in education law (LS 412.x series)

Article Extraction

Tool Description
openlex__zhlaw_get_article Extract a specific article from a law (e.g. Art. 28 VSG)
openlex__zhlaw_search_articles Search within all articles of a specific law

Metadata & Cache

Tool Description
openlex__zhlaw_get_law_metadata Get live metadata from zh.ch (PDF links, validity status)
openlex__zhlaw_update_cache Refresh the local data cache from HuggingFace

Key Legal Area Prefixes (LS Numbers)

Prefix Legal Area Example
131 Constitution and popular rights Kantonsverfassung
170 Administrative procedure Datenschutzgesetz
331 Tax law Steuergesetz
412 Education and schools Volksschulgesetz (VSG)
700 Spatial planning and building Planungs- und Baugesetz
810 Health Gesundheitsgesetz

Example Use Cases

Query Tool
"What is the Volksschulgesetz?" openlex__zhlaw_get_law
"Find laws about data protection" openlex__zhlaw_search_laws
"Show me Art. 55 VSG" openlex__zhlaw_get_article
"Which education laws mention Schulleitung?" openlex__zhlaw_find_education_laws
"Find all articles about Elternrat in the VSG" openlex__zhlaw_search_articles
"Is LS 412.100 still in force?" openlex__zhlaw_get_law_metadata

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Claude / AI   โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  OpenLex MCP                 โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  HuggingFace             โ”‚
โ”‚   (MCP Host)    โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  (MCP Server)                โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  rcds/swiss_legislation   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚                              โ”‚     โ”‚  (974 ZH laws, cached)   โ”‚
                        โ”‚  8 Tools                     โ”‚     โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                        โ”‚  SQLite + FTS5 Cache         โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  zh.ch ZH-Lex            โ”‚
                        โ”‚  Stdio | HTTP                โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  (live metadata + PDFs)  โ”‚
                        โ”‚                              โ”‚     โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                        โ”‚  No authentication required  โ”‚     โ”‚  LexFind.ch              โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚  (links only)            โ”‚
                                                             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Data Source Characteristics

Source Protocol Coverage Auth License
HuggingFace rcds/swiss_legislation Datasets API 974 ZH laws (full text) None CC-BY-SA 4.0
zh.ch ZH-Lex HTTP/HTML Current metadata, PDFs None Public
LexFind.ch HTTP Cross-cantonal links None Public

Design Decision: Tools-only (no MCP Resources)

All 8 endpoints are exposed as Tools rather than MCP Resources. Rationale:

  • Every lookup is parametric โ€” queries, abbreviations, article numbers vary per call. Static Resources (one URI per document) don't capture this naturally.
  • The corpus is 974 laws ร— many articles โ€” registering each as a Resource URI would create an impractically large resource list.
  • MCP Resource templates (zhlex://laws/{sr_number}) are a future consideration for Phase 2 if clients benefit from resource-level caching or subscriptions.

Scaling Constraints

The Streamable-HTTP transport keeps session state in-process (FastMCP default). This has two implications:

  • Single-instance only โ€” horizontal scaling (multiple replicas) breaks active sessions because there is no shared session store (Redis, Durable Objects, etc.).
  • No sticky-session LB needed today โ€” a single-replica Render deployment naturally routes all requests to one process.

Before scaling beyond one instance: either add a shared session store or configure your edge load balancer to route on the Mcp-Session-Id header with a stick-table and an appropriate TTL.


MCP Protocol Version

Item Value
Supported protocol version 2025-11-25
SDK mcp[cli] >= 1.3.0 (FastMCP)
Pinned in src/openlex_mcp/server.py โ€” MCP_PROTOCOL_VERSION constant

Update policy

  1. When mcp is upgraded (via Dependabot PR), verify the protocol version in the SDK release notes.
  2. If the protocol version changes, update MCP_PROTOCOL_VERSION in server.py, regenerate docs/tool-hashes.json (PYTHONPATH=src python scripts/gen_tool_hashes.py > docs/tool-hashes.json), and note the change in CHANGELOG.md.
  3. Run pytest tests/ -m "not live" to confirm compatibility before merging.

Project Structure

openlex-mcp/
โ”œโ”€โ”€ src/openlex_mcp/
โ”‚   โ”œโ”€โ”€ __init__.py              # Package
โ”‚   โ”œโ”€โ”€ __main__.py              # Entry point for python -m
โ”‚   โ”œโ”€โ”€ server.py                # 8 MCP tool definitions (FastMCP) + Settings
โ”‚   โ”œโ”€โ”€ responses.py             # Typed structured response envelopes (SDK-002)
โ”‚   โ”œโ”€โ”€ logging_config.py        # structlog JSON logging setup (OBS-003)
โ”‚   โ”œโ”€โ”€ net.py                   # SSRF/egress-hardened outbound HTTP
โ”‚   โ”œโ”€โ”€ api_client.py            # zh.ch HTTP client + metadata extraction
โ”‚   โ”œโ”€โ”€ data_cache.py            # SQLite + FTS5 cache management
โ”‚   โ””โ”€โ”€ law_parser.py            # Article extraction from law texts
โ”œโ”€โ”€ tests/                       # 89 unit tests (parser, cache, net, toolsโ€ฆ)
โ”œโ”€โ”€ scripts/gen_tool_hashes.py   # Tool-definition hash snapshot (SEC-022)
โ”œโ”€โ”€ docs/                        # network-egress, secret-management, tool-hashes
โ”œโ”€โ”€ .github/workflows/ci.yml     # GitHub Actions (Python 3.11/3.12/3.13)
โ”œโ”€โ”€ .github/dependabot.yml       # Weekly dependency PRs (ARCH-012)
โ”œโ”€โ”€ Dockerfile                   # Hardened multi-stage build (SEC-007/SCALE-004)
โ”œโ”€โ”€ compose.yml                  # Resource limits for local testing (SCALE-006)
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ claude_desktop_config.json   # Example config for Claude Desktop
โ”œโ”€โ”€ CHANGELOG.md
โ”œโ”€โ”€ ROADMAP.md                   # Phase plan + accepted-risk register
โ”œโ”€โ”€ CONTRIBUTING.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md                    # This file (English)
โ””โ”€โ”€ README.de.md                 # German version

Tool output format

All tools return a structured response envelope (not Markdown text), so MCP clients receive structuredContent they can parse directly:

{
  "source": "Kanton Zรผrich Rechtssammlung โ€” HuggingFace โ€ฆ & zh.ch",
  "provenance": "cache",          // cache | live | parser | cache+parser | none
  "result_type": "law_summaries", // law_summaries | law_detail | articles | metadata | cache_status
  "count": 2,
  "message": null,                // human-readable guidance for empty/edge results
  "results": [ /* typed items */ ]
}

Known Limitations

  • HuggingFace dataset: The html_content field is unreliable (cross-contaminated between laws); the server uses pdf_content instead, which is correct but has PDF extraction artefacts (hyphenation, layout artefacts)
  • Article parser: PDF text extraction sometimes merges article boundaries; complex nested articles may not parse perfectly
  • Initial load: First start requires ~25s to download and index 974 laws from HuggingFace (~38 MB SQLite database)
  • zh.ch metadata: No official API; metadata extraction relies on HTML patterns that may change
  • Offline mode: Full-text search works offline after initial load; live metadata requires internet

Safety & Limits

Aspect Details
Access Read-only (readOnlyHint: true) โ€” the server cannot modify or delete any data
Personal data No personal data โ€” all sources are aggregated, public legal texts
Rate limits Built-in per-query caps (max 50 search results, 5000 chars content preview)
Timeout 30 seconds per HTTP call to zh.ch
Egress Outbound requests are HTTPS-only and restricted to an allow-list (www.zh.ch), with SSRF IP-blocking and DNS-pinning โ€” see docs/network-egress.md
Authentication No API keys required โ€” HuggingFace dataset is public, zh.ch is open
Security posture (Lethal Trifecta) Score 1 / 3: public data only (no private/sensitive data) โœ“ ยท GET-only egress to www.zh.ch โ€” no POST, no webhooks, no email โœ“ ยท no code execution โœ“. Structurally safe by design.
Session handling Mcp-Session-Id generated and managed by the MCP SDK (cryptographically secure UUIDs). No user-identity binding โ€” auth_model=none is correct for public read-only data. If authentication is ever added, bind sessions to the validated OAuth sub claim before deployment.
Secrets No secrets held โ€” all data sources are public. See docs/secret-management.md.
Licenses Law data: CC-BY-SA 4.0 (rcds/swiss_legislation); zh.ch metadata: public
Terms of Service Subject to ToS of HuggingFace and Canton Zurich
Disclaimer This server provides legal texts for informational purposes only โ€” it does not constitute legal advice

Testing

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

# Integration tests (live API calls)
pytest tests/ -m "live"

Changelog

See CHANGELOG.md


Roadmap

See ROADMAP.md


Contributing

See CONTRIBUTING.md


License

MIT License โ€” see LICENSE


Author

Hayal Oezkan ยท 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

openlex_mcp-0.2.0.tar.gz (187.0 kB view details)

Uploaded Source

Built Distribution

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

openlex_mcp-0.2.0-py3-none-any.whl (35.9 kB view details)

Uploaded Python 3

File details

Details for the file openlex_mcp-0.2.0.tar.gz.

File metadata

  • Download URL: openlex_mcp-0.2.0.tar.gz
  • Upload date:
  • Size: 187.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openlex_mcp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 42f2693b86d67eb42a318a0ec04aae93b704d3a6c43c84e4a8dcfe80cb6b9758
MD5 a114cacf9b69dd37176a9b70c4a13025
BLAKE2b-256 59a5b8ec8ab937dd75b7d6eb5790f56d3b581ea71e48c3030d910d4273a97b18

See more details on using hashes here.

Provenance

The following attestation bundles were made for openlex_mcp-0.2.0.tar.gz:

Publisher: publish.yml on malkreide/openlex-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openlex_mcp-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: openlex_mcp-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 35.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openlex_mcp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c8746cfeb88bcf9cabba40a24390a01e7f4092c7dafe131be44207d746bc6c2
MD5 adfbecc900e7e3c815503bb644ab0f68
BLAKE2b-256 b89fde2615dde5493ff354e08f657b1362094e133d0ec61926641ea03e648810

See more details on using hashes here.

Provenance

The following attestation bundles were made for openlex_mcp-0.2.0-py3-none-any.whl:

Publisher: publish.yml on malkreide/openlex-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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