Sponsored by Deepsite.site

Africa Energy Data Mcp

Created By
Navashuba month ago
Exposes historical electricity, energy and economic indicators for 54 African countries (2000–2022). MCP tools: get_electricity_data, get_energy_data, get_economic_data, check_api_health, list_countries.
Overview

Africa Energy Data MCP Server

An MCP (Model Context Protocol) server that exposes the Africa Energy Data API as tools for Claude, Cursor, and other MCP-compatible clients.

Query electricity, energy, and economic data for all 54 African countries spanning 2000–2022. The server includes automatic caching (10-minute TTL), retry logic with exponential backoff, input validation, and structured logging.

Features

  • 5 Tools: Electricity data, Energy data, Economic indicators, Health check, Country list
  • Auto-caching: 10-minute in-memory cache to reduce API calls
  • Retry logic: 3 retries with exponential backoff on 5xx errors and timeouts
  • Input validation: Validates country names, years, and year ranges
  • Structured logging: Full debug logs output to stderr (doesn't interfere with MCP stdio)
  • Production-ready: Modular design, error handling, timeout management

Setup

1. Clone or download this repository

cd africa_energy_mcp

2. Install dependencies

pip install -r requirements.txt

3. Get an API key

  1. Go to Africa Energy Data API on RapidAPI
  2. Sign up or log in to RapidAPI
  3. Click "Subscribe" (free tier available)
  4. Copy your API key from the dashboard

4. Create a .env file

cp .env.example .env

Edit .env and paste your API key:

AFRICA_ENERGY_API_KEY=your_key_here

5. Test the server locally

python server.py

You should see startup logs on stderr. Press Ctrl+C to stop.

Available Tools

1. get_electricity_data

Fetch electricity metrics for an African country.

Parameters:

  • country (required): Country name, e.g. "Kenya", "Nigeria"
  • year (optional): Filter by year (2000–2022)
  • metric (optional): Specific metric name
  • unit (optional): Measurement unit

Example:

{
  "country": "Kenya",
  "year": 2022
}

2. get_energy_data

Fetch general energy sector data (access, efficiency, renewables, etc.).

Parameters:

  • country (required): Country name
  • sub_sector (optional): "Access", "Efficiency", "Renewables", etc.
  • start_year (optional): Year range start (2000–2022)
  • end_year (optional): Year range end
  • metric (optional): Specific metric name

Example:

{
  "country": "South Africa",
  "start_year": 2010,
  "end_year": 2022
}

3. get_economic_data

Fetch economic indicators: GDP, inflation, population, energy spending, etc.

Parameters:

  • country (required): Country name
  • start_year (optional): Year range start
  • end_year (optional): Year range end
  • metric (optional): "GDP", "Inflation", "Rural population", etc.
  • unit (optional): Measurement unit

4. check_api_health

Verify the API is online and responding.

No parameters required.

5. list_countries

Get the list of all 54 supported African countries.

No parameters required.

Connect to Claude Desktop

macOS / Linux

Edit ~/.config/claude_desktop_config.json:

{
  "mcpServers": {
    "africa-energy": {
      "command": "python",
      "args": ["/path/to/africa_energy_mcp/server.py"]
    }
  }
}

Replace /path/to/africa_energy_mcp with the full path to your repository.

Windows

Edit %APPDATA%\Claude\claude_desktop_config.json:

{
  "mcpServers": {
    "africa-energy": {
      "command": "python",
      "args": ["C:\\path\\to\\africa_energy_mcp\\server.py"]
    }
  }
}

Then restart Claude Desktop. The tool should appear in the MCP tools list.

Connect to Cursor

  1. Open Cursor Settings (Cmd/Ctrl + ,)
  2. Search for "MCP"
  3. Click "Edit in Settings JSON"
  4. Add to your settings.json:
"mcp.servers": {
  "africa-energy": {
    "command": "python",
    "args": ["/path/to/africa_energy_mcp/server.py"]
  }
}

Save and restart Cursor. Tools should be available in chat.

Optional: CI and publishing

If you'd like to publish or run this server automatically (for example to make it runnable from remote hosts or CI), consider adding CI that builds a Docker image and pushes it to a registry (GitHub Container Registry or Docker Hub). A Dockerfile and an example GitHub Actions workflow are included in the repository as a starting point — feel free to customize them for your preferred registry and release process.

For most users, the simplest path is to run the server locally (see "Setup" above) and connect an MCP client such as Claude Desktop or Cursor.

Project Structure

africa_energy_mcp/
├── server.py              # MCP server entry point, wires tools + handlers
├── config.py              # Environment variables, constants, country list
├── api_client.py          # Async HTTP client, retry logic, caching
├── tools.py               # MCP tool schema definitions
├── handlers.py            # Input validation, routing, response formatting
├── requirements.txt       # Python dependencies
├── .env.example          # Example environment configuration
└── README.md             # This file

Architecture

Request flow:

  1. server.py receives tool call via MCP
  2. handlers.py validates input, calls api_client.py
  3. api_client.py checks cache, makes HTTP request, retries on failure
  4. Response is cached and returned as JSON
  5. server.py formats and returns to MCP client

Caching:

  • Key: (endpoint, frozenset(params))
  • TTL: 10 minutes (configurable in config.py)
  • In-memory only (cleared on server restart)

Retry logic:

  • Retries on 5xx errors and timeouts
  • Up to 3 attempts
  • Exponential backoff: 1s, 2s, 4s
  • Does NOT retry on 4xx errors (client errors)

Configuration

Edit config.py to adjust:

  • REQUEST_TIMEOUT: HTTP timeout (default: 30s)
  • RETRY_MAX_ATTEMPTS: Max retries (default: 3)
  • RETRY_BACKOFF_FACTOR: Backoff multiplier (default: 2.0)
  • CACHE_TTL_SECONDS: Cache expiry (default: 600s)

Logging

Logs are written to stderr with format:

2024-05-16 10:30:45,123 - handlers - DEBUG - Tool call: get_electricity_data for Kenya, year=2022

Log levels:

  • DEBUG: Tool calls, cache hits/misses, retry attempts
  • INFO: Successful API calls, cache operations
  • WARNING: Retries, expired cache entries
  • ERROR: API failures, validation errors, network issues

Error Handling

All tool calls return well-formatted JSON error responses:

{
  "error": "Validation error: Country must be a non-empty string."
}

Common errors:

  • Validation error: Invalid input (bad country, year out of range)
  • API error: Server returned 4xx or 5xx
  • Request timeout: API didn't respond within 30s (will retry)
  • Unexpected error: Unhandled exception

Testing

# Start the server
python server.py

# In another terminal, test with curl (mock MCP call):
# This is for manual testing; MCP clients handle this automatically

Or test the handler directly in Python:

import asyncio
from handlers import call_tool
from api_client import APIClient

async def test():
    async with APIClient() as client:
        result = await call_tool(
            "get_electricity_data",
            {"country": "Kenya", "year": 2022},
            client
        )
        print(result[0].text)

asyncio.run(test())

Troubleshooting

"Unknown or invalid tool"

  • Restart Claude Desktop / Cursor
  • Check config file path is correct

"AFRICA_ENERGY_API_KEY not found"

  • Create .env file (copy from .env.example)
  • Check AFRICA_ENERGY_API_KEY is set
  • Verify it's a valid RapidAPI key

"API error 401"

  • API key is invalid or expired
  • Regenerate key from RapidAPI dashboard

"Request timeout"

  • API is slow; server will retry automatically
  • Increase REQUEST_TIMEOUT in config.py if retries exhaust

"No such file or directory: server.py"

  • Run from the africa_energy_mcp directory
  • Or provide full path: python /path/to/server.py

Contributing

To improve the server:

  1. Fork this repo (if on GitHub)
  2. Create a feature branch
  3. Make changes to handlers.py, api_client.py, or add new tools to tools.py
  4. Test locally
  5. Submit a pull request

License

MIT License — feel free to use and modify for your projects.

Support

For issues or questions:


Version: 1.0.0
Last Updated: 2024-05-16
Status: Production-ready ✅

Server Config

{
  "mcpServers": {
    "africa-energy": {
      "command": "py",
      "args": [
        "server.py"
      ],
      "description": "Run locally with Python: requires AFRICA_ENERGY_API_KEY in .env"
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
DeepChatYour AI Partner on Desktop
CursorThe AI Code Editor
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
WindsurfThe new purpose-built IDE to harness magic
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Serper MCP ServerA Serper MCP Server
Tavily Mcp
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Amap Maps高德地图官方 MCP Server
ChatWiseThe second fastest AI chatbot™
Playwright McpPlaywright MCP server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
RedisA Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.