@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,182 +1,182 @@
1
- import path from 'node:path';
2
- import { readFileSync, readdirSync, lstatSync, realpathSync } from 'node:fs';
3
- import { isDeepStrictEqual } from 'node:util';
4
- import { isAlias, isMap, isSeq, parseDocument } from 'yaml';
5
-
6
- const pluginName = '@deepseek-ai/dsh-mcp-client';
7
- const pluginId = 'mcp-craft';
8
- const isRecord = value => value !== null && typeof value === 'object' && !Array.isArray(value);
9
-
10
- function documentFor(text) {
11
- const document = parseDocument(text || '[]\n', { uniqueKeys: true });
12
- if (document.errors.length) throw new Error('Invalid DSH YAML configuration: ' + document.errors[0].message);
13
- if (document.contents === null) document.contents = document.createNode([]);
14
- if (!isSeq(document.contents)) throw new Error('DSH configuration must be a YAML patch array');
15
- return document;
16
- }
17
-
18
- /** Read plain patch data without evaluating Cordis !!js expressions. */
19
- export function readDshConfig(text) {
20
- return documentFor(text).toJS();
21
- }
22
-
23
- function isCraft(value) {
24
- const config = value?.config;
25
- return value?.id === pluginId || (value?.name === pluginName && (
26
- config?.serverName === 'craft' ||
27
- (Array.isArray(config?.args) && config.args.some(arg => typeof arg === 'string' && /^@craft_eco\/craft-mcp(?:@[^\s]+)?$/.test(arg)))
28
- ));
29
- }
30
-
31
- function hasDynamicNode(node) {
32
- if (!node || typeof node !== 'object') return false;
33
- if (isAlias(node) || node.tag) return true;
34
- if (isMap(node)) return node.items.some(pair => hasDynamicNode(pair.key) || hasDynamicNode(pair.value));
35
- if (isSeq(node)) return node.items.some(hasDynamicNode);
36
- return false;
37
- }
38
-
39
- function candidates(document) {
40
- const rows = [], identities = new Set(), allIds = new Map();
41
- const values = document.toJS();
42
- for (const [patchIndex, patch] of document.contents.items.entries()) {
43
- if (!isMap(patch)) throw new Error('DSH patch entries must be YAML mappings');
44
- const insert = patch.get('insert', true);
45
- if (insert !== undefined && !isSeq(insert)) throw new Error('DSH insert patch must contain an array');
46
- const entries = insert ? insert.items : [patch];
47
- for (const [entryIndex, node] of entries.entries()) {
48
- if (!isMap(node)) throw new Error('DSH plugin entries must be YAML mappings');
49
- const value = insert ? values[patchIndex].insert[entryIndex] : values[patchIndex];
50
- if (typeof value.id === 'string') allIds.set(value.id, (allIds.get(value.id) ?? 0) + 1);
51
- if (!isCraft(value)) continue;
52
- if (!insert || patch.items.some(pair => pair.key?.value !== 'insert')) {
53
- throw new Error('Ambiguous Craft DSH patch: use one complete insert registration without additional patch operations');
54
- }
55
- if (hasDynamicNode(node)) throw new Error('Dynamic or aliased Craft DSH configuration cannot be changed safely');
56
- if (value.name !== pluginName) throw new Error('DSH plugin id mcp-craft belongs to another plugin');
57
- if (typeof value.id !== 'string' || !value.id.trim()) throw new Error('Craft DSH registration requires an explicit plugin id');
58
- if (!isRecord(value.config) || typeof value.config.serverName !== 'string' || !/^[A-Za-z0-9_-]{1,32}$/.test(value.config.serverName)) {
59
- throw new Error('Invalid Craft DSH server configuration');
60
- }
61
- if (value.disabled !== undefined && typeof value.disabled !== 'boolean') throw new Error('Craft DSH disabled must be a boolean');
62
- if (identities.has(value.id)) throw new Error('Duplicate Craft DSH plugin id');
63
- identities.add(value.id);
64
- rows.push({ node, value });
65
- }
66
- }
67
- if (rows.length > 1) throw new Error('Multiple Craft DSH registrations found; keep one registration before installing');
68
- for (const { value } of rows) {
69
- if (allIds.get(value.id) > 1) throw new Error('Ambiguous or duplicate Craft DSH plugin id');
70
- }
71
- // A later id-only patch could silently override or disable the inserted server.
72
- for (const patch of document.contents.items) {
73
- if (patch.has('insert')) continue;
74
- const id = patch.get('id');
75
- if (identities.has(id)) throw new Error('Ambiguous Craft DSH id override; keep settings in the insert registration');
76
- }
77
- return rows;
78
- }
79
-
80
- function connectionFor(value) {
81
- const { serverName: _serverName, transport, ...connection } = value.config;
82
- return { ...connection, type: transport, disabled: value.disabled === true };
83
- }
84
-
85
- /** Craft registrations only; connection uses the common installer/probe shape. */
86
- export function dshRegistrations(text) {
87
- return candidates(documentFor(text)).map(({ value }) => ({ name: value.config.serverName, connection: connectionFor(value) }));
88
- }
89
-
90
- function connectionConfig(connection, previous = {}) {
91
- if (!isRecord(connection) || typeof connection.command !== 'string' || !connection.command.trim() ||
92
- !Array.isArray(connection.args) || connection.args.some(arg => typeof arg !== 'string') ||
93
- (connection.env !== undefined && (!isRecord(connection.env) || Object.values(connection.env).some(value => typeof value !== 'string')))) {
94
- throw new Error('Craft DSH requires a valid stdio command, args and string environment');
95
- }
96
- if (previous.env !== undefined && (!isRecord(previous.env) || Object.values(previous.env).some(value => typeof value !== 'string'))) {
97
- throw new Error('Invalid existing Craft DSH environment');
98
- }
99
- const next = { ...previous, ...connection, serverName: 'craft', transport: 'stdio', env: { ...previous.env, ...connection.env } };
100
- next.startupTimeoutMs ??= connection.timeout ?? (connection.startup_timeout_sec ? connection.startup_timeout_sec * 1000 : 120000);
101
- next.toolCallTimeoutMs ??= 3600000;
102
- next.failOnStartupError ??= true;
103
- for (const key of ['type', 'timeout', 'startup_timeout_sec', 'enabled', 'disabled', 'url', 'headers', 'http_headers', 'env_http_headers', 'bearer_token_env_var']) delete next[key];
104
- return next;
105
- }
106
-
107
- /** Merge a single owned stdio registration while retaining unrelated YAML nodes. */
108
- export function mergeDshConfig(text, connection, { previousName = 'craft', enable = true } = {}) {
109
- const document = documentFor(text), found = candidates(document);
110
- const previous = found.find(row => row.value.config.serverName === previousName);
111
- if (found.length && !previous) throw new Error('Craft DSH installation conflicts with an existing registration');
112
- if (!previous && previousName !== 'craft') throw new Error('Previous Craft DSH registration was not found');
113
- const config = connectionConfig(connection, previous?.value.config);
114
- if (!previous) {
115
- document.contents.items.push(document.createNode({ insert: [{ id: pluginId, name: pluginName, config }] }));
116
- } else {
117
- const next = { ...previous.value, config };
118
- if (enable && next.disabled === true) next.disabled = false;
119
- if (isDeepStrictEqual(previous.value, next)) return text;
120
- const configNode = previous.node.get('config', true);
121
- for (const pair of [...configNode.items]) {
122
- if (!Object.hasOwn(config, pair.key.value)) configNode.delete(pair.key.value);
123
- }
124
- for (const [key, value] of Object.entries(config)) {
125
- if (isDeepStrictEqual(previous.value.config[key], value)) continue;
126
- if (key === 'env' && isMap(configNode.get('env', true))) {
127
- const env = configNode.get('env', true);
128
- for (const [name, setting] of Object.entries(value)) {
129
- if (previous.value.config.env?.[name] !== setting) env.set(name, setting);
130
- }
131
- } else configNode.set(key, document.createNode(value));
132
- }
133
- if (enable && previous.value.disabled === true) previous.node.set('disabled', false);
134
- }
135
- document.contents.flow = false;
136
- const output = document.toString({ lineWidth: 0 });
137
- const written = dshRegistrations(output);
138
- if (written.length !== 1 || written[0].name !== 'craft' || !isDeepStrictEqual(written[0].connection, connectionFor({ config, disabled: !enable && previous?.value.disabled === true }))) {
139
- throw new Error('Generated Craft DSH configuration failed validation');
140
- }
141
- return output;
142
- }
143
-
144
- /** Read-only profile inventory; callers decide how to handle an existing scope. */
145
- export function inspectDshProfiles(dshHome) {
146
- const profiles = path.join(dshHome, 'profiles');
147
- let root;
148
- try {
149
- const stat = lstatSync(dshHome);
150
- if (stat.isSymbolicLink() || !stat.isDirectory()) throw new Error('DSH home must be a plain directory');
151
- root = realpathSync(dshHome);
152
- } catch (error) { if (error.code === 'ENOENT') return []; throw error; }
153
- const checkedPath = (file, type) => {
154
- const stat = lstatSync(file);
155
- if (stat.isSymbolicLink() || (type === 'directory' ? !stat.isDirectory() : !stat.isFile())) throw new Error('DSH profile paths must not contain links or special files: ' + file);
156
- const relative = path.relative(root, realpathSync(file));
157
- if (relative === '..' || relative.startsWith('..' + path.sep) || path.isAbsolute(relative)) throw new Error('DSH profile path escapes its home: ' + file);
158
- };
159
- let entries;
160
- try { checkedPath(profiles, 'directory'); entries = readdirSync(profiles, { withFileTypes: true }); }
161
- catch (error) { if (error.code === 'ENOENT') return []; throw error; }
162
- const found = [];
163
- for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
164
- if (entry.isSymbolicLink()) throw new Error('DSH profile paths must not contain links: ' + path.join(profiles, entry.name));
165
- if (!entry.isDirectory()) continue;
166
- checkedPath(path.join(profiles, entry.name), 'directory');
167
- const configPath = path.join(profiles, entry.name, 'cordis.patch.yml');
168
- let text;
169
- try { checkedPath(configPath, 'file'); text = readFileSync(configPath, 'utf8'); }
170
- catch (error) { if (error.code === 'ENOENT') continue; throw error; }
171
- const registrations = dshRegistrations(text);
172
- if (registrations.length) found.push({ path: configPath, registrations });
173
- }
174
- return found;
175
- }
176
-
177
- /** Shared install/doctor/update guard; never move profile-owned registrations. */
178
- export function assertDshSharedScope(paths) {
179
- if (!paths.dshHome) return;
180
- const profiles = inspectDshProfiles(paths.dshHome);
181
- if (profiles.length) throw new Error('DSH already has a profile-scoped Craft registration: ' + profiles.map(profile => profile.path).join(', ') + '. Preserve it and resolve that registration before installing or checking the shared DSH_HOME configuration; a duplicate MCP was not created.');
182
- }
1
+ import path from 'node:path';
2
+ import { readFileSync, readdirSync, lstatSync, realpathSync } from 'node:fs';
3
+ import { isDeepStrictEqual } from 'node:util';
4
+ import { isAlias, isMap, isSeq, parseDocument } from 'yaml';
5
+
6
+ const pluginName = '@deepseek-ai/dsh-mcp-client';
7
+ const pluginId = 'mcp-craft';
8
+ const isRecord = value => value !== null && typeof value === 'object' && !Array.isArray(value);
9
+
10
+ function documentFor(text) {
11
+ const document = parseDocument(text || '[]\n', { uniqueKeys: true });
12
+ if (document.errors.length) throw new Error('Invalid DSH YAML configuration: ' + document.errors[0].message);
13
+ if (document.contents === null) document.contents = document.createNode([]);
14
+ if (!isSeq(document.contents)) throw new Error('DSH configuration must be a YAML patch array');
15
+ return document;
16
+ }
17
+
18
+ /** Read plain patch data without evaluating Cordis !!js expressions. */
19
+ export function readDshConfig(text) {
20
+ return documentFor(text).toJS();
21
+ }
22
+
23
+ function isCraft(value) {
24
+ const config = value?.config;
25
+ return value?.id === pluginId || (value?.name === pluginName && (
26
+ config?.serverName === 'craft' ||
27
+ (Array.isArray(config?.args) && config.args.some(arg => typeof arg === 'string' && /^@craft_eco\/craft-mcp(?:@[^\s]+)?$/.test(arg)))
28
+ ));
29
+ }
30
+
31
+ function hasDynamicNode(node) {
32
+ if (!node || typeof node !== 'object') return false;
33
+ if (isAlias(node) || node.tag) return true;
34
+ if (isMap(node)) return node.items.some(pair => hasDynamicNode(pair.key) || hasDynamicNode(pair.value));
35
+ if (isSeq(node)) return node.items.some(hasDynamicNode);
36
+ return false;
37
+ }
38
+
39
+ function candidates(document) {
40
+ const rows = [], identities = new Set(), allIds = new Map();
41
+ const values = document.toJS();
42
+ for (const [patchIndex, patch] of document.contents.items.entries()) {
43
+ if (!isMap(patch)) throw new Error('DSH patch entries must be YAML mappings');
44
+ const insert = patch.get('insert', true);
45
+ if (insert !== undefined && !isSeq(insert)) throw new Error('DSH insert patch must contain an array');
46
+ const entries = insert ? insert.items : [patch];
47
+ for (const [entryIndex, node] of entries.entries()) {
48
+ if (!isMap(node)) throw new Error('DSH plugin entries must be YAML mappings');
49
+ const value = insert ? values[patchIndex].insert[entryIndex] : values[patchIndex];
50
+ if (typeof value.id === 'string') allIds.set(value.id, (allIds.get(value.id) ?? 0) + 1);
51
+ if (!isCraft(value)) continue;
52
+ if (!insert || patch.items.some(pair => pair.key?.value !== 'insert')) {
53
+ throw new Error('Ambiguous Craft DSH patch: use one complete insert registration without additional patch operations');
54
+ }
55
+ if (hasDynamicNode(node)) throw new Error('Dynamic or aliased Craft DSH configuration cannot be changed safely');
56
+ if (value.name !== pluginName) throw new Error('DSH plugin id mcp-craft belongs to another plugin');
57
+ if (typeof value.id !== 'string' || !value.id.trim()) throw new Error('Craft DSH registration requires an explicit plugin id');
58
+ if (!isRecord(value.config) || typeof value.config.serverName !== 'string' || !/^[A-Za-z0-9_-]{1,32}$/.test(value.config.serverName)) {
59
+ throw new Error('Invalid Craft DSH server configuration');
60
+ }
61
+ if (value.disabled !== undefined && typeof value.disabled !== 'boolean') throw new Error('Craft DSH disabled must be a boolean');
62
+ if (identities.has(value.id)) throw new Error('Duplicate Craft DSH plugin id');
63
+ identities.add(value.id);
64
+ rows.push({ node, value });
65
+ }
66
+ }
67
+ if (rows.length > 1) throw new Error('Multiple Craft DSH registrations found; keep one registration before installing');
68
+ for (const { value } of rows) {
69
+ if (allIds.get(value.id) > 1) throw new Error('Ambiguous or duplicate Craft DSH plugin id');
70
+ }
71
+ // A later id-only patch could silently override or disable the inserted server.
72
+ for (const patch of document.contents.items) {
73
+ if (patch.has('insert')) continue;
74
+ const id = patch.get('id');
75
+ if (identities.has(id)) throw new Error('Ambiguous Craft DSH id override; keep settings in the insert registration');
76
+ }
77
+ return rows;
78
+ }
79
+
80
+ function connectionFor(value) {
81
+ const { serverName: _serverName, transport, ...connection } = value.config;
82
+ return { ...connection, type: transport, disabled: value.disabled === true };
83
+ }
84
+
85
+ /** Craft registrations only; connection uses the common installer/probe shape. */
86
+ export function dshRegistrations(text) {
87
+ return candidates(documentFor(text)).map(({ value }) => ({ name: value.config.serverName, connection: connectionFor(value) }));
88
+ }
89
+
90
+ function connectionConfig(connection, previous = {}) {
91
+ if (!isRecord(connection) || typeof connection.command !== 'string' || !connection.command.trim() ||
92
+ !Array.isArray(connection.args) || connection.args.some(arg => typeof arg !== 'string') ||
93
+ (connection.env !== undefined && (!isRecord(connection.env) || Object.values(connection.env).some(value => typeof value !== 'string')))) {
94
+ throw new Error('Craft DSH requires a valid stdio command, args and string environment');
95
+ }
96
+ if (previous.env !== undefined && (!isRecord(previous.env) || Object.values(previous.env).some(value => typeof value !== 'string'))) {
97
+ throw new Error('Invalid existing Craft DSH environment');
98
+ }
99
+ const next = { ...previous, ...connection, serverName: 'craft', transport: 'stdio', env: { ...previous.env, ...connection.env } };
100
+ next.startupTimeoutMs ??= connection.timeout ?? (connection.startup_timeout_sec ? connection.startup_timeout_sec * 1000 : 120000);
101
+ next.toolCallTimeoutMs ??= 3600000;
102
+ next.failOnStartupError ??= true;
103
+ for (const key of ['type', 'timeout', 'startup_timeout_sec', 'enabled', 'disabled', 'url', 'headers', 'http_headers', 'env_http_headers', 'bearer_token_env_var']) delete next[key];
104
+ return next;
105
+ }
106
+
107
+ /** Merge a single owned stdio registration while retaining unrelated YAML nodes. */
108
+ export function mergeDshConfig(text, connection, { previousName = 'craft', enable = true } = {}) {
109
+ const document = documentFor(text), found = candidates(document);
110
+ const previous = found.find(row => row.value.config.serverName === previousName);
111
+ if (found.length && !previous) throw new Error('Craft DSH installation conflicts with an existing registration');
112
+ if (!previous && previousName !== 'craft') throw new Error('Previous Craft DSH registration was not found');
113
+ const config = connectionConfig(connection, previous?.value.config);
114
+ if (!previous) {
115
+ document.contents.items.push(document.createNode({ insert: [{ id: pluginId, name: pluginName, config }] }));
116
+ } else {
117
+ const next = { ...previous.value, config };
118
+ if (enable && next.disabled === true) next.disabled = false;
119
+ if (isDeepStrictEqual(previous.value, next)) return text;
120
+ const configNode = previous.node.get('config', true);
121
+ for (const pair of [...configNode.items]) {
122
+ if (!Object.hasOwn(config, pair.key.value)) configNode.delete(pair.key.value);
123
+ }
124
+ for (const [key, value] of Object.entries(config)) {
125
+ if (isDeepStrictEqual(previous.value.config[key], value)) continue;
126
+ if (key === 'env' && isMap(configNode.get('env', true))) {
127
+ const env = configNode.get('env', true);
128
+ for (const [name, setting] of Object.entries(value)) {
129
+ if (previous.value.config.env?.[name] !== setting) env.set(name, setting);
130
+ }
131
+ } else configNode.set(key, document.createNode(value));
132
+ }
133
+ if (enable && previous.value.disabled === true) previous.node.set('disabled', false);
134
+ }
135
+ document.contents.flow = false;
136
+ const output = document.toString({ lineWidth: 0 });
137
+ const written = dshRegistrations(output);
138
+ if (written.length !== 1 || written[0].name !== 'craft' || !isDeepStrictEqual(written[0].connection, connectionFor({ config, disabled: !enable && previous?.value.disabled === true }))) {
139
+ throw new Error('Generated Craft DSH configuration failed validation');
140
+ }
141
+ return output;
142
+ }
143
+
144
+ /** Read-only profile inventory; callers decide how to handle an existing scope. */
145
+ export function inspectDshProfiles(dshHome) {
146
+ const profiles = path.join(dshHome, 'profiles');
147
+ let root;
148
+ try {
149
+ const stat = lstatSync(dshHome);
150
+ if (stat.isSymbolicLink() || !stat.isDirectory()) throw new Error('DSH home must be a plain directory');
151
+ root = realpathSync(dshHome);
152
+ } catch (error) { if (error.code === 'ENOENT') return []; throw error; }
153
+ const checkedPath = (file, type) => {
154
+ const stat = lstatSync(file);
155
+ if (stat.isSymbolicLink() || (type === 'directory' ? !stat.isDirectory() : !stat.isFile())) throw new Error('DSH profile paths must not contain links or special files: ' + file);
156
+ const relative = path.relative(root, realpathSync(file));
157
+ if (relative === '..' || relative.startsWith('..' + path.sep) || path.isAbsolute(relative)) throw new Error('DSH profile path escapes its home: ' + file);
158
+ };
159
+ let entries;
160
+ try { checkedPath(profiles, 'directory'); entries = readdirSync(profiles, { withFileTypes: true }); }
161
+ catch (error) { if (error.code === 'ENOENT') return []; throw error; }
162
+ const found = [];
163
+ for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
164
+ if (entry.isSymbolicLink()) throw new Error('DSH profile paths must not contain links: ' + path.join(profiles, entry.name));
165
+ if (!entry.isDirectory()) continue;
166
+ checkedPath(path.join(profiles, entry.name), 'directory');
167
+ const configPath = path.join(profiles, entry.name, 'cordis.patch.yml');
168
+ let text;
169
+ try { checkedPath(configPath, 'file'); text = readFileSync(configPath, 'utf8'); }
170
+ catch (error) { if (error.code === 'ENOENT') continue; throw error; }
171
+ const registrations = dshRegistrations(text);
172
+ if (registrations.length) found.push({ path: configPath, registrations });
173
+ }
174
+ return found;
175
+ }
176
+
177
+ /** Shared install/doctor/update guard; never move profile-owned registrations. */
178
+ export function assertDshSharedScope(paths) {
179
+ if (!paths.dshHome) return;
180
+ const profiles = inspectDshProfiles(paths.dshHome);
181
+ if (profiles.length) throw new Error('DSH already has a profile-scoped Craft registration: ' + profiles.map(profile => profile.path).join(', ') + '. Preserve it and resolve that registration before installing or checking the shared DSH_HOME configuration; a duplicate MCP was not created.');
182
+ }
@@ -1,54 +1,54 @@
1
- import path from 'node:path';
2
- import { lstat, readFile, mkdir, writeFile, rename, unlink } from 'node:fs/promises';
3
- import { randomUUID } from 'node:crypto';
4
-
5
- export async function optionalFile(file) {
6
- try { return await readFile(file, 'utf8'); }
7
- catch (error) { if (error.code !== 'ENOENT') throw error; return null; }
8
- }
9
-
10
- export async function assertPlainPath(file) {
11
- let current = path.resolve(file);
12
- for (;;) {
13
- try {
14
- const info = await lstat(current);
15
- if (info.isSymbolicLink() || (current === path.resolve(file) ? !info.isFile() : !info.isDirectory())) throw new Error('Refusing linked or invalid install path: ' + current);
16
- } catch (error) { if (error.code !== 'ENOENT') throw error; }
17
- const parent = path.dirname(current); if (parent === current) break; current = parent;
18
- }
19
- }
20
-
21
- async function replace(file, text) {
22
- await mkdir(path.dirname(file), { recursive: true });
23
- const temporary = file + '.' + randomUUID() + '.tmp';
24
- try { await writeFile(temporary, text, { mode: 0o600, flag: 'wx' }); await rename(temporary, file); }
25
- finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
26
- }
27
-
28
- // Preflight all destinations, keep private backups, and roll back only our unchanged writes.
29
- export async function commitFiles(changes) {
30
- const pending = changes.filter(change => change.before !== change.after), committed = [], backups = [];
31
- for (const change of pending) {
32
- await assertPlainPath(change.file);
33
- if (await optionalFile(change.file) !== change.before) throw new Error('File changed during installation: ' + change.file);
34
- }
35
- try {
36
- for (const change of pending) {
37
- await assertPlainPath(change.file);
38
- if (await optionalFile(change.file) !== change.before) throw new Error('File changed during installation: ' + change.file);
39
- if (change.before !== null) {
40
- const backup = change.file + '.craft-backup-' + randomUUID();
41
- await writeFile(backup, change.before, { flag: 'wx', mode: 0o600 }); backups.push(backup);
42
- }
43
- await replace(change.file, change.after); committed.push(change);
44
- }
45
- } catch (error) {
46
- for (const change of committed.reverse()) {
47
- if (await optionalFile(change.file) === change.after) {
48
- if (change.before === null) await unlink(change.file); else await replace(change.file, change.before);
49
- }
50
- }
51
- throw error;
52
- }
53
- return { changed: pending.map(change => change.file), backups };
54
- }
1
+ import path from 'node:path';
2
+ import { lstat, readFile, mkdir, writeFile, rename, unlink } from 'node:fs/promises';
3
+ import { randomUUID } from 'node:crypto';
4
+
5
+ export async function optionalFile(file) {
6
+ try { return await readFile(file, 'utf8'); }
7
+ catch (error) { if (error.code !== 'ENOENT') throw error; return null; }
8
+ }
9
+
10
+ export async function assertPlainPath(file) {
11
+ let current = path.resolve(file);
12
+ for (;;) {
13
+ try {
14
+ const info = await lstat(current);
15
+ if (info.isSymbolicLink() || (current === path.resolve(file) ? !info.isFile() : !info.isDirectory())) throw new Error('Refusing linked or invalid install path: ' + current);
16
+ } catch (error) { if (error.code !== 'ENOENT') throw error; }
17
+ const parent = path.dirname(current); if (parent === current) break; current = parent;
18
+ }
19
+ }
20
+
21
+ async function replace(file, text) {
22
+ await mkdir(path.dirname(file), { recursive: true });
23
+ const temporary = file + '.' + randomUUID() + '.tmp';
24
+ try { await writeFile(temporary, text, { mode: 0o600, flag: 'wx' }); await rename(temporary, file); }
25
+ finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
26
+ }
27
+
28
+ // Preflight all destinations, keep private backups, and roll back only our unchanged writes.
29
+ export async function commitFiles(changes) {
30
+ const pending = changes.filter(change => change.before !== change.after), committed = [], backups = [];
31
+ for (const change of pending) {
32
+ await assertPlainPath(change.file);
33
+ if (await optionalFile(change.file) !== change.before) throw new Error('File changed during installation: ' + change.file);
34
+ }
35
+ try {
36
+ for (const change of pending) {
37
+ await assertPlainPath(change.file);
38
+ if (await optionalFile(change.file) !== change.before) throw new Error('File changed during installation: ' + change.file);
39
+ if (change.before !== null) {
40
+ const backup = change.file + '.craft-backup-' + randomUUID();
41
+ await writeFile(backup, change.before, { flag: 'wx', mode: 0o600 }); backups.push(backup);
42
+ }
43
+ await replace(change.file, change.after); committed.push(change);
44
+ }
45
+ } catch (error) {
46
+ for (const change of committed.reverse()) {
47
+ if (await optionalFile(change.file) === change.after) {
48
+ if (change.before === null) await unlink(change.file); else await replace(change.file, change.before);
49
+ }
50
+ }
51
+ throw error;
52
+ }
53
+ return { changed: pending.map(change => change.file), backups };
54
+ }
@@ -0,0 +1,31 @@
1
+ import path from 'node:path';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { createHash } from 'node:crypto';
4
+ import { optionalFile, assertPlainPath } from './files.mjs';
5
+
6
+ export function launcherPath(installationFile) {
7
+ return installationFile.replace(/\.json$/, '') + '.launcher.mjs';
8
+ }
9
+
10
+ export async function planLauncher(installationFile, previous, packageRoot) {
11
+ const entry = launcherPath(installationFile);
12
+ await assertPlainPath(entry);
13
+ const before = await optionalFile(entry);
14
+ if (before !== null && (!previous || previous.entry !== entry || createHash('sha256').update(before).digest('hex') !== previous.sha256)) {
15
+ throw new Error('Craft launcher ownership or integrity mismatch; preserve the file and repair the installation');
16
+ }
17
+ const source = packageRoot ? path.join(packageRoot, 'dist-server/launcher.mjs') : new URL('../launcher.mjs', import.meta.url);
18
+ const after = await readFile(source, 'utf8');
19
+ return { launcher: { schema: 1, entry, sha256: createHash('sha256').update(after).digest('hex') }, change: { file: entry, before, after } };
20
+ }
21
+
22
+ export async function verifyLauncher(state, installationFile, connection) {
23
+ if (!state.launcher) return { mode: 'legacy_versioned_entry' };
24
+ const expected = launcherPath(installationFile);
25
+ if (state.launcher.schema !== 1 || state.launcher.entry !== expected || !/^[a-f0-9]{64}$/.test(state.launcher.sha256 ?? '')) throw new Error('Invalid Craft fixed launcher identity');
26
+ if (state.launcher.kind !== undefined) throw new Error('Unsupported Craft launcher; run the standard installer and reconnect');
27
+ await assertPlainPath(expected);
28
+ if (createHash('sha256').update(await readFile(expected)).digest('hex') !== state.launcher.sha256) throw new Error('Craft fixed launcher integrity mismatch');
29
+ if (connection && path.resolve(connection.args?.[0] ?? '') !== path.resolve(expected)) throw new Error('Craft host configuration does not use the installed fixed launcher');
30
+ return { mode: 'fixed_entry', entry: expected };
31
+ }
@@ -0,0 +1,19 @@
1
+ import { hosts } from './hosts.mjs';
2
+
3
+ // Manual reconnection guidance only. Updates never invoke host controls.
4
+ const profiles = Object.freeze({
5
+ codex: { surface: 'app_server', fallback: '在 Codex 的 MCP 管理入口重连 Craft;仍旧或无入口时保存进度并完整退出重开 Codex。' },
6
+ workbuddy: { surface: 'desktop', fallback: '在 WorkBuddy 的“专家·技能·连接器 → 连接器 → 自定义连接器”中重连 Craft;仍旧时保存进度,从 Windows 托盘完整退出再重新打开,仅关闭窗口可能保留后台。' },
7
+ codebuddy: { surface: 'cli_or_ide', fallback: '使用当前 CodeBuddy 客户端的 MCP 管理入口重连 Craft;没有入口或仍旧时保存进度并重启当前客户端。' },
8
+ cursor: { surface: 'ide', fallback: '在 Cursor 的 MCP 管理界面重连 Craft;仍旧时保存进度并重新加载或重启 Cursor。' },
9
+ claude: { surface: 'claude_code', fallback: '在 Claude Code 的 /mcp 管理界面检查 Craft 并使用可用的重连操作;无法重连或仍旧时保存进度并重新启动 Claude Code,恢复当前项目。' },
10
+ dsh: { surface: 'harness', fallback: '下次启动当前 DSH 实例时加载新版,保留原 profile;需要立即使用时保存进度再重启。' },
11
+ zcode: { surface: 'cli_or_ide', fallback: '使用当前 ZCode 客户端的 MCP 管理入口重连 Craft;没有入口或仍旧时保存进度并重启当前客户端。' },
12
+ kimi: { surface: 'cli', fallback: '使用当前 Kimi Code 会话提供的 MCP 重连能力;不可用或仍旧时保存进度并重新启动 Kimi Code CLI,恢复当前项目。' },
13
+ });
14
+
15
+ export function hostReloadProfile(host) {
16
+ const profile = profiles[host];
17
+ return { host, label: hosts[host]?.label ?? '宿主', ...(profile ?? { surface: 'unknown', fallback: '使用宿主的 MCP 管理入口重连 Craft;仍旧时保存进度并重启宿主。' }),
18
+ activation: 'next_mcp_start_or_user_reconnect' };
19
+ }