Labsco
LeonMelamud logo

Pikud Haoref Real-Time Alert System

โ˜… 5

from LeonMelamud

Provides real-time access to Israeli emergency alerts from the official Pikud Haoref API.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedAccount requiredNeeds API keys

Pikud Haoref Real-Time Alert System

System Architecture

A comprehensive middleware service and MCP server for accessing Israeli emergency alerts from the official Pikud Haoref (Israeli Home Front Command) API.

Overview

This project provides two ways to access Israeli emergency alert data using a publish-subscribe architecture:

  1. FastAPI Middleware Service - Single source polling with real-time SSE streaming
  2. MCP Server - Event-driven subscriber for AI assistants (built with FastMCP)

The FastAPI service polls the official Pikud Haoref API at https://www.oref.org.il/WarningMessages/alert/alerts.json and publishes alerts via Server-Sent Events. The MCP server subscribes to this stream, creating an efficient pub-sub system that eliminates duplicate API calls while providing real-time access to emergency alerts including rocket alerts, aerial intrusions, earthquakes, and other emergencies.

Features

FastAPI Service Features

  • Single-source polling of emergency alerts (eliminates duplicate API calls)
  • Real-time SSE streaming via public webhook endpoint
  • API key authentication for client endpoints and geo-restriction to Israel
  • Docker support for easy deployment
  • Comprehensive testing suite

MCP Server Features

  • Event-driven SSE client - Subscribes to FastAPI webhook for real-time alerts
  • 3 Tools for AI assistants:
    • check_current_alerts - Check for active alerts from subscribed stream
    • get_alert_history - Get recent alerts with filtering (limit: 1-50, region filter, intelligent city matching)
    • get_connection_status - Check SSE subscription connection status
  • 2 Resources:
    • poha://alerts/recent - JSON data of recent alerts
    • poha://alerts/current-status - System status information
  • Smart City Filtering:
    • Exact substring matching - Find alerts by city name (e.g., "ืชืœ ืื‘ื™ื‘" matches all Tel Aviv areas)
    • Fuzzy matching - Intelligent matching with threshold 60 for partial/similar names
    • Multi-city support - Search multiple cities simultaneously
    • Hebrew city names - Optimized for Hebrew location names from the API
  • Built with FastMCP following proven patterns
  • Automatic reconnection and error handling for SSE connections

API Endpoints (FastAPI Service)

GET /

Basic status endpoint.

GET /api/alerts-stream

Headers: X-API-Key: your-key

Server-Sent Events stream for real-time alerts (authenticated endpoint). Returns:

  • event: new_alert - When a new alert is detected
  • : keep-alive - Periodic keep-alive messages

GET /api/webhook/alerts

Headers: X-API-Key: your-key

Internal SSE webhook endpoint for services like the MCP server. Streams the same alert data as the client endpoint and requires the same API key authentication for security. Designed for server-to-server communication.

Example response for both endpoints:

event: new_alert
data: {"id": "12345", "data": ["Tel Aviv", "Ramat Gan"], "cat": "1", "title": "Rocket Alert", "desc": "Immediate shelter required"}

: keep-alive

Architecture

The system uses a publish-subscribe architecture with the following components:

  1. Pikud Haoref API - External data source (government emergency alerts)
  2. FastAPI Middleware - Single source polling + SSE publisher (polls every 2 seconds)
  3. MCP Server - SSE subscriber + tool provider for AI assistants
  4. Client Applications - Web frontends, mobile apps, AI assistants, or other services
Pikud Haoref API โ†โ†’ [Single Poller] โ†โ†’ FastAPI Middleware โ†โ†’ [SSE Stream] โ†โ†’ MCP Server โ†’ AI Assistants
                                              โ†“
                                          [SSE Stream] โ†โ†’ Web Clients

Key Benefits:

  • โœ… 50% reduction in API calls (single polling source)
  • โœ… Real-time propagation of alerts via SSE
  • โœ… Event-driven architecture for better scalability
  • โœ… Automatic reconnection for robust SSE connections

Visual Diagram: Run python diagram.py to generate poha_sse_architecture.png showing the complete system architecture.

Security Features

API Key Authentication (Required for All Endpoints)

Important: API key authentication is required for both SSE endpoints for security.

  • Both SSE endpoints require API key authentication via X-API-Key header
  • MCP server connects with authentication to the internal webhook endpoint
  • Same API key used for both client and internal service authentication

Geo-Restriction (Optional)

Configure GEOIP_DB_PATH to restrict access to Israeli IP addresses only:

  1. Sign up at MaxMind
  2. Download GeoLite2-Country.mmdb
  3. Set GEOIP_DB_PATH in your .env file

Development

Project Structure

poha-real-time-alert-system/
โ”œโ”€โ”€ src/                     # Main source code
โ”‚   โ”œโ”€โ”€ core/               # Core MCP functionality
โ”‚   โ”‚   โ”œโ”€โ”€ mcp_server.py   # MCP server implementation
โ”‚   โ”‚   โ”œโ”€โ”€ state.py        # Application state management
โ”‚   โ”‚   โ””โ”€โ”€ alert_queue.py  # Alert queue management
โ”‚   โ”œโ”€โ”€ api/                # FastAPI services
โ”‚   โ”‚   โ”œโ”€โ”€ main.py         # FastAPI application entry point
โ”‚   โ”‚   โ””โ”€โ”€ sse_gateway.py  # SSE gateway for VSCode extension
โ”‚   โ”œโ”€โ”€ services/           # Business logic
โ”‚   โ”‚   โ”œโ”€โ”€ polling.py      # Core API polling logic
โ”‚   โ”‚   โ””โ”€โ”€ sse.py          # Server-Sent Events implementation
โ”‚   โ””โ”€โ”€ utils/              # Utilities
โ”‚       โ”œโ”€โ”€ security.py     # Authentication and geo-restriction
โ”‚       โ””โ”€โ”€ geolocation.py  # Geo-IP functionality
โ”œโ”€โ”€ docker/                 # Docker configuration
โ”‚   โ”œโ”€โ”€ Dockerfile         # Main FastAPI container
โ”‚   โ”œโ”€โ”€ mcp.Dockerfile     # MCP server container
โ”‚   โ””โ”€โ”€ docker-compose.yml # Multi-service setup
โ”œโ”€โ”€ scripts/               # Utility scripts
โ”‚   โ”œโ”€โ”€ start_mcp.sh       # MCP server startup script
โ”‚   โ””โ”€โ”€ diagram.py         # Architecture diagram generator
โ”œโ”€โ”€ tests/                 # Comprehensive test suite
โ”œโ”€โ”€ vscode-extension/      # VSCode extension for alerts
โ”œโ”€โ”€ conftest.py           # Test configuration
โ”œโ”€โ”€ pytest.ini           # Test settings
โ”œโ”€โ”€ Makefile             # Docker management commands
โ”œโ”€โ”€ requirements.txt     # Python dependencies
โ”œโ”€โ”€ README.md           # This file
โ””โ”€โ”€ .gitignore         # Git ignore rules

Testing

# Run tests in Docker
make test

# Or run tests locally
pytest -v

Dependencies

  • Core: fastapi, uvicorn, httpx, python-dotenv
  • Security: geoip2, slowapi
  • MCP: fastmcp, fuzzywuzzy, python-Levenshtein
  • Testing: pytest, pytest-asyncio, respx

Data Source

  • API: https://www.oref.org.il/WarningMessages/alert/alerts.json
  • Provider: Israeli Government (Pikud Haoref - Home Front Command)
  • Coverage: All emergency alerts in Israel
  • Update Frequency: Every 2 seconds
  • Data Types: Rocket alerts, aerial intrusions, earthquakes, emergency announcements

Use Cases

  • Emergency Response Teams - Real-time alert monitoring
  • News Organizations - Breaking news automation
  • Israeli Residents - Personal safety notifications
  • Researchers - Emergency pattern analysis
  • AI Assistants - Contextual emergency information
  • Mobile Apps - Push notification services
  • Smart Home Systems - Automated responses to alerts

SQLite Persistence

Alerts are persisted to a local SQLite database (via aiosqlite) for historical queries. The database is created automatically on startup.

  • Default path: data/alerts.db (configurable via DATABASE_PATH env var)
  • Tables: alerts (full alert data) + city_alerts (denormalized for fast city lookup)
  • Indexed on city name and timestamp for fast queries

REST API Endpoints

In addition to the SSE streaming endpoints, the following REST endpoints are available:

EndpointDescription
GET /healthHealth check (returns {"status": "ok"})
GET /api/alerts/currentCurrent active alert state
GET /api/alerts/history?city=&limit=&since=Alert history from SQLite
GET /api/alerts/city/{city_name}Alerts for a specific city
GET /api/alerts/statsAggregate alert statistics

Examples:

curl http://localhost:8000/health
curl "http://localhost:8000/api/alerts/history?city=ืชืœ ืื‘ื™ื‘&limit=10"
curl http://localhost:8000/api/alerts/city/ื—ื™ืคื”
curl http://localhost:8000/api/alerts/stats

OpenClaw Integration

To use the Pikud HaOref MCP server with OpenClaw, add to your openclaw.json:

{
  "mcpServers": {
    "pikud-haoref": {
      "url": "http://127.0.0.1:8001/mcp"
    }
  }
}

See openclaw-config-example.json and skills/pikud-haoref/SKILL.md for full details.

Additional MCP Tools (SQLite-backed)

ToolDescription
get_city_alerts(city, limit)Query local DB for alerts in a specific city
get_db_stats()Get alert database statistics

Disclaimer

This service provides access to official Israeli emergency alert data but is not affiliated with or endorsed by the Israeli government or Pikud Haoref. Always follow official emergency procedures and consult official sources for critical safety information.