@elliotding/ai-agent-mcp 0.1.5 → 0.1.7
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/ai-resource-telemetry.json +13 -1
- package/dist/git/multi-source-manager.d.ts +16 -0
- package/dist/git/multi-source-manager.d.ts.map +1 -1
- package/dist/git/multi-source-manager.js +52 -0
- package/dist/git/multi-source-manager.js.map +1 -1
- package/dist/tools/sync-resources.d.ts.map +1 -1
- package/dist/tools/sync-resources.js +170 -338
- package/dist/tools/sync-resources.js.map +1 -1
- package/dist/tools/uninstall-resource.d.ts.map +1 -1
- package/dist/tools/uninstall-resource.js +66 -150
- package/dist/tools/uninstall-resource.js.map +1 -1
- package/dist/types/tools.d.ts +53 -0
- package/dist/types/tools.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/git/multi-source-manager.ts +68 -0
- package/src/tools/sync-resources.ts +190 -386
- package/src/tools/uninstall-resource.ts +70 -162
- package/src/types/tools.ts +74 -0
|
@@ -80,224 +80,6 @@ function extractFrontmatterDescription(content) {
|
|
|
80
80
|
}
|
|
81
81
|
return undefined;
|
|
82
82
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Register a downloaded MCP resource into ~/.cursor/mcp.json.
|
|
85
|
-
*
|
|
86
|
-
* Supports:
|
|
87
|
-
* - Format A (local executable): resolves relative args to absolute paths, writes one entry.
|
|
88
|
-
* - Format B (remote URL map): merges all entries directly into mcpServers.
|
|
89
|
-
* - No mcp-config.json: heuristic fallback (scans for .py/.js entry point, logs WARN).
|
|
90
|
-
*
|
|
91
|
-
* The write is idempotent — re-running after a re-download updates existing entries.
|
|
92
|
-
*
|
|
93
|
-
* Returns a McpSetupItem when the registered server needs manual configuration
|
|
94
|
-
* (empty env vars, or a command that might differ across platforms), or null
|
|
95
|
-
* when no action is required from the user.
|
|
96
|
-
*/
|
|
97
|
-
async function registerMcpServer(serverName, installDir) {
|
|
98
|
-
// ── 1. Load mcp-config.json ────────────────────────────────────────────
|
|
99
|
-
const configFilePath = path.join(installDir, 'mcp-config.json');
|
|
100
|
-
let rawConfig = null;
|
|
101
|
-
try {
|
|
102
|
-
const raw = await fs.readFile(configFilePath, 'utf-8');
|
|
103
|
-
rawConfig = JSON.parse(raw);
|
|
104
|
-
logger_1.logger.debug({ serverName, configFilePath }, 'registerMcpServer: loaded mcp-config.json');
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
logger_1.logger.warn({ serverName, configFilePath }, 'registerMcpServer: mcp-config.json not found — falling back to heuristic detection. ' +
|
|
108
|
-
'Add an mcp-config.json to this resource for reliable registration.');
|
|
109
|
-
}
|
|
110
|
-
// ── 2. Determine what to merge into mcp.json ──────────────────────────
|
|
111
|
-
// entriesToMerge: map of serverKey → entry object (may have multiple keys for Format B)
|
|
112
|
-
let entriesToMerge = {};
|
|
113
|
-
if (rawConfig !== null && typeof rawConfig === 'object') {
|
|
114
|
-
const cfg = rawConfig;
|
|
115
|
-
if (typeof cfg['command'] === 'string') {
|
|
116
|
-
// ── Format A: local executable ───────────────────────────────────
|
|
117
|
-
const descriptor = cfg;
|
|
118
|
-
const key = descriptor.name ?? serverName;
|
|
119
|
-
// Only resolve args that look like relative file paths (contain a dot or
|
|
120
|
-
// path separator). Plain words like "mcp", "start", "--port" are kept as-is.
|
|
121
|
-
const looksLikePath = (a) => a.startsWith('./') || a.startsWith('../') || a.includes(path.sep) || /\.\w+$/.test(a);
|
|
122
|
-
const resolvedArgs = (descriptor.args ?? []).map(a => path.isAbsolute(a) || !looksLikePath(a) ? a : path.join(installDir, a));
|
|
123
|
-
entriesToMerge[key] = {
|
|
124
|
-
command: descriptor.command,
|
|
125
|
-
args: resolvedArgs,
|
|
126
|
-
...(descriptor.env && Object.keys(descriptor.env).length > 0
|
|
127
|
-
? { env: descriptor.env }
|
|
128
|
-
: {}),
|
|
129
|
-
};
|
|
130
|
-
logger_1.logger.info({ serverName, key, command: descriptor.command }, 'registerMcpServer: Format A (local executable)');
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
// ── Format B: remote URL entries map ─────────────────────────────
|
|
134
|
-
// The entire object is a ready-to-merge mcpServers map.
|
|
135
|
-
entriesToMerge = cfg;
|
|
136
|
-
logger_1.logger.info({ serverName, keys: Object.keys(entriesToMerge) }, 'registerMcpServer: Format B (remote URL entries)');
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
// ── Heuristic fallback (no mcp-config.json) ───────────────────────
|
|
141
|
-
let entryFile = null;
|
|
142
|
-
let command = 'python3';
|
|
143
|
-
try {
|
|
144
|
-
const entries = await fs.readdir(installDir);
|
|
145
|
-
if (entries.includes(`${serverName}.py`)) {
|
|
146
|
-
entryFile = path.join(installDir, `${serverName}.py`);
|
|
147
|
-
command = 'python3';
|
|
148
|
-
}
|
|
149
|
-
else if (entries.includes(`${serverName}.js`)) {
|
|
150
|
-
entryFile = path.join(installDir, `${serverName}.js`);
|
|
151
|
-
command = 'node';
|
|
152
|
-
}
|
|
153
|
-
if (!entryFile) {
|
|
154
|
-
const py = entries.find(f => f.endsWith('.py') && f !== '__init__.py');
|
|
155
|
-
if (py) {
|
|
156
|
-
entryFile = path.join(installDir, py);
|
|
157
|
-
command = 'python3';
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (!entryFile) {
|
|
161
|
-
const js = entries.find(f => f.endsWith('.js') || f.endsWith('.mjs'));
|
|
162
|
-
if (js) {
|
|
163
|
-
entryFile = path.join(installDir, js);
|
|
164
|
-
command = 'node';
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
catch (err) {
|
|
169
|
-
logger_1.logger.warn({ serverName, installDir, err }, 'registerMcpServer: could not read install directory');
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
if (!entryFile) {
|
|
173
|
-
logger_1.logger.warn({ serverName, installDir }, 'registerMcpServer: no entry point found and no mcp-config.json — skipping registration');
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
entriesToMerge[serverName] = { command, args: [entryFile] };
|
|
177
|
-
}
|
|
178
|
-
// ── 3. Read / create ~/.cursor/mcp.json ───────────────────────────────
|
|
179
|
-
const mcpJsonPath = path.join((0, cursor_paths_1.getCursorRootDir)(), 'mcp.json');
|
|
180
|
-
let mcpConfig = { mcpServers: {} };
|
|
181
|
-
try {
|
|
182
|
-
const raw = await fs.readFile(mcpJsonPath, 'utf-8');
|
|
183
|
-
const parsed = JSON.parse(raw);
|
|
184
|
-
if (parsed && typeof parsed === 'object' && 'mcpServers' in parsed) {
|
|
185
|
-
mcpConfig = parsed;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
catch {
|
|
189
|
-
// File doesn't exist or is corrupt — start fresh
|
|
190
|
-
}
|
|
191
|
-
// Smart-merge each entry into mcpServers:
|
|
192
|
-
// - Structural fields (command, args, url, transport, …): always take the
|
|
193
|
-
// value from mcp-config.json (server is authoritative for structure).
|
|
194
|
-
// - env field: preserve user-filled non-empty values; only add keys that
|
|
195
|
-
// are new or were previously empty (avoids wiping tokens / URLs the user
|
|
196
|
-
// has already configured).
|
|
197
|
-
for (const [key, incoming] of Object.entries(entriesToMerge)) {
|
|
198
|
-
const existing = mcpConfig.mcpServers[key];
|
|
199
|
-
if (!existing || typeof existing !== 'object') {
|
|
200
|
-
// No prior entry — write as-is.
|
|
201
|
-
mcpConfig.mcpServers[key] = incoming;
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
const incomingEntry = incoming;
|
|
205
|
-
const existingEntry = existing;
|
|
206
|
-
// Merge env: keep user values that are non-empty strings; fill in the rest
|
|
207
|
-
// from the incoming template (which uses empty strings as placeholders).
|
|
208
|
-
const mergedEnv = {};
|
|
209
|
-
const incomingEnv = (incomingEntry['env'] ?? {});
|
|
210
|
-
const existingEnv = (existingEntry['env'] ?? {});
|
|
211
|
-
for (const envKey of Object.keys(incomingEnv)) {
|
|
212
|
-
const userVal = existingEnv[envKey];
|
|
213
|
-
// Preserve whatever the user typed; fall back to the template placeholder.
|
|
214
|
-
mergedEnv[envKey] = (typeof userVal === 'string' && userVal !== '')
|
|
215
|
-
? userVal
|
|
216
|
-
: (incomingEnv[envKey] ?? '');
|
|
217
|
-
}
|
|
218
|
-
// Structural fields from server override local, env is smart-merged.
|
|
219
|
-
mcpConfig.mcpServers[key] = {
|
|
220
|
-
...incomingEntry,
|
|
221
|
-
...(Object.keys(mergedEnv).length > 0 ? { env: mergedEnv } : {}),
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
// ── 4. Atomic write ────────────────────────────────────────────────────
|
|
225
|
-
const tmpPath = `${mcpJsonPath}.tmp-${process.pid}`;
|
|
226
|
-
try {
|
|
227
|
-
await fs.writeFile(tmpPath, JSON.stringify(mcpConfig, null, 2), 'utf-8');
|
|
228
|
-
await fs.rename(tmpPath, mcpJsonPath);
|
|
229
|
-
logger_1.logger.info({ serverName, mergedKeys: Object.keys(entriesToMerge), mcpJsonPath }, 'MCP server(s) registered in mcp.json');
|
|
230
|
-
}
|
|
231
|
-
catch (err) {
|
|
232
|
-
await fs.unlink(tmpPath).catch(() => undefined);
|
|
233
|
-
logger_1.logger.error({ serverName, err }, 'registerMcpServer: failed to write mcp.json');
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
// ── 5. Detect setup requirements ──────────────────────────────────────
|
|
237
|
-
// Collect env keys that are still empty (user must fill in) and flag
|
|
238
|
-
// commands that may differ across platforms (python vs python3, etc.).
|
|
239
|
-
const AMBIGUOUS_COMMANDS = new Set(['python', 'python3', 'node', 'npx', 'uvx']);
|
|
240
|
-
const missingEnvKeys = [];
|
|
241
|
-
let commandNeedsVerification = false;
|
|
242
|
-
let registeredCommand = '';
|
|
243
|
-
for (const entry of Object.values(entriesToMerge)) {
|
|
244
|
-
const e = entry;
|
|
245
|
-
const env = (e['env'] ?? {});
|
|
246
|
-
for (const [k, v] of Object.entries(env)) {
|
|
247
|
-
if (v === '')
|
|
248
|
-
missingEnvKeys.push(k);
|
|
249
|
-
}
|
|
250
|
-
if (typeof e['command'] === 'string') {
|
|
251
|
-
registeredCommand = e['command'];
|
|
252
|
-
if (AMBIGUOUS_COMMANDS.has(registeredCommand)) {
|
|
253
|
-
commandNeedsVerification = true;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
if (missingEnvKeys.length === 0 && !commandNeedsVerification) {
|
|
258
|
-
return null; // No user action needed
|
|
259
|
-
}
|
|
260
|
-
// Locate the best available setup/readme doc in the install directory so the
|
|
261
|
-
// user can be pointed to it. Priority: SETUP.md > README.md > README*.md > *.md
|
|
262
|
-
let setupDocPath = null;
|
|
263
|
-
try {
|
|
264
|
-
const entries = await fs.readdir(installDir);
|
|
265
|
-
const mdFiles = entries.filter(f => /\.md$/i.test(f));
|
|
266
|
-
const pick = (name) => mdFiles.find(f => f.toLowerCase() === name.toLowerCase());
|
|
267
|
-
const found = pick('SETUP.md') ??
|
|
268
|
-
pick('README.md') ??
|
|
269
|
-
mdFiles.find(f => f.toLowerCase().startsWith('readme')) ??
|
|
270
|
-
mdFiles[0];
|
|
271
|
-
if (found) {
|
|
272
|
-
setupDocPath = path.join(installDir, found);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
catch {
|
|
276
|
-
// installDir might not exist yet for remote-URL MCPs — ignore
|
|
277
|
-
}
|
|
278
|
-
const hints = [];
|
|
279
|
-
if (commandNeedsVerification) {
|
|
280
|
-
hints.push(`The command "${registeredCommand}" may differ on your machine ` +
|
|
281
|
-
`(e.g. "python" vs "python3"). ` +
|
|
282
|
-
`Please verify the command in ${mcpJsonPath} under mcpServers["${serverName}"].`);
|
|
283
|
-
}
|
|
284
|
-
if (missingEnvKeys.length > 0) {
|
|
285
|
-
hints.push(`Fill in the following environment variables in ${mcpJsonPath} ` +
|
|
286
|
-
`under mcpServers["${serverName}"].env: ${missingEnvKeys.join(', ')}.`);
|
|
287
|
-
}
|
|
288
|
-
if (setupDocPath) {
|
|
289
|
-
hints.push(`Refer to the setup guide for details: ${setupDocPath}`);
|
|
290
|
-
}
|
|
291
|
-
return {
|
|
292
|
-
server_name: serverName,
|
|
293
|
-
mcp_json_path: mcpJsonPath,
|
|
294
|
-
missing_env: missingEnvKeys,
|
|
295
|
-
command_needs_verification: commandNeedsVerification,
|
|
296
|
-
command: registeredCommand,
|
|
297
|
-
setup_hint: hints.join(' '),
|
|
298
|
-
...(setupDocPath ? { setup_doc: setupDocPath } : {}),
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
83
|
async function syncResources(params) {
|
|
302
84
|
const startTime = Date.now();
|
|
303
85
|
const typedParams = params;
|
|
@@ -343,13 +125,18 @@ async function syncResources(params) {
|
|
|
343
125
|
},
|
|
344
126
|
});
|
|
345
127
|
}
|
|
346
|
-
// ── Step 3: Download each subscribed resource
|
|
347
|
-
|
|
128
|
+
// ── Step 3: Download each subscribed resource ──────────────────────────
|
|
129
|
+
// Command / Skill → registered as MCP Prompts on the server (no local I/O)
|
|
130
|
+
// Rule / MCP → file content is returned as LocalAction instructions
|
|
131
|
+
// so that the AI Agent executes the writes on the user's
|
|
132
|
+
// LOCAL machine (not on this potentially remote server).
|
|
133
|
+
(0, logger_1.logToolStep)('sync_resources', 'Step 3: Processing subscribed resources', {
|
|
348
134
|
count: subscriptions.total,
|
|
349
135
|
});
|
|
350
136
|
const tally = { total: subscriptions.total, synced: 0, cached: 0, failed: 0 };
|
|
351
137
|
const details = [];
|
|
352
|
-
|
|
138
|
+
// Accumulated local file-system actions the AI must perform on the user's machine.
|
|
139
|
+
const localActions = [];
|
|
353
140
|
for (let i = 0; i < subscriptions.subscriptions.length; i++) {
|
|
354
141
|
const sub = subscriptions.subscriptions[i];
|
|
355
142
|
if (!sub)
|
|
@@ -399,18 +186,34 @@ async function syncResources(params) {
|
|
|
399
186
|
fileCount: downloadResult.files.length,
|
|
400
187
|
duration: Date.now() - tDl,
|
|
401
188
|
});
|
|
189
|
+
// When the API returns no files (expected for Command/Skill in MCP Prompt
|
|
190
|
+
// mode — content lives in the server-side git repo, not the API), fall back
|
|
191
|
+
// to reading the files directly from the local git checkout.
|
|
192
|
+
let sourceFiles = downloadResult.files;
|
|
193
|
+
if (sourceFiles.length === 0) {
|
|
194
|
+
sourceFiles = await multi_source_manager_1.multiSourceGitManager.readResourceFiles(sub.name, sub.type);
|
|
195
|
+
if (sourceFiles.length > 0) {
|
|
196
|
+
(0, logger_1.logToolStep)('sync_resources', 'Loaded resource files from local git checkout', {
|
|
197
|
+
resourceId: sub.id,
|
|
198
|
+
fileCount: sourceFiles.length,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
logger_1.logger.warn({ resourceId: sub.id, resourceName: sub.name }, 'No files found via API or local git — prompt will have empty content');
|
|
203
|
+
}
|
|
204
|
+
}
|
|
402
205
|
// Primary Markdown content selection:
|
|
403
206
|
// - skill: prefer SKILL.md (canonical entrypoint for all skill content)
|
|
404
207
|
// - command: prefer the file whose name matches the resource name
|
|
405
208
|
// - fallback: first .md file, then first file of any type
|
|
406
209
|
const isSkill = sub.type === 'skill';
|
|
407
210
|
const primaryFile = isSkill
|
|
408
|
-
? (
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
: (
|
|
412
|
-
|
|
413
|
-
|
|
211
|
+
? (sourceFiles.find((f) => path.basename(f.path) === 'SKILL.md') ??
|
|
212
|
+
sourceFiles.find((f) => f.path.endsWith('.md')) ??
|
|
213
|
+
sourceFiles[0])
|
|
214
|
+
: (sourceFiles.find((f) => path.basename(f.path).replace(/\.md$/, '') === sub.name) ??
|
|
215
|
+
sourceFiles.find((f) => f.path.endsWith('.md')) ??
|
|
216
|
+
sourceFiles[0]);
|
|
414
217
|
const rawContent = primaryFile?.content ?? '';
|
|
415
218
|
// Extract description from frontmatter (---\ndescription: ...\n---)
|
|
416
219
|
// falling back to the subscription's description field or resource name.
|
|
@@ -485,134 +288,155 @@ async function syncResources(params) {
|
|
|
485
288
|
catch { /* malformed JSON — treat as normal MCP */ }
|
|
486
289
|
}
|
|
487
290
|
if (isRemoteUrlMcp) {
|
|
488
|
-
// Remote-URL MCP: no local files
|
|
489
|
-
//
|
|
291
|
+
// Remote-URL MCP (Format B): no local files needed.
|
|
292
|
+
// Return a merge_mcp_json action so the AI updates ~/.cursor/mcp.json
|
|
293
|
+
// on the user's LOCAL machine, not on this (possibly remote) server.
|
|
490
294
|
const configContent = firstFile.content;
|
|
491
295
|
const mcpJsonPath = path.join((0, cursor_paths_1.getCursorRootDir)(), 'mcp.json');
|
|
492
|
-
let mcpConfig = { mcpServers: {} };
|
|
493
|
-
try {
|
|
494
|
-
const raw = await fs.readFile(mcpJsonPath, 'utf-8');
|
|
495
|
-
const p = JSON.parse(raw);
|
|
496
|
-
if (p && typeof p === 'object' && 'mcpServers' in p) {
|
|
497
|
-
mcpConfig = p;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
catch { /* file missing or corrupt — start fresh */ }
|
|
501
296
|
const entries = JSON.parse(configContent);
|
|
502
|
-
|
|
503
|
-
for (const [key, incoming] of Object.entries(entries)) {
|
|
504
|
-
const existing = mcpConfig.mcpServers[key];
|
|
505
|
-
if (!existing || typeof existing !== 'object') {
|
|
506
|
-
mcpConfig.mcpServers[key] = incoming;
|
|
507
|
-
}
|
|
508
|
-
else {
|
|
509
|
-
const inc = incoming;
|
|
510
|
-
const ext = existing;
|
|
511
|
-
const inEnv = (inc['env'] ?? {});
|
|
512
|
-
const exEnv = (ext['env'] ?? {});
|
|
513
|
-
const mergedEnv = {};
|
|
514
|
-
for (const k of Object.keys(inEnv)) {
|
|
515
|
-
const userVal = exEnv[k];
|
|
516
|
-
mergedEnv[k] = (typeof userVal === 'string' && userVal !== '') ? userVal : (inEnv[k] ?? '');
|
|
517
|
-
}
|
|
518
|
-
mcpConfig.mcpServers[key] = {
|
|
519
|
-
...inc,
|
|
520
|
-
...(Object.keys(mergedEnv).length > 0 ? { env: mergedEnv } : {}),
|
|
521
|
-
};
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
const tmpPath = `${mcpJsonPath}.tmp-${process.pid}`;
|
|
525
|
-
await fs.writeFile(tmpPath, JSON.stringify(mcpConfig, null, 2), 'utf-8');
|
|
526
|
-
await fs.rename(tmpPath, mcpJsonPath);
|
|
527
|
-
// Detect missing env vars in remote-URL entries (no local command to check).
|
|
528
|
-
const remoteMissingEnv = [];
|
|
529
|
-
for (const entry of Object.values(entries)) {
|
|
297
|
+
for (const [serverName, entry] of Object.entries(entries)) {
|
|
530
298
|
const e = entry;
|
|
531
299
|
const env = (e['env'] ?? {});
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
if (remoteMissingEnv.length > 0) {
|
|
538
|
-
pendingSetup.push({
|
|
539
|
-
server_name: sub.name,
|
|
300
|
+
const missingEnv = Object.entries(env)
|
|
301
|
+
.filter(([, v]) => v === '')
|
|
302
|
+
.map(([k]) => k);
|
|
303
|
+
const action = {
|
|
304
|
+
action: 'merge_mcp_json',
|
|
540
305
|
mcp_json_path: mcpJsonPath,
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
`
|
|
546
|
-
|
|
547
|
-
|
|
306
|
+
server_name: serverName,
|
|
307
|
+
entry: e,
|
|
308
|
+
...(missingEnv.length > 0 ? {
|
|
309
|
+
missing_env: missingEnv,
|
|
310
|
+
setup_hint: `Fill in the following environment variables in ${mcpJsonPath} ` +
|
|
311
|
+
`under mcpServers["${serverName}"]: ${missingEnv.join(', ')}.`,
|
|
312
|
+
} : {}),
|
|
313
|
+
};
|
|
314
|
+
localActions.push(action);
|
|
548
315
|
}
|
|
549
316
|
tally.synced++;
|
|
550
317
|
details.push({ id: sub.id, name: sub.name, action: 'synced', version: resourceVersion });
|
|
551
|
-
(0, logger_1.logToolStep)('sync_resources', 'Remote-URL MCP
|
|
318
|
+
(0, logger_1.logToolStep)('sync_resources', 'Remote-URL MCP: merge_mcp_json action queued for AI', {
|
|
552
319
|
resourceId: sub.id,
|
|
553
|
-
|
|
320
|
+
serverKeys: Object.keys(entries),
|
|
554
321
|
});
|
|
555
322
|
continue;
|
|
556
323
|
}
|
|
557
|
-
//
|
|
558
|
-
//
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
catch { /* not present — fall through to write */ }
|
|
567
|
-
if (alreadyPresent) {
|
|
568
|
-
if (sub.type === 'mcp') {
|
|
569
|
-
const setupItem = await registerMcpServer(sub.name, destPath);
|
|
570
|
-
if (setupItem)
|
|
571
|
-
pendingSetup.push(setupItem);
|
|
324
|
+
// ── Rule resource ─────────────────────────────────────────────────────
|
|
325
|
+
// Return write_file actions; the AI writes the files locally.
|
|
326
|
+
if (sub.type === 'rule') {
|
|
327
|
+
const typeDir = (0, cursor_paths_1.getCursorTypeDir)(sub.type);
|
|
328
|
+
for (const file of downloadResult.files) {
|
|
329
|
+
const normalised = path.normalize(file.path);
|
|
330
|
+
if (normalised.startsWith('..')) {
|
|
331
|
+
logger_1.logger.warn({ resourceId: sub.id, filePath: file.path }, 'Skipping suspicious file path');
|
|
332
|
+
continue;
|
|
572
333
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
334
|
+
localActions.push({
|
|
335
|
+
action: 'write_file',
|
|
336
|
+
path: path.join(typeDir, normalised),
|
|
337
|
+
content: file.content,
|
|
577
338
|
});
|
|
578
|
-
continue;
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
// Ensure the Cursor type directory exists (e.g. ~/.cursor/skills/).
|
|
582
|
-
const typeDir = (0, cursor_paths_1.getCursorTypeDir)(sub.type);
|
|
583
|
-
await fs.mkdir(typeDir, { recursive: true });
|
|
584
|
-
// Determine write strategy based on resource type:
|
|
585
|
-
// Directory-based (skill, mcp): create <typeDir>/<name>/ and write files under it.
|
|
586
|
-
// File-based (command, rule): write each file directly into <typeDir>/ — no subdir.
|
|
587
|
-
const isDirectoryType = sub.type === 'skill' || sub.type === 'mcp';
|
|
588
|
-
const writeRoot = isDirectoryType ? destPath : typeDir;
|
|
589
|
-
if (isDirectoryType) {
|
|
590
|
-
await fs.mkdir(writeRoot, { recursive: true });
|
|
591
|
-
}
|
|
592
|
-
for (const file of downloadResult.files) {
|
|
593
|
-
// Reject path traversal attempts in file.path
|
|
594
|
-
const normalised = path.normalize(file.path);
|
|
595
|
-
if (normalised.startsWith('..')) {
|
|
596
|
-
logger_1.logger.warn({ resourceId: sub.id, filePath: file.path }, 'Skipping suspicious file path');
|
|
597
|
-
continue;
|
|
598
339
|
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
340
|
+
tally.synced++;
|
|
341
|
+
details.push({ id: sub.id, name: sub.name, action: 'synced', version: resourceVersion });
|
|
342
|
+
(0, logger_1.logToolStep)('sync_resources', 'Rule: write_file actions queued for AI', {
|
|
343
|
+
resourceId: sub.id,
|
|
344
|
+
fileCount: downloadResult.files.length,
|
|
345
|
+
});
|
|
346
|
+
continue;
|
|
602
347
|
}
|
|
603
|
-
//
|
|
348
|
+
// ── Local-executable MCP resource (Format A — has "command" field) ───
|
|
349
|
+
// Return write_file + merge_mcp_json actions; the AI performs them locally.
|
|
604
350
|
if (sub.type === 'mcp') {
|
|
605
|
-
const
|
|
606
|
-
|
|
607
|
-
|
|
351
|
+
const typeDir = (0, cursor_paths_1.getCursorTypeDir)(sub.type);
|
|
352
|
+
const installDir = path.join(typeDir, sub.name);
|
|
353
|
+
// Queue file writes.
|
|
354
|
+
for (const file of downloadResult.files) {
|
|
355
|
+
const normalised = path.normalize(file.path);
|
|
356
|
+
if (normalised.startsWith('..')) {
|
|
357
|
+
logger_1.logger.warn({ resourceId: sub.id, filePath: file.path }, 'Skipping suspicious file path');
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
localActions.push({
|
|
361
|
+
action: 'write_file',
|
|
362
|
+
path: path.join(installDir, normalised),
|
|
363
|
+
content: file.content,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
// Build the mcp.json entry from the downloaded descriptor.
|
|
367
|
+
// We replicate the Format-A detection logic from registerMcpServer()
|
|
368
|
+
// but without touching the server filesystem.
|
|
369
|
+
const mcpJsonPath = path.join((0, cursor_paths_1.getCursorRootDir)(), 'mcp.json');
|
|
370
|
+
let mcpEntry = {};
|
|
371
|
+
let missingEnv = [];
|
|
372
|
+
let setupHint;
|
|
373
|
+
let setupDoc;
|
|
374
|
+
const descriptorFile = downloadResult.files.find((f) => path.basename(f.path) === 'mcp-config.json');
|
|
375
|
+
if (descriptorFile) {
|
|
376
|
+
try {
|
|
377
|
+
const cfg = JSON.parse(descriptorFile.content);
|
|
378
|
+
if (typeof cfg['command'] === 'string') {
|
|
379
|
+
// Format A: single-server descriptor
|
|
380
|
+
mcpEntry = cfg;
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
// Format B disguised as local — treat whole object as entries map
|
|
384
|
+
mcpEntry = cfg;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
catch { /* malformed — leave entry empty */ }
|
|
388
|
+
}
|
|
389
|
+
// Detect missing env vars.
|
|
390
|
+
const envBlock = (mcpEntry['env'] ?? {});
|
|
391
|
+
missingEnv = Object.entries(envBlock).filter(([, v]) => v === '').map(([k]) => k);
|
|
392
|
+
if (missingEnv.length > 0) {
|
|
393
|
+
setupHint =
|
|
394
|
+
`Fill in the following environment variables in ${mcpJsonPath} ` +
|
|
395
|
+
`under mcpServers["${sub.name}"]: ${missingEnv.join(', ')}.`;
|
|
396
|
+
}
|
|
397
|
+
// Check for a setup doc among downloaded files.
|
|
398
|
+
const readmeFile = downloadResult.files.find((f) => /readme/i.test(path.basename(f.path)) && f.path.endsWith('.md'));
|
|
399
|
+
if (readmeFile) {
|
|
400
|
+
setupDoc = path.join(installDir, readmeFile.path);
|
|
401
|
+
}
|
|
402
|
+
const mergeMcpAction = {
|
|
403
|
+
action: 'merge_mcp_json',
|
|
404
|
+
mcp_json_path: mcpJsonPath,
|
|
405
|
+
server_name: sub.name,
|
|
406
|
+
entry: Object.keys(mcpEntry).length > 0 ? mcpEntry : {
|
|
407
|
+
// Fallback: auto-detect entry point from file list.
|
|
408
|
+
command: (() => {
|
|
409
|
+
const jsEntry = downloadResult.files.find((f) => f.path.endsWith('.js'));
|
|
410
|
+
const pyEntry = downloadResult.files.find((f) => f.path.endsWith('.py'));
|
|
411
|
+
if (jsEntry)
|
|
412
|
+
return 'node';
|
|
413
|
+
if (pyEntry)
|
|
414
|
+
return 'python3';
|
|
415
|
+
return 'node';
|
|
416
|
+
})(),
|
|
417
|
+
args: [(() => {
|
|
418
|
+
const jsEntry = downloadResult.files.find((f) => f.path.endsWith('.js'));
|
|
419
|
+
const pyEntry = downloadResult.files.find((f) => f.path.endsWith('.py'));
|
|
420
|
+
const entryFile = jsEntry ?? pyEntry ?? downloadResult.files[0];
|
|
421
|
+
return path.join(installDir, entryFile?.path ?? '');
|
|
422
|
+
})()],
|
|
423
|
+
},
|
|
424
|
+
...(missingEnv.length > 0 ? { missing_env: missingEnv, setup_hint: setupHint } : {}),
|
|
425
|
+
...(setupDoc ? { setup_doc: setupDoc } : {}),
|
|
426
|
+
};
|
|
427
|
+
localActions.push(mergeMcpAction);
|
|
428
|
+
tally.synced++;
|
|
429
|
+
details.push({ id: sub.id, name: sub.name, action: 'synced', version: resourceVersion });
|
|
430
|
+
(0, logger_1.logToolStep)('sync_resources', 'Local-executable MCP: write_file + merge_mcp_json actions queued for AI', {
|
|
431
|
+
resourceId: sub.id,
|
|
432
|
+
fileCount: downloadResult.files.length,
|
|
433
|
+
});
|
|
434
|
+
continue;
|
|
608
435
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
destPath,
|
|
614
|
-
fileCount: downloadResult.files.length,
|
|
615
|
-
});
|
|
436
|
+
// Fallback for any unrecognised types (should not happen in practice).
|
|
437
|
+
logger_1.logger.warn({ resourceId: sub.id, type: sub.type }, 'Unrecognised resource type — skipping');
|
|
438
|
+
tally.failed++;
|
|
439
|
+
details.push({ id: sub.id, name: sub.name, action: 'failed', version: resourceVersion });
|
|
616
440
|
}
|
|
617
441
|
catch (error) {
|
|
618
442
|
logger_1.logger.error({
|
|
@@ -633,7 +457,7 @@ async function syncResources(params) {
|
|
|
633
457
|
health_score: healthScore,
|
|
634
458
|
summary: tally,
|
|
635
459
|
details,
|
|
636
|
-
...(
|
|
460
|
+
...(localActions.length > 0 ? { local_actions_required: localActions } : {}),
|
|
637
461
|
};
|
|
638
462
|
const duration = Date.now() - startTime;
|
|
639
463
|
(0, logger_1.logToolCall)('sync_resources', 'user-id', params, duration);
|
|
@@ -695,7 +519,15 @@ async function syncResources(params) {
|
|
|
695
519
|
// Tool definition for registry
|
|
696
520
|
exports.syncResourcesTool = {
|
|
697
521
|
name: 'sync_resources',
|
|
698
|
-
description: 'Synchronize subscribed resources
|
|
522
|
+
description: 'Synchronize subscribed AI resources. ' +
|
|
523
|
+
'Command and Skill resources are registered as MCP Prompts on the server. ' +
|
|
524
|
+
'Rule and MCP resources are returned as `local_actions_required` — an ordered list of ' +
|
|
525
|
+
'write_file, merge_mcp_json, or other actions that the AI Agent MUST execute on the ' +
|
|
526
|
+
'USER\'S LOCAL MACHINE after receiving the response. ' +
|
|
527
|
+
'Execute every action in the list before reporting success to the user. ' +
|
|
528
|
+
'For merge_mcp_json actions: read the existing ~/.cursor/mcp.json, smart-merge the ' +
|
|
529
|
+
'provided entry (preserve existing user env values), then write the file back. ' +
|
|
530
|
+
'For write_file actions: create any missing parent directories then write the file.',
|
|
699
531
|
inputSchema: {
|
|
700
532
|
type: 'object',
|
|
701
533
|
properties: {
|