Skip to main content
Glama
README.md
<img src="packages/pwa/public/icon-192.png" width="96" alt="" />

# dotworkout

[![CI](https://github.com/javierferrersb/dotworkout/actions/workflows/ci.yml/badge.svg)](https://github.com/javierferrersb/dotworkout/actions/workflows/ci.yml)
[![npm](https://img.shields.io/npm/v/@dotworkout/mcp?label=%40dotworkout%2Fmcp)](https://www.npmjs.com/package/@dotworkout/mcp)

An MCP server that lets Claude, ChatGPT and other agents build custom Apple
Watch workouts. Describe the session in plain language and get back a
`.workout` file the Workout app actually accepts.

> Make me a Norwegian 4×4 — four by four minutes in zone 4 with three minute
> jogs between. Use dotworkout.

Rather build it yourself? The same workouts can be made by hand in the
[browser app](#build-one-by-hand-in-the-browser) at
<https://workout.javierferrersb.dev>.

## Connect it

**Hosted**, for anything that cannot run a process — a phone, ChatGPT, or Claude
in a browser. Add this as a custom connector; there is nothing to install and no
account to make:

```
https://mcp.javierferrersb.dev/mcp
```

In Claude that is Settings → Connectors → Add custom connector. In ChatGPT it is
Settings → Apps → Advanced → Developer mode.

**Local**, for [Claude Code](https://claude.com/claude-code) and other clients
that can spawn one. `npx` fetches it on first run:

```bash
claude mcp add -s user dotworkout -- npx -y @dotworkout/mcp
```

`-s user` registers it everywhere rather than in the current directory only.

## Ask for a workout

End the request with **use dotworkout** so the model reaches for the tools
rather than answering from memory:

> Build me a swimming pyramid workout and save it to my desktop. Use dotworkout.

> 8×50 on 1:00 with the last four as build, then 4×100 on 2:00. Use dotworkout.

> What targets does an indoor cycle actually support? Build me a 45 minute
> session using them. Use dotworkout.

All fourteen activities the Workout app offers custom workouts for — swimming,
running, cycling, rowing, HIIT, strength, yoga and the rest — with warm ups,
cool downs, repeats, time and distance goals, and heart-rate, pace, power and
cadence alerts.

## Getting it onto your Watch

Hosted, the server returns a link with the workout encoded inside it. Open it on
your iPhone and tap Save the workout; it lands in the Fitness app and syncs to
the Watch. Nothing is uploaded and nothing is stored —
the link _is_ the workout.

Run locally, it writes the file to a path you give it and prints a QR code that
opens the same link on your phone.

The [privacy policy](https://workout.javierferrersb.dev/privacy) covers both
the composer and the hosted server.

## Why it works

A model cannot write this format from memory. It is undocumented binary
protobuf, and nothing says which goals and alerts each sport allows — ask for a
power target on a swim and the Watch rejects the file. So the server gives the
agent the real answers instead of letting it guess:

| Tool                | Does                                                      |
| ------------------- | --------------------------------------------------------- |
| `list_activities`   | Every activity with the goals and alerts it supports      |
| `describe_activity` | One activity in detail, including unverified combinations |
| `validate_workout`  | Check a workout and say exactly what is wrong             |
| `create_workout`    | Build a `.workout` file                                   |
| `inspect_workout`   | Decode an existing workout and describe it                |

The activity rules were read off a real device and live in
[`constraints/compatibility.json`](constraints/compatibility.json). The format
itself was worked out from 20 real exports, all of which decode with zero
unknown fields and re-encode byte for byte — details in
[`spec/FORMAT.md`](spec/FORMAT.md).

Every tool, the client quirks, and how to host your own copy on Cloudflare's
free plan are in [`packages/mcp/README.md`](packages/mcp/README.md).

## Build one by hand in the browser

**<https://workout.javierferrersb.dev>** — free, no account, nothing to install.
It is also where the server's links open.

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="docs/builder-dark.png" />
  <img src="docs/builder-light.png" alt="The workout builder showing Thursday intervals, an outdoor run of 7.4 km in five steps: a 2 km warm up, 8 × 400 m with 1:30 rest in zone 4, a 3:00 rest, 6 × 200 m with 1:00 rest at 4:10 per km, and a 1 km cool down, under a strip drawing the session's shape." />
</picture>

Pick a sport and the workout opens as a list of steps — warm up, sets, rests,
cool down — under a strip that draws the session's shape. Each step takes a
distance or a time, repeats, rest, and a heart-rate, pace, cadence or power
target, and only what the Watch supports for that sport is offered.

It is built for the keyboard. `S` adds a set, each choice is one letter and
Enter moves to the next field, so 8 × 400 m with 90 seconds' rest in zone 4 is
`S` `D` `400m` `8` `90` `H` `Z` `4`. On a phone every step is a tap.

Download the file on your iPhone and it opens in the Fitness app, or send it
there as a link or a QR code. The workout travels inside the link and nothing
is uploaded. Install the page as an app and it works offline. More in
[`packages/pwa/README.md`](packages/pwa/README.md).

## Using the libraries directly

The server and the app sit on two TypeScript packages you can use on their own.

| Package                | What it does                                                           |
| ---------------------- | ---------------------------------------------------------------------- |
| `@dotworkout/mcp`      | The MCP server.                                                        |
| `@dotworkout/pwa`      | The browser app. Runs entirely on the client. Not published.           |
| `@dotworkout/domain`   | Builds and validates workouts. Uses the codec.                         |
| `@dotworkout/codec`    | Decodes and encodes the binary format. Nothing else.                   |
| `@dotworkout/notation` | Parses sets written as text, like `8x50 on 1:00 Build`. Not published. |

```bash
npm install @dotworkout/domain
```

That pulls in the codec. Install `@dotworkout/codec` on its own if all you want
is to read and write files.

### Writing a workout

```ts
import { swim } from "@dotworkout/domain";

const bytes = swim("Thursday threshold")
  .warmup(400)
  .repeat(8)
  .of(50)
  .rest(30)
  .label("Build")
  .repeat(4)
  .of(100)
  .on("2:00")
  .cooldown(200)
  .toBytes();
```

`.on("2:00")` is the `DISTANCE_TIME` goal: leave every two minutes no matter how
fast you finish. Most swim sets are written this way. Only swimming offers it.

### Reading and editing one

```ts
import { decode, encode } from "@dotworkout/codec";
import { editStepAt, totalDistance } from "@dotworkout/domain";

const workout = decode(bytes);
const totals = totalDistance(workout.customWorkout!);

const edited = editStepAt(
  workout,
  "custom_workout.interval_blocks[0].interval_steps[0].workout_step",
  (step) => {
    step.displayName = "Build (fins)";
  },
);

encode(edited);
```

Everything you did not touch comes back byte for byte.

## Commands

```bash
npm test
```

Builds the packages and runs the suite on Node's test runner. Also:

- `npm run build` — TypeScript only.
- `npm run generate` — regenerate the protobuf bindings from `proto/` and the
  compatibility data from `constraints/compatibility.json`. Both outputs are
  checked in, so a normal build needs neither protoc nor the buf CLI.
- `npm run check` — regenerate, then build and test.

## Releasing

The Worker at `mcp.javierferrersb.dev` deploys itself from `main` once the tests
pass. The npm package goes out in three steps:

1. In a PR, run `npm version <x.y.z> --workspace @dotworkout/mcp --no-git-tag-version`
   and set `VERSION` in `packages/mcp/src/version.ts` and both `version` fields
   in `packages/mcp/server.json` to match (a test checks they agree). Merge it.
2. Tag the merge commit and push the tag:

   ```bash
   git switch main && git pull
   git tag mcp-v0.7.0
   git push origin mcp-v0.7.0
   ```

   CI runs the tests on the tagged commit, then stages `@dotworkout/mcp` on npm
   with trusted publishing (no npm token is stored). A staged version is not
   live yet.

3. Approve it on npmjs.com (the package's staged versions, with two-factor), or
   from a terminal with `npm stage list @dotworkout/mcp` and
   `npm stage approve <stage-id>`. `npm stage download <stage-id>` fetches the
   tarball to look at first, and `npm stage reject <stage-id>` drops it.

4. Once it is live on npm, list the new version in the
   [MCP Registry](https://registry.modelcontextprotocol.io). The registry checks
   the npm package's `mcpName`, so npm has to come first:

   ```bash
   cd packages/mcp
   mcp-publisher login github
   mcp-publisher publish
   ```

   `brew install mcp-publisher` installs the CLI; `mcp-publisher validate`
   checks `server.json` without publishing.

The job refuses a tag that is not on `main`, one that does not match the package
version, a version already on npm, and an `@dotworkout/*` dependency pinned to a
version npm does not have yet. Run `node scripts/check-release.mjs mcp-v0.7.0`
to see the same checks locally.

`@dotworkout/codec` and `@dotworkout/domain` are still published by hand, before
the `mcp` release that needs them.

## The conformance suite

For each of the 20 files in `testdata/`, three assertions:

1. `decode(bytes)` matches the paired `.json`
2. `encode(decode(bytes))` is byte-identical to the input
3. decoding produces zero unknown fields

The third one is the point. Protobuf keeps fields it does not recognise and
writes them back out, so nine of these files round-trip perfectly against a
schema that is missing fields. Byte-identical output is not evidence of a
correct schema. Checking for unknown fields is — and the check walks the whole
tree, since `SpeedAlert.speed_target` sits four levels down.

`packages/codec/test/unknown-fields.test.ts` is the control. It breaks the schema
at runtime in six of the ten ways the upstream schema was actually broken, and
checks that assertion 3 catches each one. Without that, a passing assertion 3
only proves the schema agrees with itself.

## Where the rules live

| Kind of rule                             | Lives in                           |
| ---------------------------------------- | ---------------------------------- |
| Wire structure                           | `proto/`, as protovalidate options |
| Which goals and alerts each sport allows | `constraints/compatibility.json`   |
| Everything else                          | `spec/FORMAT.md`                   |

The compatibility matrix is read at build time into
`packages/domain/src/generated/compatibility-data.ts`. It is never restated in
TypeScript or in prose. A test recomputes the source file's SHA-256 and fails if
the JSON changed without regenerating.

Entries carry a confidence level. Confirmed ones are enforced: a power alert on
a swim is an error. Unverified ones warn and are still allowed, because the
matrix came off one device on one day, and a combination nobody tested is not a
combination known to be illegal. Being too permissive costs you a file the Watch
declines to import; being too strict costs you a workout you cannot create at
all. `validateWorkout(msg, { downgradeToWarning })` takes a list of issue codes,
so you never have to edit the library when the matrix is wrong.

## What this does not do

- **Convert units.** 100 m written as miles is 0.06 mi, which is 96.6 m.
  Conversion loses data, so authored units are kept as they are. `toMeters()`
  exists for display and is never written back.
- **Count laps.** Pool length is chosen on the Watch when the workout starts and
  is not in the file.
- **Assume a container.** `decode()` branches on field 10 (`SingleGoalWorkout`)
  against field 11 (`CustomWorkout`) and fails with a clear message on anything
  else. Pacer and swim-bike-run workouts probably use further fields; when one
  turns up, the error asks for a corpus file.

## Still unknown

`spec/FORMAT.md` §9 lists what has not been verified. The two that would change
this library most:

- **Pacer and swim-bike-run containers.** Structure unknown. The decoder fails
  loudly rather than guess.
- **`PowerAlert.PowerBound.unit`.** Only ever seen as `1`, assumed to be watts.

Every question settled so far was settled by exporting another `.workout` from
the phone and adding it to `testdata/`.

## Credits

The protobuf schema started from
[changeforan/DotnetWorkoutKit](https://github.com/changeforan/DotnetWorkoutKit)
(MIT), which covers steps, blocks, activities, locations and the
time/distance/open goals. Ten fields and enum values were missing; those
corrections are listed in `spec/FORMAT.md` §8. No code was taken.

Not affiliated with Apple.

## Licence

MIT. See [LICENSE](LICENSE).