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
- Go to Africa Energy Data API on RapidAPI
- Sign up or log in to RapidAPI
- Click "Subscribe" (free tier available)
- 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 nameunit(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 namesub_sector(optional): "Access", "Efficiency", "Renewables", etc.start_year(optional): Year range start (2000–2022)end_year(optional): Year range endmetric(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 namestart_year(optional): Year range startend_year(optional): Year range endmetric(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
- Open Cursor Settings (Cmd/Ctrl + ,)
- Search for "MCP"
- Click "Edit in Settings JSON"
- 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:
- server.py receives tool call via MCP
- handlers.py validates input, calls api_client.py
- api_client.py checks cache, makes HTTP request, retries on failure
- Response is cached and returned as JSON
- 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
5xxerrors and timeouts - Up to 3 attempts
- Exponential backoff: 1s, 2s, 4s
- Does NOT retry on
4xxerrors (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 attemptsINFO: Successful API calls, cache operationsWARNING: Retries, expired cache entriesERROR: 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 5xxRequest 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
.envfile (copy from.env.example) - Check
AFRICA_ENERGY_API_KEYis 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_TIMEOUTinconfig.pyif retries exhaust
"No such file or directory: server.py"
- Run from the
africa_energy_mcpdirectory - Or provide full path:
python /path/to/server.py
Contributing
To improve the server:
- Fork this repo (if on GitHub)
- Create a feature branch
- Make changes to
handlers.py,api_client.py, or add new tools totools.py - Test locally
- Submit a pull request
License
MIT License — feel free to use and modify for your projects.
Support
For issues or questions:
- Check the MCP documentation
- Review RapidAPI Africa Energy API docs
- Open an issue on GitHub (if hosted there)
Version: 1.0.0
Last Updated: 2024-05-16
Status: Production-ready ✅
サーバー設定
{
"mcpServers": {
"africa-energy": {
"command": "py",
"args": [
"server.py"
],
"description": "Run locally with Python: requires AFRICA_ENERGY_API_KEY in .env"
}
}
}