Mi Fitness MCP
by kaarlyz
README.md
<p align="center">
<img src="assets/logo.png" alt="Mi Fitness MCP" width="96" height="96">
</p>
<h1 align="center">Mi Fitness MCP</h1>
<p align="center">
Local SQLite-backed <a href="https://modelcontextprotocol.io">Model Context Protocol (MCP)</a> server for Xiaomi Mi Fitness / Xiaomi Health wearable data (Smart Band & Watch).
</p>
<p align="center">
<a href="https://github.com/kaarlyz/mi-fitness-mcp/actions/workflows/ci.yml"><img src="https://github.com/kaarlyz/mi-fitness-mcp/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
<a href="LICENSE"><img src="https://img.shields.io/github/license/kaarlyz/mi-fitness-mcp" alt="License"></a>
</p>
- **Offline-first SQLite cache** with incremental watermarks, versioned migrations, and multi-device scoping
- **Full wearable coverage**: daily activity, sleep (stages, naps, efficiency, respiratory rate, sleep HR/SpO2), workouts, body measurements, heart rate, SpO2, stress, and daily band/watch metrics (RHR, VO2 max, PAI, vitality, training load)
- **Dual transport**: standard `stdio` for local MCP clients and authenticated Streamable HTTP + Legacy SSE (`--transport http`)
- **Cross-distro & headless ready**: stores credentials in the OS keyring when available, with automatic fallback to a restricted local credentials file (`chmod 600`) or environment variables on tiling WMs (Hyprland/Sway/i3), SSH, WSL, and containers
## Quick Start
### 1. Install
```bash
git clone https://github.com/kaarlyz/mi-fitness-mcp.git
cd mi-fitness-mcp
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
```
Or with [`uv`](https://docs.astral.sh/uv/):
```bash
git clone https://github.com/kaarlyz/mi-fitness-mcp.git
cd mi-fitness-mcp
uv sync
```
### 2. Get Xiaomi Credentials
1. Open `https://account.xiaomi.com` in your browser and sign in.
2. Open DevTools → Cookies for `account.xiaomi.com`.
3. Copy `userId` and `passToken`.
### 3. Configure & Verify
```bash
mi-fitness-mcp setup --mode mi_fitness_cloud --user-id "<userId>" --region sg --timezone Asia/Jakarta
mi-fitness-mcp doctor
```
Notes:
- Interactive setup prompts for `passToken` without echoing it to the terminal.
- For automation, pass `--pass-token-stdin` or set `MI_FITNESS_USER_ID` and `MI_FITNESS_PASS_TOKEN`.
- On systems without a running OS Secret Service (`NoKeyringError`), `setup` automatically stores credentials in `~/.config/mi-fitness-mcp/credentials.json` with `0600` permissions (or pass `--credential-store file`).
- Regional backends (`cn`, `de`, `i2`, `ru`, `sg`, `us`) are supported; `ru` remains the default if omitted, and global/EU accounts automatically discover and persist their accepted region.
### 4. Sync & Run
```bash
mi-fitness-mcp sync --start-date 2026-01-01 --end-date 2026-12-31
mi-fitness-mcp serve
```
To inspect exact XDG config, credentials, and SQLite database paths (useful before switching distros):
```bash
mi-fitness-mcp paths
```
## MCP Client Configuration
### `stdio` (Claude Desktop, Cursor, Antigravity, Zed)
See [mcp-server-config.example.json](mcp-server-config.example.json) and [examples/mcp-stdio.json](examples/mcp-stdio.json):
```json
{
"mcpServers": {
"mi-fitness": {
"command": "/path/to/mi-fitness-mcp/.venv/bin/mi-fitness-mcp",
"args": ["serve"]
}
}
}
```
### Streamable HTTP / Legacy SSE
Start the HTTP server (generates a random auth token if `MI_FITNESS_MCP_HTTP_TOKEN` / `--auth-token` is omitted):
```bash
mi-fitness-mcp serve --transport http --host 127.0.0.1 --port 8765
```
Connect using either the printed secret URL prefix (`/t/<token>/mcp` or `/t/<token>/sse`) or `Authorization: Bearer <token>` on `/mcp`. See [examples/mcp-http.json](examples/mcp-http.json) and [examples/mi-fitness-mcp.service](examples/mi-fitness-mcp.service).
## Supported Data & MCP Tools
17 MCP tools across 8 synchronized data types (`daily_activity`, `sleep`, `workouts`, `body_measurements`, `heart_rate`, `spo2`, `stress`, `daily_metrics`):
| Category | Tools |
|---|---|
| Connection & Sync | `get_connection_status`, `sync_data`, `get_data_coverage`, `get_profile` |
| Activity & Trends | `get_daily_summary`, `query_metric_series`, `query_daily_metrics`, `get_health_highlights` |
| Sleep & Vitals | `query_sleep`, `query_heart_rate`, `query_spo2`, `query_stress`, `query_body_measurements` |
| Workouts | `query_workouts`, `summarize_workouts`, `get_workout_records`, `compare_workout_periods` |
Full parameter and metric documentation is available in [docs/TOOLS.md](docs/TOOLS.md).
## Documentation
- [docs/SETUP.md](docs/SETUP.md) — installation, credential extraction, CLI commands, and HTTP/stdio setup
- [docs/DISTRO_MIGRATION.md](docs/DISTRO_MIGRATION.md) — backing up/moving between Linux distros, Hyprland/Sway/headless setups, and keyring troubleshooting
- [docs/TOOLS.md](docs/TOOLS.md) — full catalog of all 17 MCP tools, data types, and supported metrics
- [SECURITY.md](SECURITY.md) — credential storage model and safe usage guidelines
- [CONTRIBUTING.md](CONTRIBUTING.md) — local setup, testing, and PR checklist
## Endpoint Probe Utility
For debugging Xiaomi regional endpoints and raw cloud keys locally:
```bash
uv run python probe_mifitness.py --user-id "<userId>"
```
It prompts for `passToken` silently or accepts `--pass-token-stdin` / `MI_FITNESS_PASS_TOKEN`.
## Development
```bash
ruff check .
pytest -q
python -m build
```
CI runs on Python 3.11, 3.12, and 3.13, keeps HTTP tests offline via `respx`, checks `ruff`, measures coverage, builds a wheel, and smoke-tests CLI subcommands in a clean virtual environment.
## Configuration Compatibility
- SQLite schema upgrades run automatically through the versioned migration runner (`LATEST_SCHEMA_VERSION = 10`) without losing existing data.
- `busy_timeout` configures SQLite lock waiting in milliseconds (default `5000`).
- Legacy keys (`auto_sync_on_start`, `stale_after_minutes`, `store_raw_payloads`, `default_lookback_days`, `logs_path`) remain accepted when loading older `config.json` files as deprecated no-ops, and are omitted when saving fresh configs.
## Credits & Disclaimer
Originally based on [kubulashvili/mi-fitness-mcp](https://github.com/kubulashvili/mi-fitness-mcp) (MIT License), extended with sleep/SpO2/stress/daily band metrics, Streamable HTTP & Legacy SSE transport, and cross-distro credential portability.
This is an unofficial project and is not affiliated with Xiaomi.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues