Ontario Grid MCP
by RamjeU
README.md
# Ontario Grid MCP
[](https://github.com/RamjeU/ontario-grid-mcp/actions/workflows/ci.yml)


An **MCP server** that lets an AI assistant answer questions about Ontario's electricity grid from the IESO's public market reports. Ask Claude (or any MCP client) *"What's Ontario's demand and price right now?"* or *"How clean was the grid last Tuesday?"* and it calls these tools instead of guessing.
I spent 16 months as a co-op analyst at the IESO. This project takes the public side of that data and makes it usable by an AI agent, with the same care about market time, missing data and preliminary values that the work needed.
## Tools
| Tool | What it answers | IESO report |
| --- | --- | --- |
| `get_current_grid_status` | Latest 5-minute Ontario demand (MW) and zonal price ($/MWh) | RealtimeTotals, RealtimeOntarioZonalPrice |
| `get_hourly_demand(date)` | Ontario and market demand by hour, with peak, low, average and a partial-day flag | PUB_Demand_YYYY.csv |
| `get_generation_mix(date, hour_ending?)` | Output by fuel, clean share and estimated carbon intensity (g CO2e/kWh) | GenOutputbyFuelHourly |
| `find_demand_peaks(start_date, end_date, top_n)` | The highest-demand hours in a range, one per day | PUB_Demand_YYYY.csv |
It also exposes a **resource**, `ieso://methodology`, describing the data sources and caveats, and a **prompt**, `daily_grid_briefing(date)`, that asks the model for a short plain-language briefing using the tools.
## Example output (live, 3 December 2026)
```json
// get_current_grid_status
{ "as_of": "2026-12-03 19:20 EST (HE20, interval 5)",
"ontario_demand_mw": 18281.0, "ontario_zonal_price_per_mwh": 44.15,
"note": "Real-time values are preliminary and may be revised." }
// get_generation_mix {"date": "2026-12-01"}
{ "hours_covered": 24, "clean_share_pct": 64.8,
"estimated_carbon_intensity_g_per_kwh": 181.1, "missing_metering_points": 59,
"by_fuel": { "NUCLEAR": {"share_pct": 38.4}, "GAS": {"share_pct": 34.5}, "HYDRO": {"share_pct": 24.2}, "...": "..." } }
// find_demand_peaks {"start_date": "2026-01-01", "end_date": "2026-12-01", "top_n": 3}
{ "peaks": [ {"date": "2026-07-14", "hour_ending": 17, "ontario_demand_mw": 25646.0}, "..." ] }
```
## Design decisions
- **Market time is EST all year.** The IESO labels hours "hour ending" 1 to 24 in Eastern Standard Time, with no daylight saving shift. Every answer is labelled in that time (for example `HE20, interval 5`), so a model can't silently mix it up with Toronto clock time.
- **Missing stays missing.** Unpublished intervals and blank cells become `null`, never `0`. A zero demand reading would be a wrong answer an assistant could repeat confidently.
- **Honest errors the model can act on.** Asking for today's generation mix returns *"This report is published daily for the previous day; the latest day available is 2026-12-02"* instead of an empty result, so the assistant can explain or retry with a valid date.
- **Tolerant parsers.** A malformed row is skipped and reported as a warning instead of failing the whole file. The fuel report's quality flag is surfaced as a count of missing metering points.
- **Estimates are labelled.** Carbon intensity uses IPCC AR5 median life-cycle factors, and every result says it is an estimate that leans low (Ontario's gas peakers emit more than the combined-cycle factor).
- **Small, cached fetches.** Yearly files are cached for five minutes, so several questions in one conversation don't re-download them.
## Security
An MCP server gives a model the ability to act, so the tools are deliberately narrow:
- **Read-only.** Every tool is annotated `readOnlyHint`; nothing writes anywhere.
- **No URLs from the model.** Tools take dates and numbers only. The server builds paths to a fixed list of IESO reports on one host, rejects path tricks (`..`, absolute paths, schemes) and does not follow redirects. A prompt-injected model can't point it at another site.
- **Safe XML.** Parsing uses `defusedxml` with DTDs, entities and external references forbidden (XXE). Tests check this.
- **Bounded inputs.** Dates must be between 2003 and today, ranges at most 366 days, `top_n` at most 20. Invalid input is rejected before any network request (also tested).
- **Size limit** on downloads, timeouts and retries with backoff.
## Quick start
Requires Python 3.10+ (or [uv](https://docs.astral.sh/uv/)).
```bash
git clone https://github.com/RamjeU/ontario-grid-mcp.git
cd ontario-grid-mcp
python3 -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
python -m pytest -q # 19 tests, offline, using saved IESO files
python scripts/smoke_test.py # live: starts the server over stdio and calls every tool
```
### Use it in Claude Desktop
Add this to `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS), replacing the path, then restart Claude:
```json
{
"mcpServers": {
"ontario-grid": {
"command": "uv",
"args": ["--directory", "/path/to/ontario-grid-mcp", "run", "ontario-grid-mcp"]
}
}
}
```
Without uv, point `command` at the virtual environment's Python instead: `"command": "/path/to/ontario-grid-mcp/.venv/bin/python", "args": ["-m", "ontario_grid_mcp.server"]`.
Then ask things like *"Give me a daily grid briefing for yesterday"* or *"When was Ontario's peak demand this summer, and what was the generation mix that day?"*
## Tests
`tests/` runs a real MCP client against the server in-process, with trimmed copies of real IESO reports from 1 to 3 December 2026 in `tests/fixtures/`. They cover the parsers, the analytics, each tool's output, clear errors for bad input, the "not published yet" path, and XXE protection. CI runs them on Python 3.10, 3.12 and 3.13.
## Limitations
- Real-time values are preliminary; the IESO revises them, and this server reports what is published at the time.
- The generation-by-fuel report covers the previous day at the earliest, so there is no "right now" fuel mix.
- Carbon intensity is an estimate from generic life-cycle factors, not measured emissions, and it excludes imports and exports.
- Data before 2003 and intertie flows are not covered yet.
## Data and licence
Data comes from the [IESO public reports site](https://reports-public.ieso.ca/public/). This is an independent project and is not affiliated with or endorsed by the IESO. Code is under the MIT licence.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues