@nimblebrain/mpak-sdk 0.1.3 → 0.2.1

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.
package/dist/index.js CHANGED
@@ -1,3 +1,16 @@
1
+ // src/mpakSDK.ts
2
+ import { spawnSync } from "child_process";
3
+ import { chmodSync, existsSync as existsSync4, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "fs";
4
+ import { join as join4, resolve as resolve3 } from "path";
5
+ import { McpbManifestSchema as McpbManifestSchema2 } from "@nimblebrain/mpak-schemas";
6
+
7
+ // src/cache.ts
8
+ import { randomUUID } from "crypto";
9
+ import { existsSync as existsSync2, readdirSync, rmSync, writeFileSync } from "fs";
10
+ import { homedir, tmpdir } from "os";
11
+ import { join as join2 } from "path";
12
+ import { CacheMetadataSchema, McpbManifestSchema } from "@nimblebrain/mpak-schemas";
13
+
1
14
  // src/client.ts
2
15
  import { createHash } from "crypto";
3
16
 
@@ -34,11 +47,44 @@ var MpakNetworkError = class extends MpakError {
34
47
  this.name = "MpakNetworkError";
35
48
  }
36
49
  };
50
+ var MpakConfigCorruptedError = class extends MpakError {
51
+ constructor(message, configPath, cause) {
52
+ super(message, "CONFIG_CORRUPTED");
53
+ this.configPath = configPath;
54
+ this.cause = cause;
55
+ this.name = "MpakConfigCorruptedError";
56
+ }
57
+ };
58
+ var MpakCacheCorruptedError = class extends MpakError {
59
+ constructor(message, filePath, cause) {
60
+ super(message, "CACHE_CORRUPTED");
61
+ this.filePath = filePath;
62
+ this.cause = cause;
63
+ this.name = "MpakCacheCorruptedError";
64
+ }
65
+ };
66
+ var MpakInvalidBundleError = class extends MpakError {
67
+ constructor(message, bundlePath, cause) {
68
+ super(message, "INVALID_BUNDLE");
69
+ this.bundlePath = bundlePath;
70
+ this.cause = cause;
71
+ this.name = "MpakInvalidBundleError";
72
+ }
73
+ };
74
+ var MpakConfigError = class extends MpakError {
75
+ constructor(packageName, missingFields) {
76
+ const fieldNames = missingFields.map((f) => f.title).join(", ");
77
+ super(`Missing required config for ${packageName}: ${fieldNames}`, "CONFIG_MISSING");
78
+ this.packageName = packageName;
79
+ this.missingFields = missingFields;
80
+ this.name = "MpakConfigError";
81
+ }
82
+ };
37
83
 
38
84
  // src/client.ts
39
85
  var DEFAULT_REGISTRY_URL = "https://registry.mpak.dev";
40
86
  var DEFAULT_TIMEOUT = 3e4;
41
- var MpakClient = class {
87
+ var MpakClient = class _MpakClient {
42
88
  registryUrl;
43
89
  timeout;
44
90
  userAgent;
@@ -150,7 +196,6 @@ var MpakClient = class {
150
196
  if (params.q) searchParams.set("q", params.q);
151
197
  if (params.tags) searchParams.set("tags", params.tags);
152
198
  if (params.category) searchParams.set("category", params.category);
153
- if (params.surface) searchParams.set("surface", params.surface);
154
199
  if (params.sort) searchParams.set("sort", params.sort);
155
200
  if (params.limit) searchParams.set("limit", String(params.limit));
156
201
  if (params.offset) searchParams.set("offset", String(params.offset));
@@ -214,179 +259,55 @@ var MpakClient = class {
214
259
  }
215
260
  return response.json();
216
261
  }
262
+ // ===========================================================================
263
+ // Download Methods
264
+ // ===========================================================================
217
265
  /**
218
- * Download skill content and verify integrity
219
- *
220
- * @throws {MpakIntegrityError} If expectedSha256 is provided and doesn't match (fail-closed)
221
- */
222
- async downloadSkillContent(downloadUrl, expectedSha256) {
223
- const response = await this.fetchWithTimeout(downloadUrl);
224
- if (!response.ok) {
225
- throw new MpakNetworkError(`Failed to download skill: HTTP ${response.status}`);
226
- }
227
- const content = await response.text();
228
- if (expectedSha256) {
229
- const actualHash = this.computeSha256(content);
230
- if (actualHash !== expectedSha256) {
231
- throw new MpakIntegrityError(expectedSha256, actualHash);
232
- }
233
- return { content, verified: true };
234
- }
235
- return { content, verified: false };
236
- }
237
- /**
238
- * Resolve a skill reference to actual content
239
- *
240
- * Supports mpak, github, and url sources. This is the main method for
241
- * fetching skill content from any supported source.
266
+ * Download content from a URL and verify its SHA-256 integrity.
242
267
  *
243
- * @throws {MpakNotFoundError} If skill not found
244
- * @throws {MpakIntegrityError} If integrity check fails (fail-closed)
268
+ * @throws {MpakIntegrityError} If SHA-256 doesn't match
245
269
  * @throws {MpakNetworkError} For network failures
246
- *
247
- * @example
248
- * ```typescript
249
- * // Resolve from mpak registry
250
- * const skill = await client.resolveSkillRef({
251
- * source: 'mpak',
252
- * name: '@nimblebraininc/folk-crm',
253
- * version: '1.3.0',
254
- * });
255
- *
256
- * // Resolve from GitHub
257
- * const skill = await client.resolveSkillRef({
258
- * source: 'github',
259
- * name: '@example/my-skill',
260
- * version: 'v1.0.0',
261
- * repo: 'owner/repo',
262
- * path: 'skills/my-skill/SKILL.md',
263
- * });
264
- *
265
- * // Resolve from URL
266
- * const skill = await client.resolveSkillRef({
267
- * source: 'url',
268
- * name: '@example/custom',
269
- * version: '1.0.0',
270
- * url: 'https://example.com/skill.md',
271
- * });
272
- * ```
273
- */
274
- async resolveSkillRef(ref) {
275
- switch (ref.source) {
276
- case "mpak":
277
- return this.resolveMpakSkill(ref);
278
- case "github":
279
- return this.resolveGithubSkill(ref);
280
- case "url":
281
- return this.resolveUrlSkill(ref);
282
- default: {
283
- const _exhaustive = ref;
284
- throw new Error(`Unknown skill source: ${_exhaustive.source}`);
285
- }
286
- }
287
- }
288
- /**
289
- * Resolve a skill from mpak registry
290
- *
291
- * The API returns a ZIP bundle containing SKILL.md and metadata.
292
- */
293
- async resolveMpakSkill(ref) {
294
- const url = `${this.registryUrl}/v1/skills/${ref.name}/versions/${ref.version}/download`;
295
- const response = await this.fetchWithTimeout(url);
296
- if (response.status === 404) {
297
- throw new MpakNotFoundError(`${ref.name}@${ref.version}`);
298
- }
299
- if (!response.ok) {
300
- throw new MpakNetworkError(`Failed to fetch skill: HTTP ${response.status}`);
301
- }
302
- const zipBuffer = await response.arrayBuffer();
303
- const content = await this.extractSkillFromZip(zipBuffer, ref.name);
304
- if (ref.integrity) {
305
- this.verifyIntegrityOrThrow(content, ref.integrity);
306
- return { content, version: ref.version, source: "mpak", verified: true };
307
- }
308
- return { content, version: ref.version, source: "mpak", verified: false };
309
- }
310
- /**
311
- * Resolve a skill from GitHub releases
312
270
  */
313
- async resolveGithubSkill(ref) {
314
- const url = `https://github.com/${ref.repo}/releases/download/${ref.version}/${ref.path}`;
271
+ async downloadContent(url, sha256) {
315
272
  const response = await this.fetchWithTimeout(url);
316
273
  if (!response.ok) {
317
- throw new MpakNotFoundError(`github:${ref.repo}/${ref.path}@${ref.version}`);
318
- }
319
- const content = await response.text();
320
- if (ref.integrity) {
321
- this.verifyIntegrityOrThrow(content, ref.integrity);
322
- return {
323
- content,
324
- version: ref.version,
325
- source: "github",
326
- verified: true
327
- };
274
+ throw new MpakNetworkError(`Failed to download: HTTP ${response.status}`);
328
275
  }
329
- return {
330
- content,
331
- version: ref.version,
332
- source: "github",
333
- verified: false
334
- };
335
- }
336
- /**
337
- * Resolve a skill from a direct URL
338
- */
339
- async resolveUrlSkill(ref) {
340
- const response = await this.fetchWithTimeout(ref.url);
341
- if (!response.ok) {
342
- throw new MpakNotFoundError(`url:${ref.url}`);
343
- }
344
- const content = await response.text();
345
- if (ref.integrity) {
346
- this.verifyIntegrityOrThrow(content, ref.integrity);
347
- return { content, version: ref.version, source: "url", verified: true };
276
+ const downloadedRawData = new Uint8Array(await response.arrayBuffer());
277
+ const computedHash = this.computeSha256(downloadedRawData);
278
+ if (computedHash !== sha256) {
279
+ throw new MpakIntegrityError(sha256, computedHash);
348
280
  }
349
- return { content, version: ref.version, source: "url", verified: false };
281
+ return downloadedRawData;
350
282
  }
351
283
  /**
352
- * Extract SKILL.md content from a skill bundle ZIP
353
- */
354
- async extractSkillFromZip(zipBuffer, skillName) {
355
- const JSZip = (await import("jszip")).default;
356
- const zip = await JSZip.loadAsync(zipBuffer);
357
- const folderName = skillName.split("/").pop() ?? skillName;
358
- const skillPath = `${folderName}/SKILL.md`;
359
- const skillFile = zip.file(skillPath);
360
- if (!skillFile) {
361
- const altFile = zip.file("SKILL.md");
362
- if (!altFile) {
363
- throw new MpakNotFoundError(`SKILL.md not found in bundle for ${skillName}`);
364
- }
365
- return altFile.async("string");
366
- }
367
- return skillFile.async("string");
368
- }
369
- /**
370
- * Verify content integrity and throw if mismatch (fail-closed)
284
+ * Download a bundle by name, with optional version and platform.
285
+ * Defaults to latest version and auto-detected platform.
286
+ *
287
+ * @throws {MpakNotFoundError} If bundle not found
288
+ * @throws {MpakIntegrityError} If SHA-256 doesn't match
289
+ * @throws {MpakNetworkError} For network failures
371
290
  */
372
- verifyIntegrityOrThrow(content, integrity) {
373
- const expectedHash = this.extractHash(integrity);
374
- const actualHash = this.computeSha256(content);
375
- if (actualHash !== expectedHash) {
376
- throw new MpakIntegrityError(expectedHash, actualHash);
377
- }
291
+ async downloadBundle(name, version, platform) {
292
+ const resolvedPlatform = platform ?? _MpakClient.detectPlatform();
293
+ const resolvedVersion = version ?? "latest";
294
+ const downloadInfo = await this.getBundleDownload(name, resolvedVersion, resolvedPlatform);
295
+ const data = await this.downloadContent(downloadInfo.url, downloadInfo.bundle.sha256);
296
+ return { data, metadata: downloadInfo.bundle };
378
297
  }
379
298
  /**
380
- * Extract hash from integrity string (removes prefix)
299
+ * Download a skill bundle by name, with optional version.
300
+ * Defaults to latest version.
301
+ *
302
+ * @throws {MpakNotFoundError} If skill not found
303
+ * @throws {MpakIntegrityError} If SHA-256 doesn't match
304
+ * @throws {MpakNetworkError} For network failures
381
305
  */
382
- extractHash(integrity) {
383
- if (integrity.startsWith("sha256:")) {
384
- return integrity.slice(7);
385
- }
386
- if (integrity.startsWith("sha256-")) {
387
- return integrity.slice(7);
388
- }
389
- return integrity;
306
+ async downloadSkillBundle(name, version) {
307
+ const resolvedVersion = version ?? "latest";
308
+ const downloadInfo = await this.getSkillVersionDownload(name, resolvedVersion);
309
+ const data = await this.downloadContent(downloadInfo.url, downloadInfo.skill.sha256);
310
+ return { data, metadata: downloadInfo.skill };
390
311
  }
391
312
  // ===========================================================================
392
313
  // Utility Methods
@@ -428,14 +349,17 @@ var MpakClient = class {
428
349
  * Compute SHA256 hash of content
429
350
  */
430
351
  computeSha256(content) {
431
- return createHash("sha256").update(content, "utf8").digest("hex");
352
+ return createHash("sha256").update(content).digest("hex");
432
353
  }
433
354
  /**
434
355
  * Validate that a name is scoped (@scope/name)
435
356
  */
436
357
  validateScopedName(name) {
437
358
  if (!name.startsWith("@")) {
438
- throw new Error("Package name must be scoped (e.g., @scope/package-name)");
359
+ throw new MpakError(
360
+ "Package name must be scoped (e.g., @scope/package-name)",
361
+ "INVALID_SPEC"
362
+ );
439
363
  }
440
364
  }
441
365
  /**
@@ -468,11 +392,780 @@ var MpakClient = class {
468
392
  }
469
393
  }
470
394
  };
395
+
396
+ // src/helpers.ts
397
+ import { execFileSync } from "child_process";
398
+ import { createHash as createHash2 } from "crypto";
399
+ import { existsSync, mkdirSync, readFileSync, statSync } from "fs";
400
+ import { resolve, join } from "path";
401
+ var MAX_UNCOMPRESSED_SIZE = 500 * 1024 * 1024;
402
+ var UPDATE_CHECK_TTL_MS = 60 * 60 * 1e3;
403
+ function isSemverEqual(a, b) {
404
+ return a.replace(/^v/, "") === b.replace(/^v/, "");
405
+ }
406
+ function extractZip(zipPath, destDir) {
407
+ try {
408
+ const listOutput = execFileSync("unzip", ["-l", zipPath], {
409
+ stdio: "pipe",
410
+ encoding: "utf8"
411
+ });
412
+ const totalMatch = listOutput.match(/^\s*(\d+)\s+\d+\s+files?$/m);
413
+ if (totalMatch) {
414
+ const totalSize = parseInt(totalMatch[1] ?? "0", 10);
415
+ if (totalSize > MAX_UNCOMPRESSED_SIZE) {
416
+ throw new MpakCacheCorruptedError(
417
+ `Bundle uncompressed size (${Math.round(totalSize / 1024 / 1024)}MB) exceeds maximum allowed (${MAX_UNCOMPRESSED_SIZE / (1024 * 1024)}MB)`,
418
+ zipPath
419
+ );
420
+ }
421
+ }
422
+ } catch (error) {
423
+ if (error instanceof MpakCacheCorruptedError) {
424
+ throw error;
425
+ }
426
+ const message = error instanceof Error ? error.message : String(error);
427
+ throw new MpakCacheCorruptedError(
428
+ `Cannot verify bundle size before extraction: ${message}`,
429
+ zipPath,
430
+ error instanceof Error ? error : void 0
431
+ );
432
+ }
433
+ mkdirSync(destDir, { recursive: true });
434
+ execFileSync("unzip", ["-o", "-q", zipPath, "-d", destDir], {
435
+ stdio: "pipe"
436
+ });
437
+ }
438
+ function hashBundlePath(bundlePath) {
439
+ return createHash2("md5").update(resolve(bundlePath)).digest("hex").slice(0, 12);
440
+ }
441
+ function localBundleNeedsExtract(bundlePath, cacheDir) {
442
+ const metaPath = join(cacheDir, ".mpak-local-meta.json");
443
+ if (!existsSync(metaPath)) return true;
444
+ try {
445
+ const meta = JSON.parse(readFileSync(metaPath, "utf8"));
446
+ if (!meta.extractedAt) return true;
447
+ const bundleStat = statSync(bundlePath);
448
+ return bundleStat.mtimeMs > new Date(meta.extractedAt).getTime();
449
+ } catch {
450
+ return true;
451
+ }
452
+ }
453
+ function readJsonFromFile(filePath, schema) {
454
+ if (!existsSync(filePath)) {
455
+ throw new MpakError(`File does not exist: ${filePath}`, "FILE_NOT_FOUND");
456
+ }
457
+ let raw;
458
+ try {
459
+ raw = JSON.parse(readFileSync(filePath, "utf8"));
460
+ } catch {
461
+ throw new MpakError(`File is not valid JSON: ${filePath}`, "INVALID_JSON");
462
+ }
463
+ const result = schema.safeParse(raw);
464
+ if (!result.success) {
465
+ throw new MpakError(
466
+ `File failed validation: ${filePath} \u2014 ${result.error.issues[0]?.message ?? "unknown error"}`,
467
+ "VALIDATION_FAILED"
468
+ );
469
+ }
470
+ return result.data;
471
+ }
472
+
473
+ // src/cache.ts
474
+ var MpakBundleCache = class {
475
+ cacheHome;
476
+ mpakClient;
477
+ constructor(client, options) {
478
+ this.mpakClient = client;
479
+ this.cacheHome = join2(options?.mpakHome ?? join2(homedir(), ".mpak"), "cache");
480
+ }
481
+ /**
482
+ * Compute the cache path for a package. Does not create the directory.
483
+ * @example getPackageCachePath('@scope/name') => '<cacheBase>/scope-name'
484
+ */
485
+ getBundleCacheDirName(packageName) {
486
+ const safeName = packageName.replace("@", "").replace("/", "-");
487
+ return join2(this.cacheHome, safeName);
488
+ }
489
+ /**
490
+ * Read and validate cache metadata for a package.
491
+ * Returns `null` if the package does not exist in the cache.
492
+ * throws Error if metadata is corrupt
493
+ */
494
+ getBundleMetadata(packageName) {
495
+ const packageCacheDir = this.getBundleCacheDirName(packageName);
496
+ if (!existsSync2(packageCacheDir)) {
497
+ return null;
498
+ }
499
+ const metaPath = join2(packageCacheDir, ".mpak-meta.json");
500
+ try {
501
+ return readJsonFromFile(metaPath, CacheMetadataSchema);
502
+ } catch (err) {
503
+ throw new MpakCacheCorruptedError(
504
+ err instanceof Error ? err.message : String(err),
505
+ metaPath,
506
+ err instanceof Error ? err : void 0
507
+ );
508
+ }
509
+ }
510
+ /**
511
+ * Read and validate the MCPB manifest from a cached package.
512
+ * Returns `null` if the package is not cached (directory doesn't exist).
513
+ *
514
+ * @throws {MpakCacheCorruptedError} If the cache directory exists but
515
+ * `manifest.json` is missing, contains invalid JSON, or fails schema validation.
516
+ */
517
+ getBundleManifest(packageName) {
518
+ const dir = this.getBundleCacheDirName(packageName);
519
+ if (!existsSync2(dir)) {
520
+ return null;
521
+ }
522
+ const manifestPath = join2(dir, "manifest.json");
523
+ try {
524
+ return readJsonFromFile(manifestPath, McpbManifestSchema);
525
+ } catch (err) {
526
+ throw new MpakCacheCorruptedError(
527
+ err instanceof Error ? err.message : String(err),
528
+ manifestPath,
529
+ err instanceof Error ? err : void 0
530
+ );
531
+ }
532
+ }
533
+ /**
534
+ * Scan the cache directory and return metadata for every cached registry bundle.
535
+ * Skips the `_local/` directory (local dev bundles) and entries with
536
+ * missing/corrupt metadata or manifests.
537
+ */
538
+ listCachedBundles() {
539
+ if (!existsSync2(this.cacheHome)) return [];
540
+ const entries = readdirSync(this.cacheHome, { withFileTypes: true });
541
+ const bundles = [];
542
+ for (const entry of entries) {
543
+ if (!entry.isDirectory() || entry.name === "_local") continue;
544
+ const cacheDir = join2(this.cacheHome, entry.name);
545
+ try {
546
+ const manifest = readJsonFromFile(join2(cacheDir, "manifest.json"), McpbManifestSchema);
547
+ const meta = this.getBundleMetadata(manifest.name);
548
+ if (!meta) continue;
549
+ bundles.push({
550
+ name: manifest.name,
551
+ version: meta.version,
552
+ pulledAt: meta.pulledAt,
553
+ cacheDir
554
+ });
555
+ } catch {
556
+ }
557
+ }
558
+ return bundles;
559
+ }
560
+ /**
561
+ * Remove a cached bundle from disk.
562
+ * @returns `true` if the bundle was cached and removed, `false` if it wasn't cached.
563
+ */
564
+ removeCachedBundle(packageName) {
565
+ const dir = this.getBundleCacheDirName(packageName);
566
+ if (!existsSync2(dir)) return false;
567
+ rmSync(dir, { recursive: true, force: true });
568
+ return true;
569
+ }
570
+ /**
571
+ * Load a bundle into the local cache, downloading from the registry only
572
+ * if the cache is missing or stale. Returns the cache directory and version.
573
+ *
574
+ * Requires an `MpakClient` to be provided at construction time.
575
+ *
576
+ * @param name - Scoped package name (e.g. `@scope/bundle`)
577
+ * @param options.version - Specific version to load. Omit for "latest".
578
+ * @param options.force - Skip cache checks and always re-download.
579
+ *
580
+ * @returns `cacheDir` — path to the extracted bundle on disk,
581
+ * `version` — the resolved version string,
582
+ * `pulled` — whether a download actually occurred.
583
+ *
584
+ * @throws If no `MpakClient` was provided at construction time.
585
+ */
586
+ async loadBundle(name, options) {
587
+ const { version: requestedVersion, force = false } = options ?? {};
588
+ const cacheDir = this.getBundleCacheDirName(name);
589
+ let cachedMeta = null;
590
+ try {
591
+ cachedMeta = this.getBundleMetadata(name);
592
+ } catch {
593
+ }
594
+ if (!options?.force && !!cachedMeta && (!requestedVersion || isSemverEqual(cachedMeta.version, requestedVersion))) {
595
+ return { cacheDir, version: cachedMeta.version, pulled: false };
596
+ }
597
+ const platform = MpakClient.detectPlatform();
598
+ const downloadInfo = await this.mpakClient.getBundleDownload(
599
+ name,
600
+ requestedVersion ?? "latest",
601
+ platform
602
+ );
603
+ if (!force && cachedMeta && isSemverEqual(cachedMeta.version, downloadInfo.bundle.version)) {
604
+ this.writeCacheMetadata(name, {
605
+ ...cachedMeta,
606
+ lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString()
607
+ });
608
+ return { cacheDir, version: cachedMeta.version, pulled: false };
609
+ }
610
+ await this.downloadAndExtract(name, downloadInfo);
611
+ return { cacheDir, version: downloadInfo.bundle.version, pulled: true };
612
+ }
613
+ /**
614
+ * Fire-and-forget background check for bundle updates.
615
+ * Return the latest version string if an update is available, null otherwise (not cached, skipped, up-to-date, or error).
616
+ * The caller can just check `if (result) { console.log("update available: " + result) }`
617
+ * @param packageName - Scoped package name (e.g. `@scope/bundle`)
618
+ */
619
+ async checkForUpdate(packageName, options) {
620
+ const cachedMeta = this.getBundleMetadata(packageName);
621
+ if (!cachedMeta) return null;
622
+ if (!options?.force && cachedMeta.lastCheckedAt) {
623
+ const elapsed = Date.now() - new Date(cachedMeta.lastCheckedAt).getTime();
624
+ if (elapsed < UPDATE_CHECK_TTL_MS) return null;
625
+ }
626
+ try {
627
+ const detail = await this.mpakClient.getBundle(packageName);
628
+ this.writeCacheMetadata(packageName, {
629
+ ...cachedMeta,
630
+ lastCheckedAt: (/* @__PURE__ */ new Date()).toISOString()
631
+ });
632
+ if (!isSemverEqual(detail.latest_version, cachedMeta.version)) {
633
+ return detail.latest_version;
634
+ }
635
+ return null;
636
+ } catch {
637
+ return null;
638
+ }
639
+ }
640
+ // ===========================================================================
641
+ // Private methods
642
+ // ===========================================================================
643
+ /**
644
+ * Write cache metadata for a package.
645
+ * @throws If the metadata fails schema validation.
646
+ */
647
+ writeCacheMetadata(packageName, metadata) {
648
+ const metaPath = join2(this.getBundleCacheDirName(packageName), ".mpak-meta.json");
649
+ writeFileSync(metaPath, JSON.stringify(metadata, null, 2));
650
+ }
651
+ /**
652
+ * Download a bundle using pre-resolved download info, extract it into
653
+ * the cache, and write metadata.
654
+ */
655
+ async downloadAndExtract(name, downloadInfo) {
656
+ const bundle = downloadInfo.bundle;
657
+ const cacheDir = this.getBundleCacheDirName(name);
658
+ const tempPath = join2(tmpdir(), `mpak-${Date.now()}-${randomUUID().slice(0, 8)}.mcpb`);
659
+ try {
660
+ const data = await this.mpakClient.downloadContent(downloadInfo.url, bundle.sha256);
661
+ writeFileSync(tempPath, data);
662
+ if (existsSync2(cacheDir)) {
663
+ rmSync(cacheDir, { recursive: true, force: true });
664
+ }
665
+ extractZip(tempPath, cacheDir);
666
+ this.writeCacheMetadata(name, {
667
+ version: bundle.version,
668
+ pulledAt: (/* @__PURE__ */ new Date()).toISOString(),
669
+ platform: bundle.platform
670
+ });
671
+ } finally {
672
+ rmSync(tempPath, { force: true });
673
+ }
674
+ }
675
+ };
676
+
677
+ // src/config-manager.ts
678
+ import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
679
+ import { homedir as homedir2 } from "os";
680
+ import { join as join3, resolve as resolve2 } from "path";
681
+ import { z } from "zod";
682
+ var CONFIG_VERSION = "1.0.0";
683
+ var PackageConfigSchema = z.record(z.string(), z.string());
684
+ var MpakConfigSchema = z.object({
685
+ version: z.string(),
686
+ lastUpdated: z.string(),
687
+ registryUrl: z.string().optional(),
688
+ packages: z.record(z.string(), PackageConfigSchema).optional()
689
+ }).strict();
690
+ var MpakConfigManager = class {
691
+ mpakHome;
692
+ configFile;
693
+ config = null;
694
+ constructor(options) {
695
+ this.mpakHome = resolve2(options?.mpakHome ?? join3(homedir2(), ".mpak"));
696
+ this.configFile = join3(this.mpakHome, "config.json");
697
+ if (options?.registryUrl !== void 0) {
698
+ this.setRegistryUrl(options.registryUrl);
699
+ }
700
+ }
701
+ // ===========================================================================
702
+ // Public methods
703
+ // ===========================================================================
704
+ /**
705
+ * Resolve the registry URL with a 2-tier fallback:
706
+ * 1. Saved value in config file
707
+ * 2. Default: `https://registry.mpak.dev`
708
+ *
709
+ * @returns The resolved registry URL
710
+ */
711
+ getRegistryUrl() {
712
+ const config = this.loadConfig();
713
+ return config.registryUrl || "https://registry.mpak.dev";
714
+ }
715
+ /**
716
+ * Get all stored user config values for a package.
717
+ *
718
+ * @param packageName - Scoped package name (e.g. `@scope/bundle`)
719
+ * @returns The key-value map, or `undefined` if the package has no stored config
720
+ */
721
+ getPackageConfig(packageName) {
722
+ const config = this.loadConfig();
723
+ return config.packages?.[packageName];
724
+ }
725
+ /**
726
+ * Store a user config value for a package. Creates the package entry if needed.
727
+ *
728
+ * @param packageName - Scoped package name (e.g. `@scope/bundle`)
729
+ * @param key - The config key (e.g. `api_key`)
730
+ * @param value - The value to store
731
+ */
732
+ setPackageConfigValue(packageName, key, value) {
733
+ const config = this.loadConfig();
734
+ if (!config.packages) {
735
+ config.packages = {};
736
+ }
737
+ if (!config.packages[packageName]) {
738
+ config.packages[packageName] = {};
739
+ }
740
+ config.packages[packageName][key] = value;
741
+ this.saveConfig();
742
+ }
743
+ /**
744
+ * Remove all stored config for a package.
745
+ *
746
+ * @param packageName - Scoped package name (e.g. `@scope/bundle`)
747
+ * @returns `true` if the package had config that was removed, `false` if it didn't exist
748
+ */
749
+ clearPackageConfig(packageName) {
750
+ const config = this.loadConfig();
751
+ if (config.packages?.[packageName]) {
752
+ delete config.packages[packageName];
753
+ this.saveConfig();
754
+ return true;
755
+ }
756
+ return false;
757
+ }
758
+ /**
759
+ * Remove a single config value for a package. If this was the last key,
760
+ * the package entry is cleaned up entirely.
761
+ *
762
+ * @param packageName - Scoped package name (e.g. `@scope/bundle`)
763
+ * @param key - The config key to remove
764
+ * @returns `true` if the key existed and was removed, `false` otherwise
765
+ */
766
+ clearPackageConfigValue(packageName, key) {
767
+ const config = this.loadConfig();
768
+ if (config.packages?.[packageName]?.[key] !== void 0) {
769
+ delete config.packages[packageName][key];
770
+ if (Object.keys(config.packages[packageName]).length === 0) {
771
+ delete config.packages[packageName];
772
+ }
773
+ this.saveConfig();
774
+ return true;
775
+ }
776
+ return false;
777
+ }
778
+ /**
779
+ * List all package names that have stored user config.
780
+ *
781
+ * @returns Array of scoped package names (e.g. `['@scope/pkg1', '@scope/pkg2']`)
782
+ */
783
+ getPackageNames() {
784
+ const config = this.loadConfig();
785
+ return Object.keys(config.packages || {});
786
+ }
787
+ // ===========================================================================
788
+ // Private methods
789
+ // ===========================================================================
790
+ /**
791
+ * Load the config from disk, or create a fresh one if the file doesn't exist yet.
792
+ * The result is cached — subsequent calls return the in-memory copy without
793
+ * re-reading the file.
794
+ *
795
+ * @returns The validated config object
796
+ * @throws {MpakConfigCorruptedError} If the file exists but contains invalid JSON or fails schema validation
797
+ */
798
+ loadConfig() {
799
+ if (this.config) {
800
+ return this.config;
801
+ }
802
+ if (!existsSync3(this.configFile)) {
803
+ this.config = {
804
+ version: CONFIG_VERSION,
805
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
806
+ };
807
+ return this.config;
808
+ }
809
+ this.config = this.readAndValidateConfig();
810
+ return this.config;
811
+ }
812
+ /**
813
+ * Read the config file from disk, parse JSON, and validate against the schema.
814
+ *
815
+ * @returns The validated config object
816
+ * @throws {MpakConfigCorruptedError} If the file can't be read, contains invalid JSON,
817
+ * or doesn't match the expected schema
818
+ */
819
+ readAndValidateConfig() {
820
+ let configJson;
821
+ try {
822
+ configJson = readFileSync2(this.configFile, "utf8");
823
+ } catch (err) {
824
+ throw new MpakConfigCorruptedError(
825
+ `Failed to read config file: ${err instanceof Error ? err.message : String(err)}`,
826
+ this.configFile,
827
+ err instanceof Error ? err : void 0
828
+ );
829
+ }
830
+ let parsed;
831
+ try {
832
+ parsed = JSON.parse(configJson);
833
+ } catch (err) {
834
+ throw new MpakConfigCorruptedError(
835
+ `Config file contains invalid JSON: ${err instanceof Error ? err.message : String(err)}`,
836
+ this.configFile,
837
+ err instanceof Error ? err : void 0
838
+ );
839
+ }
840
+ const result = MpakConfigSchema.safeParse(parsed);
841
+ if (!result.success) {
842
+ const message = result.error.issues[0]?.message ?? "Invalid config";
843
+ throw new MpakConfigCorruptedError(message, this.configFile);
844
+ }
845
+ return result.data;
846
+ }
847
+ /**
848
+ * Flush the in-memory config to disk. Creates the config directory if needed,
849
+ * validates against the schema, updates `lastUpdated`, and writes
850
+ * with mode `0o600` (owner read/write only — config may contain secrets).
851
+ *
852
+ * @throws {MpakConfigCorruptedError} If the in-memory config fails schema validation
853
+ */
854
+ saveConfig() {
855
+ if (!this.config) {
856
+ throw new MpakConfigCorruptedError(
857
+ `saveConfig called before config was loaded`,
858
+ this.configFile
859
+ );
860
+ }
861
+ if (!existsSync3(this.mpakHome)) {
862
+ mkdirSync2(this.mpakHome, { recursive: true, mode: 448 });
863
+ }
864
+ this.config.lastUpdated = (/* @__PURE__ */ new Date()).toISOString();
865
+ const result = MpakConfigSchema.safeParse(this.config);
866
+ if (!result.success) {
867
+ const message = result.error.issues[0]?.message ?? "Invalid config";
868
+ throw new MpakConfigCorruptedError(message, this.configFile);
869
+ }
870
+ const configJson = JSON.stringify(result.data, null, 2);
871
+ writeFileSync2(this.configFile, configJson, { mode: 384 });
872
+ }
873
+ /**
874
+ * Persist a custom registry URL to the config file.
875
+ *
876
+ * @param url - The registry URL to save (e.g. `https://registry.example.com`)
877
+ */
878
+ setRegistryUrl(url) {
879
+ const config = this.loadConfig();
880
+ config.registryUrl = url;
881
+ this.saveConfig();
882
+ }
883
+ };
884
+
885
+ // src/mpakSDK.ts
886
+ var Mpak = class _Mpak {
887
+ /** User configuration manager (`config.json`). */
888
+ configManager;
889
+ /** Registry API client. */
890
+ client;
891
+ /** Local bundle cache. */
892
+ bundleCache;
893
+ constructor(options) {
894
+ const configOptions = {};
895
+ if (options?.mpakHome !== void 0) configOptions.mpakHome = options.mpakHome;
896
+ if (options?.registryUrl !== void 0) configOptions.registryUrl = options.registryUrl;
897
+ this.configManager = new MpakConfigManager(configOptions);
898
+ const clientConfig = {
899
+ registryUrl: this.configManager.getRegistryUrl()
900
+ };
901
+ if (options?.timeout !== void 0) clientConfig.timeout = options.timeout;
902
+ if (options?.userAgent !== void 0) clientConfig.userAgent = options.userAgent;
903
+ this.client = new MpakClient(clientConfig);
904
+ this.bundleCache = new MpakBundleCache(this.client, {
905
+ mpakHome: this.configManager.mpakHome
906
+ });
907
+ }
908
+ /**
909
+ * Prepare a bundle for execution.
910
+ *
911
+ * Accepts either a registry spec (`{ name, version? }`) or a local bundle
912
+ * spec (`{ local }`). Downloads/extracts as needed, reads the manifest,
913
+ * validates user config, and resolves the command, args, and env needed
914
+ * to spawn the MCP server process.
915
+ *
916
+ * @param spec - Which bundle to prepare. See {@link PrepareServerSpec}.
917
+ * @param options - Force re-download/re-extract, extra env, and workspace dir.
918
+ *
919
+ * @throws {MpakConfigError} If required user config values are missing.
920
+ * @throws {MpakCacheCorruptedError} If the manifest is missing or corrupt after download.
921
+ */
922
+ async prepareServer(spec, options) {
923
+ let cacheDir;
924
+ let name;
925
+ let version;
926
+ let manifest;
927
+ if ("local" in spec) {
928
+ ({ cacheDir, name, version, manifest } = await this.prepareLocalBundle(spec.local, options));
929
+ } else {
930
+ ({ cacheDir, name, version, manifest } = await this.prepareRegistryBundle(
931
+ spec.name,
932
+ spec.version,
933
+ options
934
+ ));
935
+ }
936
+ const userConfigValues = this.gatherUserConfig(name, manifest);
937
+ const { command, args, env } = this.resolveCommand(manifest, cacheDir, userConfigValues);
938
+ env["MPAK_WORKSPACE"] = options?.workspaceDir ?? join4(process.cwd(), ".mpak");
939
+ if (options?.env) {
940
+ Object.assign(env, options.env);
941
+ }
942
+ return { command, args, env, cwd: cacheDir, name, version };
943
+ }
944
+ // ===========================================================================
945
+ // Private helpers
946
+ // ===========================================================================
947
+ /**
948
+ * Load a registry bundle into cache and read its manifest.
949
+ */
950
+ async prepareRegistryBundle(packageName, version, options) {
951
+ const loadOptions = {};
952
+ if (version !== void 0) loadOptions.version = version;
953
+ if (options?.force !== void 0) loadOptions.force = options.force;
954
+ const loadResult = await this.bundleCache.loadBundle(packageName, loadOptions);
955
+ const manifest = this.bundleCache.getBundleManifest(packageName);
956
+ if (!manifest) {
957
+ throw new MpakCacheCorruptedError(
958
+ `Manifest file missing for ${packageName}`,
959
+ join4(this.bundleCache.cacheHome, packageName)
960
+ );
961
+ }
962
+ return {
963
+ cacheDir: loadResult.cacheDir,
964
+ name: packageName,
965
+ version: loadResult.version,
966
+ manifest
967
+ };
968
+ }
969
+ /**
970
+ * Extract a local `.mcpb` bundle (if stale) and read its manifest.
971
+ * Local bundles are cached under `<cacheHome>/_local/<hash>`.
972
+ *
973
+ * The caller is responsible for validating that `bundlePath` exists
974
+ * and has a `.mcpb` extension before calling this method.
975
+ */
976
+ async prepareLocalBundle(bundlePath, options) {
977
+ const absolutePath = resolve3(bundlePath);
978
+ const hash = hashBundlePath(absolutePath);
979
+ const cacheDir = join4(this.bundleCache.cacheHome, "_local", hash);
980
+ const needsExtract = options?.force || localBundleNeedsExtract(absolutePath, cacheDir);
981
+ if (needsExtract) {
982
+ if (existsSync4(cacheDir)) {
983
+ rmSync2(cacheDir, { recursive: true, force: true });
984
+ }
985
+ try {
986
+ extractZip(absolutePath, cacheDir);
987
+ } catch (err) {
988
+ throw new MpakInvalidBundleError(
989
+ err instanceof Error ? err.message : String(err),
990
+ absolutePath,
991
+ err instanceof Error ? err : void 0
992
+ );
993
+ }
994
+ writeFileSync3(
995
+ join4(cacheDir, ".mpak-local-meta.json"),
996
+ JSON.stringify({
997
+ localPath: absolutePath,
998
+ extractedAt: (/* @__PURE__ */ new Date()).toISOString()
999
+ })
1000
+ );
1001
+ }
1002
+ let manifest;
1003
+ try {
1004
+ manifest = readJsonFromFile(join4(cacheDir, "manifest.json"), McpbManifestSchema2);
1005
+ } catch (err) {
1006
+ throw new MpakInvalidBundleError(
1007
+ err instanceof Error ? err.message : String(err),
1008
+ absolutePath,
1009
+ err instanceof Error ? err : void 0
1010
+ );
1011
+ }
1012
+ return { cacheDir, name: manifest.name, version: manifest.version, manifest };
1013
+ }
1014
+ /**
1015
+ * Gather stored user config values and validate that all required fields are present.
1016
+ * @throws If required config values are missing.
1017
+ */
1018
+ gatherUserConfig(packageName, manifest) {
1019
+ if (!manifest.user_config || Object.keys(manifest.user_config).length === 0) {
1020
+ return {};
1021
+ }
1022
+ const storedConfig = this.configManager.getPackageConfig(packageName) ?? {};
1023
+ const result = {};
1024
+ const missingFields = [];
1025
+ for (const [fieldName, fieldData] of Object.entries(manifest.user_config)) {
1026
+ const storedValue = storedConfig[fieldName];
1027
+ if (storedValue !== void 0) {
1028
+ result[fieldName] = storedValue;
1029
+ } else if (fieldData.default !== void 0 && fieldData.default !== null) {
1030
+ result[fieldName] = String(fieldData.default);
1031
+ } else if (fieldData.required) {
1032
+ const field = {
1033
+ key: fieldName,
1034
+ title: fieldData.title ?? fieldName,
1035
+ sensitive: fieldData.sensitive ?? false
1036
+ };
1037
+ if (fieldData.description !== void 0) {
1038
+ field.description = fieldData.description;
1039
+ }
1040
+ missingFields.push(field);
1041
+ }
1042
+ }
1043
+ if (missingFields.length > 0) {
1044
+ throw new MpakConfigError(packageName, missingFields);
1045
+ }
1046
+ return result;
1047
+ }
1048
+ /**
1049
+ * Resolve the manifest's `server` block into a spawnable command, args, and env.
1050
+ *
1051
+ * Handles three server types:
1052
+ * - **binary** — runs the compiled executable at `entry_point`, chmod'd +x.
1053
+ * - **node** — runs `mcp_config.command` (default `"node"`) with `mcp_config.args`,
1054
+ * or falls back to `node <entry_point>` when args are empty.
1055
+ * - **python** — like node, but resolves `python3`/`python` at runtime and
1056
+ * prepends `<cacheDir>/deps` to `PYTHONPATH` for bundled dependencies.
1057
+ *
1058
+ * All `${__dirname}` placeholders in args are replaced with `cacheDir`.
1059
+ * All `${user_config.*}` placeholders in env are replaced with gathered user values.
1060
+ *
1061
+ * @throws For unsupported server types.
1062
+ */
1063
+ resolveCommand(manifest, cacheDir, userConfigValues) {
1064
+ const { type, entry_point, mcp_config } = manifest.server;
1065
+ const env = _Mpak.substituteEnvVars(mcp_config.env, userConfigValues);
1066
+ let command;
1067
+ let args;
1068
+ switch (type) {
1069
+ case "binary": {
1070
+ command = join4(cacheDir, entry_point);
1071
+ args = _Mpak.resolveArgs(mcp_config.args ?? [], cacheDir);
1072
+ try {
1073
+ chmodSync(command, 493);
1074
+ } catch {
1075
+ }
1076
+ break;
1077
+ }
1078
+ case "node": {
1079
+ command = mcp_config.command || "node";
1080
+ args = mcp_config.args.length > 0 ? _Mpak.resolveArgs(mcp_config.args, cacheDir) : [join4(cacheDir, entry_point)];
1081
+ break;
1082
+ }
1083
+ case "python": {
1084
+ command = mcp_config.command === "python" ? _Mpak.findPythonCommand() : mcp_config.command || _Mpak.findPythonCommand();
1085
+ args = mcp_config.args.length > 0 ? _Mpak.resolveArgs(mcp_config.args, cacheDir) : [join4(cacheDir, entry_point)];
1086
+ const depsDir = join4(cacheDir, "deps");
1087
+ env["PYTHONPATH"] = env["PYTHONPATH"] ? `${depsDir}:${env["PYTHONPATH"]}` : depsDir;
1088
+ break;
1089
+ }
1090
+ case "uv": {
1091
+ command = mcp_config.command || "uv";
1092
+ args = mcp_config.args.length > 0 ? _Mpak.resolveArgs(mcp_config.args, cacheDir) : ["run", join4(cacheDir, entry_point)];
1093
+ break;
1094
+ }
1095
+ default: {
1096
+ const _exhaustive = type;
1097
+ throw new MpakCacheCorruptedError(
1098
+ `Unsupported server type "${_exhaustive}" in manifest for ${manifest.name}`,
1099
+ cacheDir
1100
+ );
1101
+ }
1102
+ }
1103
+ return { command, args, env };
1104
+ }
1105
+ /**
1106
+ * Substitute `${__dirname}` placeholders in args.
1107
+ */
1108
+ static resolveArgs(args, cacheDir) {
1109
+ return args.map((arg) => arg.replace(/\$\{__dirname\}/g, cacheDir));
1110
+ }
1111
+ /**
1112
+ * Substitute `${user_config.*}` placeholders in env vars.
1113
+ */
1114
+ static substituteEnvVars(env, userConfigValues) {
1115
+ if (!env) return {};
1116
+ const result = {};
1117
+ for (const [key, value] of Object.entries(env)) {
1118
+ result[key] = value.replace(
1119
+ /\$\{user_config\.([^}]+)\}/g,
1120
+ (match, configKey) => userConfigValues[configKey] ?? match
1121
+ );
1122
+ }
1123
+ return result;
1124
+ }
1125
+ /**
1126
+ * Find a working Python executable. Tries `python3` first, falls back to `python`.
1127
+ */
1128
+ static findPythonCommand() {
1129
+ const result = spawnSync("python3", ["--version"], { stdio: "pipe" });
1130
+ if (result.status === 0) {
1131
+ return "python3";
1132
+ }
1133
+ return "python";
1134
+ }
1135
+ };
1136
+
1137
+ // src/utils.ts
1138
+ function parsePackageSpec(spec) {
1139
+ const lastAtIndex = spec.lastIndexOf("@");
1140
+ let name;
1141
+ let version;
1142
+ if (lastAtIndex > 0) {
1143
+ name = spec.substring(0, lastAtIndex);
1144
+ version = spec.substring(lastAtIndex + 1);
1145
+ } else {
1146
+ name = spec;
1147
+ }
1148
+ if (!name.startsWith("@") || !name.includes("/")) {
1149
+ throw new MpakError(
1150
+ `Invalid package spec: "${spec}". Expected scoped format: @scope/name`,
1151
+ "INVALID_SPEC"
1152
+ );
1153
+ }
1154
+ return version ? { name, version } : { name };
1155
+ }
471
1156
  export {
1157
+ Mpak,
1158
+ MpakBundleCache,
1159
+ MpakCacheCorruptedError,
472
1160
  MpakClient,
1161
+ MpakConfigCorruptedError,
1162
+ MpakConfigError,
1163
+ MpakConfigManager,
473
1164
  MpakError,
474
1165
  MpakIntegrityError,
1166
+ MpakInvalidBundleError,
475
1167
  MpakNetworkError,
476
- MpakNotFoundError
1168
+ MpakNotFoundError,
1169
+ parsePackageSpec
477
1170
  };
478
1171
  //# sourceMappingURL=index.js.map