@almostjacked/hevy-mcp-core 0.1.0 → 0.2.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.
@@ -91,12 +91,29 @@ export declare class HevyApiError extends Error {
91
91
  path: string;
92
92
  constructor(status: number, body: string, path: string);
93
93
  }
94
+ /**
95
+ * How the client authenticates to api.hevyapp.com.
96
+ * - a bare string or `{ apiKey }`: the Hevy PRO personal key, sent as the `api-key` header.
97
+ * - `{ accessToken }`: a Hevy OAuth access token (developers.hevy.com), sent as
98
+ * `Authorization: Bearer`. `accessToken` is called per request so the caller
99
+ * can hand out a refreshed token; `onUnauthorized` (optional) is called once on
100
+ * a 401 to force a refresh before a single retry.
101
+ */
102
+ export type HevyAuth = string | {
103
+ apiKey: string;
104
+ } | {
105
+ accessToken: () => Promise<string>;
106
+ onUnauthorized?: () => Promise<void>;
107
+ };
108
+ export type HevyAuthKind = "api-key" | "oauth";
94
109
  export declare class HevyClient {
95
- private apiKey;
96
110
  private fetchFn;
97
111
  private maxRequestsPerMinute;
98
112
  private timestamps;
99
- constructor(apiKey: string, fetchFn?: typeof fetch, maxRequestsPerMinute?: number);
113
+ private auth;
114
+ constructor(auth: HevyAuth, fetchFn?: typeof fetch, maxRequestsPerMinute?: number);
115
+ get authKind(): HevyAuthKind;
116
+ private authHeaders;
100
117
  private request;
101
118
  private paged;
102
119
  validateKey(): Promise<boolean>;
@@ -12,14 +12,22 @@ export class HevyApiError extends Error {
12
12
  }
13
13
  }
14
14
  export class HevyClient {
15
- apiKey;
16
15
  fetchFn;
17
16
  maxRequestsPerMinute;
18
17
  timestamps = [];
19
- constructor(apiKey, fetchFn = (input, init) => globalThis.fetch(input, init), maxRequestsPerMinute = 30) {
20
- this.apiKey = apiKey;
18
+ auth;
19
+ constructor(auth, fetchFn = (input, init) => globalThis.fetch(input, init), maxRequestsPerMinute = 30) {
21
20
  this.fetchFn = fetchFn;
22
21
  this.maxRequestsPerMinute = maxRequestsPerMinute;
22
+ this.auth = typeof auth === "string" ? { apiKey: auth } : auth;
23
+ }
24
+ get authKind() {
25
+ return "apiKey" in this.auth ? "api-key" : "oauth";
26
+ }
27
+ async authHeaders() {
28
+ if ("apiKey" in this.auth)
29
+ return { "api-key": this.auth.apiKey };
30
+ return { Authorization: `Bearer ${await this.auth.accessToken()}` };
23
31
  }
24
32
  async request(method, path, body) {
25
33
  const now = Date.now();
@@ -28,11 +36,18 @@ export class HevyClient {
28
36
  throw new HevyApiError(429, "Client-side rate limit reached; wait a minute before more Hevy calls.", path);
29
37
  }
30
38
  this.timestamps.push(now);
31
- const res = await this.fetchFn(BASE + path, {
39
+ const send = async () => this.fetchFn(BASE + path, {
32
40
  method,
33
- headers: { "api-key": this.apiKey, "Content-Type": "application/json" },
41
+ headers: { ...(await this.authHeaders()), "Content-Type": "application/json" },
34
42
  body: body === undefined ? undefined : JSON.stringify(body),
35
43
  });
44
+ let res = await send();
45
+ // OAuth access tokens expire hourly; on a 401 force one refresh and retry once.
46
+ if (res.status === 401 && "accessToken" in this.auth && this.auth.onUnauthorized) {
47
+ await res.body?.cancel();
48
+ await this.auth.onUnauthorized();
49
+ res = await send();
50
+ }
36
51
  const text = await res.text();
37
52
  if (!res.ok)
38
53
  throw new HevyApiError(res.status, text.slice(0, 500), path);
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { createHevyClient, registerTools, keyInvalidMessage, type RegisterOptions } from "./tools.js";
2
- export { HevyClient, HevyApiError, type ExerciseTemplate } from "./hevy-client.js";
2
+ export { HevyClient, HevyApiError, type ExerciseTemplate, type HevyAuth, type HevyAuthKind } from "./hevy-client.js";
3
3
  export { searchExercises } from "./exercises.js";
4
4
  export { buildRoutinePayload } from "./routine-builder.js";
5
5
  export { summarizeWorkout, groupExerciseHistory, scanWorkouts } from "./workouts.js";
package/dist/tools.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
- import { HevyClient } from "./hevy-client.js";
3
- export declare function createHevyClient(apiKey: string): HevyClient;
4
- export declare function keyInvalidMessage(status: number, hint: string): string;
2
+ import { HevyClient, type HevyAuth, type HevyAuthKind } from "./hevy-client.js";
3
+ export declare function createHevyClient(auth: HevyAuth): HevyClient;
4
+ export declare function keyInvalidMessage(status: number, hint: string, kind?: HevyAuthKind): string;
5
5
  export interface RegisterOptions {
6
6
  /** Client-specific guidance appended to the key-invalid error, e.g.
7
7
  * "Ask the user to disconnect and reconnect the connector in claude.ai settings."
package/dist/tools.js CHANGED
@@ -31,15 +31,21 @@ const ExerciseSchema = z.object({
31
31
  sets: z.array(SetSchema).min(1),
32
32
  });
33
33
  const READ = { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true };
34
- export function createHevyClient(apiKey) {
35
- return new HevyClient(apiKey);
34
+ export function createHevyClient(auth) {
35
+ return new HevyClient(auth);
36
36
  }
37
- export function keyInvalidMessage(status, hint) {
37
+ export function keyInvalidMessage(status, hint, kind = "api-key") {
38
+ if (kind === "oauth") {
39
+ return `The Hevy connection is no longer authorized (Hevy returned HTTP ${status}). ${hint} ` +
40
+ "The user may have revoked access in Hevy, or the sign-in expired.";
41
+ }
38
42
  return `Your Hevy API key is no longer valid (Hevy returned HTTP ${status}). ${hint} ` +
39
43
  "The user can check their key at hevy.com/settings?developer.";
40
44
  }
41
45
  export function registerTools(server, client, opts = {}) {
42
- const hint = opts.keyInvalidHint ?? "Update your Hevy API key where you configured this server.";
46
+ const hint = opts.keyInvalidHint ?? (client.authKind === "oauth"
47
+ ? "Reconnect Hevy where you configured this server."
48
+ : "Update your Hevy API key where you configured this server.");
43
49
  let templates = null;
44
50
  const library = async () => {
45
51
  if (!templates)
@@ -54,7 +60,7 @@ export function registerTools(server, client, opts = {}) {
54
60
  }
55
61
  catch (e) {
56
62
  if (e instanceof HevyApiError && (e.status === 401 || e.status === 403)) {
57
- return err(keyInvalidMessage(e.status, hint));
63
+ return err(keyInvalidMessage(e.status, hint, client.authKind));
58
64
  }
59
65
  if (e instanceof Error)
60
66
  return err(e.message);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@almostjacked/hevy-mcp-core",
3
- "version": "0.1.0",
4
- "description": "Core for hevy-mcp: Hevy API client and MCP tool registrations (unofficial).",
3
+ "version": "0.2.0",
4
+ "description": "Core for Almost Jacked for Hevy (@almostjacked/hevy-mcp): Hevy API client and MCP tool registrations (unofficial).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",