Skip to main content
Glama
spot-mcp-server

spot-mcp-server

by JussiSavola
README.md
# spot-mcp-server

MCP server for Finnish electricity spot prices, powered by [api.spot-hinta.fi](https://api.spot-hinta.fi).

Exposes real-time and forward-looking spot price data as MCP tools, making electricity prices available to AI agents and LLM-based automations without requiring them to call external APIs directly.

## Background

Finnish electricity prices change every 15 minutes (96 slots per day). Knowing the current price rank and upcoming price windows is essential for smart home automations — pre-heating during cheap hours, deferring loads during expensive ones, and letting an AI agent reason about trade-offs between comfort and cost.

This server fills the gap: a lightweight MCP endpoint that any MCP-compatible AI client (Claude, etc.) can query to get structured, reasoned price data.

## Tools

| Tool | Description |
|------|-------------|
| `get_current_price` | Current quarter-hour: rank, price with/without tax. Served from cache. |
| `get_today_prices` | All quarter-hour slots for today. |
| `get_tomorrow_prices` | All quarter-hour slots for tomorrow, if published (~14:15 Finnish time). |
| `get_prices_for_hours(hour_from, hour_to)` | Filtered time window with min/max/avg summary and cheapest/most expensive slot. |
| `get_cheapest_remaining_slots(n)` | Top N cheapest slots remaining today, sorted by price. |
| `get_today_summary` | Current price context: rank, assessment (very cheap → very expensive), position in today's range, tomorrow availability. |

Prices are in EUR/kWh. **Rank** is a 1–96 percentile within today's prices (1 = cheapest quarter-hour, 96 = most expensive). Days may have 23, 24, or 25 slots per hour due to DST transitions.

## Cache strategy

A single `/TodayAndDayForward` fetch populates all data. Prices are static once published; cache expires only when new data can appear:

- Tomorrow's data present → expires at midnight after tomorrow
- Only today's data, before 14:15 → expires at midnight tonight
- Only today's data, after 14:15 → expires in 15 min (lazy poll until tomorrow is published)

Current price is derived from the cache by matching the current quarter-hour slot — no separate `/JustNow` call is made.

## Requirements

- Python 3.10+
- `fastmcp`
- `httpx`
- `tzdata`

## Installation

```bash
git clone https://github.com/JussiSavola/spot-mcp-server.git
cd spot-mcp-server
pip install -r requirements.txt
```

## Running

```bash
python spot_hinta_mcp.py
```

Default port: **8765**. Override with environment variable:

```bash
SPOT_HINTA_PORT=9000 python spot_hinta_mcp.py
```

MCP endpoint (Streamable HTTP transport):
```
http://localhost:8765/mcp
```

Or via `run.sh`:
```bash
bash run.sh
```

## Connecting to Claude

Add to your Claude MCP configuration:

```json
{
  "mcpServers": {
    "spot-hinta": {
      "type": "url",
      "url": "http://localhost:8765/mcp"
    }
  }
}
```

## Tests

```bash
pip install pytest
pytest test_spot_hinta.py -v
```

Tests cover cache TTL logic, quarter-hour slot matching, DST transition handling, and data parsing. Network calls are mocked.

## Data source

[api.spot-hinta.fi](https://api.spot-hinta.fi) — free Finnish electricity spot price API.
Endpoint used: `/TodayAndDayForward`.

## License

[PolyForm Noncommercial 1.0.0](LICENSE.md). Personal, hobby, educational and
research use, and use by noncommercial organizations, is permitted. Commercial
use requires a separate license from the author: Jussi Savola,
https://github.com/JussiSavola.

Versions up to and including commit `e611c45` were published with "MIT" stated
in this README; those versions remain available under MIT.