mcp-server-commands 0.6.0 → 0.7.2
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 -5
- package/build/always_log.js +5 -1
- package/build/decide/spawn-sync.js +31 -0
- package/build/decide/spawn.js +92 -0
- package/build/exec-utils.js +65 -55
- package/build/messages.js +49 -20
- package/build/prompts.js +17 -21
- package/build/run_process.js +90 -0
- package/build/tools.js +60 -15
- package/package.json +5 -2
- package/build/run-command.js +0 -61
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
## Tools
|
|
2
2
|
|
|
3
|
-
Tools are for LLMs to request. Claude Sonnet 3.5 intelligently uses `
|
|
3
|
+
Tools are for LLMs to request. Claude Sonnet 3.5 intelligently uses `run_process`. And, initial testing shows promising results with [Groq Desktop with MCP](https://github.com/groq/groq-desktop-beta) and `llama4` models.
|
|
4
4
|
|
|
5
5
|
Currently, just one command to rule them all!
|
|
6
6
|
|
|
7
|
-
- `
|
|
7
|
+
- `run_process` - run a command, i.e. `hostname` or `ls -al` or `echo "hello world"` etc
|
|
8
8
|
- Returns `STDOUT` and `STDERR` as text
|
|
9
9
|
- Optional `stdin` parameter means your LLM can
|
|
10
|
-
- pass
|
|
10
|
+
- pass scripts over `STDIN` to commands like `fish`, `bash`, `zsh`, `python`
|
|
11
11
|
- create files with `cat >> foo/bar.txt` from the text in `stdin`
|
|
12
12
|
|
|
13
13
|
> [!WARNING]
|
|
@@ -24,7 +24,9 @@ Currently, just one command to rule them all!
|
|
|
24
24
|
|
|
25
25
|
Prompts are for users to include in chat history, i.e. via `Zed`'s slash commands (in its AI Chat panel)
|
|
26
26
|
|
|
27
|
-
- `
|
|
27
|
+
- `run_process` - generate a prompt message with the command output
|
|
28
|
+
|
|
29
|
+
* FYI this was mostly a learning exercise... I see this as a user requested tool call. That's a fancy way to say, it's a template for running a command and passing the outputs to the model!
|
|
28
30
|
|
|
29
31
|
## Development
|
|
30
32
|
|
|
@@ -86,7 +88,7 @@ Make sure to run `npm run build`
|
|
|
86
88
|
|
|
87
89
|
- Most models are trained such that they don't think they can run commands for you.
|
|
88
90
|
- Sometimes, they use tools w/o hesitation... other times, I have to coax them.
|
|
89
|
-
- Use a system prompt or prompt template to instruct that they should follow user requests. Including to use `
|
|
91
|
+
- Use a system prompt or prompt template to instruct that they should follow user requests. Including to use `run_processs` without double checking.
|
|
90
92
|
- Ollama is a great way to run a model locally (w/ Open-WebUI)
|
|
91
93
|
|
|
92
94
|
```sh
|
package/build/always_log.js
CHANGED
|
@@ -13,7 +13,7 @@ export function verbose_log(message, data) {
|
|
|
13
13
|
// https://modelcontextprotocol.io/docs/tools/debugging#implementing-logging
|
|
14
14
|
// mentions STDERR is captured by the host app (i.e. Claude Desktop app)
|
|
15
15
|
// server.sendLoggingMessage is captured by MCP client (not Claude Desktop app)
|
|
16
|
-
// SO, IIUC use STDERR for logging into Claude Desktop app logs in:
|
|
16
|
+
// * SO, IIUC use STDERR for logging into Claude Desktop app logs in:
|
|
17
17
|
// '~/Library/Logs/Claude/mcp.log'
|
|
18
18
|
if (verbose) {
|
|
19
19
|
always_log(message, data);
|
|
@@ -39,6 +39,10 @@ export function verbose_log(message, data) {
|
|
|
39
39
|
// FYI client should also requets a log level from the server, so that needs to be here at some point too
|
|
40
40
|
}
|
|
41
41
|
export function always_log(message, data) {
|
|
42
|
+
const isJest = typeof process !== 'undefined' && !!process.env.JEST_WORKER_ID;
|
|
43
|
+
if (isJest) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
42
46
|
if (data) {
|
|
43
47
|
console.error(message + ": " + JSON.stringify(data));
|
|
44
48
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
// NOTES:
|
|
3
|
+
// - blocks event loop in exchange for convenience
|
|
4
|
+
async function runSyncWithStdio(stdioValue) {
|
|
5
|
+
console.log("\n\n" + "=".repeat(20) + " run " + "=".repeat(20) + "\n");
|
|
6
|
+
const options = {
|
|
7
|
+
stdio: stdioValue,
|
|
8
|
+
};
|
|
9
|
+
console.log("options", options);
|
|
10
|
+
const command = "ls";
|
|
11
|
+
const args = ["-lh", "/usr"];
|
|
12
|
+
const result = spawnSync(command, args, options);
|
|
13
|
+
const chalk = (await import('chalk')).default;
|
|
14
|
+
console.log(chalk.green("\n## spawnSync COMPLETE\n"));
|
|
15
|
+
console.log("\n## spawnSync COMPLETE\n");
|
|
16
|
+
console.log("result", result);
|
|
17
|
+
if (result.stdout && result.stdout.byteLength > 0) {
|
|
18
|
+
console.log(`stdout (#${result.stdout.byteLength}):`, result.stdout.toString());
|
|
19
|
+
}
|
|
20
|
+
if (result.stderr && result.stderr.byteLength > 0) {
|
|
21
|
+
// PRN look for diff .length vs .byteLength => see if can find scenarios where they aren't mismatched in my codebases (for other purposes, i.e. RAG matches column offset calculations)
|
|
22
|
+
console.log(`stderr (#${result.stderr.byteLength}):`, result.stderr.toString());
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
// *** KEY setting => DO NOT USE "inherit" type behavior w/ MCP server
|
|
27
|
+
// b/c then it is writing to the STDOUT/ERR and reading from STDIN that are intended for client to server JSON based comms only!
|
|
28
|
+
//
|
|
29
|
+
runSyncWithStdio("inherit"); // ls output is BEFORE result returned (b/c inherit means use parent process STDIN/OUT/ERR... so it writes immediately to STDOUT!
|
|
30
|
+
// buffer STDOUT/ERR
|
|
31
|
+
runSyncWithStdio("pipe"); // ls output is obtained after result returned (b/c piped to buffer)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
async function ls() {
|
|
4
|
+
const ls = spawn('ls', ['-lh', '/usr']);
|
|
5
|
+
// NOTES:
|
|
6
|
+
// - spawn/spawnSync are the foundation of all other methods
|
|
7
|
+
// - async, non-blocking (event loop)
|
|
8
|
+
ls.stdout.on('data', (data) => {
|
|
9
|
+
console.log(`stdout: ${data}`);
|
|
10
|
+
});
|
|
11
|
+
ls.stderr.on('data', (data) => {
|
|
12
|
+
console.error(`stderr: ${data}`);
|
|
13
|
+
});
|
|
14
|
+
const [code] = await once(ls, 'close');
|
|
15
|
+
console.log(`child process exited with code ${code}`);
|
|
16
|
+
}
|
|
17
|
+
// await ls();
|
|
18
|
+
async function cat_hello() {
|
|
19
|
+
const cat = spawn('cat');
|
|
20
|
+
cat.stdout.on('data', (data) => {
|
|
21
|
+
console.log(`stdout: ${data}`);
|
|
22
|
+
});
|
|
23
|
+
cat.stderr.on('data', (data) => {
|
|
24
|
+
console.error(`stderr: ${data}`);
|
|
25
|
+
});
|
|
26
|
+
cat.stdin.write("HEY FUCKTURD");
|
|
27
|
+
cat.stdin.end();
|
|
28
|
+
const [code] = await once(cat, 'close');
|
|
29
|
+
console.log(`child process exited with code ${code}`);
|
|
30
|
+
}
|
|
31
|
+
// await cat_hello();
|
|
32
|
+
async function sleep_abort() {
|
|
33
|
+
const aborter = new AbortController();
|
|
34
|
+
const options = {};
|
|
35
|
+
// options.shell = ""
|
|
36
|
+
options.signal = aborter.signal;
|
|
37
|
+
const cat = spawn('sleep', ["5"], options);
|
|
38
|
+
if (cat.stdout) {
|
|
39
|
+
cat.stdout.on('data', (data) => {
|
|
40
|
+
console.log(`stdout: ${data}`);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (cat.stderr) {
|
|
44
|
+
cat.stderr.on('data', (data) => {
|
|
45
|
+
console.error(`stderr: ${data}`);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
cat.on('error', (err) => {
|
|
49
|
+
console.log("ERR", err);
|
|
50
|
+
});
|
|
51
|
+
aborter.abort();
|
|
52
|
+
const [code] = await once(cat, 'close');
|
|
53
|
+
console.log(`child process exited with code ${code}`);
|
|
54
|
+
}
|
|
55
|
+
// await sleep_abort();
|
|
56
|
+
async function shells(child_process) {
|
|
57
|
+
if (child_process.stdout) {
|
|
58
|
+
child_process.stdout.on('data', (data) => {
|
|
59
|
+
console.log(`stdout: ${data}`);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (child_process.stderr) {
|
|
63
|
+
child_process.stderr.on('data', (data) => {
|
|
64
|
+
console.error(`stderr: ${data}`);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
child_process.on('error', (err) => {
|
|
68
|
+
console.log("ERR", err);
|
|
69
|
+
});
|
|
70
|
+
const [code] = await once(child_process, 'close');
|
|
71
|
+
console.log(`child process exited with code ${code}`);
|
|
72
|
+
}
|
|
73
|
+
await shells(spawn('echo', ["5"], { shell: false })); // works
|
|
74
|
+
// await shells(spawn('echo 55', { shell: false })); // fails
|
|
75
|
+
// *** pass only command arg w/ cmd+args and shell=true
|
|
76
|
+
await shells(spawn('echo 66', { shell: true })); // works
|
|
77
|
+
await shells(spawn('echo $fish_pid', { shell: true })); // no fish_pid (not fish shell)
|
|
78
|
+
await shells(spawn('echo $fish_pid', { shell: "fish" })); // works! fish shell!
|
|
79
|
+
// await shells(spawn('echo', ["5"], { shell: true })); // works but w/ warning about args are just concatenated (not sanitized)
|
|
80
|
+
// default shell info
|
|
81
|
+
await shells(spawn("echo", ['$PATH'], { shell: false })); // verbatim prints "$PATH" b/c no shell
|
|
82
|
+
await shells(spawn("echo", ['$PATH'], { shell: true })); // shows PATH variable value // warning too (first call w/ cmd,args array)
|
|
83
|
+
await shells(spawn("echo", ['$$'], { shell: false })); // verbatim $$ printed (no shell)
|
|
84
|
+
await shells(spawn("echo", ['$$'], { shell: true })); // prints PID of default shell
|
|
85
|
+
// await shells(spawn("set", { shell: true })); // prints PID of default shell
|
|
86
|
+
await shells(spawn("set -x; echo $0; echo $PATH; echo $BASH_VERSION", { shell: true })); // /bin/sh => bash in POSIX mode
|
|
87
|
+
// $0 = /bin/sh (on mac/linux)
|
|
88
|
+
// $BASH_VERSION
|
|
89
|
+
await shells(spawn("declare", { shell: true })); // dump default shell variables
|
|
90
|
+
// await shells(spawn("ps", { shell: false })); // default shell info
|
|
91
|
+
console.log("HERE:");
|
|
92
|
+
await shells(spawn("echo", ['$PATH'])); // default is shell=false
|
package/build/exec-utils.js
CHANGED
|
@@ -1,62 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @param {string} interpreter - The file to execute.
|
|
5
|
-
* @param {string} stdin - The string to pipe to stdin.
|
|
6
|
-
* @returns {Promise<ExecResult>}
|
|
7
|
-
*/
|
|
8
|
-
function execFileWithInput(interpreter, stdin, options) {
|
|
9
|
-
// FYI for now, using `exec()` so the interpreter can have cmd+args AIO
|
|
10
|
-
// could switch to `execFile()` to pass args array separately
|
|
11
|
-
// TODO starts with fish too? "fish -..." PRN use a library to parse the command and determine this?
|
|
12
|
-
if (interpreter.split(" ")[0] === "fish") {
|
|
13
|
-
// PRN also check error from fish and add possible clarification to error message though there are legit ways to trigger that same error message! i.e. `fish .` which is not the same issue!
|
|
14
|
-
return fishWorkaround(interpreter, stdin, options);
|
|
15
|
-
}
|
|
1
|
+
// TODO cleanup exec usages once spawn is ready
|
|
2
|
+
import { spawn } from "child_process";
|
|
3
|
+
export async function spawn_wrapped(command, args, stdin, options) {
|
|
16
4
|
return new Promise((resolve, reject) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (stdin) {
|
|
31
|
-
if (child.stdin === null) {
|
|
32
|
-
reject(new Error("Unexpected failure: child.stdin is null"));
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
5
|
+
if (!stdin) {
|
|
6
|
+
// FYI default is all 'pipe' (works when stdin is provided)
|
|
7
|
+
// 'ignore' attaches /dev/null
|
|
8
|
+
// order: [STDIN, STDOUT, STDERR]
|
|
9
|
+
options.stdio = ['ignore', 'pipe', 'pipe'];
|
|
10
|
+
// ? I wonder if this was related to fishWorkaround issue w/ STDIN (see commit history)... I was using base64 encoding to pass STDIN
|
|
11
|
+
}
|
|
12
|
+
const child = spawn(command, args, options);
|
|
13
|
+
// PRN return pid to callers?
|
|
14
|
+
console.log(`child.pid: ${child.pid}`);
|
|
15
|
+
let stdout = "";
|
|
16
|
+
let stderr = "";
|
|
17
|
+
if (child.stdin && stdin) {
|
|
35
18
|
child.stdin.write(stdin);
|
|
36
19
|
child.stdin.end();
|
|
37
20
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
21
|
+
if (child.stdout) {
|
|
22
|
+
child.stdout.on("data", (chunk) => {
|
|
23
|
+
stdout += chunk.toString();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
if (child.stderr) {
|
|
27
|
+
child.stderr.on("data", (chunk) => {
|
|
28
|
+
stderr += chunk.toString();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
let errored = false;
|
|
32
|
+
child.on("error", (err) => {
|
|
33
|
+
// ChildProcess 'error' docs: https://nodejs.org/api/child_process.html#event-error
|
|
34
|
+
// error running process
|
|
35
|
+
// IIUC not just b/c of command failed w/ non-zero exit code
|
|
36
|
+
const result = {
|
|
37
|
+
stdout: stdout,
|
|
38
|
+
stderr: stderr,
|
|
39
|
+
//
|
|
40
|
+
code: err.code,
|
|
41
|
+
signal: err.signal,
|
|
42
|
+
//
|
|
43
|
+
message: err.message,
|
|
44
|
+
// killed: (err as any).killed,
|
|
45
|
+
cmd: command, // TODO does error have .cmd on it? is it the composite result of processing in spawn too? (or same as I passed)
|
|
46
|
+
};
|
|
47
|
+
// console.log("ON_ERROR", result);
|
|
48
|
+
errored = true;
|
|
49
|
+
reject(result);
|
|
50
|
+
});
|
|
51
|
+
child.on("close", (code, signal) => {
|
|
52
|
+
// ChildProcess 'close' docs: https://nodejs.org/api/child_process.html#event-close
|
|
53
|
+
// 'close' is after child process ends AND stdio streams are closed
|
|
54
|
+
// - after 'exit' or 'error'
|
|
55
|
+
//
|
|
56
|
+
// * do not resolve if 'error' already called (promise is already resolved)
|
|
57
|
+
if (errored)
|
|
58
|
+
return;
|
|
59
|
+
// either code is set, or signal, but NOT BOTH
|
|
60
|
+
// signal if process killed
|
|
61
|
+
// FYI close does not mean code==0
|
|
62
|
+
const result = {
|
|
63
|
+
stdout: stdout,
|
|
64
|
+
stderr: stderr,
|
|
65
|
+
code: code ?? undefined,
|
|
66
|
+
signal: signal ?? undefined,
|
|
67
|
+
};
|
|
68
|
+
// console.log("ON_CLOSE", result);
|
|
69
|
+
resolve(result);
|
|
59
70
|
});
|
|
60
71
|
});
|
|
61
72
|
}
|
|
62
|
-
export { execFileWithInput };
|
package/build/messages.js
CHANGED
|
@@ -1,50 +1,79 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export function resultFor(spawn_result) {
|
|
2
|
+
const result_obj = {
|
|
3
|
+
content: messagesFor(spawn_result),
|
|
4
|
+
};
|
|
5
|
+
if (spawn_result.code !== 0) {
|
|
6
|
+
result_obj.isError = true;
|
|
7
|
+
}
|
|
8
|
+
return result_obj;
|
|
9
|
+
}
|
|
4
10
|
export function messagesFor(result) {
|
|
5
11
|
const messages = [];
|
|
6
12
|
if (result.code !== undefined) {
|
|
13
|
+
// FYI include EXIT_CODE always, to make EXPLICIT when it is NOT a FAILURE!
|
|
14
|
+
// will double underscore when a comman fails vs not stating if it was a failure
|
|
15
|
+
// some commands give no other indication of success!
|
|
7
16
|
messages.push({
|
|
8
|
-
type: "text",
|
|
9
|
-
text: `${result.code}`,
|
|
10
17
|
name: "EXIT_CODE",
|
|
18
|
+
type: "text",
|
|
19
|
+
text: String(result.code),
|
|
11
20
|
});
|
|
12
21
|
}
|
|
13
|
-
// PRN
|
|
14
|
-
//
|
|
15
|
-
// and maybe include message when it doesn't contain stderr?
|
|
16
|
-
// FYI if I put these back, start with tests first
|
|
17
|
-
// PRN use a test to add these, sleep 10s maybe and then kill that process?
|
|
18
|
-
// definitely could be useful to know if a command was killed
|
|
19
|
-
// make sure signal is not null, which is what's used when no signal killed the process
|
|
20
|
-
// if (result.signal) {
|
|
22
|
+
// // PRN map COMMAND for failures so model can adjust what it passes vs what actually ran?
|
|
23
|
+
// if ("cmd" in result && result.cmd) {
|
|
21
24
|
// messages.push({
|
|
25
|
+
// name: "COMMAND",
|
|
22
26
|
// type: "text",
|
|
23
|
-
// text:
|
|
24
|
-
// name: "SIGNAL",
|
|
27
|
+
// text: result.cmd,
|
|
25
28
|
// });
|
|
26
|
-
// }
|
|
27
|
-
|
|
29
|
+
// }
|
|
30
|
+
if ("message" in result && result.message) {
|
|
31
|
+
// at least need error.message from spawn errors
|
|
32
|
+
messages.push({
|
|
33
|
+
name: "MESSAGE",
|
|
34
|
+
type: "text",
|
|
35
|
+
text: result.message,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (result.signal) {
|
|
39
|
+
messages.push({
|
|
40
|
+
name: "SIGNAL",
|
|
41
|
+
type: "text",
|
|
42
|
+
text: result.signal,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// // when is this set? what conditions? I tried `kill -9` and didn't trigger "error" event
|
|
46
|
+
// if ("killed" in result && result.killed) {
|
|
28
47
|
// // killed == true is the only time to include this
|
|
29
48
|
// messages.push({
|
|
49
|
+
// name: "KILLED",
|
|
30
50
|
// type: "text",
|
|
31
51
|
// text: "Process was killed",
|
|
32
|
-
// name: "KILLED",
|
|
33
52
|
// });
|
|
34
53
|
// }
|
|
35
54
|
if (result.stdout) {
|
|
36
55
|
messages.push({
|
|
56
|
+
name: "STDOUT",
|
|
37
57
|
type: "text",
|
|
38
58
|
text: result.stdout,
|
|
39
|
-
name: "STDOUT",
|
|
40
59
|
});
|
|
41
60
|
}
|
|
42
61
|
if (result.stderr) {
|
|
43
62
|
messages.push({
|
|
63
|
+
name: "STDERR",
|
|
44
64
|
type: "text",
|
|
45
65
|
text: result.stderr,
|
|
46
|
-
name: "STDERR",
|
|
47
66
|
});
|
|
48
67
|
}
|
|
49
68
|
return messages;
|
|
50
69
|
}
|
|
70
|
+
export function errorResult(message) {
|
|
71
|
+
return {
|
|
72
|
+
isError: true,
|
|
73
|
+
content: [{
|
|
74
|
+
name: "ERROR",
|
|
75
|
+
type: "text",
|
|
76
|
+
text: message,
|
|
77
|
+
}],
|
|
78
|
+
};
|
|
79
|
+
}
|
package/build/prompts.js
CHANGED
|
@@ -9,27 +9,22 @@ export function registerPrompts(server) {
|
|
|
9
9
|
verbose_log("INFO: ListPrompts");
|
|
10
10
|
return {
|
|
11
11
|
prompts: [
|
|
12
|
-
//
|
|
12
|
+
// ?? add prompts for various LLMs that tailor instructions to make them optimize use of run_process tool
|
|
13
13
|
// idea is, users could insert those manually, or perhaps automatically if necessary, depending on context
|
|
14
14
|
// that way you don't need one prompt for everything and certain models won't need any help (i.e. Claude) vs
|
|
15
|
-
// llama4 which struggled with old run_script tool (now just stdin on
|
|
15
|
+
// llama4 which struggled with old run_script tool (now just stdin on run_process) so it might need some
|
|
16
16
|
// special instructions and yeah... I think that's a use case for these prompts
|
|
17
17
|
// /prompt llama4 ?
|
|
18
18
|
{
|
|
19
|
-
name: "
|
|
20
|
-
description: "Novel examples of run_command tool use to nudge models to the possibilities. " +
|
|
21
|
-
"Based on assumption that most models understand shell commands/scripts very well.",
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: "run_command",
|
|
19
|
+
name: "run_process",
|
|
25
20
|
description: "Include command output in the prompt. " +
|
|
26
21
|
"This is effectively a user tool call.",
|
|
27
22
|
arguments: [
|
|
28
|
-
{
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
},
|
|
32
|
-
//
|
|
23
|
+
{ name: "mode", },
|
|
24
|
+
{ name: "command_line", },
|
|
25
|
+
{ name: "argv", },
|
|
26
|
+
{ name: "dry_run", },
|
|
27
|
+
// ? other args?
|
|
33
28
|
],
|
|
34
29
|
},
|
|
35
30
|
],
|
|
@@ -39,20 +34,21 @@ export function registerPrompts(server) {
|
|
|
39
34
|
// if (request.params.name == "examples") {
|
|
40
35
|
// return GetExamplePromptMessages();
|
|
41
36
|
// } else
|
|
42
|
-
if (request.params.name !== "
|
|
37
|
+
if (request.params.name !== "run_process") {
|
|
43
38
|
throw new Error("Unknown or not implemented prompt: " + request.params.name);
|
|
44
39
|
}
|
|
45
40
|
verbose_log("INFO: PromptRequest", request);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
throw new Error("run_process not yet ported to prompts");
|
|
42
|
+
const command_line = String(request.params.arguments?.command_line);
|
|
43
|
+
const argv = String(request.params.arguments?.argv);
|
|
44
|
+
const mode = String(request.params.arguments?.mode);
|
|
45
|
+
const dry_run = Boolean(request.params.arguments?.dry_run);
|
|
51
46
|
// Is it possible/feasible to pass a path for the workdir when running the command?
|
|
52
47
|
// - currently it uses / (yikez)
|
|
53
48
|
// - IMO makes more sense to have it be based on the Zed workdir of each project
|
|
54
49
|
// - Fallback could be to configure on server level (i.e. home dir of current user) - perhaps CLI arg? (thinking of zed's context_servers config section)
|
|
55
|
-
|
|
50
|
+
// TODO RUN_PROCESS MIGRATION - finish rest of migrtation to run_process
|
|
51
|
+
const { stdout, stderr } = await execAsync(command_line);
|
|
56
52
|
// TODO gracefully handle errors and turn them into a prompt message that can be used by LLM to troubleshoot the issue, currently errors result in nothing inserted into the prompt and instead it shows the Zed's chat panel as a failure
|
|
57
53
|
const messages = [
|
|
58
54
|
{
|
|
@@ -60,7 +56,7 @@ export function registerPrompts(server) {
|
|
|
60
56
|
content: {
|
|
61
57
|
type: "text",
|
|
62
58
|
text: "I ran the following command, if there is any output it will be shown below:\n" +
|
|
63
|
-
|
|
59
|
+
command_line,
|
|
64
60
|
},
|
|
65
61
|
},
|
|
66
62
|
];
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { spawn_wrapped } from "./exec-utils.js";
|
|
2
|
+
import { always_log } from "./always_log.js";
|
|
3
|
+
import { errorResult, messagesFor, resultFor } from "./messages.js";
|
|
4
|
+
export async function runProcess(args) {
|
|
5
|
+
const command_line = args?.command_line;
|
|
6
|
+
const argv = args?.argv;
|
|
7
|
+
const mode = String(args?.mode);
|
|
8
|
+
if (!mode) {
|
|
9
|
+
return errorResult("Mode is required");
|
|
10
|
+
}
|
|
11
|
+
const isShell = mode === "shell";
|
|
12
|
+
const isExecutable = mode === "executable";
|
|
13
|
+
if (!isShell && !isExecutable) {
|
|
14
|
+
return errorResult(`Invalid mode '${mode}'. Allowed values are 'shell' or 'executable'.`);
|
|
15
|
+
}
|
|
16
|
+
// * shared args
|
|
17
|
+
const spawn_options = {
|
|
18
|
+
// spawn options: https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
|
|
19
|
+
encoding: "utf8"
|
|
20
|
+
};
|
|
21
|
+
if (args?.cwd) {
|
|
22
|
+
spawn_options.cwd = String(args.cwd);
|
|
23
|
+
}
|
|
24
|
+
// PRN args.env
|
|
25
|
+
if (args?.timeout_ms) {
|
|
26
|
+
spawn_options.timeout = Number(args.timeout_ms);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// default timeout after 30s
|
|
30
|
+
spawn_options.timeout = 30000;
|
|
31
|
+
}
|
|
32
|
+
// PRN windowsHide on Windows, signal, killSignal
|
|
33
|
+
// FYI spawn_options.stdio => default is perfect ['pipe', 'pipe', 'pipe'] https://nodejs.org/api/child_process.html#optionsstdio
|
|
34
|
+
// do not set inherit (this is what causes ripgrep to see STDIN socket and search it, thus hanging)
|
|
35
|
+
const stdin = args?.stdin ? String(args.stdin) : undefined; // TODO
|
|
36
|
+
const dryRun = Boolean(args?.dry_run);
|
|
37
|
+
try {
|
|
38
|
+
if (dryRun) {
|
|
39
|
+
// Build a descriptive plan without executing anything
|
|
40
|
+
let plan = '';
|
|
41
|
+
if (isShell) {
|
|
42
|
+
const cmd = String(args?.command_line);
|
|
43
|
+
const shell = process.env.SHELL || (process.platform === 'win32' ? 'cmd.exe' : '/bin/sh');
|
|
44
|
+
plan = `Shell mode: will execute command_line via ${shell}: ${cmd}`;
|
|
45
|
+
}
|
|
46
|
+
else if (isExecutable) {
|
|
47
|
+
const argv = args?.argv;
|
|
48
|
+
plan = `Executable mode: will spawn '${argv[0]}' with arguments ${JSON.stringify(argv.slice(1))}`;
|
|
49
|
+
}
|
|
50
|
+
return { content: [{ name: "PLAN", type: "text", text: plan }] };
|
|
51
|
+
}
|
|
52
|
+
if (isShell) {
|
|
53
|
+
if (!args?.command_line) {
|
|
54
|
+
return errorResult("Mode 'shell' requires a non‑empty 'command_line' parameter.");
|
|
55
|
+
}
|
|
56
|
+
if (args?.argv) {
|
|
57
|
+
return errorResult("Mode 'shell' does not accept an 'argv' parameter.");
|
|
58
|
+
}
|
|
59
|
+
spawn_options.shell = true;
|
|
60
|
+
const command_line = String(args?.command_line);
|
|
61
|
+
const result = await spawn_wrapped(command_line, [], stdin, spawn_options);
|
|
62
|
+
return resultFor(result);
|
|
63
|
+
}
|
|
64
|
+
if (isExecutable) {
|
|
65
|
+
const argv = args?.argv;
|
|
66
|
+
if (!Array.isArray(argv) || argv.length === 0) {
|
|
67
|
+
return errorResult("Mode 'executable' requires a non‑empty 'argv' array.");
|
|
68
|
+
}
|
|
69
|
+
if (args?.command_line) {
|
|
70
|
+
return errorResult("Mode 'executable' does not accept a 'command_line' parameter.");
|
|
71
|
+
}
|
|
72
|
+
spawn_options.shell = false;
|
|
73
|
+
const command = argv[0];
|
|
74
|
+
const commandArgs = argv.slice(1);
|
|
75
|
+
const result = await spawn_wrapped(command, commandArgs, stdin, spawn_options);
|
|
76
|
+
return resultFor(result);
|
|
77
|
+
}
|
|
78
|
+
// TODO... fish shell workaround (see exec-utils.ts) - ADD or FIX via a TEST FIRST
|
|
79
|
+
return errorResult("Unexpected execution path in runProcess, should not be possible");
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
// TODO check if not SpawnFailure => i.e. test aborting/killing
|
|
83
|
+
const response = {
|
|
84
|
+
isError: true,
|
|
85
|
+
content: messagesFor(error),
|
|
86
|
+
};
|
|
87
|
+
always_log("WARN: run_process failed", error);
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/build/tools.js
CHANGED
|
@@ -1,36 +1,81 @@
|
|
|
1
1
|
import os from "os";
|
|
2
|
-
import { CallToolRequestSchema, ListToolsRequestSchema
|
|
2
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
3
3
|
import { verbose_log } from "./always_log.js";
|
|
4
|
-
import {
|
|
4
|
+
import { runProcess } from "./run_process.js";
|
|
5
5
|
export function reisterTools(server) {
|
|
6
6
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
7
7
|
verbose_log("INFO: ListTools");
|
|
8
8
|
return {
|
|
9
|
+
// https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool // tool definition
|
|
10
|
+
// https://modelcontextprotocol.io/docs/learn/architecture#understanding-the-tool-execution-request // tool request/response
|
|
11
|
+
// typescript SDK docs:
|
|
12
|
+
// servers: https://github.com/modelcontextprotocol/typescript-sdk/blob/main/docs/server.md
|
|
9
13
|
tools: [
|
|
10
14
|
{
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
// TODO RUN_PROCESS MIGRATION! provide examples in system message, that way it is very clear how to use these!
|
|
16
|
+
name: "run_process",
|
|
17
|
+
description: "Run a process on this " + os.platform() + " machine",
|
|
13
18
|
inputSchema: {
|
|
14
19
|
type: "object",
|
|
15
20
|
properties: {
|
|
16
|
-
|
|
21
|
+
// ListToolsResult => Tool type (in protocol) => https://modelcontextprotocol.io/specification/2025-06-18/schema#tool
|
|
22
|
+
// * appears I can do w/e I want to describe properties (as long as give a string key (name)) => hence enum below
|
|
23
|
+
mode: {
|
|
24
|
+
enum: ["shell", "executable"], // * I made this up
|
|
25
|
+
description: "What are you running, two choices: 'shell' or 'executable' (required, no default)",
|
|
26
|
+
// FYI only use default system shell, if the model wants a different shell, can explicitly run it with command_line/argv
|
|
27
|
+
// DO NOT duplicate the mechanism of specifying what runs by adding yet another executable field!
|
|
28
|
+
// spawn's options.shell is bool/string... string is command_name/path... that duplicates and complicates needlessly!
|
|
29
|
+
},
|
|
30
|
+
command_line: {
|
|
17
31
|
type: "string",
|
|
18
|
-
description: "
|
|
32
|
+
description: "Shell command line. Required when mode='shell'. Forbidden when mode='executable'."
|
|
33
|
+
},
|
|
34
|
+
argv: {
|
|
35
|
+
minItems: 1, // * made up too
|
|
36
|
+
type: "array",
|
|
37
|
+
items: { type: "string" },
|
|
38
|
+
description: "Executable and arguments. argv[0] is the executable. Required when mode='executable'. Forbidden when mode='shell'."
|
|
19
39
|
},
|
|
20
|
-
|
|
21
|
-
//
|
|
40
|
+
cwd: {
|
|
41
|
+
// or "workdir" like before? => eval model behavior w/ each name?
|
|
22
42
|
type: "string",
|
|
23
|
-
description: "Optional
|
|
43
|
+
description: "Optional to set working directory",
|
|
24
44
|
},
|
|
25
45
|
stdin: {
|
|
26
46
|
type: "string",
|
|
27
|
-
description: "Optional
|
|
47
|
+
description: "Optional text written to STDIN (written fully, then closed). Useful for heredoc-style input or file contents."
|
|
48
|
+
},
|
|
49
|
+
timeout_ms: {
|
|
50
|
+
type: "number",
|
|
51
|
+
description: "Optional timeout in milliseconds, defaults to 30,000ms",
|
|
28
52
|
},
|
|
29
|
-
|
|
53
|
+
dry_run: {
|
|
54
|
+
type: "boolean",
|
|
55
|
+
description: "Optional explain what would run, defaults to False."
|
|
56
|
+
// FYI this can help avoid the need for logging this information, I can call this as a user too!
|
|
57
|
+
// TODO, dump info about shell (if applicable) and program versions!
|
|
58
|
+
// - explain the node calls too, i.e. spawn('echo foo')
|
|
59
|
+
// - show paths of resolved resources (i.e. shell)
|
|
60
|
+
// - i.e. could run command --version to see its version (attempt various flags until one works, and/or have a lookup of common tools to know how to find their version)
|
|
61
|
+
}
|
|
62
|
+
// MAYBEs:
|
|
30
63
|
// - env - obscure cases where command takes a param only via an env var?
|
|
31
|
-
// - timeout - lets just hard code this for now
|
|
32
64
|
},
|
|
33
|
-
required: ["
|
|
65
|
+
required: ["mode"],
|
|
66
|
+
// PRN add proprietary constraints explanation? only if model seems to struggle and there isn't a better fix...
|
|
67
|
+
// oneOf: [
|
|
68
|
+
// {
|
|
69
|
+
// properties: { mode: { const: "shell" } },
|
|
70
|
+
// required: ["command_line"],
|
|
71
|
+
// not: { required: ["argv"] }
|
|
72
|
+
// },
|
|
73
|
+
// {
|
|
74
|
+
// properties: { mode: { const: "executable" } },
|
|
75
|
+
// required: ["argv"],
|
|
76
|
+
// not: { required: ["command_line"] }
|
|
77
|
+
// }
|
|
78
|
+
// ]
|
|
34
79
|
},
|
|
35
80
|
},
|
|
36
81
|
],
|
|
@@ -39,8 +84,8 @@ export function reisterTools(server) {
|
|
|
39
84
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
40
85
|
verbose_log("INFO: ToolRequest", request);
|
|
41
86
|
switch (request.params.name) {
|
|
42
|
-
case "
|
|
43
|
-
return await
|
|
87
|
+
case "run_process": {
|
|
88
|
+
return await runProcess(request.params.arguments);
|
|
44
89
|
}
|
|
45
90
|
default:
|
|
46
91
|
throw new Error("Unknown tool");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-server-commands",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "An MCP server to run arbitrary commands",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
"inspector": "npx @modelcontextprotocol/inspector build/index.js",
|
|
19
19
|
"test": "jest",
|
|
20
20
|
"test:watch": "jest --watch",
|
|
21
|
-
"test:integration": "jest tests/integration"
|
|
21
|
+
"test:integration-all": "jest tests/integration",
|
|
22
|
+
"test:watch:integration-all": "jest --watch tests/integration",
|
|
23
|
+
"test:watch:integration-run_process": "jest --watch tests/integration/run_process.test.ts"
|
|
22
24
|
},
|
|
23
25
|
"dependencies": {
|
|
24
26
|
"@modelcontextprotocol/sdk": "1.9.0"
|
|
@@ -26,6 +28,7 @@
|
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"@types/jest": "^29.5.14",
|
|
28
30
|
"@types/node": "^22.14.1",
|
|
31
|
+
"chalk": "^5.6.2",
|
|
29
32
|
"jest": "^29.7.0",
|
|
30
33
|
"ts-jest": "^29.3.2",
|
|
31
34
|
"typescript": "^5.8.3"
|
package/build/run-command.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { exec } from "node:child_process";
|
|
2
|
-
import { promisify } from "node:util";
|
|
3
|
-
import { execFileWithInput } from "./exec-utils.js";
|
|
4
|
-
import { always_log } from "./always_log.js";
|
|
5
|
-
import { messagesFor } from "./messages.js";
|
|
6
|
-
const execAsync = promisify(exec);
|
|
7
|
-
async function execute(command, stdin, options) {
|
|
8
|
-
// PRN merge calls to exec into one single paradigm with conditional STDIN handled in one spot?
|
|
9
|
-
// right now no STDIN => exec directly and let it throw to catch failures
|
|
10
|
-
// w/ STDIN => you manually glue together callbacks + promises (i.e. reject)
|
|
11
|
-
// feels sloppy to say the least, notably the error handling with ExecExeption error that has stdin/stderr on it
|
|
12
|
-
if (!stdin) {
|
|
13
|
-
return await execAsync(command, options);
|
|
14
|
-
}
|
|
15
|
-
return await execFileWithInput(command, stdin, options);
|
|
16
|
-
}
|
|
17
|
-
export async function runCommand(args) {
|
|
18
|
-
const command = args?.command;
|
|
19
|
-
if (!command) {
|
|
20
|
-
const message = "Command is required, current value: " + command;
|
|
21
|
-
return {
|
|
22
|
-
isError: true,
|
|
23
|
-
content: [{ type: "text", text: message }],
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
const options = { encoding: "utf8" };
|
|
27
|
-
if (args?.workdir) {
|
|
28
|
-
options.cwd = String(args.workdir);
|
|
29
|
-
}
|
|
30
|
-
const stdin = args?.stdin;
|
|
31
|
-
try {
|
|
32
|
-
const result = await execute(command, stdin, options);
|
|
33
|
-
return {
|
|
34
|
-
content: messagesFor(result),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
// PRN do I want to differentiate non-command related error (i.e. if messagesFor blows up
|
|
39
|
-
// or presumably if smth else goes wrong with the node code in exec that isn't command related
|
|
40
|
-
// if so, write a test first
|
|
41
|
-
// console.log("ERROR_runCommand", error);
|
|
42
|
-
// ExecException (error + stdout/stderr) merged
|
|
43
|
-
// - IIUC this happens on uncaught failures
|
|
44
|
-
// - but if you catch an exec() promise failure (or use exec's callback) => you get separated values: error, stdout, stderr
|
|
45
|
-
// - which is why I mirror this response type in my reject(error) calls
|
|
46
|
-
//
|
|
47
|
-
// 'error' example:
|
|
48
|
-
// code: 127,
|
|
49
|
-
// killed: false,
|
|
50
|
-
// signal: null,
|
|
51
|
-
// cmd: 'nonexistentcommand',
|
|
52
|
-
// stdout: '',
|
|
53
|
-
// stderr: '/bin/sh: nonexistentcommand: command not found\n'
|
|
54
|
-
const response = {
|
|
55
|
-
isError: true,
|
|
56
|
-
content: messagesFor(error),
|
|
57
|
-
};
|
|
58
|
-
always_log("WARN: run_command failed", response);
|
|
59
|
-
return response;
|
|
60
|
-
}
|
|
61
|
-
}
|