netlify 27.18.0 → 27.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/base-command.js +21 -0
- package/dist/commands/init/index.js +14 -1
- package/dist/commands/init/init.d.ts +5 -1
- package/dist/commands/init/init.js +49 -7
- package/dist/utils/agent-detection.js +1 -1
- package/dist/utils/init/agent-location.d.ts +6 -0
- package/dist/utils/init/agent-location.js +22 -0
- package/dist/utils/init/agent-mcp-config.d.ts +30 -0
- package/dist/utils/init/agent-mcp-config.js +206 -0
- package/dist/utils/init/agent-mcp.d.ts +57 -0
- package/dist/utils/init/agent-mcp.js +539 -0
- package/dist/utils/init/agent-skills-refresh-worker.d.ts +2 -0
- package/dist/utils/init/agent-skills-refresh-worker.js +17 -0
- package/dist/utils/init/agent-skills-refresh.d.ts +37 -0
- package/dist/utils/init/agent-skills-refresh.js +138 -0
- package/dist/utils/init/agent-skills-state.d.ts +16 -0
- package/dist/utils/init/agent-skills-state.js +42 -0
- package/dist/utils/init/agent-skills.d.ts +41 -2
- package/dist/utils/init/agent-skills.js +104 -21
- package/package.json +2 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { checkbox } from '@inquirer/prompts';
|
|
5
|
+
import { isexe } from 'isexe';
|
|
6
|
+
import { getDrivingAgent } from '../agent-detection.js';
|
|
7
|
+
import { chalk, log, netlifyCommand, warn } from '../command-helpers.js';
|
|
8
|
+
import execa from '../execa.js';
|
|
9
|
+
import { isInteractive } from '../scripted-commands.js';
|
|
10
|
+
import { addToProjectConfig, readProjectConfig } from './agent-mcp-config.js';
|
|
11
|
+
export const NETLIFY_MCP_URL = 'https://mcp.netlify.com/mcp';
|
|
12
|
+
export const NETLIFY_MCP_SERVER_NAME = 'netlify';
|
|
13
|
+
const NETLIFY_MCP_HOSTS = new Set(['mcp.netlify.com', 'netlify-mcp.netlify.app']);
|
|
14
|
+
const NETLIFY_MCP_PACKAGE = '@netlify/mcp';
|
|
15
|
+
const NATIVE_TIMEOUT_MS = 15_000;
|
|
16
|
+
const LIST_TIMEOUT_MS = 30_000;
|
|
17
|
+
const ADD_MCP_VERSION = '2.4.1';
|
|
18
|
+
export const MCP_SCOPES = ['project', 'user'];
|
|
19
|
+
const inEditorTerminal = (env) => env.TERM_PROGRAM === 'vscode';
|
|
20
|
+
const jsonServers = (file, entry = { url: NETLIFY_MCP_URL }, key = 'mcpServers', alternatives) => ({ path: file, alternatives, format: 'json', key, entry });
|
|
21
|
+
const HTTP_ENTRY = { type: 'http', url: NETLIFY_MCP_URL };
|
|
22
|
+
const geminiAdd = (scope) => [
|
|
23
|
+
'mcp',
|
|
24
|
+
'add',
|
|
25
|
+
'--transport',
|
|
26
|
+
'http',
|
|
27
|
+
'--scope',
|
|
28
|
+
scope,
|
|
29
|
+
NETLIFY_MCP_SERVER_NAME,
|
|
30
|
+
NETLIFY_MCP_URL,
|
|
31
|
+
];
|
|
32
|
+
const claudeAdd = (scope) => [
|
|
33
|
+
'mcp',
|
|
34
|
+
'add',
|
|
35
|
+
'--transport',
|
|
36
|
+
'http',
|
|
37
|
+
'--scope',
|
|
38
|
+
scope,
|
|
39
|
+
NETLIFY_MCP_SERVER_NAME,
|
|
40
|
+
NETLIFY_MCP_URL,
|
|
41
|
+
];
|
|
42
|
+
export const MCP_AGENTS = [
|
|
43
|
+
{
|
|
44
|
+
id: 'claude-code',
|
|
45
|
+
label: 'Claude Code',
|
|
46
|
+
binary: 'claude',
|
|
47
|
+
drivenBy: ['claude'],
|
|
48
|
+
projectMarkers: ['.claude', '.mcp.json', 'CLAUDE.md'],
|
|
49
|
+
projectFile: jsonServers('.mcp.json', HTTP_ENTRY),
|
|
50
|
+
native: {
|
|
51
|
+
lookup: 'list',
|
|
52
|
+
scopes: {
|
|
53
|
+
project: {
|
|
54
|
+
add: claudeAdd('local'),
|
|
55
|
+
remove: ['mcp', 'remove', '--scope', 'local', NETLIFY_MCP_SERVER_NAME],
|
|
56
|
+
savedIn: "~/.claude.json, under this project's path",
|
|
57
|
+
},
|
|
58
|
+
user: {
|
|
59
|
+
add: claudeAdd('user'),
|
|
60
|
+
remove: ['mcp', 'remove', '--scope', 'user', NETLIFY_MCP_SERVER_NAME],
|
|
61
|
+
savedIn: '~/.claude.json',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: 'codex',
|
|
68
|
+
label: 'Codex',
|
|
69
|
+
binary: 'codex',
|
|
70
|
+
drivenBy: ['codex'],
|
|
71
|
+
projectMarkers: ['.codex'],
|
|
72
|
+
projectFile: {
|
|
73
|
+
path: path.join('.codex', 'config.toml'),
|
|
74
|
+
format: 'toml',
|
|
75
|
+
key: 'mcp_servers',
|
|
76
|
+
entry: { url: NETLIFY_MCP_URL },
|
|
77
|
+
note: 'Codex reads it once this project is trusted.',
|
|
78
|
+
},
|
|
79
|
+
native: {
|
|
80
|
+
lookup: 'list-json',
|
|
81
|
+
scopes: {
|
|
82
|
+
user: {
|
|
83
|
+
add: ['mcp', 'add', NETLIFY_MCP_SERVER_NAME, '--url', NETLIFY_MCP_URL],
|
|
84
|
+
remove: ['mcp', 'remove', NETLIFY_MCP_SERVER_NAME],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: 'opencode',
|
|
91
|
+
label: 'OpenCode',
|
|
92
|
+
binary: 'opencode',
|
|
93
|
+
drivenBy: ['opencode'],
|
|
94
|
+
projectMarkers: ['opencode.json', 'opencode.jsonc', '.opencode'],
|
|
95
|
+
projectFile: jsonServers('opencode.jsonc', { type: 'remote', url: NETLIFY_MCP_URL, enabled: true }, 'mcp', [
|
|
96
|
+
'opencode.json',
|
|
97
|
+
path.join('.opencode', 'opencode.jsonc'),
|
|
98
|
+
path.join('.opencode', 'opencode.json'),
|
|
99
|
+
]),
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: 'vscode',
|
|
103
|
+
label: 'VS Code',
|
|
104
|
+
binary: 'code',
|
|
105
|
+
projectMarkers: [path.join('.vscode', 'mcp.json')],
|
|
106
|
+
projectFile: jsonServers(path.join('.vscode', 'mcp.json'), HTTP_ENTRY, 'servers'),
|
|
107
|
+
inTerminal: (env) => inEditorTerminal(env) && !env.CURSOR_TRACE_ID,
|
|
108
|
+
native: {
|
|
109
|
+
scopes: {
|
|
110
|
+
user: {
|
|
111
|
+
add: ['--add-mcp', JSON.stringify({ name: NETLIFY_MCP_SERVER_NAME, type: 'http', url: NETLIFY_MCP_URL })],
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: 'cursor',
|
|
118
|
+
label: 'Cursor',
|
|
119
|
+
binary: 'cursor-agent',
|
|
120
|
+
drivenBy: ['cursor'],
|
|
121
|
+
projectMarkers: ['.cursor'],
|
|
122
|
+
projectFile: jsonServers(path.join('.cursor', 'mcp.json')),
|
|
123
|
+
inTerminal: (env) => inEditorTerminal(env) && Boolean(env.CURSOR_TRACE_ID),
|
|
124
|
+
},
|
|
125
|
+
{ id: 'windsurf', label: 'Windsurf', binary: 'windsurf', projectMarkers: ['.windsurf'] },
|
|
126
|
+
{
|
|
127
|
+
id: 'gemini-cli',
|
|
128
|
+
label: 'Gemini CLI',
|
|
129
|
+
binary: 'gemini',
|
|
130
|
+
drivenBy: ['gemini'],
|
|
131
|
+
projectMarkers: ['.gemini', 'GEMINI.md'],
|
|
132
|
+
projectFile: jsonServers(path.join('.gemini', 'settings.json'), HTTP_ENTRY),
|
|
133
|
+
native: { scopes: { project: { add: geminiAdd('project') }, user: { add: geminiAdd('user') } } },
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: 'github-copilot-cli',
|
|
137
|
+
label: 'GitHub Copilot CLI',
|
|
138
|
+
binary: 'copilot',
|
|
139
|
+
drivenBy: ['copilot'],
|
|
140
|
+
projectFile: jsonServers('.mcp.json', HTTP_ENTRY, 'mcpServers', [path.join('.github', 'mcp.json')]),
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
id: 'kiro-cli',
|
|
144
|
+
label: 'Kiro CLI',
|
|
145
|
+
binary: 'kiro-cli',
|
|
146
|
+
drivenBy: ['kiro'],
|
|
147
|
+
projectMarkers: ['.kiro'],
|
|
148
|
+
projectFile: jsonServers(path.join('.kiro', 'settings', 'mcp.json')),
|
|
149
|
+
},
|
|
150
|
+
{ id: 'cline', label: 'Cline', drivenBy: ['cline'], projectMarkers: ['.clinerules'] },
|
|
151
|
+
];
|
|
152
|
+
const where = (scope) => (scope === 'project' ? 'this project' : 'all your projects');
|
|
153
|
+
const chooseWithPrompt = (scope) => async (candidates) => await checkbox({
|
|
154
|
+
message: `Add the Netlify MCP server for ${where(scope)} to which AI agents?`,
|
|
155
|
+
choices: candidates.map(({ agent, detected }) => ({ name: agent.label, value: agent, checked: detected })),
|
|
156
|
+
});
|
|
157
|
+
const pathExists = async (target) => {
|
|
158
|
+
try {
|
|
159
|
+
await fs.access(target);
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const findOnPath = async (binary, env) => {
|
|
167
|
+
const pathExtensions = (env.PATHEXT ?? '').split(';').filter(Boolean);
|
|
168
|
+
const extensions = process.platform === 'win32' ? (pathExtensions.length > 0 ? pathExtensions : ['.EXE', '.CMD', '.BAT']) : [''];
|
|
169
|
+
const isExecutableFile = (target) => isexe(target, { ignoreErrors: true, pathExt: extensions.join(';') });
|
|
170
|
+
const candidates = (env.PATH ?? '')
|
|
171
|
+
.split(path.delimiter)
|
|
172
|
+
.filter((directory) => path.isAbsolute(directory))
|
|
173
|
+
.flatMap((directory) => extensions.map((extension) => path.join(directory, binary + extension)));
|
|
174
|
+
const executable = await Promise.all(candidates.map(isExecutableFile));
|
|
175
|
+
return candidates.find((_, index) => executable[index]);
|
|
176
|
+
};
|
|
177
|
+
const hasProjectMarker = async (agent, workingDir) => {
|
|
178
|
+
for (const marker of agent.projectMarkers ?? []) {
|
|
179
|
+
if (await pathExists(path.join(workingDir, marker))) {
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
};
|
|
185
|
+
const supportsScope = (agent, scope) => scope === 'user' || agent.projectFile !== undefined || agent.native?.scopes.project !== undefined;
|
|
186
|
+
export const findCandidates = async (workingDir, env, scope = 'project') => {
|
|
187
|
+
const candidates = [];
|
|
188
|
+
for (const agent of MCP_AGENTS.filter((candidate) => supportsScope(candidate, scope))) {
|
|
189
|
+
const detected = Boolean(agent.inTerminal?.(env)) || (await hasProjectMarker(agent, workingDir));
|
|
190
|
+
if (detected || (agent.binary !== undefined && (await findOnPath(agent.binary, env)) !== undefined)) {
|
|
191
|
+
candidates.push({ agent, detected });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return candidates.sort((a, b) => Number(b.detected) - Number(a.detected));
|
|
195
|
+
};
|
|
196
|
+
const parseUrl = (value) => {
|
|
197
|
+
try {
|
|
198
|
+
return new URL(value);
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
const urlsIn = (text) => (text.match(/https?:\/\/[^\s"'<>]+/g) ?? []).map(parseUrl).filter((url) => url !== undefined);
|
|
205
|
+
const OUR_URL = new URL(NETLIFY_MCP_URL);
|
|
206
|
+
const isOurUrl = (url) => url.protocol === OUR_URL.protocol &&
|
|
207
|
+
url.username === '' &&
|
|
208
|
+
url.password === '' &&
|
|
209
|
+
url.hostname.toLowerCase() === OUR_URL.hostname &&
|
|
210
|
+
url.port === OUR_URL.port &&
|
|
211
|
+
url.pathname.replace(/\/$/, '') === OUR_URL.pathname &&
|
|
212
|
+
url.search === '' &&
|
|
213
|
+
url.hash === '';
|
|
214
|
+
const pointsAtNetlify = (target) => urlsIn(target).some((url) => NETLIFY_MCP_HOSTS.has(url.hostname.toLowerCase())) ||
|
|
215
|
+
target.split(/\s+/).some((word) => word === NETLIFY_MCP_PACKAGE || word.startsWith(`${NETLIFY_MCP_PACKAGE}@`));
|
|
216
|
+
const isNetlifyName = (name) => name === NETLIFY_MCP_SERVER_NAME || name.endsWith(`:${NETLIFY_MCP_SERVER_NAME}`);
|
|
217
|
+
const isControlCharacter = (character) => {
|
|
218
|
+
const code = character.codePointAt(0) ?? 0;
|
|
219
|
+
return code < 0x20 || (code >= 0x7f && code <= 0x9f);
|
|
220
|
+
};
|
|
221
|
+
const printable = (text) => Array.from(text)
|
|
222
|
+
.filter((character) => !isControlCharacter(character))
|
|
223
|
+
.join('');
|
|
224
|
+
const shellQuote = (arg) => {
|
|
225
|
+
if (process.platform === 'win32') {
|
|
226
|
+
if (/^[\w@%+=:,./\\-]+$/.test(arg)) {
|
|
227
|
+
return arg;
|
|
228
|
+
}
|
|
229
|
+
return `"${arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, '$1$1')}"`;
|
|
230
|
+
}
|
|
231
|
+
return /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`;
|
|
232
|
+
};
|
|
233
|
+
const formatCommand = (binary, args) => chalk.cyanBright.bold([binary, ...args].map(shellQuote).join(' '));
|
|
234
|
+
const addMcpArgs = (agents, scope) => {
|
|
235
|
+
const global = scope === 'user';
|
|
236
|
+
return [
|
|
237
|
+
'-y',
|
|
238
|
+
`add-mcp@${ADD_MCP_VERSION}`,
|
|
239
|
+
NETLIFY_MCP_URL,
|
|
240
|
+
'--name',
|
|
241
|
+
NETLIFY_MCP_SERVER_NAME,
|
|
242
|
+
...(global ? ['-g'] : []),
|
|
243
|
+
...(agents.length > 0 ? ['-y', ...agents.flatMap(({ id }) => ['-a', id])] : []),
|
|
244
|
+
];
|
|
245
|
+
};
|
|
246
|
+
const runNative = async (executable, args, cwd, timeout = NATIVE_TIMEOUT_MS) => {
|
|
247
|
+
const result = await execa(executable, args, { cwd, reject: false, stdin: 'ignore', timeout });
|
|
248
|
+
return {
|
|
249
|
+
exitCode: result.exitCode,
|
|
250
|
+
output: [result.stderr, result.stdout].filter(Boolean).join('\n').trim(),
|
|
251
|
+
stdout: result.stdout,
|
|
252
|
+
timedOut: result.timedOut,
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
const describeFailure = ({ timedOut, output, exitCode }) => timedOut ? 'timed out' : output.split('\n').map(printable).join('\n') || `exit code ${String(exitCode)}`;
|
|
256
|
+
const PENDING_APPROVAL = 'Pending approval';
|
|
257
|
+
const USER_SCOPE_DETAIL = /^\s*Scope: User config\b/m;
|
|
258
|
+
const listedServers = (stdout) => stdout.split('\n').flatMap((line) => {
|
|
259
|
+
const separator = line.indexOf(': ');
|
|
260
|
+
if (separator <= 0) {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
const status = line.lastIndexOf(' - ');
|
|
264
|
+
const end = status > separator ? status : line.length;
|
|
265
|
+
if (line.slice(end).includes(PENDING_APPROVAL)) {
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
return [{ name: line.slice(0, separator).trim(), target: line.slice(separator + 2, end).trim() }];
|
|
269
|
+
});
|
|
270
|
+
const codexServers = (stdout) => {
|
|
271
|
+
try {
|
|
272
|
+
const parsed = JSON.parse(stdout);
|
|
273
|
+
if (!Array.isArray(parsed)) {
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
return parsed.map(({ name, transport }) => ({
|
|
277
|
+
name: typeof name === 'string' ? name : '',
|
|
278
|
+
target: [
|
|
279
|
+
transport?.url,
|
|
280
|
+
transport?.command,
|
|
281
|
+
...(Array.isArray(transport?.args) ? transport.args : []),
|
|
282
|
+
]
|
|
283
|
+
.filter((part) => typeof part === 'string')
|
|
284
|
+
.join(' '),
|
|
285
|
+
}));
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
const classify = (servers) => {
|
|
292
|
+
const ours = servers.find(({ target }) => urlsIn(target).some(isOurUrl));
|
|
293
|
+
if (ours) {
|
|
294
|
+
return { found: 'ours', name: ours.name };
|
|
295
|
+
}
|
|
296
|
+
const other = servers.find(({ name, target }) => isNetlifyName(name) || pointsAtNetlify(target));
|
|
297
|
+
return other ? { found: 'other', name: other.name } : { found: 'none' };
|
|
298
|
+
};
|
|
299
|
+
const isExisting = (lookup) => lookup.found === 'ours' || lookup.found === 'other';
|
|
300
|
+
const lookUpAtUserScope = async (executable, lookup, workingDir) => {
|
|
301
|
+
if (!isExisting(lookup) || lookup.name.startsWith('plugin:')) {
|
|
302
|
+
return lookup;
|
|
303
|
+
}
|
|
304
|
+
const details = await runNative(executable, ['mcp', 'get', lookup.name], workingDir, LIST_TIMEOUT_MS);
|
|
305
|
+
if (details.exitCode !== 0) {
|
|
306
|
+
return { found: 'unknown', run: details };
|
|
307
|
+
}
|
|
308
|
+
return USER_SCOPE_DETAIL.test(details.stdout) ? lookup : { found: 'none' };
|
|
309
|
+
};
|
|
310
|
+
const lookUp = async (executable, kind, workingDir, scope) => {
|
|
311
|
+
if (kind === 'list-json') {
|
|
312
|
+
const run = await runNative(executable, ['mcp', 'list', '--json'], scope === 'user' ? homedir() : workingDir);
|
|
313
|
+
const servers = run.exitCode === 0 ? codexServers(run.stdout) : undefined;
|
|
314
|
+
return servers ? classify(servers) : { found: 'unknown', run };
|
|
315
|
+
}
|
|
316
|
+
const run = await runNative(executable, ['mcp', 'list'], workingDir, LIST_TIMEOUT_MS);
|
|
317
|
+
if (run.exitCode !== 0) {
|
|
318
|
+
return { found: 'unknown', run };
|
|
319
|
+
}
|
|
320
|
+
const lookup = classify(listedServers(run.stdout));
|
|
321
|
+
return scope === 'user' ? await lookUpAtUserScope(executable, lookup, workingDir) : lookup;
|
|
322
|
+
};
|
|
323
|
+
const reportExisting = (agent, { found, name }) => {
|
|
324
|
+
if (found === 'ours') {
|
|
325
|
+
log(`${agent.label} already has the Netlify MCP server.`);
|
|
326
|
+
return 'present';
|
|
327
|
+
}
|
|
328
|
+
log(`${agent.label} already has a Netlify MCP server (${chalk.cyan(printable(name))}) that isn't ${chalk.underline(NETLIFY_MCP_URL)}, so another one wasn't added.`);
|
|
329
|
+
log(` To switch, remove ${chalk.cyan(printable(name))} in ${agent.label} and run ${chalk.cyanBright.bold(`${netlifyCommand()} init --reset-mcp-setup`)}`);
|
|
330
|
+
return 'kept';
|
|
331
|
+
};
|
|
332
|
+
const lookUpExisting = async (agent, env, workingDir, scope) => {
|
|
333
|
+
if (agent.binary === undefined) {
|
|
334
|
+
return {};
|
|
335
|
+
}
|
|
336
|
+
const executable = await findOnPath(agent.binary, env);
|
|
337
|
+
const kind = agent.native?.lookup;
|
|
338
|
+
if (!executable || !kind) {
|
|
339
|
+
return { executable };
|
|
340
|
+
}
|
|
341
|
+
return { executable, lookup: await lookUp(executable, kind, workingDir, scope) };
|
|
342
|
+
};
|
|
343
|
+
const restartHint = (agent, scope) => ` The Netlify MCP server loads the next time you start ${agent.label}${scope === 'project' ? ' in this folder' : ''}.`;
|
|
344
|
+
const commandFor = (agent, scope) => {
|
|
345
|
+
const native = agent.native?.scopes[scope];
|
|
346
|
+
if (native && agent.binary) {
|
|
347
|
+
return formatCommand(agent.binary, native.add);
|
|
348
|
+
}
|
|
349
|
+
return formatCommand('npx', addMcpArgs([agent], scope));
|
|
350
|
+
};
|
|
351
|
+
const fileNote = (agent, scope) => {
|
|
352
|
+
const native = agent.native?.scopes[scope];
|
|
353
|
+
if (native && !agent.native?.lookup) {
|
|
354
|
+
return ` Running it replaces any ${chalk.cyan(NETLIFY_MCP_SERVER_NAME)} server ${agent.label} already has.`;
|
|
355
|
+
}
|
|
356
|
+
if (native) {
|
|
357
|
+
return undefined;
|
|
358
|
+
}
|
|
359
|
+
if (scope === 'project') {
|
|
360
|
+
return ` This writes ${agent.projectFile?.path ?? ''} in this folder. Commit it only if everyone on the project should get it.`;
|
|
361
|
+
}
|
|
362
|
+
return undefined;
|
|
363
|
+
};
|
|
364
|
+
const printCommand = (agent, scope, lead) => {
|
|
365
|
+
log(`${lead} ${commandFor(agent, scope)}`);
|
|
366
|
+
const note = fileNote(agent, scope);
|
|
367
|
+
if (note) {
|
|
368
|
+
log(note);
|
|
369
|
+
}
|
|
370
|
+
log(restartHint(agent, scope));
|
|
371
|
+
};
|
|
372
|
+
const addToProjectFile = async (agent, file, workingDir) => {
|
|
373
|
+
const read = await readProjectConfig(workingDir, file);
|
|
374
|
+
const printInstead = (reason) => {
|
|
375
|
+
log(`Could not add the Netlify MCP server to ${read.path}: ${reason}.`);
|
|
376
|
+
printCommand(agent, 'project', ' Add it yourself with');
|
|
377
|
+
return 'failed';
|
|
378
|
+
};
|
|
379
|
+
if (!read.readable) {
|
|
380
|
+
return printInstead(read.reason);
|
|
381
|
+
}
|
|
382
|
+
const existing = classify(read.servers);
|
|
383
|
+
if (isExisting(existing)) {
|
|
384
|
+
if (existing.found === 'ours' && read.servers.some(({ name, disabled }) => name === existing.name && disabled)) {
|
|
385
|
+
log(`${agent.label} already has the Netlify MCP server, turned off in ${read.path}. Turn it on there to use it.`);
|
|
386
|
+
return 'kept';
|
|
387
|
+
}
|
|
388
|
+
return reportExisting(agent, existing);
|
|
389
|
+
}
|
|
390
|
+
let written;
|
|
391
|
+
try {
|
|
392
|
+
written = await addToProjectConfig(workingDir, file, NETLIFY_MCP_SERVER_NAME, read);
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
warn(`Could not write ${read.path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
396
|
+
printCommand(agent, 'project', ' Add it yourself with');
|
|
397
|
+
return 'failed';
|
|
398
|
+
}
|
|
399
|
+
if (!written) {
|
|
400
|
+
return printInstead('the result would not parse');
|
|
401
|
+
}
|
|
402
|
+
log(`Added the Netlify MCP server to ${agent.label} for this project in ${read.path}. Commit it only if everyone on the project should get it.`);
|
|
403
|
+
if (file.note) {
|
|
404
|
+
log(` ${file.note}`);
|
|
405
|
+
}
|
|
406
|
+
log(restartHint(agent, 'project'));
|
|
407
|
+
return 'added';
|
|
408
|
+
};
|
|
409
|
+
const add = async (agent, scope, env, workingDir) => {
|
|
410
|
+
if (!supportsScope(agent, scope)) {
|
|
411
|
+
log(`${agent.label} has no per-project MCP setting, so nothing was set up. To add the Netlify MCP server for all your projects, run ${chalk.cyanBright.bold(`${netlifyCommand()} init --mcp-scope user`)}`);
|
|
412
|
+
return 'unsupported';
|
|
413
|
+
}
|
|
414
|
+
const commands = agent.native?.scopes[scope];
|
|
415
|
+
if (scope === 'project' && agent.projectFile && !(commands && agent.native?.lookup)) {
|
|
416
|
+
return await addToProjectFile(agent, agent.projectFile, workingDir);
|
|
417
|
+
}
|
|
418
|
+
const { executable, lookup } = await lookUpExisting(agent, env, workingDir, scope);
|
|
419
|
+
if (lookup?.found === 'unknown') {
|
|
420
|
+
warn(`Could not check ${agent.label} for an existing Netlify MCP server: ${describeFailure(lookup.run)}`);
|
|
421
|
+
printCommand(agent, scope, ` Add it yourself with`);
|
|
422
|
+
return 'failed';
|
|
423
|
+
}
|
|
424
|
+
if (lookup && isExisting(lookup)) {
|
|
425
|
+
return reportExisting(agent, lookup);
|
|
426
|
+
}
|
|
427
|
+
if (!commands || !executable || !agent.binary || !agent.native?.lookup) {
|
|
428
|
+
printCommand(agent, scope, `To add the Netlify MCP server to ${agent.label} for ${where(scope)}, run`);
|
|
429
|
+
return 'printed';
|
|
430
|
+
}
|
|
431
|
+
const added = await runNative(executable, commands.add, workingDir);
|
|
432
|
+
if (added.exitCode !== 0) {
|
|
433
|
+
warn(`Could not add the Netlify MCP server to ${agent.label}: ${describeFailure(added)}`);
|
|
434
|
+
log(` Add it yourself with ${formatCommand(agent.binary, commands.add)}`);
|
|
435
|
+
return 'failed';
|
|
436
|
+
}
|
|
437
|
+
const savedIn = commands.savedIn ? `Saved in ${commands.savedIn}; nothing` : 'Nothing';
|
|
438
|
+
log(`Added the Netlify MCP server to ${agent.label} for ${where(scope)}. ${savedIn} was written to the repository.`);
|
|
439
|
+
if (commands.remove) {
|
|
440
|
+
log(` Remove it with ${formatCommand(agent.binary, commands.remove)}`);
|
|
441
|
+
}
|
|
442
|
+
log(restartHint(agent, scope));
|
|
443
|
+
return 'added';
|
|
444
|
+
};
|
|
445
|
+
const SAVED_CHOICES_KEY = 'agentMcpSetup';
|
|
446
|
+
const savedChoiceKey = (agent, scope) => `${SAVED_CHOICES_KEY}.${scope}.${agent.id}`;
|
|
447
|
+
const savedChoice = (store, agent, scope) => {
|
|
448
|
+
const saved = store.get(savedChoiceKey(agent, scope));
|
|
449
|
+
return saved?.url === NETLIFY_MCP_URL ? saved : undefined;
|
|
450
|
+
};
|
|
451
|
+
const saveChoice = (store, agent, scope, status) => {
|
|
452
|
+
if (status === 'failed') {
|
|
453
|
+
if (store.get(savedChoiceKey(agent, scope)) !== undefined) {
|
|
454
|
+
store.delete(savedChoiceKey(agent, scope));
|
|
455
|
+
}
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const choice = status === 'declined' ? 'declined' : status === 'kept' ? 'kept' : 'accepted';
|
|
459
|
+
store.set(savedChoiceKey(agent, scope), { url: NETLIFY_MCP_URL, choice });
|
|
460
|
+
};
|
|
461
|
+
const saveChoices = (store, scope, offered, chosen, statuses) => {
|
|
462
|
+
for (const agent of offered) {
|
|
463
|
+
saveChoice(store, agent, scope, chosen.includes(agent) ? statuses[agent.id] : 'declined');
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
const resetHint = () => `Run ${chalk.cyanBright.bold(`${netlifyCommand()} init --reset-mcp-setup`)} to choose again.`;
|
|
467
|
+
const selectAgents = async ({ workingDir, env, interactive, choose, reset, scope, store, }) => {
|
|
468
|
+
const drivingAgent = getDrivingAgent(env);
|
|
469
|
+
const agent = drivingAgent && MCP_AGENTS.find(({ drivenBy }) => drivenBy?.includes(drivingAgent.name));
|
|
470
|
+
if (agent) {
|
|
471
|
+
return {
|
|
472
|
+
selection: 'driving',
|
|
473
|
+
agents: [agent],
|
|
474
|
+
declined: !reset && savedChoice(store, agent, scope)?.choice === 'declined',
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
if (!interactive) {
|
|
478
|
+
return { selection: 'none', agents: [] };
|
|
479
|
+
}
|
|
480
|
+
const candidates = await findCandidates(workingDir, env, scope);
|
|
481
|
+
if (candidates.length === 0) {
|
|
482
|
+
return { selection: 'none', agents: [] };
|
|
483
|
+
}
|
|
484
|
+
const undecided = reset ? candidates : candidates.filter(({ agent }) => !savedChoice(store, agent, scope));
|
|
485
|
+
if (undecided.length === 0) {
|
|
486
|
+
return { selection: 'saved', agents: [] };
|
|
487
|
+
}
|
|
488
|
+
return { selection: 'prompt', agents: await choose(undecided), offered: undecided.map(({ agent }) => agent) };
|
|
489
|
+
};
|
|
490
|
+
export const setupAgentMcp = async ({ workingDir, store, scope = 'project', env = process.env, interactive = isInteractive(), choose = chooseWithPrompt(scope), reset = false, }) => {
|
|
491
|
+
const summary = { scope, selection: 'none', agents: {} };
|
|
492
|
+
try {
|
|
493
|
+
const selected = await selectAgents({ workingDir, env, interactive, choose, reset, scope, store });
|
|
494
|
+
summary.selection = selected.selection;
|
|
495
|
+
if (selected.selection === 'saved') {
|
|
496
|
+
log(`Using your saved choices for the Netlify MCP server. ${resetHint()}`);
|
|
497
|
+
return summary;
|
|
498
|
+
}
|
|
499
|
+
if (selected.selection === 'none') {
|
|
500
|
+
log(`To add the Netlify MCP server to your AI agents, run ${formatCommand('npx', addMcpArgs([], scope))}`);
|
|
501
|
+
log(` It asks which agents to set up. To skip the questions, add ${chalk.cyan('-y -a <agent>')}; ${formatCommand('npx', [`add-mcp@${ADD_MCP_VERSION}`, 'list-agents'])} shows the agent names.`);
|
|
502
|
+
return summary;
|
|
503
|
+
}
|
|
504
|
+
if (selected.selection === 'driving') {
|
|
505
|
+
const [agent] = selected.agents;
|
|
506
|
+
if (selected.declined) {
|
|
507
|
+
log(`You chose not to add the Netlify MCP server to ${agent.label}. ${resetHint()}`);
|
|
508
|
+
summary.agents[agent.id] = 'declined';
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
const status = await add(agent, scope, env, workingDir);
|
|
512
|
+
summary.agents[agent.id] = status;
|
|
513
|
+
const installed = status === 'added' || status === 'present' || status === 'kept';
|
|
514
|
+
if (reset && installed && savedChoice(store, agent, scope)) {
|
|
515
|
+
saveChoice(store, agent, scope, status);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return summary;
|
|
519
|
+
}
|
|
520
|
+
for (const agent of selected.agents) {
|
|
521
|
+
summary.agents[agent.id] = await add(agent, scope, env, workingDir);
|
|
522
|
+
}
|
|
523
|
+
saveChoices(store, scope, selected.offered, selected.agents, summary.agents);
|
|
524
|
+
const toRun = selected.agents.filter(({ id }) => summary.agents[id] === 'printed');
|
|
525
|
+
if (toRun.length > 0) {
|
|
526
|
+
log(`Run the command shown for ${toRun.map(({ label }) => label).join(', ')} to finish. You won't be asked about ${toRun.length === 1 ? 'it' : 'them'} again in this project.`);
|
|
527
|
+
}
|
|
528
|
+
log(`Your choices are saved for this project. ${resetHint()}`);
|
|
529
|
+
}
|
|
530
|
+
catch (error) {
|
|
531
|
+
if (error instanceof Error && error.name === 'ExitPromptError') {
|
|
532
|
+
throw error;
|
|
533
|
+
}
|
|
534
|
+
warn(`Could not set up the Netlify MCP server: ${error instanceof Error ? error.message : String(error)}`);
|
|
535
|
+
summary.failed = true;
|
|
536
|
+
}
|
|
537
|
+
return summary;
|
|
538
|
+
};
|
|
539
|
+
//# sourceMappingURL=agent-mcp.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Entry point of the detached process that `startAgentSkillsRefresh` spawns.
|
|
2
|
+
import process from 'process';
|
|
3
|
+
import { refreshAgentSkills } from './agent-skills-refresh.js';
|
|
4
|
+
// Installs stage and rename each skill, so stopping mid-sync leaves no half-written skill.
|
|
5
|
+
const WORKER_TIMEOUT_MS = 2 * 60_000;
|
|
6
|
+
// eslint-disable-next-line n/no-process-exit
|
|
7
|
+
setTimeout(() => process.exit(0), WORKER_TIMEOUT_MS).unref();
|
|
8
|
+
const [repositoryRoot] = process.argv.slice(2);
|
|
9
|
+
if (repositoryRoot) {
|
|
10
|
+
try {
|
|
11
|
+
await refreshAgentSkills({ repositoryRoot });
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// The command that started this has moved on, so there is nobody to tell. The next check retries.
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=agent-skills-refresh-worker.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type SkillsSyncResult } from './agent-skills.js';
|
|
2
|
+
export declare const AUTO_UPDATE_ENV = "NETLIFY_SKILLS_AUTO_UPDATE";
|
|
3
|
+
export declare const REFRESH_INTERVAL_MS: number;
|
|
4
|
+
/**
|
|
5
|
+
* Lists the skills directories, relative to `repositoryRoot`, that already hold a Netlify skill.
|
|
6
|
+
* A background refresh only writes to these, so it never adds skills to a project, or to an agent
|
|
7
|
+
* directory, that has none.
|
|
8
|
+
*/
|
|
9
|
+
export declare const installedSkillsDirectories: (repositoryRoot: string, knownSkills?: string[]) => Promise<string[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Starts a background refresh of the project's Netlify skills when one is due, and returns whether
|
|
12
|
+
* it did. Only local files are read here. The manifest request and any downloads happen in a
|
|
13
|
+
* detached process, so the calling command never waits for them.
|
|
14
|
+
*/
|
|
15
|
+
export declare const startAgentSkillsRefresh: ({ repositoryRoot, env, now, ci, spawn, }: {
|
|
16
|
+
repositoryRoot: string;
|
|
17
|
+
env?: NodeJS.ProcessEnv;
|
|
18
|
+
now?: number;
|
|
19
|
+
ci?: boolean;
|
|
20
|
+
spawn?: (repositoryRoot: string) => void;
|
|
21
|
+
}) => Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Runs in the detached process. When the manifest changed since the last sync, brings every
|
|
24
|
+
* directory that already holds Netlify skills up to date and saves what changed for the next
|
|
25
|
+
* command to show. A skill missing from a directory is added unless the project has had it
|
|
26
|
+
* installed before, so skills someone deleted stay deleted.
|
|
27
|
+
*/
|
|
28
|
+
export declare const refreshAgentSkills: ({ repositoryRoot, env, }: {
|
|
29
|
+
repositoryRoot: string;
|
|
30
|
+
env?: NodeJS.ProcessEnv;
|
|
31
|
+
}) => Promise<SkillsSyncResult[]>;
|
|
32
|
+
/** Shows what earlier background refreshes changed, once, and returns the lines it wrote. */
|
|
33
|
+
export declare const showAgentSkillsRefreshNotice: ({ repositoryRoot, write, }: {
|
|
34
|
+
repositoryRoot: string;
|
|
35
|
+
write?: (line: string) => void;
|
|
36
|
+
}) => Promise<string[]>;
|
|
37
|
+
//# sourceMappingURL=agent-skills-refresh.d.ts.map
|