catence 0.2.1-beta.21 → 0.2.1-beta.22

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.
@@ -3,7 +3,7 @@
3
3
  * Keep these values synchronized with release/manifest.json; release checks
4
4
  * deliberately reject drift before a package can be published.
5
5
  */
6
- export declare const CATENCE_RUNTIME_VERSION = "0.2.1-beta.21";
6
+ export declare const CATENCE_RUNTIME_VERSION = "0.2.1-beta.22";
7
7
  export declare const CATENCE_PROTOCOL_VERSION = 1;
8
8
  export type CatenceRuntimeCapabilities = {
9
9
  mcp: true;
@@ -3,7 +3,7 @@
3
3
  * Keep these values synchronized with release/manifest.json; release checks
4
4
  * deliberately reject drift before a package can be published.
5
5
  */
6
- export const CATENCE_RUNTIME_VERSION = '0.2.1-beta.21';
6
+ export const CATENCE_RUNTIME_VERSION = '0.2.1-beta.22';
7
7
  export const CATENCE_PROTOCOL_VERSION = 1;
8
8
  export function catenceRuntimeHealth() {
9
9
  return {
@@ -1,6 +1,6 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import path from 'node:path';
3
- import { SYNC_STAGES } from '../../contracts/progress.js';
3
+ import { STALE_RUN_TIMEOUT_MS, SYNC_STAGES } from '../../contracts/progress.js';
4
4
  import { mergeProgress, readRunningProgress } from '../../elt/storage/progress-sidecar.js';
5
5
  import { sqlString } from './sql.js';
6
6
  import { DATASET_CATALOG, getDataset } from './catalog.js';
@@ -142,6 +142,11 @@ export class ReadOnlyRepository {
142
142
  async progress() {
143
143
  // Same sequential-read discipline as status(): the shared native
144
144
  // connection must not run these small reads concurrently.
145
+ // A run killed with its container keeps status='running' in the database;
146
+ // the heartbeat age filter mirrors the sidecar contract and db.syncProgress
147
+ // so such a run stops reading as active (and stops blocking new syncs)
148
+ // after the stale-run window.
149
+ const cutoff = new Date(Date.now() - STALE_RUN_TIMEOUT_MS).toISOString();
145
150
  const running = await this.rows(`
146
151
  SELECT runs.run_id, runs.provider, coalesce(progress.stage, 'starting') AS stage,
147
152
  progress.current_step,
@@ -154,8 +159,9 @@ export class ReadOnlyRepository {
154
159
  FROM sync_runs AS runs
155
160
  LEFT JOIN sync_run_progress AS progress USING (run_id)
156
161
  WHERE runs.status = 'running'
162
+ AND coalesce(progress.heartbeat_at, runs.started_at) > $cutoff
157
163
  ORDER BY runs.started_at DESC
158
- `);
164
+ `, { cutoff });
159
165
  const recent = await this.rows(`
160
166
  SELECT run_id, provider, stage, current_step, completed_units, total_units, percent,
161
167
  elapsed_seconds, estimated_remaining_seconds,
@@ -22,6 +22,40 @@ function bearerTokenFrom(request) {
22
22
  return null;
23
23
  return header.slice('Bearer '.length);
24
24
  }
25
+ /**
26
+ * One-line diagnostics for the auth and MCP surfaces: gate outcomes and every
27
+ * /mcp or /oauth exchange are otherwise invisible in container logs. Routine
28
+ * dashboard polling stays quiet; set CATENCE_HTTP_ACCESS_LOG=0 to disable.
29
+ * Logs method, path, status, latency, user agent, and the MCP protocol hints —
30
+ * never headers carrying credentials.
31
+ */
32
+ function logRequestCompletion(request, response, pathname) {
33
+ if (process.env.CATENCE_HTTP_ACCESS_LOG === '0')
34
+ return;
35
+ const startedAt = Date.now();
36
+ response.once('finish', () => {
37
+ const status = response.statusCode;
38
+ const interesting = pathname === '/mcp' ||
39
+ pathname.startsWith('/oauth/') ||
40
+ pathname.startsWith('/.well-known/oauth-') ||
41
+ status >= 400;
42
+ if (!interesting)
43
+ return;
44
+ const parts = [`[http] ${new Date().toISOString()}`, `${request.method} ${pathname}`, String(status), `${Date.now() - startedAt}ms`];
45
+ const userAgent = request.headers['user-agent'];
46
+ if (typeof userAgent === 'string' && userAgent)
47
+ parts.push(`ua="${userAgent.slice(0, 120)}"`);
48
+ const protocolVersion = request.headers['mcp-protocol-version'];
49
+ if (typeof protocolVersion === 'string' && protocolVersion)
50
+ parts.push(`mcp-protocol-version=${protocolVersion}`);
51
+ const mcpMethod = request.headers['mcp-method'];
52
+ if (typeof mcpMethod === 'string' && mcpMethod)
53
+ parts.push(`mcp-method=${mcpMethod}`);
54
+ if (pathname === '/mcp')
55
+ parts.push(`auth=${bearerTokenFrom(request) !== null ? 'bearer' : 'none'}`);
56
+ console.log(parts.join(' '));
57
+ });
58
+ }
25
59
  function matchesLegacyToken(rawToken, expected) {
26
60
  const received = Buffer.from(rawToken);
27
61
  const expectedBuffer = Buffer.from(expected);
@@ -438,16 +472,66 @@ function singleJsonRpcRequest(body) {
438
472
  function mcpServerCapabilities(server) {
439
473
  return server.server.getCapabilities();
440
474
  }
475
+ /**
476
+ * Answer server/discover from a throwaway scoped server so the descriptor
477
+ * (capabilities, server identity, instructions) can never drift from what the
478
+ * initialize response reports. See MODERN_MCP_PROTOCOL_VERSION.
479
+ */
480
+ async function writeDiscoveryResult(response, paths, scope, id) {
481
+ const server = createCatenceMcpServer(paths, {}, { scope });
482
+ try {
483
+ json(response, 200, {
484
+ jsonrpc: '2.0',
485
+ id: id ?? null,
486
+ result: {
487
+ resultType: 'complete',
488
+ supportedVersions: [MODERN_MCP_PROTOCOL_VERSION, ...SUPPORTED_PROTOCOL_VERSIONS],
489
+ capabilities: mcpServerCapabilities(server),
490
+ _meta: { 'io.modelcontextprotocol/serverInfo': CATENCE_MCP_SERVER_INFO },
491
+ instructions: MCP_INSTRUCTIONS,
492
+ ttlMs: 3_600_000,
493
+ cacheScope: 'public',
494
+ },
495
+ });
496
+ }
497
+ finally {
498
+ await server.close();
499
+ }
500
+ }
501
+ /**
502
+ * Hosted connectors may probe server/discover before attaching the bearer
503
+ * token; the result exposes nothing athlete-scoped, so it is answered ahead
504
+ * of the 401. Every other unauthenticated /mcp request stays behind the gate.
505
+ * Returns true when the response was written.
506
+ */
507
+ async function handleUnauthenticatedDiscovery(request, response, paths) {
508
+ let body;
509
+ try {
510
+ body = await readJsonBody(request);
511
+ }
512
+ catch {
513
+ return false;
514
+ }
515
+ const jsonRpc = singleJsonRpcRequest(body);
516
+ if (jsonRpc?.method !== 'server/discover')
517
+ return false;
518
+ await writeDiscoveryResult(response, paths, { athleteIds: 'all' }, jsonRpc.id);
519
+ return true;
520
+ }
441
521
  async function handleMcpRequest(request, response, paths, principal) {
442
522
  if (request.method !== 'POST') {
443
523
  methodNotAllowed(response);
444
524
  return;
445
525
  }
446
- // ChatGPT's discovery probe and the requests that follow it carry the
447
- // 2026-07-28 revision; the transport understands only the older list. The
448
- // Node adapter rebuilds the web-standard headers from rawHeaders (not the
449
- // parsed headers object), so both views are rewritten.
450
- if (request.headers['mcp-protocol-version'] === MODERN_MCP_PROTOCOL_VERSION) {
526
+ // ChatGPT's discovery probe and the requests that follow it carry a dated
527
+ // 2026 MCP revision (2026-07-28 as of today, later revisions as they ship);
528
+ // the SDK transport understands only its own older list, and the surface
529
+ // here is stateless either way, so any dated 2026 revision is bridged down
530
+ // while still-unknown values keep failing closed. The Node adapter rebuilds
531
+ // the web-standard headers from rawHeaders (not the parsed headers object),
532
+ // so both views are rewritten.
533
+ const requestedVersion = request.headers['mcp-protocol-version'];
534
+ if (typeof requestedVersion === 'string' && /^2026-\d{2}-\d{2}$/.test(requestedVersion)) {
451
535
  request.headers['mcp-protocol-version'] = LATEST_PROTOCOL_VERSION;
452
536
  const rawHeaders = request.rawHeaders;
453
537
  for (let index = 0; index < rawHeaders.length; index += 2) {
@@ -471,25 +555,7 @@ async function handleMcpRequest(request, response, paths, principal) {
471
555
  if (jsonRpc?.method === 'server/discover') {
472
556
  // Modern clients probe discovery before anything else; answer from the
473
557
  // same descriptor the initialize response reports so the two never drift.
474
- const server = createCatenceMcpServer(paths, {}, { scope: { athleteIds: principal.athleteIds } });
475
- try {
476
- json(response, 200, {
477
- jsonrpc: '2.0',
478
- id: jsonRpc.id ?? null,
479
- result: {
480
- resultType: 'complete',
481
- supportedVersions: [MODERN_MCP_PROTOCOL_VERSION, ...SUPPORTED_PROTOCOL_VERSIONS],
482
- capabilities: mcpServerCapabilities(server),
483
- _meta: { 'io.modelcontextprotocol/serverInfo': CATENCE_MCP_SERVER_INFO },
484
- instructions: MCP_INSTRUCTIONS,
485
- ttlMs: 3_600_000,
486
- cacheScope: 'public',
487
- },
488
- });
489
- }
490
- finally {
491
- await server.close();
492
- }
558
+ await writeDiscoveryResult(response, paths, { athleteIds: principal.athleteIds }, jsonRpc.id);
493
559
  return;
494
560
  }
495
561
  // The HTTP gate has already authenticated this request; the MCP server
@@ -599,6 +665,7 @@ export function createCatenceHttpServer(options = {}) {
599
665
  return createServer((request, response) => {
600
666
  void (async () => {
601
667
  const pathname = new URL(request.url ?? '/', 'http://localhost').pathname;
668
+ logRequestCompletion(request, response, pathname);
602
669
  // The OAuth endpoints are the auth surface itself: they bypass the gate
603
670
  // and CORS entirely and are delegated to the Express sub-app. When OAuth
604
671
  // is effectively disabled they answer 404 — no metadata is advertised.
@@ -626,6 +693,13 @@ export function createCatenceHttpServer(options = {}) {
626
693
  ? FULL_ACCESS_PRINCIPAL
627
694
  : await resolvePrincipal(request, effectiveOptions, accountPaths);
628
695
  if (principal === null) {
696
+ // A hosted connector may probe server/discover before attaching the
697
+ // bearer token. The discovery result exposes nothing athlete-scoped,
698
+ // so that one probe is answered ahead of the 401; every other
699
+ // unauthenticated /mcp request stays behind the gate.
700
+ if (pathname === '/mcp' && request.method === 'POST' && await handleUnauthenticatedDiscovery(request, response, catalogPaths ?? staticPaths ?? resolveCatalogPaths())) {
701
+ return;
702
+ }
629
703
  // A presented-but-rejected token is a bearer problem, not an OAuth
630
704
  // discovery problem: answer RFC 6750 invalid_token without
631
705
  // resource_metadata, so static-key clients get a clear "your token is
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catence",
3
- "version": "0.2.1-beta.21",
3
+ "version": "0.2.1-beta.22",
4
4
  "description": "Local MCP access to normalized Garmin, Intervals.icu, and targeted Strava fitness data.",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "repository": {