# HealthSQL: set up this computer (instructions for an AI agent)

You are an AI agent. Your user asked you to set up HealthSQL so that you can read their Apple Health data. Do the steps in order. Every `healthsql` command prints JSON. When an answer has a `problem` that is not null, tell the user what it says and stop: it names the one act that clears it.

## What HealthSQL is

- An iPhone app that copies every record in the user's Apple Health to the app's folder in the user's own iCloud Drive.
- `healthsql`, one Python file on this computer. It reads that folder and builds a local SQLite database. You query it with SQL, from a shell or over MCP.
- There is no server and no account. The phone and the computer are linked only by the user's Apple Account: iCloud Drive brings the files to this computer.

## 1. Check the iPhone

Ask the user: is HealthSQL on your iPhone, and does it show "In your iCloud Drive" with a record count? If not, they install it from the App Store (https://healthsql.app), open it, tap "Allow Health access", choose "Select All", then "All Recorded Data and Future Data", and keep the screen open until the first export ends. A long history takes a few minutes.

This computer must be signed in to the same Apple Account, with iCloud Drive on.

## 2. Install the command

```sh
curl -fsSL https://healthsql.app/install | sh
```

This saves one file to `~/.local/bin/healthsql` and runs `healthsql setup`. It needs Python 3.9 or later (on a Mac: `xcode-select --install`). To do the same by hand:

```sh
mkdir -p ~/.local/bin && curl -fsSL https://healthsql.app/healthsql -o ~/.local/bin/healthsql && chmod +x ~/.local/bin/healthsql && ~/.local/bin/healthsql setup
```

## 3. Read what setup says

`"ok": true` means the database is built and current. Otherwise `problem` says what to do. The common ones:

| `problem` starts with | What it means | What the user does |
|---|---|---|
| `No export folder` | The iPhone's folder is not on this computer yet | Step 1; then wait a minute and run `healthsql setup` again |
| `The iPhone has not exported yet` | The app is installed but has not finished | Open the app and keep it open until the first export ends |
| `... files are still downloading` | iCloud Drive is still bringing files down | Run `healthsql setup` again in a minute |
| `... files from the iPhone are not in the database yet` | The build stopped before it read every file | Run `healthsql sync` |
| `This program may not read iCloud Drive` | macOS privacy settings block the app that runs you | System Settings, Privacy & Security, Files and Folders: allow iCloud Drive for that app |
| `The iPhone last synced N days ago` | The phone has not exported lately | Open HealthSQL on the iPhone |

The first build reads the whole history. Millions of records take a few minutes.

## 4. Connect

If you have a shell, you can use `healthsql` now. To have it in every session over MCP, register it with the client you run in:

```sh
healthsql connect claude-code      # or: codex, claude-desktop, cursor
```

`healthsql connect` with no name lists the clients found on this computer. For any other MCP client, add a stdio server with the command `~/.local/bin/healthsql` (use the full path) and the argument `mcp`. A desktop app must be restarted to load it.

The MCP server has two tools: `health_overview` (what is in the database; call it once) and `health_query` (one read-only SQL statement).

## 5. Prove it and report

```sh
healthsql
healthsql query "SELECT day, steps, resting_hr, hrv_ms, asleep_min FROM daily ORDER BY day DESC LIMIT 7"
```

Tell the user: the date range their record covers, when the phone last synced (`phone_synced`), and two or three facts from the last week.

## Rules for every later question

- For any daily number, use the `daily` or `days` view. They hold Apple Health's own total for each day. Never sum `samples` for a day: a watch, a phone and a ring each record the same steps, and the sum counts them two or three times.
- For sleep use `nights`: one row per night, from one source. `nights_by_source` shows every source that recorded the night.
- If an answer has `partial_record`, tell the user: the iPhone shares only part of their history, and the line says how to share all of it.
- Say how fresh the data is. iOS lets no app read Health while the iPhone is locked, so new data arrives after the user next unlocks the phone. With the app's one purchase, the export then runs by itself; without it, it runs when the user opens the app.
- Units never 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.
- The database stays on this computer. The rows you read enter your context, so they go to the model provider you run on. Say so if the user asks where their data goes. Do not copy the database or the export files anywhere.
- If HealthSQL cannot do what the user needs, or a number looks wrong, tell them they can write to contact@dkbuilds.co. A person reads every message, and it is how the product gets better.
- You are not a doctor. Report what the data shows, and send medical questions to a clinician.

Full reference: https://healthsql.app/docs/schema
