@trainheroic-unofficial/athlete-mcp 4.1.1 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/server.mjs +71 -60
  2. package/package.json +8 -8
package/dist/server.mjs CHANGED
@@ -2,7 +2,7 @@ import { CLIENT_CAPABILITIES_META_KEY, McpServer, acceptedContent, inputRequired
2
2
  import { serveStdio } from "@modelcontextprotocol/server/stdio";
3
3
  import process from "node:process";
4
4
  import { z } from "zod";
5
- //#region ../dto/src/common.ts
5
+ // ../dto/src/common.ts
6
6
  /** An entity id as it arrives over the wire — a number or a numeric string. */
7
7
  const idSchema = z.union([z.string(), z.number()]);
8
8
  /**
@@ -11,8 +11,7 @@ const idSchema = z.union([z.string(), z.number()]);
11
11
  * a bad argument fails validation instead of becoming NaN in a URL or a query.
12
12
  */
13
13
  const idArgSchema = z.union([z.number().int(), z.string().regex(/^\d+$/u)]);
14
- //#endregion
15
- //#region ../dto/src/athlete.ts
14
+ // ../dto/src/athlete.ts
16
15
  const intLike$1 = z.union([z.number(), z.string()]);
17
16
  const intLikeOrNull$1 = z.union([
18
17
  z.number(),
@@ -416,8 +415,7 @@ const presentedExerciseHistorySchema = z.object({
416
415
  });
417
416
  /** Presented history first so the loose raw detail schema cannot swallow it. */
418
417
  const exerciseHistoryOutputSchema = z.union([presentedExerciseHistorySchema, exerciseHistoryDetailSchema]);
419
- //#endregion
420
- //#region ../dto/src/exercise.ts
418
+ // ../dto/src/exercise.ts
421
419
  /**
422
420
  * A row presented for display. The raw param-type codes are dropped and the fixed
423
421
  * measurement units are surfaced positionally in `units`, ordered by entry slot
@@ -476,8 +474,7 @@ z.object({
476
474
  text: z.string().min(1),
477
475
  replyTo: idSchema.optional()
478
476
  });
479
- //#endregion
480
- //#region ../dto/src/responses.ts
477
+ // ../dto/src/responses.ts
481
478
  const intLike = z.union([z.number(), z.string()]);
482
479
  const intLikeOrNull = z.union([
483
480
  z.number(),
@@ -519,8 +516,7 @@ z.object({
519
516
  pub_time: z.unknown().optional(),
520
517
  pub_timezone: z.string().optional()
521
518
  }).refine((v) => v.pub_enabled !== void 0 || v.pub_days !== void 0 || v.pub_time !== void 0 || v.pub_timezone !== void 0, { message: "Provide at least one pub_* field" });
522
- //#endregion
523
- //#region ../dto/src/tool-output.ts
519
+ // ../dto/src/tool-output.ts
524
520
  const nullableNumber = z.number().nullable();
525
521
  const nullableString = z.string().nullable();
526
522
  const truncationMarkerSchema = z.looseObject({
@@ -656,6 +652,7 @@ const workoutReadBlockOutputSchema = z.object({
656
652
  });
657
653
  const workoutReadOutputSchema = z.object({
658
654
  pwId: z.number(),
655
+ workoutId: z.number().nullable(),
659
656
  date: z.string(),
660
657
  published: z.json().optional(),
661
658
  instruction: z.string(),
@@ -723,8 +720,7 @@ z.object({
723
720
  programId: z.number(),
724
721
  containerId: nullableNumber
725
722
  });
726
- //#endregion
727
- //#region ../dto/src/workout.ts
723
+ // ../dto/src/workout.ts
728
724
  /** A single exercise prescription inside a block. */
729
725
  const exerciseSpecSchema = z.object({
730
726
  id: z.union([z.number(), z.string()]),
@@ -736,8 +732,8 @@ const exerciseSpecSchema = z.object({
736
732
  ]).optional().describe("Primary slot values; may be reps, distance, or time. State primaryUnit."),
737
733
  sets: z.number().optional(),
738
734
  weight: z.union([z.number(), z.array(z.number())]).optional().describe("Secondary slot values; may be weight or time. State secondaryUnit."),
739
- primaryUnit: z.string().trim().min(1).optional().describe("Intended primary unit from exercise_resolve, such as reps, m, mi, or sec."),
740
- secondaryUnit: z.string().trim().min(1).optional().describe("Intended secondary unit from exercise_resolve, such as lb or sec."),
735
+ primaryUnit: z.string().trim().min(1).optional().describe("Unit of the primary values, such as reps, m, mi, or sec. A unit other than the exercise's default from exercise_resolve is set on this session."),
736
+ secondaryUnit: z.string().trim().min(1).optional().describe("Unit of the secondary values, such as lb, %max, RPE, or sec. A unit other than the exercise's default from exercise_resolve is set on this session."),
741
737
  rpe: z.union([z.number(), z.string()]).optional(),
742
738
  instr: z.string().optional(),
743
739
  param_1_type: z.number().optional(),
@@ -797,8 +793,12 @@ z.object({
797
793
  title: z.string().min(1),
798
794
  instruction: z.string().optional()
799
795
  });
800
- //#endregion
801
- //#region ../core/src/context.ts
796
+ z.object({
797
+ workoutId: z.number().int().describe("The session's workout_id (workout_build returns it as workoutId)."),
798
+ pwId: z.number().int().describe("The session's programWorkout id (workout_build returns it as pwId)."),
799
+ title: z.string().trim().min(1).describe("Title of the new library template.")
800
+ });
801
+ // ../core/src/context.ts
802
802
  /** A tool argument that accepts a numeric id as a number or a string of digits. */
803
803
  const idParam = idArgSchema;
804
804
  function toId(value) {
@@ -978,8 +978,7 @@ function errorResult(message) {
978
978
  }]
979
979
  };
980
980
  }
981
- //#endregion
982
- //#region ../core/src/confirm.ts
981
+ // ../core/src/confirm.ts
983
982
  /**
984
983
  * Returned when the user was asked and said no. It must not tell the model how to retry: the
985
984
  * refusal is the answer. (The `confirm: true` fallback for clients that cannot elicit is
@@ -1035,8 +1034,7 @@ function confirmGate(ctx, message, confirmArg) {
1035
1034
  }
1036
1035
  }) } });
1037
1036
  }
1038
- //#endregion
1039
- //#region ../core/src/instructions.ts
1037
+ // ../core/src/instructions.ts
1040
1038
  /**
1041
1039
  * Server-level guidance passed as the MCP `instructions` string. The host model receives it
1042
1040
  * in the initialize result and treats it like a system hint. Its job is to stop tool-layer
@@ -1046,8 +1044,7 @@ function confirmGate(ctx, message, confirmArg) {
1046
1044
  * leak further. This tells the host to keep that wiring internal and speak in app terms.
1047
1045
  */
1048
1046
  const SERVER_INSTRUCTIONS = "Speak to the user in plain, everyday language about their training. Describe what you are doing in the TrainHeroic app's own terms (for example, say you are creating a workout rather than naming a tool). Do not surface internal tool names (the snake_case identifiers such as athlete_session_create), raw parameter names, or numeric ids in your replies unless the user explicitly asks for them; they are implementation details. The tool descriptions cross-reference each other by name only so you can chain them correctly. Keep that wiring to yourself.";
1049
- //#endregion
1050
- //#region ../js/src/date-window.ts
1047
+ // ../js/src/date-window.ts
1051
1048
  const DAY_MS = 864e5;
1052
1049
  const ISO_DAY = /^\d{4}-\d{2}-\d{2}$/u;
1053
1050
  function dayNumber(value) {
@@ -1062,8 +1059,7 @@ function dateSpanDays(start, end) {
1062
1059
  const last = dayNumber(end);
1063
1060
  return first === null || last === null || first > last ? null : last - first + 1;
1064
1061
  }
1065
- //#endregion
1066
- //#region ../js/src/http-error.ts
1062
+ // ../js/src/http-error.ts
1067
1063
  const MAX_REQUEST_KEYS = 50;
1068
1064
  const MAX_RESPONSE_DEPTH = 4;
1069
1065
  const MAX_RESPONSE_KEYS = 20;
@@ -1264,6 +1260,28 @@ var TrainHeroicHttpError = class extends Error {
1264
1260
  this.responseBody = diagnostics.responseBody === void 0 ? void 0 : safeResponseBodyDiagnostics(diagnostics.responseBody);
1265
1261
  }
1266
1262
  };
1263
+ /**
1264
+ * True for statuses that mean TrainHeroic's servers could not be reached or did not answer: a
1265
+ * gateway failure (502, 503, 504) or a Cloudflare edge error (520-530). TrainHeroic sits behind
1266
+ * Cloudflare, so a 522 means the edge timed out connecting to TrainHeroic's origin. These describe
1267
+ * an upstream outage, so the same request can succeed when retried later.
1268
+ */
1269
+ function isTrainHeroicUnavailableStatus(status) {
1270
+ return status === 502 || status === 503 || status === 504 || status >= 520 && status <= 530;
1271
+ }
1272
+ /**
1273
+ * Thrown when TrainHeroic answers with an outage status (see `isTrainHeroicUnavailableStatus`).
1274
+ * The message tells the reader to retry later, so a model relaying it does not ask the user to
1275
+ * re-check credentials that are fine.
1276
+ */
1277
+ var TrainHeroicUnavailableError = class extends Error {
1278
+ name = "TrainHeroicUnavailableError";
1279
+ status;
1280
+ constructor(status) {
1281
+ super(`TrainHeroic is not responding right now (HTTP ${status}). Try again in a few minutes.`);
1282
+ this.status = status;
1283
+ }
1284
+ };
1267
1285
  /** Call observability hooks without allowing them to change SDK behavior. */
1268
1286
  function notifyHttpError(handler, method, url, status, diagnostics = {}) {
1269
1287
  if (!handler) return;
@@ -1271,11 +1289,9 @@ function notifyHttpError(handler, method, url, status, diagnostics = {}) {
1271
1289
  Promise.resolve(handler(new TrainHeroicHttpError(method, url, status, diagnostics))).catch(() => {});
1272
1290
  } catch {}
1273
1291
  }
1274
- //#endregion
1275
- //#region ../js/src/transport.ts
1292
+ // ../js/src/transport.ts
1276
1293
  const defaultTrainHeroicTransport = (url, init) => fetch(url, init);
1277
- //#endregion
1278
- //#region ../js/src/auth.ts
1294
+ // ../js/src/auth.ts
1279
1295
  const DEFAULT_AUTH_URL = "https://apis.trainheroic.com/auth";
1280
1296
  /**
1281
1297
  * The login endpoint, allowing an env override so a test harness can authenticate against a local
@@ -1291,8 +1307,10 @@ function authUrl() {
1291
1307
  return DEFAULT_AUTH_URL;
1292
1308
  }
1293
1309
  /**
1294
- * Authenticate against TrainHeroic. Returns the session bundle, or null on bad
1295
- * credentials. TrainHeroic returns only { id, scope, role, session_id } (verified in
1310
+ * Authenticate against TrainHeroic. Returns the session bundle, or null when TrainHeroic
1311
+ * rejects the login. Throws `TrainHeroicUnavailableError` when TrainHeroic is unreachable (an
1312
+ * outage status such as a Cloudflare 522), so callers can tell the user to retry later.
1313
+ * TrainHeroic returns only { id, scope, role, session_id } (verified in
1296
1314
  * the Phase 0 spike: no refresh_token, no api_token, no TTL). The 48-char session_id
1297
1315
  * is sent as the `session-token` header and works against both API hosts.
1298
1316
  */
@@ -1312,6 +1330,7 @@ async function loginTrainHeroic(email, password, options = {}) {
1312
1330
  if (!res.ok) {
1313
1331
  await res.body?.cancel().catch(() => {});
1314
1332
  notifyHttpError(options.onHttpError, "POST", url, res.status);
1333
+ if (isTrainHeroicUnavailableStatus(res.status)) throw new TrainHeroicUnavailableError(res.status);
1315
1334
  return null;
1316
1335
  }
1317
1336
  const data = await res.json().catch(() => null);
@@ -1323,13 +1342,12 @@ async function loginTrainHeroic(email, password, options = {}) {
1323
1342
  role: data.role ?? ""
1324
1343
  };
1325
1344
  }
1326
- //#endregion
1327
- //#region ../js/src/exercise-util.ts
1345
+ // ../js/src/exercise-util.ts
1328
1346
  /**
1329
- * Display labels for TrainHeroic parameter types. The unit is FIXED PER EXERCISE
1330
- * (the API forces param_1_type/param_2_type back to the library default on save), so
1331
- * resolve/search surface it to stop callers picking, say, the miles "Run" for a
1332
- * metric workout. Keep in sync with the workout builder's unit table.
1347
+ * Display labels for TrainHeroic parameter types. Each library exercise has a default type per
1348
+ * slot, which resolve/search surface so callers know, say, that stock "Run" defaults to miles.
1349
+ * A session can set a different type on an exercise; the workout builder maps a stated unit
1350
+ * back to its type code through this table, so keep it in sync with the builder's unit aliases.
1333
1351
  */
1334
1352
  const PARAM_UNIT = {
1335
1353
  0: null,
@@ -1362,7 +1380,7 @@ function unitLabel(paramType) {
1362
1380
  return PARAM_UNIT[t] ?? null;
1363
1381
  }
1364
1382
  /**
1365
- * Fixed measurement units for an exercise, ordered by entry slot (param 1, then param 2).
1383
+ * Default measurement units for an exercise, ordered by entry slot (param 1, then param 2).
1366
1384
  * Positional, not semantic: some exercises reverse the slots, so the array is not labelled
1367
1385
  * by role. A null entry is an unset slot.
1368
1386
  */
@@ -1502,8 +1520,7 @@ function createLimiter(max) {
1502
1520
  function assertPositiveInteger(value, label) {
1503
1521
  if (!Number.isInteger(value) || value < 1) throw new RangeError(`${label} must be a positive integer; received ${value}.`);
1504
1522
  }
1505
- //#endregion
1506
- //#region ../js/src/client.ts
1523
+ // ../js/src/client.ts
1507
1524
  const DEFAULT_COACH_BASE = "https://api.trainheroic.com";
1508
1525
  const DEFAULT_APIS_BASE = "https://apis.trainheroic.com";
1509
1526
  const MAX_REQUEST_CONCURRENCY = 4;
@@ -1623,8 +1640,7 @@ var TrainHeroicClient = class {
1623
1640
  return this.#transport(url, init);
1624
1641
  }
1625
1642
  };
1626
- //#endregion
1627
- //#region ../js/src/athlete.ts
1643
+ // ../js/src/athlete.ts
1628
1644
  async function getJson(client, path, label) {
1629
1645
  const res = await client.request("GET", path);
1630
1646
  if (!res.ok) throw new Error(`${label} failed (HTTP ${res.status}).`);
@@ -2111,8 +2127,7 @@ function presentExerciseHistory(detail) {
2111
2127
  }))
2112
2128
  };
2113
2129
  }
2114
- //#endregion
2115
- //#region ../js/src/exercise-history.ts
2130
+ // ../js/src/exercise-history.ts
2116
2131
  /**
2117
2132
  * Trim a presented exercise history's session time-series to an inclusive YYYY-MM-DD window.
2118
2133
  * The `liftPRs` board stays all-time (PRs are not a windowed concept). Dates compare as their
@@ -2133,8 +2148,7 @@ function historyInRange(presented, since, until) {
2133
2148
  sessions
2134
2149
  };
2135
2150
  }
2136
- //#endregion
2137
- //#region ../js/src/exercise-set-payload.ts
2151
+ // ../js/src/exercise-set-payload.ts
2138
2152
  function slotData(exercise, key) {
2139
2153
  const value = exercise?.[key];
2140
2154
  return value === void 0 || value === null ? "" : String(value);
@@ -2202,8 +2216,7 @@ function buildExerciseSetPayload(savedWorkoutSetExerciseId, savedWorkoutSetId, w
2202
2216
  body.completed = logging && anyMade && allActiveSlotsMade ? 1 : 0;
2203
2217
  return body;
2204
2218
  }
2205
- //#endregion
2206
- //#region ../js/src/athlete-set-write.ts
2219
+ // ../js/src/athlete-set-write.ts
2207
2220
  const WRITE_CONCURRENCY = 4;
2208
2221
  /**
2209
2222
  * Coerce the loosely-typed `results` from a validated log/prescribe args object into the SDK's
@@ -2724,8 +2737,7 @@ async function logAdHocSession(client, args) {
2724
2737
  if (scheduledAlternatives.length > 0) result.scheduledAlternatives = scheduledAlternatives;
2725
2738
  return result;
2726
2739
  }
2727
- //#endregion
2728
- //#region ../js/src/util.ts
2740
+ // ../js/src/util.ts
2729
2741
  /**
2730
2742
  * Drop the keys whose value is `undefined`, so the result can be passed to a function or
2731
2743
  * spread into an object under `exactOptionalPropertyTypes` without the per-key
@@ -2740,8 +2752,7 @@ function definedProps(obj) {
2740
2752
  for (const [key, value] of Object.entries(obj)) if (value !== void 0) result[key] = value;
2741
2753
  return result;
2742
2754
  }
2743
- //#endregion
2744
- //#region ../js/src/athlete-workout-note.ts
2755
+ // ../js/src/athlete-workout-note.ts
2745
2756
  /**
2746
2757
  * PUT /1.0/athlete/savedworkout/{id} — set the athlete's session note (the free-text box on the
2747
2758
  * workout screen) and/or session RPE. The date + programWorkoutId (`athlete_workouts` `id`) locate
@@ -2771,8 +2782,7 @@ async function setAthleteWorkoutNote(client, args) {
2771
2782
  rpe: coerceInt(data.rpe) ?? (args.rpe !== void 0 ? args.rpe : null)
2772
2783
  };
2773
2784
  }
2774
- //#endregion
2775
- //#region ../js/src/athlete-exercise-note.ts
2785
+ // ../js/src/athlete-exercise-note.ts
2776
2786
  /**
2777
2787
  * PUT /1.0/athlete/savedworkoutsetexercise/{id} — set the athlete's per-exercise note (the
2778
2788
  * "Add exercise note" box on the exercise screen). A notes-only body leaves logged reps/weight
@@ -2793,9 +2803,13 @@ async function setAthleteExerciseNote(client, args) {
2793
2803
  notes: typeof data.notes === "string" ? data.notes : args.notes
2794
2804
  };
2795
2805
  }
2806
+ (() => {
2807
+ const map = /* @__PURE__ */ new Map();
2808
+ for (const [code, unit] of Object.entries(PARAM_UNIT)) if (unit !== null) map.set(unit, [...map.get(unit) ?? [], Number(code)]);
2809
+ return map;
2810
+ })();
2796
2811
  z.number().int().positive().max(36).optional();
2797
- //#endregion
2798
- //#region ../core/src/tools/athlete-training.ts
2812
+ // ../core/src/tools/athlete-training.ts
2799
2813
  /** Identity, profile, prefs, working maxes, leaderboard. */
2800
2814
  function registerProfileTools(server, ctx, whoami, userId) {
2801
2815
  server.registerTool("athlete_whoami", {
@@ -3223,11 +3237,9 @@ function registerAthleteTrainingTools(server, ctx) {
3223
3237
  registerLogTool(server, ctx);
3224
3238
  registerSwapTool(server, ctx);
3225
3239
  }
3226
- //#endregion
3227
- //#region package.json
3228
- var version = "4.1.1";
3229
- //#endregion
3230
- //#region src/server.ts
3240
+ // package.json
3241
+ var version = "4.3.0";
3242
+ // src/server.ts
3231
3243
  function main() {
3232
3244
  const email = process.env.TRAINHEROIC_EMAIL;
3233
3245
  const password = process.env.TRAINHEROIC_PASSWORD;
@@ -3246,5 +3258,4 @@ function main() {
3246
3258
  });
3247
3259
  }
3248
3260
  main();
3249
- //#endregion
3250
3261
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/athlete-mcp",
3
- "version": "4.1.1",
3
+ "version": "4.3.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,17 +19,17 @@
19
19
  "dist"
20
20
  ],
21
21
  "dependencies": {
22
- "@modelcontextprotocol/server": "2.3.1",
23
- "zod": "^4.6.5",
24
- "@trainheroic-unofficial/core": "4.1.1",
25
- "@trainheroic-unofficial/js": "4.1.1"
22
+ "@modelcontextprotocol/server": "2.4.0",
23
+ "zod": "^4.6.7",
24
+ "@trainheroic-unofficial/core": "4.3.0",
25
+ "@trainheroic-unofficial/js": "4.3.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/node": "^26.6.4",
29
- "tsdown": "^0.23.0",
28
+ "@types/node": "^26.6.6",
29
+ "tsdown": "^0.23.7",
30
30
  "tsx": "^4.23.15",
31
31
  "typescript": "^7.0.2",
32
- "vitest": "^5.0.3"
32
+ "vitest": "^5.1.0"
33
33
  },
34
34
  "scripts": {
35
35
  "start": "tsx src/server.ts",