@craft_eco/craft-mcp 0.1.29 → 0.1.30

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 (152) hide show
  1. package/README.md +10 -55
  2. package/bin/craft-mcp.mjs +4 -1
  3. package/dist-server/game-workflow/check.mjs +3 -2
  4. package/dist-server/game-workflow/context-state.mjs +67 -67
  5. package/dist-server/game-workflow/draft-archive.mjs +147 -2
  6. package/dist-server/game-workflow/draft-upload-identity.mjs +11 -0
  7. package/dist-server/game-workflow/draft-uploads.mjs +105 -60
  8. package/dist-server/game-workflow/local-context.mjs +62 -62
  9. package/dist-server/game-workflow/native-context.mjs +289 -289
  10. package/dist-server/game-workflow/project-bundles.mjs +246 -246
  11. package/dist-server/game-workflow/project-context.mjs +8 -8
  12. package/dist-server/game-workflow/projects.mjs +6 -4
  13. package/dist-server/game-workflow/tasks.mjs +1 -1
  14. package/dist-server/game-workflow/work-scope.mjs +7 -7
  15. package/dist-server/integrations/config.mjs +145 -145
  16. package/dist-server/integrations/connect.mjs +36 -0
  17. package/dist-server/integrations/connection-handoff.mjs +36 -42
  18. package/dist-server/integrations/dsh-config.mjs +182 -182
  19. package/dist-server/integrations/files.mjs +54 -54
  20. package/dist-server/integrations/fixed-launcher.mjs +31 -0
  21. package/dist-server/integrations/host-reload.mjs +19 -0
  22. package/dist-server/integrations/hosts.mjs +55 -55
  23. package/dist-server/integrations/install.mjs +223 -205
  24. package/dist-server/integrations/installation-state.mjs +84 -84
  25. package/dist-server/integrations/launcher-start.mjs +52 -0
  26. package/dist-server/integrations/npm-diagnostics.mjs +32 -0
  27. package/dist-server/integrations/runtime-gc.mjs +191 -188
  28. package/dist-server/integrations/runtime-identity.mjs +22 -0
  29. package/dist-server/integrations/runtime.mjs +111 -126
  30. package/dist-server/integrations/updates.mjs +258 -225
  31. package/dist-server/integrations/version-policy.mjs +32 -32
  32. package/dist-server/launcher.mjs +1845 -0
  33. package/dist-server/mcp/control-panel.mjs +8 -23
  34. package/dist-server/mcp/server.mjs +31 -80
  35. package/dist-server/mcp/update-session.mjs +107 -92
  36. package/dist-server/upstream/craft-aigc.mjs +43 -16
  37. package/dist-server/upstream/draft-source.mjs +29 -0
  38. package/package.json +3 -2
  39. package/resources/context-filter-report.json +1 -1
  40. package/resources/craft-create/SKILL.md +19 -19
  41. package/resources/craft-update/SKILL.md +9 -11
  42. package/resources/targets/claude/js/bootstrap/SKILL.md +3 -1
  43. package/resources/targets/claude/js/context-filter-report.json +1 -1
  44. package/resources/targets/claude/js/craft-game-maker/SKILL.md +11 -15
  45. package/resources/targets/claude/js/js/bundle.json +7 -7
  46. package/resources/targets/claude/js/recovery.md +5 -1
  47. package/resources/targets/claude/js/target.json +1 -1
  48. package/resources/targets/claude/js3d/bootstrap/SKILL.md +3 -1
  49. package/resources/targets/claude/js3d/context-filter-report.json +1 -1
  50. package/resources/targets/claude/js3d/craft-game-maker/SKILL.md +11 -15
  51. package/resources/targets/claude/js3d/js3d/bundle.json +7 -7
  52. package/resources/targets/claude/js3d/recovery.md +5 -1
  53. package/resources/targets/claude/js3d/target.json +1 -1
  54. package/resources/targets/codebuddy/js/bootstrap/SKILL.md +3 -1
  55. package/resources/targets/codebuddy/js/context-filter-report.json +1 -1
  56. package/resources/targets/codebuddy/js/craft-game-maker/SKILL.md +11 -15
  57. package/resources/targets/codebuddy/js/js/bundle.json +7 -7
  58. package/resources/targets/codebuddy/js/recovery.md +5 -1
  59. package/resources/targets/codebuddy/js/target.json +1 -1
  60. package/resources/targets/codebuddy/js3d/bootstrap/SKILL.md +3 -1
  61. package/resources/targets/codebuddy/js3d/context-filter-report.json +1 -1
  62. package/resources/targets/codebuddy/js3d/craft-game-maker/SKILL.md +11 -15
  63. package/resources/targets/codebuddy/js3d/js3d/bundle.json +7 -7
  64. package/resources/targets/codebuddy/js3d/recovery.md +5 -1
  65. package/resources/targets/codebuddy/js3d/target.json +1 -1
  66. package/resources/targets/codex/js/bootstrap/SKILL.md +2 -4
  67. package/resources/targets/codex/js/context-filter-report.json +1 -1
  68. package/resources/targets/codex/js/craft-game-maker/SKILL.md +11 -15
  69. package/resources/targets/codex/js/js/bundle.json +7 -7
  70. package/resources/targets/codex/js/recovery.md +3 -3
  71. package/resources/targets/codex/js/target.json +1 -1
  72. package/resources/targets/codex/js3d/bootstrap/SKILL.md +2 -4
  73. package/resources/targets/codex/js3d/context-filter-report.json +1 -1
  74. package/resources/targets/codex/js3d/craft-game-maker/SKILL.md +11 -15
  75. package/resources/targets/codex/js3d/js3d/bundle.json +7 -7
  76. package/resources/targets/codex/js3d/recovery.md +3 -3
  77. package/resources/targets/codex/js3d/target.json +1 -1
  78. package/resources/targets/cursor/js/bootstrap/SKILL.md +3 -1
  79. package/resources/targets/cursor/js/context-filter-report.json +1 -1
  80. package/resources/targets/cursor/js/craft-game-maker/SKILL.md +11 -15
  81. package/resources/targets/cursor/js/js/bundle.json +7 -7
  82. package/resources/targets/cursor/js/recovery.md +5 -1
  83. package/resources/targets/cursor/js/target.json +1 -1
  84. package/resources/targets/cursor/js3d/bootstrap/SKILL.md +3 -1
  85. package/resources/targets/cursor/js3d/context-filter-report.json +1 -1
  86. package/resources/targets/cursor/js3d/craft-game-maker/SKILL.md +11 -15
  87. package/resources/targets/cursor/js3d/js3d/bundle.json +7 -7
  88. package/resources/targets/cursor/js3d/recovery.md +5 -1
  89. package/resources/targets/cursor/js3d/target.json +1 -1
  90. package/resources/targets/dsh/js/bootstrap/SKILL.md +3 -1
  91. package/resources/targets/dsh/js/context-filter-report.json +1 -1
  92. package/resources/targets/dsh/js/craft-game-maker/SKILL.md +11 -15
  93. package/resources/targets/dsh/js/js/bundle.json +7 -7
  94. package/resources/targets/dsh/js/recovery.md +5 -1
  95. package/resources/targets/dsh/js/target.json +1 -1
  96. package/resources/targets/dsh/js3d/bootstrap/SKILL.md +3 -1
  97. package/resources/targets/dsh/js3d/context-filter-report.json +1 -1
  98. package/resources/targets/dsh/js3d/craft-game-maker/SKILL.md +11 -15
  99. package/resources/targets/dsh/js3d/js3d/bundle.json +7 -7
  100. package/resources/targets/dsh/js3d/recovery.md +5 -1
  101. package/resources/targets/dsh/js3d/target.json +1 -1
  102. package/resources/targets/index.json +32 -32
  103. package/resources/targets/kimi/js/bootstrap/SKILL.md +3 -1
  104. package/resources/targets/kimi/js/context-filter-report.json +1 -1
  105. package/resources/targets/kimi/js/craft-game-maker/SKILL.md +11 -15
  106. package/resources/targets/kimi/js/js/bundle.json +7 -7
  107. package/resources/targets/kimi/js/recovery.md +5 -1
  108. package/resources/targets/kimi/js/target.json +1 -1
  109. package/resources/targets/kimi/js3d/bootstrap/SKILL.md +3 -1
  110. package/resources/targets/kimi/js3d/context-filter-report.json +1 -1
  111. package/resources/targets/kimi/js3d/craft-game-maker/SKILL.md +11 -15
  112. package/resources/targets/kimi/js3d/js3d/bundle.json +7 -7
  113. package/resources/targets/kimi/js3d/recovery.md +5 -1
  114. package/resources/targets/kimi/js3d/target.json +1 -1
  115. package/resources/targets/workbuddy/js/bootstrap/SKILL.md +22 -24
  116. package/resources/targets/workbuddy/js/context-filter-report.json +1 -1
  117. package/resources/targets/workbuddy/js/craft-game-maker/SKILL.md +11 -15
  118. package/resources/targets/workbuddy/js/js/bundle.json +8 -8
  119. package/resources/targets/workbuddy/js/js/context/AGENTS.md +29 -29
  120. package/resources/targets/workbuddy/js/recovery.md +19 -19
  121. package/resources/targets/workbuddy/js/target.json +1 -1
  122. package/resources/targets/workbuddy/js3d/bootstrap/SKILL.md +22 -24
  123. package/resources/targets/workbuddy/js3d/context-filter-report.json +1 -1
  124. package/resources/targets/workbuddy/js3d/craft-game-maker/SKILL.md +11 -15
  125. package/resources/targets/workbuddy/js3d/js3d/bundle.json +8 -8
  126. package/resources/targets/workbuddy/js3d/js3d/context/AGENTS.md +30 -30
  127. package/resources/targets/workbuddy/js3d/recovery.md +19 -19
  128. package/resources/targets/workbuddy/js3d/target.json +1 -1
  129. package/resources/targets/zcode/js/bootstrap/SKILL.md +3 -1
  130. package/resources/targets/zcode/js/context-filter-report.json +1 -1
  131. package/resources/targets/zcode/js/craft-game-maker/SKILL.md +11 -15
  132. package/resources/targets/zcode/js/js/bundle.json +7 -7
  133. package/resources/targets/zcode/js/recovery.md +5 -1
  134. package/resources/targets/zcode/js/target.json +1 -1
  135. package/resources/targets/zcode/js3d/bootstrap/SKILL.md +3 -1
  136. package/resources/targets/zcode/js3d/context-filter-report.json +1 -1
  137. package/resources/targets/zcode/js3d/craft-game-maker/SKILL.md +11 -15
  138. package/resources/targets/zcode/js3d/js3d/bundle.json +7 -7
  139. package/resources/targets/zcode/js3d/recovery.md +5 -1
  140. package/resources/targets/zcode/js3d/target.json +1 -1
  141. package/vendor/craft-runtime/SOURCE.json +4 -4
  142. package/vendor/craft-runtime/package.json +1 -1
  143. package/dist-server/game-workflow/publication-materials.mjs +0 -92
  144. package/dist-server/game-workflow/publications.mjs +0 -168
  145. package/dist-server/game-workflow/publish-capture-worker.mjs +0 -57
  146. package/dist-server/game-workflow/publish-capture.mjs +0 -75
  147. package/dist-server/mcp/publication-page.html +0 -86
  148. package/dist-server/mcp/publication-page.mjs +0 -375
  149. package/dist-server/mcp/publication-preview.mjs +0 -63
  150. package/dist-server/mcp/publication-tools.mjs +0 -22
  151. package/dist-server/mcp/publish-panel.mjs +0 -210
  152. package/dist-server/upstream/publication-transport.mjs +0 -101
@@ -1,126 +1,111 @@
1
- import path from 'node:path';
2
- import os from 'node:os';
3
- import { existsSync } from 'node:fs';
4
- import { mkdir, mkdtemp, readFile, writeFile, rename, rm, readdir, lstat } from 'node:fs/promises';
5
- import { createHash } from 'node:crypto';
6
- import { execFile } from 'node:child_process';
7
- import { promisify } from 'node:util';
8
- import { Client } from '@modelcontextprotocol/sdk/client/index.js';
9
- import { StdioClientTransport, getDefaultEnvironment } from '@modelcontextprotocol/sdk/client/stdio.js';
10
-
11
- const execute = promisify(execFile);
12
- export function npmEntry() {
13
- for (const dir of [path.dirname(process.execPath), ...(process.env.PATH || process.env.Path || '').split(path.delimiter)]) {
14
- for (const candidate of ['node_modules/npm/bin/npm-cli.js', '../lib/node_modules/npm/bin/npm-cli.js', '../share/nodejs/npm/bin/npm-cli.js']) {
15
- const file = path.resolve(dir, candidate); if (existsSync(file)) return file;
16
- }
17
- }
18
- throw new Error('Node.js must include npm');
19
- }
20
-
21
- export async function runNpm(args, cwd) {
22
- try { return await execute(process.execPath, [npmEntry(), ...args], { cwd, windowsHide: true, timeout: 180000, maxBuffer: 8 * 1024 * 1024 }); }
23
- catch { throw new Error('Could not materialize Craft runtime with npm; check Node/npm and registry connectivity'); }
24
- }
25
-
26
- // The content identity is independent of tarball repacking and is read from the
27
- // running package, never from a host-provided version environment variable.
28
- export async function getRuntimeIdentity(packageRoot) {
29
- packageRoot = path.resolve(packageRoot);
30
- const pkg = JSON.parse(await readFile(path.join(packageRoot, 'package.json'), 'utf8'));
31
- const hash = createHash('sha256');
32
- async function visit(relative) {
33
- const file = path.join(packageRoot, relative);
34
- let info; try { info = await lstat(file); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
35
- if (info.isSymbolicLink()) throw new Error('Craft runtime contains a linked product file: ' + relative);
36
- if (info.isDirectory()) {
37
- for (const entry of (await readdir(file)).sort()) if (entry !== 'node_modules') await visit(relative + '/' + entry);
38
- } else if (info.isFile()) hash.update(relative + '\0').update(await readFile(file)).update('\0');
39
- else throw new Error('Invalid Craft runtime file: ' + relative);
40
- }
41
- for (const relative of ['package.json', 'bin', 'dist-server', 'resources', 'vendor']) await visit(relative);
42
- return { package: pkg.name, version: pkg.version, build_id: hash.digest('hex'), package_root: packageRoot, entry: path.join(packageRoot, 'bin/craft-mcp.mjs') };
43
- }
44
-
45
- export function verifyArchiveIntegrity(bytes, integrity) {
46
- const candidates = String(integrity ?? '').trim().split(/\s+/).map(value => /^(sha512|sha384|sha256|sha1)-([A-Za-z0-9+/]+={0,2})$/.exec(value)).filter(Boolean);
47
- if (!candidates.length) throw new Error('npm package has no supported integrity digest');
48
- const order = ['sha512', 'sha384', 'sha256', 'sha1'];
49
- const algorithm = order.find(value => candidates.some(item => item[1] === value));
50
- const actual = createHash(algorithm).update(bytes).digest('base64');
51
- if (!candidates.some(item => item[1] === algorithm && item[2] === actual)) throw new Error('npm package integrity mismatch');
52
- }
53
-
54
- async function stageArchive(archive, pkg, runtimeRoot, { registry, integrity } = {}) {
55
- const bytes = await readFile(archive);
56
- if (integrity) verifyArchiveIntegrity(bytes, integrity);
57
- const hash = createHash('sha256').update(bytes).digest('hex');
58
- const target = path.join(runtimeRoot, pkg.version + '-' + hash.slice(0, 16));
59
- const packageRoot = path.join(target, 'node_modules', ...pkg.name.split('/'));
60
- const metadata = path.join(target, 'craft-runtime.json');
61
- if (!existsSync(metadata)) {
62
- await mkdir(runtimeRoot, { recursive: true });
63
- const staging = await mkdtemp(path.join(runtimeRoot, '.staging-'));
64
- try {
65
- await writeFile(path.join(staging, 'package.json'), '{"private":true}\n');
66
- await runNpm(['install', '--ignore-scripts', '--engine-strict', '--no-audit', '--no-fund', '--omit=dev', ...(registry ? ['--registry=' + registry] : []), archive], staging);
67
- const stagedRoot = path.join(staging, 'node_modules', ...pkg.name.split('/'));
68
- const installed = JSON.parse(await readFile(path.join(stagedRoot, 'package.json'), 'utf8'));
69
- if (installed.name !== pkg.name || installed.version !== pkg.version || !installed.bin?.['craft-mcp'] || !installed.files?.includes('resources')) throw new Error('Downloaded package is not the expected Craft distribution');
70
- const identity = await getRuntimeIdentity(stagedRoot);
71
- await writeFile(path.join(staging, 'craft-runtime.json'), JSON.stringify({ version: pkg.version, package: pkg.name, sha256: hash, build_id: identity.build_id, ...(integrity ? { integrity } : {}) }) + '\n');
72
- await rename(staging, target);
73
- } finally { await rm(staging, { recursive: true, force: true }); }
74
- }
75
- const recorded = JSON.parse(await readFile(metadata, 'utf8'));
76
- const identity = await getRuntimeIdentity(packageRoot);
77
- if (recorded.sha256 !== hash || recorded.package !== pkg.name || identity.package !== pkg.name || identity.version !== pkg.version || (recorded.build_id && recorded.build_id !== identity.build_id)) throw new Error('Cached Craft runtime integrity mismatch');
78
- return { ...identity, sha256: hash, directory: target, ...(integrity ? { integrity } : {}) };
79
- }
80
-
81
- export async function stageRuntime(packageRoot, runtimeRoot) {
82
- const pkg = JSON.parse(await readFile(path.join(packageRoot, 'package.json'), 'utf8'));
83
- if (!pkg.bin?.['craft-mcp'] || !pkg.files?.includes('resources')) throw new Error('Install from the built npm distribution, not the source/kit directory');
84
- const temp = await mkdtemp(path.join(os.tmpdir(), 'craft-runtime-'));
85
- try {
86
- const packed = JSON.parse((await runNpm(['pack', '--ignore-scripts', '--json', '--pack-destination', temp], packageRoot)).stdout);
87
- const archive = path.join(temp, packed[0].filename);
88
- return await stageArchive(archive, pkg, runtimeRoot, { registry: pkg.publishConfig?.registry });
89
- } finally { await rm(temp, { recursive: true, force: true }); }
90
- }
91
-
92
- export async function stageRegistryRuntime(source, metadata, runtimeRoot) {
93
- const temp = await mkdtemp(path.join(os.tmpdir(), 'craft-update-package-'));
94
- try {
95
- const packed = JSON.parse((await runNpm(['pack', source.package + '@' + metadata.version, '--ignore-scripts', '--json', '--registry=' + source.registry, '--pack-destination', temp], temp)).stdout);
96
- if (!Array.isArray(packed) || packed.length !== 1 || path.basename(packed[0].filename) !== packed[0].filename) throw new Error('Unexpected npm package archive');
97
- return await stageArchive(path.join(temp, packed[0].filename), { name: source.package, version: metadata.version }, runtimeRoot, { registry: source.registry, integrity: metadata.integrity });
98
- } finally { await rm(temp, { recursive: true, force: true }); }
99
- }
100
-
101
- export async function probeRuntime(connection, expected = {}) {
102
- const state = await mkdtemp(path.join(os.tmpdir(), 'craft-probe-'));
103
- const env = { ...getDefaultEnvironment(), ...connection.env, CLIENT_DATA_DIR: state };
104
- delete env.CRAFT_RUNTIME_WORKER;
105
- delete env.CRAFT_TOKEN; delete env.NODE_OPTIONS; delete env.CRAFT_MCP_RESOURCES_DIR; delete env.CRAFT_INSTALLATION_FILE;
106
- const transport = new StdioClientTransport({ command: connection.command, args: connection.args, env, stderr: 'pipe' });
107
- // Consume diagnostics without exposing config values or credentials in installer reports.
108
- transport.stderr?.resume();
109
- const client = new Client({ name: 'craft-install-check', version: '1.0.0' });
110
- try {
111
- await client.connect(transport, { timeout: 120000 });
112
- const listed = await client.listTools();
113
- const response = await client.callTool({ name: 'craft_game_info', arguments: {} });
114
- if (response.isError) throw new Error('craft_game_info failed');
115
- const info = response.structuredContent ?? JSON.parse(response.content.find(item => item.type === 'text').text);
116
- if (!listed.tools.some(tool => tool.name === 'craft_game_prepare') || !listed.tools.some(tool => tool.name === 'craft_game_review')) throw new Error('Craft tools missing');
117
- if(info.lifecycle && (!listed.tools.some(tool=>tool.name==='craft_game_release') || info.lifecycle.query_updates_context!==false)) throw new Error('Craft work lifecycle is incomplete');
118
- if (expected.version && info.version !== expected.version) throw new Error('Runtime version mismatch');
119
- if (expected.build_id && info.runtime?.build_id !== expected.build_id) throw new Error('Runtime build identity mismatch');
120
- if (expected.host && info.host !== expected.host) throw new Error('Runtime host mismatch');
121
- if (expected.workspace && path.resolve(info.workspace) !== path.resolve(expected.workspace)) throw new Error('Runtime workspace mismatch');
122
- if (expected.platform && info.platform !== expected.platform) throw new Error('Runtime platform mismatch');
123
- if (expected.host && ['js','js3d'].some(engine=>info.installed_contexts?.[engine]?.host !== expected.host)) throw new Error('Runtime context host mismatch');
124
- return { scope: 'standalone_probe', version: info.version, runtime: info.runtime, host: info.host, workspace: info.workspace, platform: info.platform, tools: listed.tools.length, engines: info.supported_engines, contexts:info.installed_contexts };
125
- } finally { await client.close(); await rm(state, { recursive: true, force: true }); }
126
- }
1
+ import { getRuntimeIdentity } from './runtime-identity.mjs';
2
+ import path from 'node:path';
3
+ import os from 'node:os';
4
+ import { existsSync } from 'node:fs';
5
+ import { mkdir, mkdtemp, readFile, writeFile, rename, rm, readdir, lstat } from 'node:fs/promises';
6
+ import { createHash } from 'node:crypto';
7
+ import { execFile } from 'node:child_process';
8
+ import { promisify } from 'node:util';
9
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
10
+ import { StdioClientTransport, getDefaultEnvironment } from '@modelcontextprotocol/sdk/client/stdio.js';
11
+ import { npmFailure } from './npm-diagnostics.mjs';
12
+
13
+ const execute = promisify(execFile);
14
+ export function npmEntry() {
15
+ for (const dir of [path.dirname(process.execPath), ...(process.env.PATH || process.env.Path || '').split(path.delimiter)]) {
16
+ for (const candidate of ['node_modules/npm/bin/npm-cli.js', '../lib/node_modules/npm/bin/npm-cli.js', '../share/nodejs/npm/bin/npm-cli.js']) {
17
+ const file = path.resolve(dir, candidate); if (existsSync(file)) return file;
18
+ }
19
+ }
20
+ throw new Error('Node.js must include npm');
21
+ }
22
+
23
+ export async function runNpm(args, cwd) {
24
+ try { return await execute(process.execPath, [npmEntry(), ...args], { cwd, windowsHide: true, timeout: 180000, maxBuffer: 8 * 1024 * 1024 }); }
25
+ catch (error) { throw npmFailure(error); }
26
+ }
27
+
28
+ export { getRuntimeIdentity } from './runtime-identity.mjs';
29
+
30
+ export function verifyArchiveIntegrity(bytes, integrity) {
31
+ const candidates = String(integrity ?? '').trim().split(/\s+/).map(value => /^(sha512|sha384|sha256|sha1)-([A-Za-z0-9+/]+={0,2})$/.exec(value)).filter(Boolean);
32
+ if (!candidates.length) throw new Error('npm package has no supported integrity digest');
33
+ const order = ['sha512', 'sha384', 'sha256', 'sha1'];
34
+ const algorithm = order.find(value => candidates.some(item => item[1] === value));
35
+ const actual = createHash(algorithm).update(bytes).digest('base64');
36
+ if (!candidates.some(item => item[1] === algorithm && item[2] === actual)) throw new Error('npm package integrity mismatch');
37
+ }
38
+
39
+ async function stageArchive(archive, pkg, runtimeRoot, { registry, integrity } = {}) {
40
+ const bytes = await readFile(archive);
41
+ if (integrity) verifyArchiveIntegrity(bytes, integrity);
42
+ const hash = createHash('sha256').update(bytes).digest('hex');
43
+ const target = path.join(runtimeRoot, pkg.version + '-' + hash.slice(0, 16));
44
+ const packageRoot = path.join(target, 'node_modules', ...pkg.name.split('/'));
45
+ const metadata = path.join(target, 'craft-runtime.json');
46
+ if (!existsSync(metadata)) {
47
+ await mkdir(runtimeRoot, { recursive: true });
48
+ const staging = await mkdtemp(path.join(runtimeRoot, '.staging-'));
49
+ try {
50
+ await writeFile(path.join(staging, 'package.json'), '{"private":true}\n');
51
+ await runNpm(['install', '--ignore-scripts', '--engine-strict', '--no-audit', '--no-fund', '--omit=dev', ...(registry ? ['--registry=' + registry] : []), archive], staging);
52
+ const stagedRoot = path.join(staging, 'node_modules', ...pkg.name.split('/'));
53
+ const installed = JSON.parse(await readFile(path.join(stagedRoot, 'package.json'), 'utf8'));
54
+ if (installed.name !== pkg.name || installed.version !== pkg.version || !installed.bin?.['craft-mcp'] || !installed.files?.includes('resources')) throw new Error('Downloaded package is not the expected Craft distribution');
55
+ const identity = await getRuntimeIdentity(stagedRoot);
56
+ await writeFile(path.join(staging, 'craft-runtime.json'), JSON.stringify({ version: pkg.version, package: pkg.name, sha256: hash, build_id: identity.build_id, ...(integrity ? { integrity } : {}) }) + '\n');
57
+ await rename(staging, target);
58
+ } finally { await rm(staging, { recursive: true, force: true }); }
59
+ }
60
+ const recorded = JSON.parse(await readFile(metadata, 'utf8'));
61
+ const identity = await getRuntimeIdentity(packageRoot);
62
+ if (recorded.sha256 !== hash || recorded.package !== pkg.name || identity.package !== pkg.name || identity.version !== pkg.version || (recorded.build_id && recorded.build_id !== identity.build_id)) throw new Error('Cached Craft runtime integrity mismatch');
63
+ return { ...identity, sha256: hash, directory: target, ...(integrity ? { integrity } : {}) };
64
+ }
65
+
66
+ export async function stageRuntime(packageRoot, runtimeRoot) {
67
+ const pkg = JSON.parse(await readFile(path.join(packageRoot, 'package.json'), 'utf8'));
68
+ if (!pkg.bin?.['craft-mcp'] || !pkg.files?.includes('resources')) throw new Error('Install from the built npm distribution, not the source/kit directory');
69
+ const temp = await mkdtemp(path.join(os.tmpdir(), 'craft-runtime-'));
70
+ try {
71
+ const packed = JSON.parse((await runNpm(['pack', '--ignore-scripts', '--json', '--pack-destination', temp], packageRoot)).stdout);
72
+ const archive = path.join(temp, packed[0].filename);
73
+ return await stageArchive(archive, pkg, runtimeRoot, { registry: pkg.publishConfig?.registry });
74
+ } finally { await rm(temp, { recursive: true, force: true }); }
75
+ }
76
+
77
+ export async function stageRegistryRuntime(source, metadata, runtimeRoot) {
78
+ const temp = await mkdtemp(path.join(os.tmpdir(), 'craft-update-package-'));
79
+ try {
80
+ const packed = JSON.parse((await runNpm(['pack', source.package + '@' + metadata.version, '--ignore-scripts', '--json', '--registry=' + source.registry, '--pack-destination', temp], temp)).stdout);
81
+ if (!Array.isArray(packed) || packed.length !== 1 || path.basename(packed[0].filename) !== packed[0].filename) throw new Error('Unexpected npm package archive');
82
+ return await stageArchive(path.join(temp, packed[0].filename), { name: source.package, version: metadata.version }, runtimeRoot, { registry: source.registry, integrity: metadata.integrity });
83
+ } finally { await rm(temp, { recursive: true, force: true }); }
84
+ }
85
+
86
+ export async function probeRuntime(connection, expected = {}) {
87
+ const state = await mkdtemp(path.join(os.tmpdir(), 'craft-probe-'));
88
+ const env = { ...getDefaultEnvironment(), ...connection.env, CLIENT_DATA_DIR: state, CRAFT_STANDALONE_PROBE: '1' };
89
+ delete env.CRAFT_RUNTIME_WORKER;
90
+ delete env.CRAFT_TOKEN; delete env.NODE_OPTIONS; delete env.CRAFT_MCP_RESOURCES_DIR; delete env.CRAFT_INSTALLATION_FILE;
91
+ const transport = new StdioClientTransport({ command: connection.command, args: connection.args, env, stderr: 'pipe' });
92
+ // Consume diagnostics without exposing config values or credentials in installer reports.
93
+ transport.stderr?.resume();
94
+ const client = new Client({ name: 'craft-install-check', version: '1.0.0' });
95
+ try {
96
+ await client.connect(transport, { timeout: 120000 });
97
+ const listed = await client.listTools();
98
+ const response = await client.callTool({ name: 'craft_game_info', arguments: {} });
99
+ if (response.isError) throw new Error('craft_game_info failed');
100
+ const info = response.structuredContent ?? JSON.parse(response.content.find(item => item.type === 'text').text);
101
+ if (!listed.tools.some(tool => tool.name === 'craft_game_prepare') || !listed.tools.some(tool => tool.name === 'craft_game_review')) throw new Error('Craft tools missing');
102
+ if(info.lifecycle && (!listed.tools.some(tool=>tool.name==='craft_game_release') || info.lifecycle.query_updates_context!==false)) throw new Error('Craft work lifecycle is incomplete');
103
+ if (expected.version && info.version !== expected.version) throw new Error('Runtime version mismatch');
104
+ if (expected.build_id && info.runtime?.build_id !== expected.build_id) throw new Error('Runtime build identity mismatch');
105
+ if (expected.host && info.host !== expected.host) throw new Error('Runtime host mismatch');
106
+ if (expected.workspace && path.resolve(info.workspace) !== path.resolve(expected.workspace)) throw new Error('Runtime workspace mismatch');
107
+ if (expected.platform && info.platform !== expected.platform) throw new Error('Runtime platform mismatch');
108
+ if (expected.host && ['js','js3d'].some(engine=>info.installed_contexts?.[engine]?.host !== expected.host)) throw new Error('Runtime context host mismatch');
109
+ return { scope: 'standalone_probe', version: info.version, runtime: info.runtime, host: info.host, workspace: info.workspace, platform: info.platform, tools: listed.tools.length, engines: info.supported_engines, contexts:info.installed_contexts };
110
+ } finally { await client.close(); await rm(state, { recursive: true, force: true }); }
111
+ }