DEV Community

Cover image for Making Africa Energy Data Accessible to LLMs - a Practical Guide
Navas Herbert
Navas Herbert

Posted on

Making Africa Energy Data Accessible to LLMs - a Practical Guide

Introduction

As an AI enthusiast and trainer, I believe high-quality open data is essential for building useful language models and agents. This project - the "Africa Energy Data MCP" - wraps a public Africa Energy Data API and exposes it as MCP tools so LLMs (Claude, Cursor, etc.) can call them directly.

In this short guide you'll find a clear, step-by-step walkthrough for:

  • Setting up the project locally
  • Understanding the architecture
  • Connecting an MCP client (Claude / Cursor)
  • Running and testing the tools
  • Publishing options (brief)

Special thanks to my friend Denzel (dkkinyua) who developed the underlying API powering this project - check out his work here: https://github.com/dkkinyua/AfricaEnergyDataAPI/tree/main

Why this matters

LLMs become far more useful when they can call tools that fetch real data. This project makes historical electricity, energy and economic indicators for 54 African countries (2000–2022) trivially available to agents, with caching, retries and validation built in.

Quick highlights

  • Tools: get_electricity_data, get_energy_data, get_economic_data, check_api_health, list_countries
  • Tech: Python 3.11, httpx, mcp server, simple in-memory cache, structured logging
  • Containerization: Dockerfile + GitHub Actions example included

Step 1 - Get the code

Clone the repo and enter the project directory:

git clone https://github.com/Navashub/africa_energy_mcp.git
cd africa_energy_mcp
Enter fullscreen mode Exit fullscreen mode

Step 2 - Install dependencies

Create a virtualenv and install requirements:

python -m venv venv
# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activate
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 3 - Add your API key

Copy the example env file and paste your RapidAPI key from the API provider (Denzel's API or the RapidAPI listing):

cp .env.example .env
# edit .env and replace AFRICA_ENERGY_API_KEY with your key
Enter fullscreen mode Exit fullscreen mode

Important: never commit .env or your API key to GitHub. The repo includes a .gitignore already.

Step 4 - Run the MCP server

Start the server locally:

py server.py
Enter fullscreen mode Exit fullscreen mode

You should see structured logs on stderr indicating the MCP server initialized and registered tools. Leave this running — it listens on stdio for MCP clients.

Step 5 - Connect an MCP client (Claude / Cursor)

For local testing, point your MCP-capable client at the server.py process. Examples:

  • Claude Desktop (Windows) — edit %APPDATA%\Claude\claude_desktop_config.json and add:
{
  "mcpServers": {
    "africa-energy": {
      "command": "py",
      "args": ["D:\\mcp_projects\\africa_energy_mcp\\server.py"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Cursor — add the equivalent mcp.servers entry in Cursor settings pointing to your local server.py.

Step 6 - Test useful queries

Start with discovery commands so you confirm connectivity:

  • "What countries are supported by the Africa Energy Data tool?" — should return the 54-country list (tool list_countries).
  • "Is the Africa Energy Data API online?" — check_api_health tool should return a health JSON.
  • Example: "Get electricity data for Kenya in 2022." — will call get_electricity_data and return JSON results.

If an endpoint returns a 500, that indicates the remote API backend failed — the error is on the API provider side. The MCP server will surface a clear error message in that case.

Architecture (brief)

  1. server.py - MCP server that declares tools and routes calls
  2. handlers.py - Input validation and mapping of tool arguments to API calls
  3. api_client.py - httpx.AsyncClient wrapper with retries, 30s timeout, and a 10-minute in-memory cache
  4. tools.py - Schemas for each MCP tool
  5. config.py - Environment and constants (including the hardcoded list of 54 countries)

Containerization & CI

If you want remote hosting, the repo includes a Dockerfile and an example GitHub Actions workflow that builds and pushes a container to GHCR. Publishing requires a registry token — we added a workflow that prefers a GHCR_TOKEN secret or falls back to the repo GITHUB_TOKEN when allowed.

Acknowledgements

Huge thanks to Denzel (dkkinyua) for building the underlying Africa Energy Data API — please check his repository: https://github.com/dkkinyua/AfricaEnergyDataAPI/tree/main

Shout-out: This project was adapted and wrapped into an MCP server by an AI enthusiast and trainer (that’s me — Navashub) to make the data accessible to LLMs for education, research and production usage.

Best practices & tips

  • Keep your API keys secret - use .env locally and repository secrets for CI.
  • Test with list_countries and check_api_health before running large queries.
  • Increase REQUEST_TIMEOUT in config.py only if you know the backend requires more time.
  • For production hosting, use the Docker image and provide secrets through the host or the platform's secret mechanism.

Conclusion

Making structured, historical energy data available to LLMs unlocks better automated analysis, answers, and decision support. This project is a small but practical bridge - thanks to Denzel for the API and to you for exploring it.

Top comments (0)