Skip to main content
Glama
README.md
# fitness-mcp



An [MCP](https://modelcontextprotocol.io) server and analyzer that lets an LLM answer
questions about cycling power data: *"How hard was Saturday's ride?"*, *"Am I fresh or
fatigued?"*, *"How did this week compare to last?"*

All bundled data is **synthetic**, produced by a seeded generator. No real athlete data is
included anywhere in this repository.

![Training load chart](docs/training-load.png)

## Problem

Ride files are raw 1 Hz streams of watts and heart rate. An LLM cannot reliably compute
normalized power from 4,000 numbers pasted into a prompt, and it should not have to. This
project parses the files, computes the standard training-science metrics deterministically
(NP, IF, TSS, and the CTL/ATL/TSB fitness model), and exposes the results as small,
typed MCP tools so the model reasons over *answers*, not streams.

## Architecture

```mermaid
flowchart LR
    Files["Ride files<br/>GPX / JSON / CSV / FIT"] --> Parsers["parsers.py"]
    Synth["synth.py<br/>seeded generator"] -.writes.-> Files
    Parsers --> Activity["Activity model<br/>(parallel streams)"]
    Activity --> Metrics["metrics.py<br/>NP / IF / TSS / CTL-ATL-TSB"]
    Metrics --> Service["service.py<br/>analysis API"]
    Service --> MCP["server.py<br/>MCP tools (stdio)"]
    Service --> CLI["cli.py<br/>chart / list / generate"]
    MCP --> Client["Claude Desktop,<br/>Claude Code, any MCP client"]
    CLI --> PNG["training-load.png"]
```

## Quickstart (under 2 minutes)

Requires Python 3.13.

```bash
git clone <this repo> fitness-mcp && cd fitness-mcp
python3.13 -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"          # add ",fit" for FIT file support

pytest -q                        # 32 tests
fitness-cli list | head          # NP / IF / TSS per ride
fitness-cli chart --out chart.png
fitness-mcp                      # starts the MCP server on stdio
```

Register with an MCP client (example for Claude Code):

```bash
claude mcp add fitness -- /absolute/path/to/fitness-mcp/.venv/bin/fitness-mcp
```

Configuration is by environment variable: `FITNESS_MCP_DATA` (directory of ride files,
default is the bundled samples) and `FITNESS_MCP_FTP` (watts, default 250).

## Tools

| Tool | Purpose |
|---|---|
| `list_activities(since?, limit?, ftp?)` | Rides with duration, NP, IF, TSS. Use first to discover ids. |
| `activity_summary(activity_id, ftp?)` | NP, IF, TSS, variability index, best 5/20 min, HR, elevation. |
| `training_load(days?, ftp?)` | CTL, ATL, TSB per day plus a `fresh`/`neutral`/`fatigued` label. |
| `compare_activities(activity_id_a, activity_id_b, ftp?)` | Side-by-side metrics and deltas. |
| `weekly_report(week_start?, ftp?)` | Mon-Sun totals, week-over-week TSS change, end-of-week form. |

## Input formats

* **GPX**: `trkpt` with `time`, `ele`, and `<extensions><power>` / `<hr>`.
* **JSON**: `{"name", "start", "interval_s", "power": [...], "heart_rate": [...]}`.
* **CSV**: `# name:`, `# start:`, `# interval_s:` header lines, then `power,heart_rate,altitude` columns.
* **FIT**: via the optional `fitparse` extra. Parsing is implemented but not covered by a
  test fixture (no FIT writer is available to generate a synthetic one).

Regenerate the samples with `fitness-cli generate --out data/samples` (deterministic, seed
`20260101`).

## Metrics

| Metric | Definition |
|---|---|
| NP | 4th-root of the mean of the 4th power of the 30 s rolling average power |
| IF | NP / FTP |
| TSS | duration_s x NP x IF / (FTP x 3600) x 100 (one hour at FTP = 100) |
| CTL / ATL | Exponentially weighted daily TSS, time constants 42 and 7 days |
| TSB | CTL - ATL from the end of the previous day |

Tests include known values: one hour at FTP is exactly 100 TSS, 75% FTP for an hour is 56.25,
the NP of an aligned 100/300 W square wave is 253.04, a single 100 TSS day yields
CTL = 100 x (1 - e^(-1/42)), and a long constant load converges to CTL = ATL = that load.

## Design decisions

* **Logic lives in `service.py`, not in the MCP layer.** The server and CLI are thin
  wrappers, so everything is testable without a protocol client, and a second transport is
  cheap. One test still drives the real server through an in-memory MCP client.
* **Compute in code, return small JSON.** Tools return rounded summaries rather than raw
  streams, keeping tool results small and the numbers exact.
* **Sample interval aware.** The NP window is `round(30 / interval_s)` samples, so 5 s
  sampling (used for the bundled data to keep the repo small) and 1 Hz data both work.
  Real files with dropped samples are treated as uniformly spaced, a deliberate
  simplification.
* **Standard definitions, stated assumptions.** TSB uses the previous day's CTL/ATL, the
  model is seeded at zero on the first ride (early values understate fitness), and FTP is a
  parameter, never inferred.
* **Windowing never resets the model.** `training_load(days=28)` integrates from the first
  ride and then slices, so recent values are not distorted by the window.
* **Synthetic-only data, deterministic generator.** Safe to publish, reproducible, and
  rides are structured (sweet spot, VO2, group-ride surges) so the metrics are meaningful.
* **`mcp` 2.x (`MCPServer`).** Pinned to `>=2.3,<3`; the older `FastMCP` import path was
  removed in 2.x.
* **No cache invalidation beyond file mtimes.** Parsed activities are cached per directory
  keyed on file names and mtimes; enough for a local single-user server.

## Development

```bash
ruff check . && ruff format --check . && pytest -q
```

## License

MIT

TDQS

B3.3/5.0

Scored across 5 tools

Disambiguation5/5

Each tool targets a clearly distinct output: activity listing, single-activity detail, multi-day load trends, pairwise comparison, and weekly aggregation. Overlap between training_load and weekly_report is minimal because one is a rolling CTL/ATL/TSB window and the other is calendar-week totals.

Naming Consistency4/5

All names use consistent snake_case and are readable, but the verb pattern is mixed: list_ and compare_ are verbs while activity_summary, training_load, and weekly_report are noun phrases. The deviation is minor and does not impede selection.

Tool Count5/5

Five tools is well-scoped for a fitness analytics server, with each tool covering a distinct analytical need. There is no obvious filler or missing high-level operation at this count.

Completeness4/5

The surface covers core ride analysis: listing, detail, load trends, comparison, and weekly totals. Minor gaps exist around activity streams, segments, or goal tracking, but the stated metrics-focused purpose is well served.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues