@indigoai-us/hq-cli 5.50.0 → 5.50.2

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 (46) hide show
  1. package/dist/commands/mcp-registration.d.ts +905 -0
  2. package/dist/commands/mcp-registration.js +2001 -0
  3. package/dist/commands/mcp-status.d.ts +130 -0
  4. package/dist/commands/mcp-status.js +406 -0
  5. package/dist/commands/onboard-warning.d.ts +7 -0
  6. package/dist/commands/onboard-warning.js +14 -0
  7. package/dist/commands/onboard.js +5 -5
  8. package/dist/commands/pack-install.d.ts +74 -0
  9. package/dist/commands/pack-install.js +493 -14
  10. package/dist/commands/packs.js +42 -4
  11. package/dist/commands/pkg-install.js +5 -2
  12. package/dist/index.js +7 -2
  13. package/dist/types.d.ts +26 -1
  14. package/dist/utils/contribution-table.d.ts +103 -0
  15. package/dist/utils/contribution-table.js +65 -0
  16. package/dist/utils/pack-contributions.d.ts +93 -10
  17. package/dist/utils/pack-contributions.js +140 -48
  18. package/dist/utils/secrets-cache.d.ts +9 -0
  19. package/dist/utils/secrets-cache.js +24 -2
  20. package/dist/utils/version-gate.d.ts +40 -1
  21. package/dist/utils/version-gate.js +91 -20
  22. package/package.json +3 -2
  23. package/scripts/generate-scan-packages-table.mjs +113 -0
  24. package/src/commands/mcp-registration.test.ts +2787 -0
  25. package/src/commands/mcp-registration.ts +2612 -0
  26. package/src/commands/mcp-status.test.ts +483 -0
  27. package/src/commands/mcp-status.ts +575 -0
  28. package/src/commands/mcp-status.us011.test.ts +243 -0
  29. package/src/commands/onboard-warning.test.ts +26 -0
  30. package/src/commands/onboard-warning.ts +12 -0
  31. package/src/commands/onboard.ts +4 -7
  32. package/src/commands/pack-install.test.ts +733 -0
  33. package/src/commands/pack-install.ts +582 -13
  34. package/src/commands/packs.ts +45 -1
  35. package/src/commands/pkg-install.ts +4 -1
  36. package/src/index.ts +6 -0
  37. package/src/types.ts +28 -9
  38. package/src/utils/contribution-table.ts +83 -0
  39. package/src/utils/pack-contributions.test.ts +310 -25
  40. package/src/utils/pack-contributions.ts +194 -47
  41. package/src/utils/secrets-cache.ts +22 -0
  42. package/src/utils/version-gate.test.ts +122 -0
  43. package/src/utils/version-gate.ts +109 -13
  44. package/test/e2e/smoke-install-mcp.sh +113 -0
  45. package/test/fixtures/hq-pack-smoke-mcp/mcp/smoke-http.json +1 -0
  46. package/test/fixtures/hq-pack-smoke-mcp/package.yaml +11 -0
@@ -53,9 +53,19 @@ import semverValid from 'semver/functions/valid.js';
53
53
  import semverValidRange from 'semver/ranges/valid.js';
54
54
  import semverGt from 'semver/functions/gt.js';
55
55
  import { findHqRoot } from '../utils/manifest.js';
56
- import { readHqVersion } from '../utils/pack-contributions.js';
56
+ import { readHqVersion, routeContribution, listInstalledPacks } from '../utils/pack-contributions.js';
57
+ import { CONTRIBUTION_TABLE, payloadFor } from '../utils/contribution-table.js';
57
58
  import { safeExtractTarball } from './safe-extract.js';
58
59
  import { vaultApiFetchPublic } from '../utils/vault-api.js';
60
+ import {
61
+ redactSecrets,
62
+ SECRET_REDACTION,
63
+ registerMcpServers,
64
+ McpManifestError,
65
+ type McpManifest,
66
+ type SecretResolver,
67
+ } from './mcp-registration.js';
68
+ import { readCache, listSecretCacheScopes } from '../utils/secrets-cache.js';
59
69
  import type { PackManifest, PackContributeKey } from '../types.js';
60
70
 
61
71
  // ---------------------------------------------------------------------------
@@ -978,6 +988,165 @@ export function verifyArtifact(input: VerifyArtifactInput): void {
978
988
  }
979
989
  }
980
990
 
991
+ // ---------------------------------------------------------------------------
992
+ // MCP per-server manifest validation (US-005, the TS counterpart of the bash
993
+ // `validate_mcp_manifest` arm in core/scripts/scan-packages.sh).
994
+ //
995
+ // The `mcp` contributes key is `wire: 'merge'` — its per-server manifests are
996
+ // NEVER symlinked; they are validated here and (US-006) merged into the agent
997
+ // configs via registerMcpServers. This function MIRRORS the bash transport
998
+ // rules hand-coded against the draft-07 schema
999
+ // (repos/public/knowledge-hq-core/mcp-manifest.schema.json). There is no `ajv`
1000
+ // dependency in hq-cli; the schema file is single-source documentation, so the
1001
+ // rules are replicated here (dependency-free, robust if the schema is absent).
1002
+ // ---------------------------------------------------------------------------
1003
+
1004
+ /** Allowed top-level keys (schema `additionalProperties: false`). */
1005
+ const MCP_ALLOWED_KEYS = ['type', 'url', 'headers', 'command', 'args', 'env', 'tools'];
1006
+
1007
+ /** Schema `$id` for an at-a-glance error reference (kept inline; no file read). */
1008
+ const MCP_SCHEMA_ID = 'mcp-manifest.schema.json';
1009
+
1010
+ /** True iff a header/env value carries a `${secret:NAME}` reference. */
1011
+ function hasSecretRef(value: string): boolean {
1012
+ return /\$\{secret:/.test(value);
1013
+ }
1014
+
1015
+ /**
1016
+ * Validate that a record (`headers` or `env`) is an object of STRING values,
1017
+ * pushing one error per offending entry into `errs`. Mirrors the schema's
1018
+ * `additionalProperties: { type: 'string' }`.
1019
+ */
1020
+ function checkStringMap(
1021
+ errs: string[],
1022
+ label: 'headers' | 'env',
1023
+ value: unknown,
1024
+ ): void {
1025
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
1026
+ errs.push(`${label} must be an object`);
1027
+ return;
1028
+ }
1029
+ for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
1030
+ if (typeof v !== 'string') {
1031
+ errs.push(`${label} "${k}" value must be a string`);
1032
+ }
1033
+ }
1034
+ }
1035
+
1036
+ /**
1037
+ * Parse + shape-validate one pack's per-server MCP manifest (`mcp/{item}.json`),
1038
+ * mirroring the bash `validate_mcp_manifest` arm. EXPORTED so the acceptance
1039
+ * test (and US-006's registration engine) can call it directly.
1040
+ *
1041
+ * Throws an `Error` (whose message names the pack-relative payload path, e.g.
1042
+ * `mcp/foo.json`) when the file does not parse as JSON or violates a transport
1043
+ * rule:
1044
+ * - `type` required, one of `http|stdio|sse`;
1045
+ * - no unknown top-level keys (allowed: {@link MCP_ALLOWED_KEYS});
1046
+ * - http/sse: `url` required + `^https?://`; `command`/`args`/`env` forbidden;
1047
+ * - stdio: non-empty `command` required; `url`/`headers` forbidden;
1048
+ * - `headers`/`env` (if present) are objects of string values;
1049
+ * - NO inline literal `Bearer ` secret — such a value MUST carry a
1050
+ * `${secret:NAME}` reference, never a literal token.
1051
+ *
1052
+ * @param payloadDir the pack payload root (holds `mcp/{item}.json`)
1053
+ * @param item the bare server name declared under `contributes.mcp`
1054
+ */
1055
+ export function validateMcpManifest(payloadDir: string, item: string): void {
1056
+ const rel = payloadFor('mcp', item); // mcp/{item}.json
1057
+ const abs = path.join(payloadDir, rel);
1058
+
1059
+ // EXISTS — the existsSync gate in validateManifest already covers this, but
1060
+ // re-check so the helper is safe to call standalone (tests, US-006).
1061
+ if (!fs.existsSync(abs)) {
1062
+ throw new Error(`MCP manifest missing: ${rel} (declared under contributes.mcp but no such file)`);
1063
+ }
1064
+
1065
+ // PARSE — a non-JSON file is a hard error that names the file.
1066
+ let doc: unknown;
1067
+ try {
1068
+ doc = JSON.parse(fs.readFileSync(abs, 'utf-8'));
1069
+ } catch (e) {
1070
+ throw new Error(`MCP manifest malformed: ${rel} is not valid JSON (failed to parse: ${(e as Error).message})`);
1071
+ }
1072
+ if (doc === null || typeof doc !== 'object' || Array.isArray(doc)) {
1073
+ throw new Error(`MCP manifest malformed: ${rel} must be a JSON object`);
1074
+ }
1075
+ const m = doc as Record<string, unknown>;
1076
+ const errs: string[] = [];
1077
+
1078
+ // type: required + enum.
1079
+ const type = m.type;
1080
+ if (!('type' in m)) {
1081
+ errs.push('missing required field: type');
1082
+ } else if (type !== 'http' && type !== 'stdio' && type !== 'sse') {
1083
+ errs.push(`type must be one of http|stdio|sse (got: ${JSON.stringify(type)})`);
1084
+ }
1085
+
1086
+ // No unknown top-level keys (schema additionalProperties: false).
1087
+ for (const k of Object.keys(m)) {
1088
+ if (!MCP_ALLOWED_KEYS.includes(k)) {
1089
+ errs.push(`unknown top-level key: ${k}`);
1090
+ }
1091
+ }
1092
+
1093
+ // http / sse transport: url required (^https?://); command/args/env forbidden.
1094
+ if (type === 'http' || type === 'sse') {
1095
+ if (!('url' in m)) {
1096
+ errs.push(`${type} transport requires a url`);
1097
+ } else if (typeof m.url !== 'string' || !/^https?:\/\//.test(m.url)) {
1098
+ errs.push('url must be an absolute http(s) URL');
1099
+ }
1100
+ for (const forbidden of ['command', 'args', 'env'] as const) {
1101
+ if (forbidden in m) errs.push(`${type} transport forbids: ${forbidden}`);
1102
+ }
1103
+ }
1104
+
1105
+ // stdio transport: non-empty command required; url/headers forbidden.
1106
+ if (type === 'stdio') {
1107
+ if (typeof m.command !== 'string' || m.command.length === 0) {
1108
+ errs.push('stdio transport requires a non-empty command');
1109
+ }
1110
+ for (const forbidden of ['url', 'headers'] as const) {
1111
+ if (forbidden in m) errs.push(`stdio transport forbids: ${forbidden}`);
1112
+ }
1113
+ }
1114
+
1115
+ // headers / env values must be strings.
1116
+ if ('headers' in m) checkStringMap(errs, 'headers', m.headers);
1117
+ if ('env' in m) checkStringMap(errs, 'env', m.env);
1118
+
1119
+ if (errs.length > 0) {
1120
+ throw new Error(
1121
+ `MCP manifest fails shape validation: ${rel} (schema: ${MCP_SCHEMA_ID})\n` +
1122
+ errs.map((e) => ` - ${e}`).join('\n'),
1123
+ );
1124
+ }
1125
+
1126
+ // Reject INLINE LITERAL Bearer secrets. A header/env value containing
1127
+ // 'Bearer ' MUST use a ${secret:NAME} reference, never a literal token baked
1128
+ // into a synced/committed artifact.
1129
+ const bad: string[] = [];
1130
+ for (const [label, mapVal] of [
1131
+ ['header', m.headers],
1132
+ ['env', m.env],
1133
+ ] as const) {
1134
+ if (mapVal === null || typeof mapVal !== 'object' || Array.isArray(mapVal)) continue;
1135
+ for (const [k, v] of Object.entries(mapVal as Record<string, unknown>)) {
1136
+ if (typeof v === 'string' && v.includes('Bearer ') && !hasSecretRef(v)) {
1137
+ bad.push(`${label} "${k}"`);
1138
+ }
1139
+ }
1140
+ }
1141
+ if (bad.length > 0) {
1142
+ throw new Error(
1143
+ `MCP manifest contains an inline literal Bearer secret: ${rel} (${bad.join(', ')}) — ` +
1144
+ 'a \'Bearer \' header/env value MUST use a ${secret:NAME} reference, never a literal token ' +
1145
+ '(a resolved secret is never written to a synced/committed artifact)',
1146
+ );
1147
+ }
1148
+ }
1149
+
981
1150
  // ---------------------------------------------------------------------------
982
1151
  // Manifest validation (spec §Validation, 10 checks)
983
1152
  // ---------------------------------------------------------------------------
@@ -1032,6 +1201,40 @@ export function validateManifest(
1032
1201
  `Host hqCore ${hqVersion} does not satisfy pack requirement ${range}`
1033
1202
  );
1034
1203
  }
1204
+ // 6b. requires.packs (M0) — OPTIONAL pack-to-pack dependencies. Absent → legacy
1205
+ // behavior (hqCore is the only prerequisite). Present → a list of
1206
+ // { name, version? } where `name` is a valid hq-pack name and `version` (if
1207
+ // given) is a valid semver RANGE. SHAPE validation only (no filesystem) so a
1208
+ // malformed dependency can't masquerade as valid; whether the named packs are
1209
+ // actually INSTALLED is enforced at install time by assertPackDependencies
1210
+ // (which needs hqRoot — a pure manifest validator doesn't have it).
1211
+ const reqPacks = m.requires?.packs;
1212
+ if (reqPacks !== undefined) {
1213
+ if (!Array.isArray(reqPacks)) {
1214
+ throw new Error('requires.packs must be a list of { name, version? } entries');
1215
+ }
1216
+ for (const dep of reqPacks) {
1217
+ if (!dep || typeof dep !== 'object' || Array.isArray(dep)) {
1218
+ throw new Error(
1219
+ 'requires.packs entries must be mappings with a name (and optional version)'
1220
+ );
1221
+ }
1222
+ const d = dep as unknown as Record<string, unknown>;
1223
+ if (typeof d.name !== 'string' || !/^hq-pack-[a-z0-9][a-z0-9-]*$/.test(d.name)) {
1224
+ throw new Error(
1225
+ `requires.packs[].name "${d.name}" must match ^hq-pack-[a-z0-9][a-z0-9-]*$`
1226
+ );
1227
+ }
1228
+ if (
1229
+ d.version !== undefined &&
1230
+ (typeof d.version !== 'string' || !semverValidRange(d.version))
1231
+ ) {
1232
+ throw new Error(
1233
+ `requires.packs entry for "${d.name}" has an invalid version range "${d.version}"`
1234
+ );
1235
+ }
1236
+ }
1237
+ }
1035
1238
  // 7. contributes has at least one non-empty subfield
1036
1239
  const contributes = (m.contributes ?? {}) as PackManifest['contributes'];
1037
1240
  const nonEmpty = Object.values(contributes).some(
@@ -1040,26 +1243,33 @@ export function validateManifest(
1040
1243
  if (!nonEmpty) {
1041
1244
  throw new Error('contributes must have at least one non-empty subfield');
1042
1245
  }
1043
- // 10. payload files exist (hooks check happens separately in step 8)
1044
- const subpaths: Record<PackContributeKey, (item: string) => string> = {
1045
- workers: (i) => path.join('workers', i),
1046
- knowledge: (i) => path.join('knowledge', i),
1047
- skills: (i) => path.join('skills', i),
1048
- commands: (i) => path.join('commands', `${i}.md`),
1049
- hooks: (i) => path.join('hooks', `${i}.sh`),
1050
- policies: (i) => path.join('policies', `${i}.md`),
1051
- scripts: (i) => path.join('scripts', i),
1052
- };
1053
- for (const [key, items] of Object.entries(contributes) as [PackContributeKey, string[]][]) {
1246
+ // 10. payload files exist (hooks check happens separately in step 8).
1247
+ // Payload paths are READ from the single-source CONTRIBUTION_TABLE (US-003)
1248
+ // via payloadFor -- no restated `subpaths` record. An unknown key (not in the
1249
+ // table) is rejected here so a typo can't silently install unwired.
1250
+ for (const [key, items] of Object.entries(contributes) as [string, unknown][]) {
1251
+ if (!(key in CONTRIBUTION_TABLE)) {
1252
+ throw new Error(`contributes.${key} is not a known contribution type`);
1253
+ }
1054
1254
  if (!Array.isArray(items)) continue;
1055
1255
  for (const item of items) {
1056
- const rel = subpaths[key](item);
1256
+ const rel = payloadFor(key as PackContributeKey, item as string);
1057
1257
  const abs = path.join(payloadDir, rel);
1058
1258
  if (!fs.existsSync(abs)) {
1059
1259
  throw new Error(
1060
1260
  `contributes.${key} declares "${item}" but payload file missing: ${rel}`
1061
1261
  );
1062
1262
  }
1263
+ // The `mcp` key (wire: 'merge') is NOT existsSync-only: after the file is
1264
+ // confirmed present it must PARSE + SHAPE-validate against the per-server
1265
+ // MCP schema (US-005), mirroring the bash validate_mcp_manifest arm. Other
1266
+ // (symlink) keys keep their existsSync-only behavior. We dispatch on the
1267
+ // ROUTE from the single-source table (not a hardcoded key) so the merge
1268
+ // path is table-driven; the merge itself (registering into the agent
1269
+ // configs via registerMcpServers, US-006) is NEVER a symlink.
1270
+ if (routeContribution(key as PackContributeKey) === 'merge') {
1271
+ validateMcpManifest(payloadDir, item as string);
1272
+ }
1063
1273
  }
1064
1274
  }
1065
1275
  // author + capabilities (US-001) — both OPTIONAL and backwards-compatible.
@@ -1136,6 +1346,51 @@ export function validateManifest(
1136
1346
  return m as PackManifest;
1137
1347
  }
1138
1348
 
1349
+ // ---------------------------------------------------------------------------
1350
+ // Pack-to-pack dependency pre-flight (M0)
1351
+ // ---------------------------------------------------------------------------
1352
+
1353
+ /**
1354
+ * Enforce a pack's `requires.packs`: every named dependency MUST already be
1355
+ * installed (and satisfy its optional semver RANGE) before we write anything.
1356
+ *
1357
+ * Installed packs are discovered by FILESYSTEM PRESENCE via `listInstalledPacks`
1358
+ * — deliberately NOT `modules.yaml`, which `installPack` no longer writes under
1359
+ * the v12+ layout (a modules.yaml-based check would silently ignore every modern
1360
+ * pack). Throws on the first unmet dependency so the install aborts with NO
1361
+ * partial state (it is called before installToPackages). No-op when
1362
+ * `requires.packs` is absent/empty, keeping legacy packs unaffected.
1363
+ */
1364
+ export function assertPackDependencies(hqRoot: string, pkg: PackManifest): void {
1365
+ const deps = pkg.requires?.packs ?? [];
1366
+ if (deps.length === 0) return;
1367
+ const installed = new Map<string, string | undefined>();
1368
+ for (const p of listInstalledPacks(hqRoot)) {
1369
+ // Key on the manifest name when readable, else the directory name.
1370
+ installed.set(p.manifest?.name ?? p.name, p.manifest?.version);
1371
+ }
1372
+ for (const dep of deps) {
1373
+ if (dep.name === pkg.name) {
1374
+ throw new Error(`Pack ${pkg.name} cannot list itself in requires.packs.`);
1375
+ }
1376
+ if (!installed.has(dep.name)) {
1377
+ throw new Error(
1378
+ `Pack ${pkg.name} requires ${dep.name}, which is not installed. ` +
1379
+ `Install it first, e.g.: hq install marketplace:${dep.name}`
1380
+ );
1381
+ }
1382
+ if (dep.version) {
1383
+ const have = installed.get(dep.name);
1384
+ if (!have || !semverSatisfies(have, dep.version, { includePrerelease: true })) {
1385
+ throw new Error(
1386
+ `Pack ${pkg.name} requires ${dep.name} ${dep.version}, but ` +
1387
+ `${dep.name}${have ? ` ${have}` : ' (version unknown)'} is installed.`
1388
+ );
1389
+ }
1390
+ }
1391
+ }
1392
+ }
1393
+
1139
1394
  // ---------------------------------------------------------------------------
1140
1395
  // Post-install get-started line (US-005)
1141
1396
  // ---------------------------------------------------------------------------
@@ -1200,6 +1455,148 @@ async function confirmHooks(
1200
1455
  return /^(y|yes)$/i.test(answer.trim());
1201
1456
  }
1202
1457
 
1458
+ // ---------------------------------------------------------------------------
1459
+ // MCP confirmation (US-010)
1460
+ //
1461
+ // The MCP equivalent of confirmHooks. MCP servers are a high-trust surface: an
1462
+ // http/sse server points your agent at a REMOTE endpoint (typosquat / phishing
1463
+ // risk — the FULL url is disclosed verbatim so the operator can eyeball it), and
1464
+ // a stdio server runs a LOCAL binary with your shell permissions. We GATE on a
1465
+ // non-empty `contributes.mcp` (NEVER the advisory `capabilities` field, which is
1466
+ // reserved/unenforced — gating on it would let a pack omit a capability to dodge
1467
+ // the prompt) and render per-server transport + url-or-command + a count of ALL
1468
+ // servers being added.
1469
+ //
1470
+ // REDACTION: header/env VALUES are never printed. The manifests use
1471
+ // `${secret:NAME}` references (never literal Bearers), but we STILL run the
1472
+ // rendered prompt through `redactSecrets` against the raw header/env values so
1473
+ // that even a LITERAL secret could not leak. We show header KEY names with a
1474
+ // redacted value marker.
1475
+ //
1476
+ // FAIL-SAFE: a manifest that fails to load/parse here is NOT a bypass — we
1477
+ // surface the server name with a `[manifest unreadable]` note and STILL require
1478
+ // confirmation (a malformed manifest should make the operator MORE cautious).
1479
+ // ---------------------------------------------------------------------------
1480
+
1481
+ /**
1482
+ * Render one declared MCP server's prompt line, redacting every header/env value.
1483
+ * EXPORTED so the acceptance/redaction self-test can assert no secret/Bearer
1484
+ * substring ever appears in the rendered output.
1485
+ */
1486
+ export function renderMcpServerLine(payloadDir: string, item: string): string {
1487
+ let manifest: McpManifest;
1488
+ try {
1489
+ const abs = path.join(payloadDir, payloadFor('mcp', item));
1490
+ manifest = JSON.parse(fs.readFileSync(abs, 'utf-8')) as McpManifest;
1491
+ if (manifest === null || typeof manifest !== 'object') throw new Error('not an object');
1492
+ } catch {
1493
+ // Fail-safe: do not crash the prompt; flag the server and keep requiring
1494
+ // confirmation.
1495
+ return ` - ${item} [manifest unreadable] — could not load mcp/${item}.json; treat with caution`;
1496
+ }
1497
+
1498
+ const transport = manifest.type;
1499
+ // Accumulate every raw header/env value so a final redactSecrets pass scrubs
1500
+ // even a literal token that slipped past `${secret:}` (defense in depth — we
1501
+ // already replace the values with the redaction marker below).
1502
+ const rawValues = new Set<string>();
1503
+ for (const v of Object.values(manifest.headers ?? {})) rawValues.add(v);
1504
+ for (const v of Object.values(manifest.env ?? {})) rawValues.add(v);
1505
+
1506
+ // Header KEYS shown, VALUES always redacted (never the raw value).
1507
+ const headerKeys = Object.keys(manifest.headers ?? {});
1508
+ const headerStr =
1509
+ headerKeys.length > 0
1510
+ ? ` headers: { ${headerKeys.map((k) => `${k}: ${SECRET_REDACTION}`).join(', ')} }`
1511
+ : '';
1512
+
1513
+ let line: string;
1514
+ if (transport === 'http' || transport === 'sse') {
1515
+ // FULL url verbatim — typosquat disclosure is the whole point.
1516
+ line =
1517
+ ` - ${item} [${transport}] url: ${manifest.url ?? '(missing)'}` +
1518
+ headerStr +
1519
+ ` (contacts a remote endpoint)`;
1520
+ } else {
1521
+ // stdio — local binary with shell permissions.
1522
+ const args = manifest.args ?? [];
1523
+ const argsStr = args.length > 0 ? ` args: [${args.join(', ')}]` : '';
1524
+ line =
1525
+ ` - ${item} [stdio] command: ${manifest.command ?? '(missing)'}` +
1526
+ argsStr +
1527
+ headerStr +
1528
+ ` (runs a local binary with your shell permissions)`;
1529
+ }
1530
+
1531
+ // Final defense-in-depth scrub: even though values are already replaced with
1532
+ // the marker above, run the whole line through redactSecrets so a stray
1533
+ // literal token (e.g. inside command/args) can never reach the terminal.
1534
+ return redactSecrets(line, rawValues);
1535
+ }
1536
+
1537
+ /**
1538
+ * Install-time MCP trust prompt. EXPORTED so the acceptance test can drive the
1539
+ * gate / bypass / non-TTY branches directly (capture stdout, assert no secret
1540
+ * substring leaks, assert deny returns false). See {@link confirmHooks} for the
1541
+ * voice/shape this mirrors.
1542
+ */
1543
+ export async function confirmMcp(
1544
+ pkg: PackManifest,
1545
+ payloadDir: string,
1546
+ allowMcp: boolean,
1547
+ ): Promise<boolean> {
1548
+ // GATE on non-empty `contributes.mcp` — mirrors confirmHooks. Never on the
1549
+ // advisory `capabilities` field.
1550
+ const servers = pkg.contributes.mcp ?? [];
1551
+ if (servers.length === 0) return true;
1552
+
1553
+ if (allowMcp) {
1554
+ console.log(
1555
+ chalk.yellow(
1556
+ `--allow-mcp set; registering ${servers.length} MCP server(s) without prompting.`,
1557
+ ),
1558
+ );
1559
+ return true;
1560
+ }
1561
+
1562
+ console.log('');
1563
+ console.log(
1564
+ chalk.yellow(
1565
+ `Pack ${pkg.publisher}/${pkg.name} declares ${servers.length} MCP server(s):`,
1566
+ ),
1567
+ );
1568
+ for (const item of servers) {
1569
+ console.log(chalk.yellow(renderMcpServerLine(payloadDir, item)));
1570
+ }
1571
+ console.log(
1572
+ chalk.yellow(
1573
+ 'These servers will be registered into your Claude and Codex agent configs.',
1574
+ ),
1575
+ );
1576
+
1577
+ // Non-TTY guard (mirrors the secrets.ts pattern): do NOT call rl.question in a
1578
+ // non-interactive shell — it would hang. Instruct the operator to re-run with
1579
+ // --allow-mcp and DENY (return false -> abort, no partial state).
1580
+ if (!process.stdin.isTTY) {
1581
+ console.log(
1582
+ chalk.red(
1583
+ `Refusing to register ${servers.length} MCP server(s) without confirmation ` +
1584
+ 'in a non-interactive shell. Re-run with --allow-mcp to approve.',
1585
+ ),
1586
+ );
1587
+ return false;
1588
+ }
1589
+
1590
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
1591
+ const answer: string = await new Promise((resolve) => {
1592
+ rl.question('Install anyway? [y/N] ', (a) => {
1593
+ rl.close();
1594
+ resolve(a);
1595
+ });
1596
+ });
1597
+ return /^(y|yes)$/i.test(answer.trim());
1598
+ }
1599
+
1203
1600
  // ---------------------------------------------------------------------------
1204
1601
  // Conditional predicate
1205
1602
  //
@@ -1246,6 +1643,130 @@ function evalConditional(expr: string): boolean {
1246
1643
  return r.status === 0;
1247
1644
  }
1248
1645
 
1646
+ // ---------------------------------------------------------------------------
1647
+ // MCP registration wiring (US-007) — call registerMcpServers from installPack.
1648
+ //
1649
+ // The registration CORE + Claude/Codex emitters live in mcp-registration.ts and
1650
+ // are fully unit-tested; this is the install-time SEAM that feeds them a real
1651
+ // manifest loader and a production, vault-backed secret resolver, with per-server
1652
+ // secret-deferral so a fresh install (before `/connect-shopify` has minted the
1653
+ // key) still SUCCEEDS, skipping only the server whose secret is not yet present.
1654
+ // ---------------------------------------------------------------------------
1655
+
1656
+ /**
1657
+ * Build the install-time {@link SecretResolver} bound to the HQ vault's local
1658
+ * secrets-cache (`~/.hq/secrets-cache/<scope>/<NAME>`, AES-256-GCM, 0600, TTL'd).
1659
+ *
1660
+ * Active-company resolution at install time is INDIRECT by design: `hq install`
1661
+ * has no `--company` flag and runs OFFLINE (no token), so we cannot resolve a
1662
+ * single active company UID the way `hq run` / `hq secrets` do (via
1663
+ * `getEntityUid` over the network). Instead we probe EVERY cached scope
1664
+ * (`cmp_*`/`prs_*` — whichever has minted secrets locally) for the requested
1665
+ * name and return the first hit. This naturally resolves to whichever company
1666
+ * context just provisioned the secret (e.g. the one `/connect-shopify` minted
1667
+ * `VYG_API_KEY` under), without guessing, and works whether the vault scoped the
1668
+ * key under a company or person entity.
1669
+ *
1670
+ * On MISS across all scopes (no cache, expired TTL, or key never minted) it
1671
+ * returns `null` — which is exactly what {@link registerMcpServers}'s
1672
+ * unresolvable-secret path keys off to defer that server gracefully. With no
1673
+ * cached scopes at all (`listSecretCacheScopes()` → `[]`) it simply returns
1674
+ * `null` for every name, the desired graceful-deferral behavior.
1675
+ */
1676
+ export function makeInstallSecretResolver(): SecretResolver {
1677
+ return (name: string): string | null => {
1678
+ for (const scope of listSecretCacheScopes()) {
1679
+ const value = readCache(scope, name);
1680
+ if (value !== null) return value;
1681
+ }
1682
+ return null;
1683
+ };
1684
+ }
1685
+
1686
+ /**
1687
+ * Load + shape-validate + parse a pack's per-server MCP manifest from disk.
1688
+ * `validateMcpManifest(destDir, name)` is the SAME shape gate `validateManifest`
1689
+ * runs at install; it returns void and THROWS on bad shape, so we call it first
1690
+ * (re-using one source of truth), then parse `mcp/<name>.json` and return it
1691
+ * typed as {@link McpManifest}. `destDir` is the installed pack root
1692
+ * (`core/packages/<pkg>/`), the realpath `registerMcpServers` emits from.
1693
+ */
1694
+ function loadMcpManifestFrom(destDir: string, name: string): McpManifest {
1695
+ validateMcpManifest(destDir, name); // throws on bad shape (void on success)
1696
+ const abs = path.join(destDir, 'mcp', `${name}.json`);
1697
+ return JSON.parse(fs.readFileSync(abs, 'utf-8')) as McpManifest;
1698
+ }
1699
+
1700
+ /**
1701
+ * Derive the per-server "needs secret" remedy command from the pack's
1702
+ * `initialization.entrypoint` (e.g. the vyg pack's `connect-shopify` →
1703
+ * `/connect-shopify`). Falls back to a generic "provision the secret then re-run
1704
+ * `hq install`" when the pack declares no entrypoint.
1705
+ */
1706
+ function mcpSecretRemedy(initialization: PackManifest['initialization']): string {
1707
+ const entrypoint = initialization?.entrypoint;
1708
+ if (typeof entrypoint === 'string' && entrypoint.trim() !== '') {
1709
+ const command = '/' + entrypoint.trim().replace(/^\/+/, '');
1710
+ return `run \`${command}\` to provision it, then re-run \`hq install\``;
1711
+ }
1712
+ return 'provision the secret then re-run `hq install`';
1713
+ }
1714
+
1715
+ /** Pull the `${secret:NAME}` token out of a `cannot resolve ${secret:NAME}` error message. */
1716
+ function extractDeferredSecretName(message: string): string {
1717
+ const m = /\$\{secret:([^}]+)\}/.exec(message);
1718
+ return m ? m[1] : 'a required secret';
1719
+ }
1720
+
1721
+ /**
1722
+ * Register one pack's `contributes.mcp` servers into the shared Claude/Codex
1723
+ * agent configs, PER SERVER, so a single unresolvable `${secret:NAME}` defers
1724
+ * ONLY that server instead of aborting the whole install. Returns the registered
1725
+ * and skipped (deferred) server-name lists for the one-line summary.
1726
+ *
1727
+ * Per-server policy:
1728
+ * - SUCCESS → push to `registered`.
1729
+ * - McpManifestError matching
1730
+ * `/cannot resolve \$\{secret:/` → SKIP (push to `skipped`), warn on stderr,
1731
+ * install still succeeds (the key gets minted later, then a re-install wires it).
1732
+ * - ANY OTHER error → RE-THROW (abort install). Only the
1733
+ * unresolvable-secret case is swallowed.
1734
+ */
1735
+ function wireMcpServers(
1736
+ pkg: PackManifest,
1737
+ destDir: string,
1738
+ ): { registered: string[]; skipped: string[] } {
1739
+ const resolveSecret = makeInstallSecretResolver();
1740
+ const loadManifest = (name: string): McpManifest => loadMcpManifestFrom(destDir, name);
1741
+ const registered: string[] = [];
1742
+ const skipped: string[] = [];
1743
+
1744
+ for (const name of pkg.contributes.mcp ?? []) {
1745
+ try {
1746
+ // Per-server call: registerMcpServers throws on the FIRST unresolvable
1747
+ // secret, so calling it one name at a time lets us catch + continue.
1748
+ registerMcpServers(pkg.name, [name], { loadManifest, resolveSecret });
1749
+ registered.push(name);
1750
+ } catch (e) {
1751
+ const isUnresolvableSecret =
1752
+ e instanceof McpManifestError && /cannot resolve \$\{secret:/.test(e.message);
1753
+ if (!isUnresolvableSecret) {
1754
+ // ConfigParseError / ConfigPermissionError / McpNameCollisionError / a
1755
+ // malformed-manifest McpManifestError / etc. — propagate, abort install.
1756
+ throw e;
1757
+ }
1758
+ skipped.push(name);
1759
+ const secret = extractDeferredSecretName(e.message);
1760
+ // Always stderr (never suppressed by --quiet): the user must see WHY a
1761
+ // server was deferred and exactly how to finish wiring it.
1762
+ process.stderr.write(
1763
+ `MCP server '${name}' needs secret ${secret} — ${mcpSecretRemedy(pkg.initialization)}.\n`,
1764
+ );
1765
+ }
1766
+ }
1767
+ return { registered, skipped };
1768
+ }
1769
+
1249
1770
  // ---------------------------------------------------------------------------
1250
1771
  // Move into core/packages/ + run core/scripts/scan-packages.sh
1251
1772
  // ---------------------------------------------------------------------------
@@ -1398,6 +1919,13 @@ export function runScanPackages(hqRoot: string, opts: { quiet?: boolean } = {}):
1398
1919
 
1399
1920
  export interface InstallPackOptions {
1400
1921
  allowHooks?: boolean;
1922
+ /**
1923
+ * US-010 install-time MCP trust prompt bypass (CI/ambient-trust). Mirrors
1924
+ * `allowHooks`: when set, `confirmMcp` skips the prompt and prints a yellow
1925
+ * notice. Absent/false → the operator is prompted (or, in a non-TTY shell,
1926
+ * the install is refused with a "re-run with --allow-mcp" hint).
1927
+ */
1928
+ allowMcp?: boolean;
1401
1929
  followBranch?: boolean;
1402
1930
  /**
1403
1931
  * Route this function's human output to stderr (and silence scan-packages
@@ -1470,6 +1998,12 @@ export async function installPack(
1470
1998
 
1471
1999
  const pkg = validateManifest(fetched.payloadDir, hqVersion);
1472
2000
 
2001
+ // M0 — pack-to-pack dependency pre-flight. Runs BEFORE any prompts or writes
2002
+ // so a missing/unsatisfied `requires.packs` aborts with no partial state, and
2003
+ // before we bother the operator with hook/MCP trust prompts for a pack that
2004
+ // can't install anyway.
2005
+ assertPackDependencies(hqRoot, pkg);
2006
+
1473
2007
  if (pkg.conditional) {
1474
2008
  const allowed = await confirmConditional(pkg, opts.allowHooks ?? false);
1475
2009
  if (!allowed) {
@@ -1497,6 +2031,22 @@ export async function installPack(
1497
2031
  return;
1498
2032
  }
1499
2033
 
2034
+ // US-010 — install-time MCP trust prompt. Positioned AFTER confirmHooks and
2035
+ // BEFORE installToPackages so a deny aborts with NO partial state (no config
2036
+ // written, no pack moved into core/packages, no registerMcpServers call). The
2037
+ // actual registration (US-007) now fires AFTER installToPackages +
2038
+ // runScanPackages below (pack on disk + symlinks wired), once this gate has
2039
+ // confirmed the servers — so a deny here means registration never runs.
2040
+ const mcpConfirmed = await confirmMcp(
2041
+ pkg,
2042
+ fetched.payloadDir,
2043
+ opts.allowMcp ?? false,
2044
+ );
2045
+ if (!mcpConfirmed) {
2046
+ say(chalk.red('Install aborted (MCP servers denied).'));
2047
+ return;
2048
+ }
2049
+
1500
2050
  const destDir = installToPackages(fetched.payloadDir, pkg, hqRoot);
1501
2051
  // Under the v12+ HQ layout, packs live at `core/packages/<name>/` and
1502
2052
  // are tracked by filesystem presence — no `modules.yaml` write (that
@@ -1516,6 +2066,25 @@ export async function installPack(
1516
2066
  stampInstallSource(destDir, stampedSource);
1517
2067
  runScanPackages(hqRoot, { quiet: opts.quiet });
1518
2068
 
2069
+ // US-007 — register `contributes.mcp` servers into the shared Claude/Codex
2070
+ // agent configs. Fires HERE (after installToPackages + runScanPackages, i.e.
2071
+ // pack on disk + symlinks wired) and only once the confirmMcp gate above
2072
+ // approved them. The `mcp` key is wire:'merge' — NEVER symlinked; this merge
2073
+ // is its only wiring path. registerMcpServers honors the
2074
+ // HQ_DISABLE_MCP_REGISTRATION kill-switch internally, so we never re-check it.
2075
+ // Per-server secret-deferral keeps a fresh install (key not yet minted) at
2076
+ // exit 0, deferring only the unresolvable server (see wireMcpServers).
2077
+ if (Array.isArray(pkg.contributes.mcp) && pkg.contributes.mcp.length > 0) {
2078
+ const { registered, skipped } = wireMcpServers(pkg, destDir);
2079
+ // One-line summary (server NAMES only — never resolved secret VALUES).
2080
+ say(
2081
+ chalk.dim(
2082
+ ` MCP servers: registered [${registered.join(', ')}]; ` +
2083
+ `skipped [${skipped.join(', ')}].`
2084
+ )
2085
+ );
2086
+ }
2087
+
1519
2088
  say(
1520
2089
  chalk.green(
1521
2090
  `\nOK Installed ${pkg.name}@${pkg.version} -> ${path.relative(hqRoot, destDir)}/`