@heroku/mcp-server 1.2.12 → 1.2.13
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/README.md +7 -0
- package/dist/index.js +21 -3
- package/dist/repl/heroku-cli-repl.js +249 -44
- package/dist/repl/heroku-cli-repl.spec.js +335 -78
- package/dist/tools/ai.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -505,3 +505,10 @@ Example configuration with custom timeout:
|
|
|
505
505
|
}
|
|
506
506
|
}
|
|
507
507
|
```
|
|
508
|
+
|
|
509
|
+
### `MCP_SERVER_IDLE_TIMEOUT`
|
|
510
|
+
|
|
511
|
+
Timeout in milliseconds of inactivity after which the underlying Heroku CLI process is shut down so an idle server does
|
|
512
|
+
not keep a CLI process running. The process is started on the first tool call and transparently restarted on the next
|
|
513
|
+
call after a teardown. Defaults to 300000 (5 minutes) if not set; set to `0` to disable idle teardown and keep the
|
|
514
|
+
process running for the lifetime of the server.
|
package/dist/index.js
CHANGED
|
@@ -27,12 +27,30 @@ const server = new McpServer({ name: 'Heroku MCP Server', version: VERSION }, {
|
|
|
27
27
|
const requestTimeout = isNaN(Number(process.env.MCP_SERVER_REQUEST_TIMEOUT))
|
|
28
28
|
? 15_000
|
|
29
29
|
: Number(process.env.MCP_SERVER_REQUEST_TIMEOUT);
|
|
30
|
-
|
|
31
|
-
//
|
|
30
|
+
// Tear down the underlying Heroku CLI REPL process after this many ms of
|
|
31
|
+
// inactivity so an idle server does not keep a CLI process spinning
|
|
32
|
+
// (W-24704373 / #180). Defaults to 10 minutes; set to 0 to disable idle teardown.
|
|
33
|
+
const idleTimeout = isNaN(Number(process.env.MCP_SERVER_IDLE_TIMEOUT))
|
|
34
|
+
? 600_000
|
|
35
|
+
: Number(process.env.MCP_SERVER_IDLE_TIMEOUT);
|
|
36
|
+
const herokuRepl = new HerokuREPL(requestTimeout, idleTimeout);
|
|
37
|
+
// Log MCP-formatted fatal errors but do NOT exit. With lazy start + idle
|
|
38
|
+
// respawn (W-24704373 / #180) a fatalError can now fire mid-session on a
|
|
39
|
+
// transient CLI failure; the REPL settles the in-flight command with the error
|
|
40
|
+
// text, so the client gets a tool error and the server stays up to retry on the
|
|
41
|
+
// next call. Exiting here would crash the whole server on a single bad respawn.
|
|
32
42
|
herokuRepl.on('fatalError', (mcpError) => {
|
|
33
43
|
process.stderr.write(JSON.stringify(mcpError) + '\n');
|
|
34
|
-
process.exit(1);
|
|
35
44
|
});
|
|
45
|
+
// Clean up the Heroku CLI REPL process on shutdown so it is not left running as
|
|
46
|
+
// an orphan when the MCP client terminates the server (W-24704373 / #180).
|
|
47
|
+
const shutdown = () => {
|
|
48
|
+
herokuRepl[Symbol.dispose]();
|
|
49
|
+
process.exit(0);
|
|
50
|
+
};
|
|
51
|
+
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP']) {
|
|
52
|
+
process.once(signal, shutdown);
|
|
53
|
+
}
|
|
36
54
|
// App-related tools
|
|
37
55
|
apps.registerListAppsTool(server, herokuRepl);
|
|
38
56
|
apps.registerGetAppInfoTool(server, herokuRepl);
|
|
@@ -38,17 +38,27 @@ export class HerokuREPL extends EventEmitter {
|
|
|
38
38
|
pauseIteratorResolver;
|
|
39
39
|
commandTimeout;
|
|
40
40
|
commandTimeoutId;
|
|
41
|
+
idleTimeout;
|
|
42
|
+
idleTimeoutId;
|
|
43
|
+
restartTimeoutId;
|
|
44
|
+
pauseIteratorResolved = false;
|
|
45
|
+
resolvedCli;
|
|
41
46
|
userAgent = `Heroku-MCP-Server/${VERSION} (${process.platform}; ${process.arch}; node/${process.version})`;
|
|
42
47
|
/**
|
|
43
48
|
* Create a new HerokuREPL instance
|
|
44
49
|
*
|
|
45
50
|
* @param commandTimeout the timeout for command execution
|
|
51
|
+
* @param idleTimeout ms of inactivity after which the idle REPL process is torn
|
|
52
|
+
* down so it stops consuming CPU (W-24704373 / #180). 0 disables idle teardown.
|
|
46
53
|
*/
|
|
47
|
-
constructor(commandTimeout = 15_000) {
|
|
54
|
+
constructor(commandTimeout = 15_000, idleTimeout = 600_000) {
|
|
48
55
|
super();
|
|
49
56
|
this.commandTimeout = commandTimeout;
|
|
57
|
+
this.idleTimeout = idleTimeout;
|
|
50
58
|
this.pauseQueue();
|
|
51
|
-
|
|
59
|
+
// The REPL process is started lazily on the first command (see executeCommand)
|
|
60
|
+
// and torn down after a period of inactivity, so an idle or never-used server
|
|
61
|
+
// does not keep a Heroku CLI process spinning (W-24704373 / #180).
|
|
52
62
|
this.abortController.signal.addEventListener('abort', this.onAbort);
|
|
53
63
|
}
|
|
54
64
|
/**
|
|
@@ -88,57 +98,83 @@ export class HerokuREPL extends EventEmitter {
|
|
|
88
98
|
if (this.abortController.signal.aborted) {
|
|
89
99
|
throw new Error('REPL process has been aborted and cannot execute commands');
|
|
90
100
|
}
|
|
101
|
+
// A command cancels any pending idle teardown and lazily (re)starts the REPL
|
|
102
|
+
// process if it was never started or was torn down while idle (W-24704373 / #180).
|
|
103
|
+
this.clearIdleTimeout();
|
|
91
104
|
// Replace with Promise.withResolvers once we move to node 22
|
|
92
105
|
let resolver;
|
|
93
106
|
const commandPromise = new Promise((resolve) => {
|
|
94
107
|
resolver = resolve;
|
|
95
108
|
});
|
|
109
|
+
// Enqueue BEFORE (re)starting the process so that a synchronous startup
|
|
110
|
+
// failure (no suitable CLI, spawn throw) can settle this command's promise
|
|
111
|
+
// with an error instead of leaving the caller hanging (see handleFatal).
|
|
96
112
|
this.commandQueue.push({ command, promise: commandPromise, resolver });
|
|
113
|
+
this.ensureProcess();
|
|
97
114
|
this.pauseIteratorResolver?.();
|
|
115
|
+
// Bound the wait even if the process spawns but never becomes ready to
|
|
116
|
+
// dispatch (e.g. it never prints the prompt). Normally the async iterator
|
|
117
|
+
// re-arms this on dispatch; arming here covers the never-ready case so the
|
|
118
|
+
// command times out and restarts instead of hanging forever.
|
|
119
|
+
if (!this.isReady && this.commandQueue.length > 0) {
|
|
120
|
+
this.setCommandTimeout();
|
|
121
|
+
}
|
|
98
122
|
return commandPromise;
|
|
99
123
|
}
|
|
100
124
|
/**
|
|
101
125
|
* Determines the appropriate CLI command and arguments to use for spawning the Heroku REPL process.
|
|
102
|
-
* Checks for npx first, then falls back to heroku CLI >= 10.10.0 if available.
|
|
103
|
-
* Emits a fatalError and returns null if neither is available.
|
|
104
126
|
*
|
|
105
|
-
*
|
|
127
|
+
* Prefers a locally installed, new-enough (>= 10.10.0) `heroku` CLI over `npx` because:
|
|
128
|
+
* - it avoids the npx cold-start penalty (npm registry resolution, often 5-15s) on every spawn,
|
|
129
|
+
* - it works offline and in air-gapped / firewalled corporate environments where
|
|
130
|
+
* `registry.npmjs.org` is unreachable, and
|
|
131
|
+
* - it avoids the Windows `npx` spawn failure where the extracted, extensionless `bin/run`
|
|
132
|
+
* script cannot be spawned without a `.cmd`/`.exe` shim (ENOENT).
|
|
133
|
+
*
|
|
134
|
+
* Falls back to `npx -y heroku@latest repl` only when no suitable local CLI is found
|
|
135
|
+
* (missing, too old, or an unexpected `version` output). Emits a fatalError and returns
|
|
136
|
+
* null only when neither a usable local CLI nor npx is available.
|
|
137
|
+
*
|
|
138
|
+
* @returns An object with cliCommand and cliArgs, or null if no CLI is available.
|
|
106
139
|
*/
|
|
107
140
|
getHerokuCliCommandAndArgs() {
|
|
108
|
-
//
|
|
141
|
+
// Reuse the previously resolved CLI. Idle teardown respawns the process
|
|
142
|
+
// (W-24704373 / #180), and re-running the blocking spawnSync probes on every
|
|
143
|
+
// respawn would add latency to the hot path. The resolution does not change
|
|
144
|
+
// within a process lifetime, so cache the first successful result. Failures
|
|
145
|
+
// are not cached so a transient probe failure can recover on the next call.
|
|
146
|
+
if (this.resolvedCli) {
|
|
147
|
+
return this.resolvedCli;
|
|
148
|
+
}
|
|
149
|
+
// Prefer a locally installed heroku CLI.
|
|
150
|
+
// On Windows, we need to use shell: true to find heroku.cmd
|
|
151
|
+
const herokuCheck = HerokuREPL.spawnSync('heroku', ['version'], {
|
|
152
|
+
encoding: 'utf-8',
|
|
153
|
+
shell: process.platform === 'win32'
|
|
154
|
+
});
|
|
155
|
+
if (!herokuCheck.error && herokuCheck.status === 0 && typeof herokuCheck.stdout === 'string') {
|
|
156
|
+
const versionMatch = herokuCheck.stdout.match(/heroku\/(\d+\.\d+\.\d+)/);
|
|
157
|
+
if (versionMatch) {
|
|
158
|
+
const [major, minor] = versionMatch[1].split('.').map(Number);
|
|
159
|
+
if (major > 10 || (major === 10 && minor >= 10)) {
|
|
160
|
+
this.resolvedCli = { cliCommand: 'heroku', cliArgs: ['repl'] };
|
|
161
|
+
return this.resolvedCli;
|
|
162
|
+
}
|
|
163
|
+
// Local CLI is present but too old for `repl` — fall through to npx@latest.
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Fall back to npx, which fetches the latest Heroku CLI.
|
|
109
167
|
// On Windows, we need to use shell: true to find npx.cmd
|
|
110
168
|
const npxCheck = HerokuREPL.spawnSync('npx', ['--version'], {
|
|
111
169
|
encoding: 'utf-8',
|
|
112
170
|
shell: process.platform === 'win32'
|
|
113
171
|
});
|
|
114
172
|
if (!npxCheck.error && npxCheck.status === 0) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
else {
|
|
118
|
-
// Fallback: check for heroku CLI and version
|
|
119
|
-
const herokuCheck = HerokuREPL.spawnSync('heroku', ['version'], {
|
|
120
|
-
encoding: 'utf-8',
|
|
121
|
-
shell: process.platform === 'win32'
|
|
122
|
-
});
|
|
123
|
-
if (!herokuCheck.error &&
|
|
124
|
-
herokuCheck.status === 0 &&
|
|
125
|
-
typeof herokuCheck.stdout === 'string' &&
|
|
126
|
-
/heroku\/(\d+\.\d+\.\d+)/.test(herokuCheck.stdout)) {
|
|
127
|
-
const versionMatch = herokuCheck.stdout.match(/heroku\/(\d+\.\d+\.\d+)/);
|
|
128
|
-
if (versionMatch) {
|
|
129
|
-
const [major, minor] = versionMatch[1].split('.').map(Number);
|
|
130
|
-
if (major > 10 || (major === 10 && minor >= 10)) {
|
|
131
|
-
return { cliCommand: 'heroku', cliArgs: ['repl'] };
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
this.emit('fatalError', handleCliOutput(`Startup error: Heroku CLI version 10.10.0 or higher is required for this MCP server. Detected version: ${versionMatch[1]}`));
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
this.emit('fatalError', handleCliOutput('Startup error: npx is not installed and Heroku CLI (10.10.0+) is not available in your PATH. Please install one of them.'));
|
|
140
|
-
return null;
|
|
173
|
+
this.resolvedCli = { cliCommand: 'npx', cliArgs: ['-y', 'heroku@latest', 'repl'] };
|
|
174
|
+
return this.resolvedCli;
|
|
141
175
|
}
|
|
176
|
+
this.handleFatal('Startup error: No suitable Heroku CLI found. Install the Heroku CLI (version 10.10.0 or higher) in your PATH, or make npx available. Please install one of them.');
|
|
177
|
+
return null;
|
|
142
178
|
}
|
|
143
179
|
/**
|
|
144
180
|
* Initializes the Heroku CLI process in REPL mode and sets up
|
|
@@ -151,6 +187,8 @@ export class HerokuREPL extends EventEmitter {
|
|
|
151
187
|
*/
|
|
152
188
|
initializeProcess() {
|
|
153
189
|
this.isReady = false;
|
|
190
|
+
this.clearIdleTimeout();
|
|
191
|
+
this.clearRestartTimeout();
|
|
154
192
|
if (this.abortController.signal.aborted) {
|
|
155
193
|
return;
|
|
156
194
|
}
|
|
@@ -160,8 +198,7 @@ export class HerokuREPL extends EventEmitter {
|
|
|
160
198
|
}
|
|
161
199
|
const { cliCommand, cliArgs } = cliInfo;
|
|
162
200
|
if (this.process) {
|
|
163
|
-
this.process
|
|
164
|
-
this.process.kill();
|
|
201
|
+
this.killChild(this.process);
|
|
165
202
|
this.process = undefined;
|
|
166
203
|
}
|
|
167
204
|
try {
|
|
@@ -180,23 +217,27 @@ export class HerokuREPL extends EventEmitter {
|
|
|
180
217
|
});
|
|
181
218
|
}
|
|
182
219
|
catch (err) {
|
|
183
|
-
//
|
|
184
|
-
this.
|
|
220
|
+
// Settle the queued command(s) with the error and surface it; do not exit.
|
|
221
|
+
this.handleFatal(`Startup error: ${err.message}`);
|
|
185
222
|
return;
|
|
186
223
|
}
|
|
187
224
|
this.process.once('error', (err) => {
|
|
188
|
-
this.
|
|
225
|
+
this.handleFatal(`Startup error: ${err.message}`);
|
|
189
226
|
});
|
|
190
227
|
this.process.stdout?.on('data', (data) => {
|
|
191
228
|
const output = data.toString();
|
|
192
|
-
// Detect old CLI warning and
|
|
229
|
+
// Detect old CLI warning and surface it as a fatal error
|
|
193
230
|
if (output.includes('Warning: --repl is not a heroku command.')) {
|
|
194
|
-
this.
|
|
231
|
+
this.handleFatal('Startup error: Your Heroku CLI version does not support --repl mode. Please upgrade to the latest Heroku CLI.');
|
|
195
232
|
return;
|
|
196
233
|
}
|
|
197
234
|
if (output.includes(READY_MESSAGE)) {
|
|
198
235
|
this.isReady = true;
|
|
199
236
|
this.pauseIteratorResolver?.();
|
|
237
|
+
// If the process came up ready with nothing to do, start the idle countdown.
|
|
238
|
+
if (!this.isProcessingCommand && this.commandQueue.length === 0) {
|
|
239
|
+
this.scheduleIdleTimeout();
|
|
240
|
+
}
|
|
200
241
|
}
|
|
201
242
|
this.handleOutput(output);
|
|
202
243
|
});
|
|
@@ -227,8 +268,11 @@ export class HerokuREPL extends EventEmitter {
|
|
|
227
268
|
}
|
|
228
269
|
else {
|
|
229
270
|
// The process was closed normally but this could be
|
|
230
|
-
// from a rogue command or a user aborting the parent process
|
|
231
|
-
|
|
271
|
+
// from a rogue command or a user aborting the parent process.
|
|
272
|
+
// Track the timer so an idle teardown / abort can cancel a pending
|
|
273
|
+
// restart, and unref it so it can't by itself keep Node alive.
|
|
274
|
+
this.restartTimeoutId = setTimeout(() => void this.initializeProcess(), 1000);
|
|
275
|
+
this.restartTimeoutId.unref?.();
|
|
232
276
|
}
|
|
233
277
|
});
|
|
234
278
|
}
|
|
@@ -254,6 +298,8 @@ export class HerokuREPL extends EventEmitter {
|
|
|
254
298
|
}
|
|
255
299
|
if (this.commandQueue.length === 0) {
|
|
256
300
|
this.pauseQueue();
|
|
301
|
+
// Nothing left to run — begin the idle countdown to tear down the process.
|
|
302
|
+
this.scheduleIdleTimeout();
|
|
257
303
|
}
|
|
258
304
|
}
|
|
259
305
|
}
|
|
@@ -262,9 +308,22 @@ export class HerokuREPL extends EventEmitter {
|
|
|
262
308
|
* or for the REPL to be ready for input
|
|
263
309
|
*/
|
|
264
310
|
pauseQueue() {
|
|
311
|
+
// Idempotent on purpose: if the async iterator is already parked on an
|
|
312
|
+
// unresolved pauseIterator promise, keep it. Replacing it would orphan the
|
|
313
|
+
// promise the iterator is awaiting, so a later resolver call would wake a
|
|
314
|
+
// promise nobody is listening to and the queued command would never be
|
|
315
|
+
// written to stdin — the respawn/close hang this class has hit twice
|
|
316
|
+
// (W-24704373). Only mint a fresh promise once the current one has resolved.
|
|
317
|
+
if (this.pauseIterator && !this.pauseIteratorResolved) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
this.pauseIteratorResolved = false;
|
|
265
321
|
// Replace with Promise.withResolvers once we move to node 22
|
|
266
322
|
this.pauseIterator = new Promise((resolve) => {
|
|
267
|
-
this.pauseIteratorResolver =
|
|
323
|
+
this.pauseIteratorResolver = () => {
|
|
324
|
+
this.pauseIteratorResolved = true;
|
|
325
|
+
resolve();
|
|
326
|
+
};
|
|
268
327
|
});
|
|
269
328
|
}
|
|
270
329
|
/**
|
|
@@ -277,15 +336,161 @@ export class HerokuREPL extends EventEmitter {
|
|
|
277
336
|
// in case it's in a bad state or an error occurred
|
|
278
337
|
// that we can't recover from
|
|
279
338
|
void this.initializeProcess();
|
|
280
|
-
|
|
339
|
+
// Settle the head command directly rather than injecting a synthetic END
|
|
340
|
+
// marker through handleOutput: the latter only resolves when a command is
|
|
341
|
+
// actively processing, so a command that was queued but never dispatched
|
|
342
|
+
// (the process spawned but never printed the prompt) would hang.
|
|
343
|
+
this.failHeadCommand(`The command failed to complete in ${this.commandTimeout}ms`);
|
|
281
344
|
}, this.commandTimeout);
|
|
282
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Resolves the command at the head of the queue with the given (error) text
|
|
348
|
+
* and resets per-command state. Used by the command-timeout path so a stalled
|
|
349
|
+
* or never-dispatched command settles instead of hanging.
|
|
350
|
+
*/
|
|
351
|
+
failHeadCommand(message) {
|
|
352
|
+
clearTimeout(this.commandTimeoutId);
|
|
353
|
+
this.isProcessingCommand = false;
|
|
354
|
+
this.buffer = '';
|
|
355
|
+
const head = this.commandQueue.shift();
|
|
356
|
+
head?.resolver(message);
|
|
357
|
+
if (this.commandQueue.length === 0) {
|
|
358
|
+
this.pauseQueue();
|
|
359
|
+
this.scheduleIdleTimeout();
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Surfaces a fatal/startup error and settles every pending command with it, so
|
|
364
|
+
* a transient respawn failure returns an error to the caller instead of
|
|
365
|
+
* hanging the request (the server stays up and can retry on the next command).
|
|
366
|
+
*
|
|
367
|
+
* @param message the error text to surface and resolve pending commands with
|
|
368
|
+
*/
|
|
369
|
+
handleFatal(message) {
|
|
370
|
+
this.emit('fatalError', handleCliOutput(message));
|
|
371
|
+
this.failPendingCommands(message);
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Resolves every queued command with the given (error) text and resets
|
|
375
|
+
* per-command state. The iterator is left parked on its current promise.
|
|
376
|
+
*
|
|
377
|
+
* @param message the text to resolve each pending command with
|
|
378
|
+
*/
|
|
379
|
+
failPendingCommands(message) {
|
|
380
|
+
clearTimeout(this.commandTimeoutId);
|
|
381
|
+
this.isProcessingCommand = false;
|
|
382
|
+
this.buffer = '';
|
|
383
|
+
while (this.commandQueue.length > 0) {
|
|
384
|
+
this.commandQueue.shift()?.resolver(message);
|
|
385
|
+
}
|
|
386
|
+
this.pauseQueue();
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Tears down a child process we own. Detaches only the listeners we added
|
|
390
|
+
* (not Node's internal 'exit' handler, which spawn({signal}) uses to release
|
|
391
|
+
* its AbortSignal listener — removing it leaks one listener + child reference
|
|
392
|
+
* per respawn) and silences late output so a stale 'heroku >' can't mark a new
|
|
393
|
+
* process ready and a late 'error' (e.g. a failed kill) can't go unhandled.
|
|
394
|
+
*
|
|
395
|
+
* @param child the child process to tear down
|
|
396
|
+
*/
|
|
397
|
+
killChild(child) {
|
|
398
|
+
child.removeAllListeners('close');
|
|
399
|
+
child.removeAllListeners('error');
|
|
400
|
+
// Keep an error sink so a post-kill 'error' doesn't become an uncaught throw.
|
|
401
|
+
child.on('error', () => { });
|
|
402
|
+
child.stdout?.removeAllListeners('data');
|
|
403
|
+
child.stderr?.removeAllListeners('data');
|
|
404
|
+
if (process.platform === 'win32' && typeof child.pid === 'number') {
|
|
405
|
+
// shell: true on Windows means `child` is cmd.exe; child.kill() leaves the
|
|
406
|
+
// heroku/node grandchild running. Kill the whole tree (W-24704374).
|
|
407
|
+
try {
|
|
408
|
+
HerokuREPL.spawnSync('taskkill', ['/pid', String(child.pid), '/t', '/f']);
|
|
409
|
+
}
|
|
410
|
+
catch {
|
|
411
|
+
// Fall through to child.kill() below.
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
child.kill();
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Clears any pending unexpected-close restart timer.
|
|
418
|
+
*/
|
|
419
|
+
clearRestartTimeout() {
|
|
420
|
+
clearTimeout(this.restartTimeoutId);
|
|
421
|
+
this.restartTimeoutId = undefined;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Ensures a REPL process exists, lazily (re)starting it on demand.
|
|
425
|
+
*
|
|
426
|
+
* The process is not started at construction time and is torn down after a
|
|
427
|
+
* period of inactivity, so this is called when a command arrives to bring the
|
|
428
|
+
* process back up if it is not currently running (W-24704373 / #180).
|
|
429
|
+
*/
|
|
430
|
+
ensureProcess() {
|
|
431
|
+
if (!this.process && !this.abortController.signal.aborted) {
|
|
432
|
+
this.initializeProcess();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* (Re)starts the idle teardown timer. When it fires, an idle REPL process is
|
|
437
|
+
* killed so it stops consuming CPU (W-24704373 / #180). A value of 0 (or less)
|
|
438
|
+
* disables idle teardown entirely.
|
|
439
|
+
*/
|
|
440
|
+
scheduleIdleTimeout() {
|
|
441
|
+
this.clearIdleTimeout();
|
|
442
|
+
if (this.idleTimeout > 0) {
|
|
443
|
+
this.idleTimeoutId = setTimeout(() => this.teardownIdleProcess(), this.idleTimeout);
|
|
444
|
+
// Don't let the idle timer by itself keep the Node.js process alive.
|
|
445
|
+
this.idleTimeoutId.unref?.();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Clears any pending idle teardown timer.
|
|
450
|
+
*/
|
|
451
|
+
clearIdleTimeout() {
|
|
452
|
+
clearTimeout(this.idleTimeoutId);
|
|
453
|
+
this.idleTimeoutId = undefined;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Tears down the REPL process once it has been idle for the configured timeout.
|
|
457
|
+
* The process is killed (not aborted) so it can be lazily restarted by the next
|
|
458
|
+
* command. Does nothing if a command is in flight or still queued.
|
|
459
|
+
*/
|
|
460
|
+
teardownIdleProcess() {
|
|
461
|
+
if (this.isProcessingCommand || this.commandQueue.length > 0) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
this.isReady = false;
|
|
465
|
+
// Cancel any pending unexpected-close restart so it can't respawn the
|
|
466
|
+
// process we are about to intentionally tear down.
|
|
467
|
+
this.clearRestartTimeout();
|
|
468
|
+
// NB: do NOT call pauseQueue() here. The async iterator is already parked on
|
|
469
|
+
// the current `pauseIterator` promise (created when the last command drained
|
|
470
|
+
// the queue). pauseQueue() is now idempotent and would no-op, but leaving the
|
|
471
|
+
// parked promise untouched makes the intent explicit: the next executeCommand
|
|
472
|
+
// resolves the exact promise the iterator is awaiting, so the respawned
|
|
473
|
+
// process receives the queued command instead of hanging (W-24704373).
|
|
474
|
+
if (this.process) {
|
|
475
|
+
// killChild detaches our listeners first so the 'close' handler does not
|
|
476
|
+
// treat this intentional teardown as an unexpected exit and restart.
|
|
477
|
+
this.killChild(this.process);
|
|
478
|
+
this.process = undefined;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
283
481
|
/**
|
|
284
482
|
* Handles the abort event
|
|
285
483
|
*/
|
|
286
484
|
onAbort = () => {
|
|
287
|
-
|
|
485
|
+
// Settle any in-flight/queued commands so their awaiters don't hang when the
|
|
486
|
+
// instance is disposed. (The signal-handler dispose path exits the process,
|
|
487
|
+
// but a programmatic dispose should not strand pending callers.)
|
|
488
|
+
while (this.commandQueue.length > 0) {
|
|
489
|
+
this.commandQueue.shift()?.resolver('The REPL process was shut down before the command completed.');
|
|
490
|
+
}
|
|
288
491
|
this.pauseIteratorResolver?.();
|
|
289
492
|
clearTimeout(this.commandTimeoutId);
|
|
493
|
+
this.clearRestartTimeout();
|
|
494
|
+
this.clearIdleTimeout();
|
|
290
495
|
};
|
|
291
496
|
}
|
|
@@ -22,7 +22,12 @@ describe('HerokuREPL', () => {
|
|
|
22
22
|
})();
|
|
23
23
|
// Stub the spawn function
|
|
24
24
|
spawnStub = sinon.stub(HerokuREPL, 'spawn').returns(mockProcess);
|
|
25
|
-
|
|
25
|
+
// Idle teardown disabled (0) for the shared fixture so these tests are not
|
|
26
|
+
// affected by the idle timer; the dedicated idle tests opt back in.
|
|
27
|
+
repl = new HerokuREPL(500, 0);
|
|
28
|
+
// The process is started lazily. Most tests below assume
|
|
29
|
+
// a spawned, ready process, so prime one explicitly here.
|
|
30
|
+
repl['initializeProcess']();
|
|
26
31
|
mockProcess.stdout.emit('data', 'heroku >');
|
|
27
32
|
(async () => {
|
|
28
33
|
for await (const command of repl) {
|
|
@@ -140,6 +145,118 @@ describe('HerokuREPL', () => {
|
|
|
140
145
|
expect(repl['abortController'].signal.aborted).to.be.true;
|
|
141
146
|
});
|
|
142
147
|
});
|
|
148
|
+
describe('lazy start and idle teardown', () => {
|
|
149
|
+
// These tests construct their own REPL instances so they can observe spawn
|
|
150
|
+
// behavior from a clean slate, independent of the primed shared fixture.
|
|
151
|
+
it('does not spawn a process at construction', () => {
|
|
152
|
+
repl[Symbol.dispose]();
|
|
153
|
+
spawnStub.resetHistory();
|
|
154
|
+
const lazyRepl = new HerokuREPL(500, 0);
|
|
155
|
+
expect(spawnStub.called).to.be.false;
|
|
156
|
+
expect(lazyRepl['process']).to.be.undefined;
|
|
157
|
+
lazyRepl[Symbol.dispose]();
|
|
158
|
+
});
|
|
159
|
+
it('spawns the process lazily on the first executeCommand', () => {
|
|
160
|
+
repl[Symbol.dispose]();
|
|
161
|
+
spawnStub.resetHistory();
|
|
162
|
+
const lazyRepl = new HerokuREPL(500, 0);
|
|
163
|
+
void (async () => {
|
|
164
|
+
for await (const command of lazyRepl) {
|
|
165
|
+
// drain
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
168
|
+
void lazyRepl.executeCommand('apps');
|
|
169
|
+
expect(spawnStub.calledOnce).to.be.true;
|
|
170
|
+
expect(lazyRepl['process']).to.not.be.undefined;
|
|
171
|
+
lazyRepl[Symbol.dispose]();
|
|
172
|
+
});
|
|
173
|
+
it('tears down a ready, idle process after the idle timeout', () => {
|
|
174
|
+
repl[Symbol.dispose]();
|
|
175
|
+
const clock = sinon.useFakeTimers();
|
|
176
|
+
spawnStub.resetHistory();
|
|
177
|
+
mockProcess.kill.resetHistory();
|
|
178
|
+
const idleRepl = new HerokuREPL(500, 1000);
|
|
179
|
+
idleRepl['initializeProcess']();
|
|
180
|
+
expect(spawnStub.calledOnce).to.be.true;
|
|
181
|
+
// Process becomes ready with an empty queue -> schedules idle teardown.
|
|
182
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
183
|
+
expect(mockProcess.kill.called).to.be.false;
|
|
184
|
+
clock.tick(1001);
|
|
185
|
+
expect(mockProcess.kill.called).to.be.true;
|
|
186
|
+
expect(idleRepl['process']).to.be.undefined;
|
|
187
|
+
expect(idleRepl['isReady']).to.be.false;
|
|
188
|
+
clock.restore();
|
|
189
|
+
idleRepl[Symbol.dispose]();
|
|
190
|
+
});
|
|
191
|
+
it('does not tear down while a command is queued or in flight', () => {
|
|
192
|
+
repl[Symbol.dispose]();
|
|
193
|
+
const clock = sinon.useFakeTimers();
|
|
194
|
+
spawnStub.resetHistory();
|
|
195
|
+
mockProcess.kill.resetHistory();
|
|
196
|
+
const idleRepl = new HerokuREPL(500, 1000);
|
|
197
|
+
idleRepl['initializeProcess']();
|
|
198
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
199
|
+
// Queue work after the idle timer was armed; the pending timer must not
|
|
200
|
+
// kill a process that now has work to do.
|
|
201
|
+
void idleRepl.executeCommand('apps');
|
|
202
|
+
clock.tick(1001);
|
|
203
|
+
expect(mockProcess.kill.called).to.be.false;
|
|
204
|
+
expect(idleRepl['process']).to.not.be.undefined;
|
|
205
|
+
clock.restore();
|
|
206
|
+
idleRepl[Symbol.dispose]();
|
|
207
|
+
});
|
|
208
|
+
it('respawns the process on the next command after an idle teardown', () => {
|
|
209
|
+
repl[Symbol.dispose]();
|
|
210
|
+
spawnStub.resetHistory();
|
|
211
|
+
const idleRepl = new HerokuREPL(500, 1000);
|
|
212
|
+
idleRepl['initializeProcess']();
|
|
213
|
+
expect(spawnStub.calledOnce).to.be.true;
|
|
214
|
+
// Simulate an idle teardown.
|
|
215
|
+
idleRepl['teardownIdleProcess']();
|
|
216
|
+
expect(idleRepl['process']).to.be.undefined;
|
|
217
|
+
spawnStub.resetHistory();
|
|
218
|
+
void (async () => {
|
|
219
|
+
for await (const command of idleRepl) {
|
|
220
|
+
// drain
|
|
221
|
+
}
|
|
222
|
+
})();
|
|
223
|
+
void idleRepl.executeCommand('apps');
|
|
224
|
+
expect(spawnStub.calledOnce).to.be.true;
|
|
225
|
+
expect(idleRepl['process']).to.not.be.undefined;
|
|
226
|
+
idleRepl[Symbol.dispose]();
|
|
227
|
+
});
|
|
228
|
+
it('delivers and resolves a command issued after an idle teardown', async () => {
|
|
229
|
+
// Tearing the process down must not swap out the pauseIterator promise the
|
|
230
|
+
// async iterator is parked on, otherwise the next command's resolver wakes a
|
|
231
|
+
// promise nobody is awaiting and the respawned process never receives the
|
|
232
|
+
// queued command — the request would hang forever.
|
|
233
|
+
repl[Symbol.dispose]();
|
|
234
|
+
spawnStub.resetHistory();
|
|
235
|
+
// Auto-idle disabled (0); we drive teardown manually to control timing.
|
|
236
|
+
const idleRepl = new HerokuREPL(500, 0);
|
|
237
|
+
idleRepl['initializeProcess']();
|
|
238
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
239
|
+
void (async () => {
|
|
240
|
+
for await (const command of idleRepl) {
|
|
241
|
+
// drain
|
|
242
|
+
}
|
|
243
|
+
})();
|
|
244
|
+
// First command completes normally.
|
|
245
|
+
const first = idleRepl.executeCommand('apps');
|
|
246
|
+
setTimeout(() => mockProcess.stdout.emit('data', 'first output\n<<<END RESULTS>>>'));
|
|
247
|
+
expect(await first).to.include('first output');
|
|
248
|
+
// Simulate the idle timer firing while the queue is empty.
|
|
249
|
+
idleRepl['teardownIdleProcess']();
|
|
250
|
+
expect(idleRepl['process']).to.be.undefined;
|
|
251
|
+
// A command issued after teardown must respawn the process AND actually
|
|
252
|
+
// resolve — not hang. The respawned process signals ready, then returns.
|
|
253
|
+
const second = idleRepl.executeCommand('addons');
|
|
254
|
+
setTimeout(() => mockProcess.stdout.emit('data', 'heroku >'), 0);
|
|
255
|
+
setTimeout(() => mockProcess.stdout.emit('data', 'second output\n<<<END RESULTS>>>'), 10);
|
|
256
|
+
expect(await second).to.include('second output');
|
|
257
|
+
idleRepl[Symbol.dispose]();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
143
260
|
describe('MCP startup error handling', () => {
|
|
144
261
|
it('should emit fatalError if spawn throws', () => {
|
|
145
262
|
spawnStub.restore();
|
|
@@ -173,6 +290,138 @@ describe('HerokuREPL', () => {
|
|
|
173
290
|
expect(mcpError.content[0].text).to.include('Your Heroku CLI version does not support --repl mode');
|
|
174
291
|
});
|
|
175
292
|
});
|
|
293
|
+
describe('respawn and failure hardening', () => {
|
|
294
|
+
it('resolves a command issued after an unexpected close re-parks the iterator', async () => {
|
|
295
|
+
// The 'close' handler calls pauseQueue() while the async iterator is parked
|
|
296
|
+
// on the current pauseIterator promise. pauseQueue() must be idempotent, or
|
|
297
|
+
// it orphans that promise and the post-close command never reaches stdin.
|
|
298
|
+
repl[Symbol.dispose]();
|
|
299
|
+
spawnStub.resetHistory();
|
|
300
|
+
const r = new HerokuREPL(500, 0);
|
|
301
|
+
r['initializeProcess']();
|
|
302
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
303
|
+
void (async () => {
|
|
304
|
+
for await (const command of r) {
|
|
305
|
+
// drain
|
|
306
|
+
}
|
|
307
|
+
})();
|
|
308
|
+
// First command completes, draining the queue and parking the iterator.
|
|
309
|
+
const first = r.executeCommand('apps');
|
|
310
|
+
setTimeout(() => mockProcess.stdout.emit('data', 'first\n<<<END RESULTS>>>'));
|
|
311
|
+
expect(await first).to.include('first');
|
|
312
|
+
// Unexpected close (code 1) restarts the process and calls pauseQueue().
|
|
313
|
+
mockProcess.emit('close', 1);
|
|
314
|
+
// Respawned process signals ready.
|
|
315
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
316
|
+
// A command issued after the close must reach stdin and actually resolve.
|
|
317
|
+
const second = r.executeCommand('addons');
|
|
318
|
+
setTimeout(() => mockProcess.stdout.emit('data', 'second\n<<<END RESULTS>>>'));
|
|
319
|
+
expect(await second).to.include('second');
|
|
320
|
+
r[Symbol.dispose]();
|
|
321
|
+
});
|
|
322
|
+
it('settles a queued command instead of hanging when no CLI is available', async () => {
|
|
323
|
+
repl[Symbol.dispose]();
|
|
324
|
+
const spawnSyncStub = sinon.stub(HerokuREPL, 'spawnSync').returns({
|
|
325
|
+
error: new Error('not found'),
|
|
326
|
+
status: 1,
|
|
327
|
+
stdout: ''
|
|
328
|
+
});
|
|
329
|
+
const r = new HerokuREPL(500, 0);
|
|
330
|
+
const fatalSpy = sinon.spy();
|
|
331
|
+
r.on('fatalError', fatalSpy);
|
|
332
|
+
void (async () => {
|
|
333
|
+
for await (const command of r) {
|
|
334
|
+
// drain
|
|
335
|
+
}
|
|
336
|
+
})();
|
|
337
|
+
const result = await r.executeCommand('apps');
|
|
338
|
+
expect(result).to.include('No suitable Heroku CLI found');
|
|
339
|
+
expect(fatalSpy.called).to.be.true;
|
|
340
|
+
r[Symbol.dispose]();
|
|
341
|
+
spawnSyncStub.restore();
|
|
342
|
+
});
|
|
343
|
+
it('settles a queued command instead of hanging when spawn throws', async () => {
|
|
344
|
+
repl[Symbol.dispose]();
|
|
345
|
+
spawnStub.restore();
|
|
346
|
+
const badSpawn = sinon.stub(HerokuREPL, 'spawn').throws(new Error('spawn boom'));
|
|
347
|
+
const r = new HerokuREPL(500, 0);
|
|
348
|
+
const fatalSpy = sinon.spy();
|
|
349
|
+
r.on('fatalError', fatalSpy);
|
|
350
|
+
void (async () => {
|
|
351
|
+
for await (const command of r) {
|
|
352
|
+
// drain
|
|
353
|
+
}
|
|
354
|
+
})();
|
|
355
|
+
const result = await r.executeCommand('apps');
|
|
356
|
+
expect(result).to.include('spawn boom');
|
|
357
|
+
expect(fatalSpy.called).to.be.true;
|
|
358
|
+
r[Symbol.dispose]();
|
|
359
|
+
badSpawn.restore();
|
|
360
|
+
});
|
|
361
|
+
it('settles a command if the process spawns but never becomes ready', async () => {
|
|
362
|
+
repl[Symbol.dispose]();
|
|
363
|
+
spawnStub.resetHistory();
|
|
364
|
+
const r = new HerokuREPL(500, 0);
|
|
365
|
+
r['initializeProcess']();
|
|
366
|
+
void (async () => {
|
|
367
|
+
for await (const command of r) {
|
|
368
|
+
// drain
|
|
369
|
+
}
|
|
370
|
+
})();
|
|
371
|
+
// Never emit 'heroku >': the process is up but never ready to dispatch.
|
|
372
|
+
const result = await r.executeCommand('apps');
|
|
373
|
+
expect(result).to.include('The command failed to complete in 500ms');
|
|
374
|
+
r[Symbol.dispose]();
|
|
375
|
+
});
|
|
376
|
+
it('resolves the CLI once and reuses it across respawns', () => {
|
|
377
|
+
repl[Symbol.dispose]();
|
|
378
|
+
const spawnSyncStub = sinon.stub(HerokuREPL, 'spawnSync');
|
|
379
|
+
spawnSyncStub.withArgs('heroku', ['version'], sinon.match.any).returns({
|
|
380
|
+
error: null,
|
|
381
|
+
status: 0,
|
|
382
|
+
stdout: 'heroku/11.0.0 (darwin-x64) node-v18.17.0'
|
|
383
|
+
});
|
|
384
|
+
const r = new HerokuREPL(500, 0);
|
|
385
|
+
r['initializeProcess']();
|
|
386
|
+
r['initializeProcess']();
|
|
387
|
+
r['initializeProcess']();
|
|
388
|
+
const herokuProbes = spawnSyncStub.getCalls().filter((c) => c.args[0] === 'heroku').length;
|
|
389
|
+
expect(herokuProbes).to.equal(1);
|
|
390
|
+
r[Symbol.dispose]();
|
|
391
|
+
spawnSyncStub.restore();
|
|
392
|
+
});
|
|
393
|
+
it('ignores late stdout from a torn-down process', () => {
|
|
394
|
+
repl[Symbol.dispose]();
|
|
395
|
+
spawnStub.resetHistory();
|
|
396
|
+
const r = new HerokuREPL(500, 0);
|
|
397
|
+
r['initializeProcess']();
|
|
398
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
399
|
+
expect(r['isReady']).to.be.true;
|
|
400
|
+
r['teardownIdleProcess']();
|
|
401
|
+
expect(r['isReady']).to.be.false;
|
|
402
|
+
// Late output from the killed child must not revive readiness.
|
|
403
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
404
|
+
expect(r['isReady']).to.be.false;
|
|
405
|
+
r[Symbol.dispose]();
|
|
406
|
+
});
|
|
407
|
+
it('settles pending commands when the instance is disposed mid-flight', async () => {
|
|
408
|
+
repl[Symbol.dispose]();
|
|
409
|
+
spawnStub.resetHistory();
|
|
410
|
+
const r = new HerokuREPL(500, 0);
|
|
411
|
+
r['initializeProcess']();
|
|
412
|
+
mockProcess.stdout.emit('data', 'heroku >');
|
|
413
|
+
void (async () => {
|
|
414
|
+
for await (const command of r) {
|
|
415
|
+
// drain
|
|
416
|
+
}
|
|
417
|
+
})();
|
|
418
|
+
const pending = r.executeCommand('apps');
|
|
419
|
+
// Dispose before the command completes.
|
|
420
|
+
r[Symbol.dispose]();
|
|
421
|
+
const result = await pending;
|
|
422
|
+
expect(result).to.include('shut down before the command completed');
|
|
423
|
+
});
|
|
424
|
+
});
|
|
176
425
|
describe('getHerokuCliCommandAndArgs', () => {
|
|
177
426
|
let spawnSyncStub;
|
|
178
427
|
let fatalSpy;
|
|
@@ -180,11 +429,22 @@ describe('HerokuREPL', () => {
|
|
|
180
429
|
spawnSyncStub = sinon.stub(HerokuREPL, 'spawnSync');
|
|
181
430
|
fatalSpy = sinon.spy();
|
|
182
431
|
repl.on('fatalError', fatalSpy);
|
|
432
|
+
// The shared fixture already resolved (and now caches) a CLI via the real
|
|
433
|
+
// spawnSync in its beforeEach. Clear that cache so these tests exercise the
|
|
434
|
+
// resolver against the stub rather than returning the cached real result.
|
|
435
|
+
repl['resolvedCli'] = undefined;
|
|
183
436
|
});
|
|
184
437
|
afterEach(() => {
|
|
185
438
|
spawnSyncStub.restore();
|
|
186
439
|
});
|
|
187
|
-
it('should
|
|
440
|
+
it('should prefer the local heroku CLI (>= 10.10.0) over npx, without checking npx', () => {
|
|
441
|
+
// heroku CLI available with version >= 10.10.0
|
|
442
|
+
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
443
|
+
error: null,
|
|
444
|
+
status: 0,
|
|
445
|
+
stdout: 'heroku/10.15.0 (darwin-x64) node-v18.17.0'
|
|
446
|
+
});
|
|
447
|
+
// npx is also available, but must not be used when a suitable local CLI exists
|
|
188
448
|
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
189
449
|
error: null,
|
|
190
450
|
status: 0,
|
|
@@ -192,23 +452,18 @@ describe('HerokuREPL', () => {
|
|
|
192
452
|
});
|
|
193
453
|
const result = repl['getHerokuCliCommandAndArgs']();
|
|
194
454
|
expect(result).to.deep.equal({
|
|
195
|
-
cliCommand: '
|
|
196
|
-
cliArgs: ['
|
|
455
|
+
cliCommand: 'heroku',
|
|
456
|
+
cliArgs: ['repl']
|
|
197
457
|
});
|
|
458
|
+
// npx must not even be probed when a suitable local CLI is found
|
|
459
|
+
expect(spawnSyncStub.calledWith('npx', ['--version'], sinon.match.any)).to.be.false;
|
|
198
460
|
expect(fatalSpy.called).to.be.false;
|
|
199
461
|
});
|
|
200
|
-
it('should
|
|
201
|
-
// npx not available
|
|
202
|
-
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
203
|
-
error: new Error('npx not found'),
|
|
204
|
-
status: 1,
|
|
205
|
-
stdout: ''
|
|
206
|
-
});
|
|
207
|
-
// heroku CLI available with version >= 10.10.0
|
|
462
|
+
it('should prefer the local heroku CLI when it is > 10.x', () => {
|
|
208
463
|
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
209
464
|
error: null,
|
|
210
465
|
status: 0,
|
|
211
|
-
stdout: 'heroku/
|
|
466
|
+
stdout: 'heroku/11.0.0 (darwin-x64) node-v18.17.0'
|
|
212
467
|
});
|
|
213
468
|
const result = repl['getHerokuCliCommandAndArgs']();
|
|
214
469
|
expect(result).to.deep.equal({
|
|
@@ -217,94 +472,96 @@ describe('HerokuREPL', () => {
|
|
|
217
472
|
});
|
|
218
473
|
expect(fatalSpy.called).to.be.false;
|
|
219
474
|
});
|
|
220
|
-
it('should
|
|
221
|
-
//
|
|
222
|
-
spawnSyncStub.withArgs('
|
|
223
|
-
error: new Error('
|
|
475
|
+
it('should fall back to npx when heroku CLI is not available', () => {
|
|
476
|
+
// heroku CLI not available
|
|
477
|
+
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
478
|
+
error: new Error('heroku not found'),
|
|
224
479
|
status: 1,
|
|
225
480
|
stdout: ''
|
|
226
481
|
});
|
|
227
|
-
//
|
|
228
|
-
spawnSyncStub.withArgs('
|
|
482
|
+
// npx available
|
|
483
|
+
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
229
484
|
error: null,
|
|
230
485
|
status: 0,
|
|
231
|
-
stdout: '
|
|
486
|
+
stdout: '9.8.1'
|
|
232
487
|
});
|
|
233
488
|
const result = repl['getHerokuCliCommandAndArgs']();
|
|
234
489
|
expect(result).to.deep.equal({
|
|
235
|
-
cliCommand: '
|
|
236
|
-
cliArgs: ['repl']
|
|
490
|
+
cliCommand: 'npx',
|
|
491
|
+
cliArgs: ['-y', 'heroku@latest', 'repl']
|
|
237
492
|
});
|
|
238
493
|
expect(fatalSpy.called).to.be.false;
|
|
239
494
|
});
|
|
240
|
-
it('should
|
|
241
|
-
//
|
|
242
|
-
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
243
|
-
error: new Error('npx not found'),
|
|
244
|
-
status: 1,
|
|
245
|
-
stdout: ''
|
|
246
|
-
});
|
|
247
|
-
// heroku CLI available but version < 10.10.0
|
|
495
|
+
it('should fall back to npx when the local heroku CLI is too old (< 10.10.0)', () => {
|
|
496
|
+
// heroku CLI available but version < 10.10.0 — too old for `repl`
|
|
248
497
|
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
249
498
|
error: null,
|
|
250
499
|
status: 0,
|
|
251
500
|
stdout: 'heroku/10.9.0 (darwin-x64) node-v18.17.0'
|
|
252
501
|
});
|
|
253
|
-
|
|
254
|
-
expect(result).to.be.null;
|
|
255
|
-
expect(fatalSpy.called).to.be.true;
|
|
256
|
-
const mcpError = fatalSpy.firstCall.args[0];
|
|
257
|
-
expect(mcpError.content[0].text).to.include('Startup error: Heroku CLI version 10.10.0 or higher is required');
|
|
258
|
-
expect(mcpError.content[0].text).to.include('Detected version: 10.9.0');
|
|
259
|
-
});
|
|
260
|
-
it('should emit fatalError and return null when npx is not available and heroku CLI is not available', () => {
|
|
261
|
-
// npx not available
|
|
502
|
+
// npx available
|
|
262
503
|
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
263
|
-
error:
|
|
264
|
-
status:
|
|
265
|
-
stdout: ''
|
|
266
|
-
});
|
|
267
|
-
// heroku CLI not available
|
|
268
|
-
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
269
|
-
error: new Error('heroku not found'),
|
|
270
|
-
status: 1,
|
|
271
|
-
stdout: ''
|
|
504
|
+
error: null,
|
|
505
|
+
status: 0,
|
|
506
|
+
stdout: '9.8.1'
|
|
272
507
|
});
|
|
273
508
|
const result = repl['getHerokuCliCommandAndArgs']();
|
|
274
|
-
expect(result).to.
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
expect(mcpError.content[0].text).to.include('Startup error: npx is not installed and Heroku CLI (10.10.0+) is not available');
|
|
278
|
-
});
|
|
279
|
-
it('should emit fatalError and return null when npx is not available and heroku CLI version format is unexpected', () => {
|
|
280
|
-
// npx not available
|
|
281
|
-
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
282
|
-
error: new Error('npx not found'),
|
|
283
|
-
status: 1,
|
|
284
|
-
stdout: ''
|
|
509
|
+
expect(result).to.deep.equal({
|
|
510
|
+
cliCommand: 'npx',
|
|
511
|
+
cliArgs: ['-y', 'heroku@latest', 'repl']
|
|
285
512
|
});
|
|
513
|
+
expect(fatalSpy.called).to.be.false;
|
|
514
|
+
});
|
|
515
|
+
it('should fall back to npx when the heroku CLI version format is unexpected', () => {
|
|
286
516
|
// heroku CLI available but version format is unexpected
|
|
287
517
|
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
288
518
|
error: null,
|
|
289
519
|
status: 0,
|
|
290
520
|
stdout: 'some unexpected output without version'
|
|
291
521
|
});
|
|
522
|
+
// npx available
|
|
523
|
+
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
524
|
+
error: null,
|
|
525
|
+
status: 0,
|
|
526
|
+
stdout: '9.8.1'
|
|
527
|
+
});
|
|
292
528
|
const result = repl['getHerokuCliCommandAndArgs']();
|
|
293
|
-
expect(result).to.
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
529
|
+
expect(result).to.deep.equal({
|
|
530
|
+
cliCommand: 'npx',
|
|
531
|
+
cliArgs: ['-y', 'heroku@latest', 'repl']
|
|
532
|
+
});
|
|
533
|
+
expect(fatalSpy.called).to.be.false;
|
|
297
534
|
});
|
|
298
|
-
it('should
|
|
299
|
-
//
|
|
535
|
+
it('should fall back to npx when the heroku CLI stdout is not a string', () => {
|
|
536
|
+
// heroku CLI available but stdout is not a string
|
|
537
|
+
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
538
|
+
error: null,
|
|
539
|
+
status: 0,
|
|
540
|
+
stdout: Buffer.from('heroku/10.15.0')
|
|
541
|
+
});
|
|
542
|
+
// npx available
|
|
300
543
|
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
301
|
-
error:
|
|
544
|
+
error: null,
|
|
545
|
+
status: 0,
|
|
546
|
+
stdout: '9.8.1'
|
|
547
|
+
});
|
|
548
|
+
const result = repl['getHerokuCliCommandAndArgs']();
|
|
549
|
+
expect(result).to.deep.equal({
|
|
550
|
+
cliCommand: 'npx',
|
|
551
|
+
cliArgs: ['-y', 'heroku@latest', 'repl']
|
|
552
|
+
});
|
|
553
|
+
expect(fatalSpy.called).to.be.false;
|
|
554
|
+
});
|
|
555
|
+
it('should emit fatalError and return null when neither heroku CLI nor npx is available', () => {
|
|
556
|
+
// heroku CLI not available
|
|
557
|
+
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
558
|
+
error: new Error('heroku not found'),
|
|
302
559
|
status: 1,
|
|
303
560
|
stdout: ''
|
|
304
561
|
});
|
|
305
|
-
//
|
|
306
|
-
spawnSyncStub.withArgs('
|
|
307
|
-
error: new Error('
|
|
562
|
+
// npx not available
|
|
563
|
+
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
564
|
+
error: new Error('npx not found'),
|
|
308
565
|
status: 1,
|
|
309
566
|
stdout: ''
|
|
310
567
|
});
|
|
@@ -312,26 +569,26 @@ describe('HerokuREPL', () => {
|
|
|
312
569
|
expect(result).to.be.null;
|
|
313
570
|
expect(fatalSpy.called).to.be.true;
|
|
314
571
|
const mcpError = fatalSpy.firstCall.args[0];
|
|
315
|
-
expect(mcpError.content[0].text).to.include('Startup error:
|
|
572
|
+
expect(mcpError.content[0].text).to.include('Startup error: No suitable Heroku CLI found');
|
|
316
573
|
});
|
|
317
|
-
it('should emit fatalError and return null when
|
|
574
|
+
it('should emit fatalError and return null when a too-old heroku CLI and no npx are available', () => {
|
|
575
|
+
// heroku CLI present but too old
|
|
576
|
+
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
577
|
+
error: null,
|
|
578
|
+
status: 0,
|
|
579
|
+
stdout: 'heroku/10.9.0 (darwin-x64) node-v18.17.0'
|
|
580
|
+
});
|
|
318
581
|
// npx not available
|
|
319
582
|
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
320
583
|
error: new Error('npx not found'),
|
|
321
584
|
status: 1,
|
|
322
585
|
stdout: ''
|
|
323
586
|
});
|
|
324
|
-
// heroku CLI available but stdout is not a string
|
|
325
|
-
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
|
|
326
|
-
error: null,
|
|
327
|
-
status: 0,
|
|
328
|
-
stdout: Buffer.from('heroku/10.15.0')
|
|
329
|
-
});
|
|
330
587
|
const result = repl['getHerokuCliCommandAndArgs']();
|
|
331
588
|
expect(result).to.be.null;
|
|
332
589
|
expect(fatalSpy.called).to.be.true;
|
|
333
590
|
const mcpError = fatalSpy.firstCall.args[0];
|
|
334
|
-
expect(mcpError.content[0].text).to.include('Startup error:
|
|
591
|
+
expect(mcpError.content[0].text).to.include('Startup error: No suitable Heroku CLI found');
|
|
335
592
|
});
|
|
336
593
|
});
|
|
337
594
|
});
|
package/dist/tools/ai.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heroku/mcp-server",
|
|
3
3
|
"description": "Heroku Platform MCP Server",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.13",
|
|
5
5
|
"author": "Heroku",
|
|
6
6
|
"bugs": "https://github.com/heroku/heroku-mcp-server/issues",
|
|
7
7
|
"bin": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"jsonschema": "^1.5.0",
|
|
16
16
|
"node-fetch": "^3.3.2",
|
|
17
17
|
"tar-stream": "^3.1.7",
|
|
18
|
-
"zod": "^
|
|
18
|
+
"zod": "^4.6.7",
|
|
19
19
|
"zod-to-json-schema": "^3.25.1"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"prepublishOnly": "npm run build && npm run build:mcpb"
|
|
89
89
|
},
|
|
90
90
|
"overrides": {
|
|
91
|
-
"serialize-javascript": "^7.
|
|
91
|
+
"serialize-javascript": "^7.1.4",
|
|
92
92
|
"tmp": "^0.2.7"
|
|
93
93
|
}
|
|
94
94
|
}
|