ratio-mcp
by aaearon
README.md
# ratio-mcp
An MCP server that lets an AI assistant check on a [Ratio](https://ratio.energy/) EV charger and start or stop a charge session. It talks to the same cloud API as the Ratio mobile app, through the [`aioratio`](https://pypi.org/project/aioratio/) library. It's built for one person with one Ratio account.
> **Unofficial.** Not affiliated with or endorsed by Ratio. The cloud API is reverse-engineered from the mobile app and can change or break without notice.
>
> **Start and stop control a real charger** connected to your car and your mains supply. Anyone who can call this server can do that, so keep the secret private and think about which assistants you connect.
Writes are limited to start and stop. There are no tools for changing settings, firmware, vehicles or OCPP.
## Requirements
- Python 3.11+
- A Ratio account (email and password)
- For remote use: an HTTPS reverse proxy or tunnel in front of the server
## Install
ratio-mcp is not on PyPI yet. Install from git:
```bash
pip install git+https://github.com/aaearon/ratio-mcp
# or run without installing:
uvx --from git+https://github.com/aaearon/ratio-mcp ratio-mcp
```
## Quick start: stdio (Claude Desktop, Claude Code)
The client starts the server itself, so no secret or port is needed. For Claude Desktop, add this to `claude_desktop_config.json`:
```json
{
"mcpServers": {
"ratio": {
"command": "uvx",
"args": ["--from", "git+https://github.com/aaearon/ratio-mcp", "ratio-mcp"],
"env": {
"RATIO_EMAIL": "you@example.com",
"RATIO_PASSWORD": "your-ratio-password"
}
}
}
}
```
If you installed with `pip`, use `"command": "ratio-mcp"` and drop `args`. For Claude Code:
```bash
claude mcp add ratio -e RATIO_EMAIL=you@example.com -e RATIO_PASSWORD=your-ratio-password -- ratio-mcp
```
## Quick start: HTTP
Use this for claude.ai, ChatGPT and other remote clients.
```bash
export RATIO_EMAIL=you@example.com
export RATIO_PASSWORD=your-ratio-password
export RATIO_MCP_SECRET=$(openssl rand -hex 32)
export RATIO_MCP_ALLOWED_HOSTS=ratio.example.com # the hostname clients use
ratio-mcp streamable-http
```
This serves `/mcp` on `127.0.0.1:8000`. Put an HTTPS reverse proxy in front and forward `ratio.example.com` to it.
With Docker (the image listens on `0.0.0.0:8000` and keeps tokens in `/data`):
```bash
docker build -t ratio-mcp .
docker run -d -p 127.0.0.1:8000:8000 -v ratio-data:/data \
-e RATIO_EMAIL -e RATIO_PASSWORD -e RATIO_MCP_SECRET -e RATIO_MCP_ALLOWED_HOSTS \
ratio-mcp
```
In a Docker `--env-file`, don't quote values. The quotes become part of the secret.
## Configuration
All settings are environment variables. `.env.example` has the full list.
| Variable | Required | Default | Meaning |
|---|---|---|---|
| `RATIO_EMAIL`, `RATIO_PASSWORD` | yes | | Ratio account credentials |
| `RATIO_TOKEN_PATH` | no | `$XDG_STATE_HOME/ratio-mcp/tokens.json`, else `~/.local/state/ratio-mcp/tokens.json` (Docker: `/data/ratio-tokens.json`) | Where refreshed tokens are cached (file mode 0600) |
| `RATIO_MCP_SECRET` | HTTP only | | Bearer secret. At least 32 characters from `A-Za-z0-9_-`; the server won't start otherwise. `openssl rand -hex 32` works |
| `RATIO_MCP_HOST` | no | `127.0.0.1` | HTTP bind address. Anything else exposes the server to the network |
| `RATIO_MCP_PORT` | no | `8000` | HTTP port |
| `RATIO_MCP_ALLOWED_HOSTS` | behind a proxy | loopback only | Comma-separated `Host` values to accept, for example `ratio.example.com` |
| `RATIO_MCP_ALLOW_PATH_SECRET` | no | off | `1`, `true` or `yes` also accepts `https://<host>/<secret>/mcp`, for clients that can't send headers. The secret then shows up in proxy logs |
| `RATIO_MCP_CONTROL_WARNING` | no | | Free text added to the server instructions and to the start/stop tool descriptions. Use it to tell the assistant that something else (Home Assistant, an OCPP backend) also controls the charger |
`ratio-mcp [stdio | streamable-http]` selects the transport (default `stdio`); `--help` prints usage. `python -m ratio_mcp` works too.
## Connecting clients
All of these need the HTTP setup above, reachable over HTTPS.
**claude.ai.** Add a custom connector with URL `https://<host>/mcp`, authentication set to "No sign-in", and the request header `Authorization: Bearer <secret>`.
**Codex and ChatGPT desktop.** Add a custom MCP server with URL `https://<host>/mcp` and a bearer token read from an environment variable holding the secret. In Codex that is `bearer_token_env_var` in the server's entry in `config.toml`. Don't commit the secret.
**ChatGPT web and mobile.** These can't send a custom `Authorization` header to an arbitrary URL. OpenAI's Secure MCP Tunnel works around that: run its `tunnel-client` next to the server (same Docker network, pointing at `http://ratio-mcp:8000/mcp`) and configure it to add `Authorization: Bearer <secret>` to forwarded requests. Add `ratio-mcp:8000` to `RATIO_MCP_ALLOWED_HOSTS`. An example `docker-compose.yml`:
```yaml
services:
ratio-mcp:
build: .
command: ["python", "-m", "ratio_mcp", "streamable-http"]
env_file: .env # RATIO_EMAIL, RATIO_PASSWORD, RATIO_MCP_SECRET (unquoted)
environment:
RATIO_MCP_ALLOWED_HOSTS: "ratio-mcp:8000"
volumes:
- ratio-data:/data
tunnel:
image: ghcr.io/openai/tunnel-client
depends_on: [ratio-mcp]
environment:
MCP_SERVER_URL: http://ratio-mcp:8000/mcp
MCP_EXTRA_HEADERS: "Authorization: file:/run/secrets/ratio_mcp_bearer"
MCP_OAUTH_DISCOVERY: disabled
command: ["--control-plane.api-key=file:/run/secrets/openai_tunnel_api_key"]
secrets:
- ratio_mcp_bearer
- openai_tunnel_api_key
secrets:
ratio_mcp_bearer:
file: ./secrets/ratio_mcp_bearer # one line: Bearer <secret>
openai_tunnel_api_key:
file: ./secrets/openai_tunnel_api_key
volumes:
ratio-data:
```
The server reads its secret from `RATIO_MCP_SECRET`, not from a file. The `ratio_mcp_bearer` file holds the full header value, `Bearer <secret>`. The `tunnel-client` options are OpenAI's and may change, so check their current documentation. In ChatGPT, set the connector's connection to Tunnel and authentication to none.
## Tools
`serial` is optional when your account has one charger. With more than one, the tool returns an error listing the serials.
| Tool | Type | What it does |
|---|---|---|
| `list_chargers` | read | Serial numbers and cloud connection state |
| `get_charger_status` | read | Charging state, power in watts, connected vehicle, whether start/stop is allowed, firmware update status |
| `list_vehicles` | read | Vehicles on the account (IDs usable with `start_charging`) |
| `get_settings` | read | User, solar and schedule settings. A section that fails is omitted and listed under `errors` |
| `get_diagnostics` | read | Firmware versions, connection and backend state. SSID, IP addresses, server URLs and controller serials are left out |
| `get_session_history` | read | One page of past sessions, newest first. Optional `begin` and `end` (ISO 8601); pass the returned `next_token` for the next page |
| `start_charging` | write | Starts a session; optional `vehicle_id` |
| `stop_charging` | write | Stops the active session |
A write sends the command once, never retries it, then polls for up to 10 seconds. The result's `outcome` is one of:
- `confirmed`: the new state was seen. Something else can still change it afterwards.
- `unconfirmed`: the command may have been sent but the change wasn't seen. Check `get_charger_status` before trying again.
- `already_stopped`: `stop_charging` found no active session and sent nothing.
- `refused`: `start_charging` wasn't allowed by the charger and sent nothing.
If Home Assistant, an OCPP backend or an automation also drives the charger, it can undo a command right after `confirmed`. Set `RATIO_MCP_CONTROL_WARNING` so the assistant knows.
## Security
In HTTP mode every request to `/mcp` needs `Authorization: Bearer <secret>`. Anything else, including a wrong secret, another path or another auth scheme, gets a bare 404 with no `WWW-Authenticate` header, so clients don't start OAuth discovery. The server binds to loopback by default, validates `Host` and `Origin` headers, and has no TLS of its own, so serve it only behind an HTTPS proxy.
There are no per-user permissions: whoever has the secret controls the charger. To rotate it, change `RATIO_MCP_SECRET`, restart, and update every client. Your Ratio password sits in the server's environment and tokens in `RATIO_TOKEN_PATH`, so protect both.
Outbound traffic is limited to the Ratio API and AWS Cognito over HTTPS, without following redirects. Errors returned to the client are generic (`retry later`, `re-auth needed`, `unreachable`, `internal error`, an HTTP status, or a validation message such as `unknown charger serial`); tokens, URLs and response bodies are never included. Known gaps: DNS rebinding and IP-literal hosts on the outbound allow-list.
Run a single worker; the client, token file and cache are per process. To report a vulnerability, see [SECURITY.md](SECURITY.md).
## Development
```bash
python3 -m venv .venv
.venv/bin/pip install -e . --group dev # needs pip 25.1+, or: uv pip install -e . --group dev
.venv/bin/pytest -q
.venv/bin/ruff check . && .venv/bin/ruff format --check . && .venv/bin/mypy
```
`RATIO_LIVE=1 RATIO_EMAIL=... RATIO_PASSWORD=... PYTHONPATH=src .venv/bin/python3 scripts/live_verify.py` runs a read-only check against a real account and prints key names and shapes only. Contributor conventions are in [CLAUDE.md](CLAUDE.md).
## License
MIT. See [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues