healthsql
# HealthSQL
Your whole Apple Health record as a local SQLite database, so that an AI agent on your computer can answer questions about your body. The agent queries it with SQL, from a shell or as an MCP server.
Apple Health lives on the iPhone. Apple gives it no way to a computer except a manual export, and that export is out of date the day you make it. HealthSQL has two parts:
1. **An iPhone app** that copies every Health record to its own folder in your iCloud Drive and keeps the copy current.
2. **`healthsql`, this repository.** One Python file that reads that folder and builds the database. Claude Code, Codex, Claude Desktop, Cursor, and any other agent that has a shell or speaks MCP can then query it.
```
Watch, ring, scale → Health on the iPhone → the HealthSQL app → your iCloud Drive → healthsql → your agent
```
There is no server and no account. Your Apple Account is the only link between the phone and the computer: iCloud Drive carries the files.
Guides and the full reference are at [healthsql.app](https://healthsql.app).
> **The iPhone app is with Apple for review.** Until it is in the App Store, you can try the command on the sample data in this repository: see [Try it without an iPhone](#try-it-without-an-iphone).
## Install
```sh
curl -fsSL https://healthsql.app/install | sh
```
This saves one file to `~/.local/bin/healthsql` and then starts `healthsql setup`, which checks the computer, builds the database, and prints how to connect each agent. It needs Python 3.9 or later and nothing else. On a Mac, `xcode-select --install` gives you Python.
You can read the code before you use it. It is [`healthsql.py`](healthsql.py): one file, standard library only.
With uv or pipx:
```sh
uv tool install git+https://github.com/declankra/healthsql
```
```sh
pipx install git+https://github.com/declankra/healthsql
```
The computer must be signed in to the same Apple Account as the iPhone, with iCloud Drive on. The first build reads your whole history, so a record of millions of samples takes a few minutes.
## Connect your agent
An agent that has a shell needs nothing more: it can call `healthsql` and read the answer.
For MCP, the command registers itself with the client you name:
```sh
healthsql connect claude-code # or: codex, claude-desktop, cursor
```
For any other MCP client, add a stdio server whose command is the full path to `healthsql` and whose one argument is `mcp`:
```json
{
"mcpServers": {
"healthsql": { "command": "/Users/you/.local/bin/healthsql", "args": ["mcp"] }
}
}
```
HealthSQL is in the [MCP registry](https://registry.modelcontextprotocol.io/v0.1/servers?search=io.github.declankra/healthsql) as `io.github.declankra/healthsql`.
The server has two tools:
| Tool | What it does |
|---|---|
| `health_overview` | Says what the database holds: when the iPhone last synced, each view with its columns, example queries, and every metric with its unit and date range. The agent calls it once. |
| `health_query` | Takes one read-only SQL statement and returns the rows as JSON. |
You can also give the whole setup to your agent. Tell it:
```text
Set up HealthSQL on this computer so you can read my Apple Health data. Follow https://healthsql.app/agents.md and tell me what my record covers when you are done.
```
## Ask in SQL
```sh
healthsql # start here: the views, their columns, examples, every metric
healthsql query "SELECT day, steps, resting_hr, hrv_ms, asleep_min FROM daily ORDER BY day DESC LIMIT 7"
healthsql query --csv "SELECT * FROM daily" > daily.csv
healthsql status # is the feed alive, and what to do if it is not
healthsql sync # read every waiting file from the iPhone now
```
Each command first reads the files that are new, so an answer is as fresh as the phone's last sync. Every answer is JSON and says how fresh it is:
```json
{"rows":[{"day":"2026-11-30","steps":1200,"active_kcal":1150.0,"hr_avg":151.21,"asleep_min":420.0}],
"truncated":false,"phone_synced":"2026-11-30T14:17:30-08:00"}
```
The views:
| View | One row for each | Use it for |
|---|---|---|
| `daily` | day | Any question about a day or a trend: steps, distance, energy, heart rate, HRV, weight, sleep and more, side by side. |
| `days` | metric and day | Apple Health's own total for every metric it holds, with `sum` for a metric that adds up and `avg`, `min`, `max` for one that does not. |
| `nights` | night of sleep | Time asleep and in each stage, from one source. |
| `nights_by_source` | night and source | A watch compared with a ring, or a source other than the one `nights` chose. |
| `sleep` | sleep stage interval | The stages of a night, minute by minute. |
| `workouts` | workout | Activity, minutes, distance, heart rate, and the whole record as JSON. |
| `samples` | raw measurement | A value at a time of day, or one source's own numbers. |
| `rings` | day | Move, Exercise and Stand, with the goals. |
Under the views is one table, `records`, with every record the phone sent: quantities, categories, workouts, GPS routes, State of Mind entries, ECG summaries.
## What it is careful about
- **Daily totals are Apple Health's own.** A watch, a phone and a ring each record the same walk, so a sum of the raw step samples counts it two or three times. Only Health knows which source counts for which minute. The iPhone app asks Health for each day's total, the number the Health app shows, and `daily` and `days` hold those numbers.
- **Every answer says how fresh it is.** `phone_synced` is the phone's last export. When something is wrong (the phone went quiet, files are still downloading, Health refused a type), there is one `problem` line that names what you must do.
- **Unknown is not zero.** A day that has no total has no row.
- **A night comes from one source.** If a watch and a ring both recorded it, `nights` picks the one with sleep stages and `nights_by_source` shows both.
- **Units do not depend on the phone's region.** Lengths are meters, mass is kg, energy is kcal, temperature is degC, and a percent is a 0-1 fraction.
- **Deletions carry over.** A record deleted on the phone leaves the database.
- **Queries cannot change anything.** The database opens read-only for every query.
## Privacy
- The command makes no network requests. It reads the files in your iCloud Drive folder and writes one database file on your computer (on a Mac: `~/Library/Application Support/HealthSQL/health.db`). You can delete that file at any time; the next command builds it again.
- The rows your agent reads enter its context, so they go to the model provider that agent uses. With a local model they go nowhere.
- What the iPhone app does is in the [privacy policy](https://healthsql.app/privacy).
## Limits
- iOS lets no app read Health while the iPhone is locked. New data arrives after you next unlock the phone.
- Not in the export: clinical records, medications, vision prescriptions, ECG voltages, heartbeat series, audiograms.
- We test on macOS. The command also knows where iCloud for Windows keeps the folder, but that path is not tested yet.
- HealthSQL is not a medical device, and an agent is not a doctor.
## Try it without an iPhone
The repository has a small sample export: one made-up day, written by the app in the iOS Simulator.
```sh
git clone https://github.com/declankra/healthsql && cd healthsql
export HEALTHSQL_EXPORT_DIR="$PWD/testdata/export" HEALTHSQL_DB=/tmp/healthsql-sample.db
python3 healthsql.py
python3 healthsql.py query "SELECT day, activity, minutes, meters, avg_hr FROM workouts"
python3 healthsql.py mcp # an MCP server on stdin and stdout
```
The sample is from 30 November 2026. Because that is more than three days ago, each answer has a `problem` line that says the iPhone last synced some days ago. That is what you would see for a real phone that stopped exporting.
## In this repository
- [`healthsql.py`](healthsql.py): the command.
- [`test_healthsql.py`](test_healthsql.py): its behavior checks. Run `python3 -m unittest`.
- [`FORMAT.md`](FORMAT.md): the files the iPhone app writes. Any program can read them.
- [`testdata/`](testdata): the sample export.
## Feedback
If HealthSQL cannot do what you need, or a number looks wrong, write to contact@dkbuilds.co or open an issue. A person reads every message.
## License
MIT. Copyright (c) 2026 dkBuilds LLC.
<!-- mcp-name: io.github.declankra/healthsql -->
TDQS
Scored across 2 tools
The two tools occupy clearly separate phases: health_overview is a one-time schema/metric discovery call, while health_query executes SQL. The descriptions explicitly sequence them ('Call this first, once' vs 'Run one read-only SQLite query'), leaving no room for misselection.
Both names follow a strict `health_<noun>` snake_case pattern, and the nouns (overview, query) accurately reflect each tool's role. No mixed conventions or stray verbs.
Two tools is on the thin side, but for a read-only SQL interface the discovery+execute pair is a legitimate minimal design rather than a gap. A third convenience tool (e.g. saved/favorite queries or a metric catalog) could add value but is not required.
Arbitrary read-only SQL over documented views means any analytical question is answerable, and health_overview prevents dead ends by exposing every view, column, and metric. Write operations and any result-export or pagination helpers are absent, though read-only scope appears intentional for a personal health database.