-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellCall.js
More file actions
142 lines (122 loc) · 4.77 KB
/
shellCall.js
File metadata and controls
142 lines (122 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// ===================================================================
// Handler Function: shellCall
// Shell command wrapper for executing system commands
// Uses configurable shell and parameters array
// ===================================================================
// Import shared functions
const shared = require('sharedFunctions');
function taskLog(message) {
console.log(message);
stdOut.push(message);
}
/**
* Execute a shell command with optional parameters
* @param {Object} params - Command parameters
* @param {string} params.command - Shell command to execute (required)
* @param {Array<string>} [params.parameters=[]] - Array of command parameters (optional)
* @param {string} [params.shell="/bin/bash"] - Shell interpreter path (/bin/sh or /bin/bash) (optional, default /bin/bash)
*/
function shellCall(params) {
var command = params.command || "";
var parameters = params.parameters || [];
var shell = params.shell || "/bin/bash";
var shellValidation;
taskLog("=== Shell Call Task ===");
taskLog("Command: " + command);
taskLog("Parameters: " + JSON.stringify(parameters));
taskLog("Shell: " + shell);
// Validate input parameters
if (!command) {
return shared.setErrorResult("Missing required parameter: command", {
operation: "shellCall"
});
}
if (shell.length === 0) {
shell = "/bin/bash";
}
shellValidation = shared.validateFilePath(shell, "shell", { absolute: true });
if (!shellValidation.ok) {
return shared.setErrorResult(shellValidation.message, {
operation: "shellCall"
});
}
shell = shellValidation.value;
var isCustomShell = false;
// Build shell script based on function parameters
// Parameter 3 : string : shell (/bin/sh /bin/bash)
// -> Optional -> default /bin/bash This is used at #! first line
// Check if shell parameter differs from default - treat as custom shell
var shellScript = "#!/usr/bin/env bash\n";
if (shell === "/bin/bash") {
shellScript += "#!/bin/bash -e\n";
isCustomShell = true;
}
// Build command with parameters based on function parameters
shellScript += 'set -euo pipefail\n';
shellScript += "cd \"$(dirname \"$0\")\" || exit 1\n";
if (parameters && parameters.length > 0) {
taskLog("[Script] Building command with parameters...");
var paramString = "";
for (var i = 0; i < parameters.length; i++) {
var arg = parameters[i];
if (arg != null) {
var argStr = String(arg);
// cleanup JSON escapings
var escapedArg = argStr
.replace(/"/g, '\\"')
.replace(/\$/g, '\\$')
.replace(/`/g, '\\`');
paramString += " \"" + escapedArg + "\"";
} else {
paramString += " \"\"";
}
}
shellScript += command + paramString + '\n';
} else {
// Single command without additional parameters
shellScript += command + '\n';
}
taskLog("[Script] Run: " + shellScript);
var success = MCPStudio.shell(shellScript);
if (success) {
MCPStudio.setToolResult(JSON.stringify({
text: "[Script] Command executed successfully\n" +
(stdOut && stdOut.length > 0 ? stdOut.join("\n") : "") +
(stdErr && stdErr.length > 0 ? "\nErrors and Warnings:\n" + stdErr.join("\n") : ""),
metadata: {
path: ".",
command: command,
shell: shell,
parameters: parameters,
operation: "shellCall",
success: true,
stdout: stdOut,
stderr: stdErr,
}
}));
taskLog("[Script] Command successful!");
} else {
MCPStudio.setToolResult(JSON.stringify({
text: "[Script] Command failed.\n" +
(stdOut && stdOut.length > 0 ? stdOut.join("\n") : "") +
(stdErr && stdErr.length > 0 ? "\nErrors and Warnings:\n" + stdErr.join("\n") : ""),
metadata: {
path: ".",
command: command,
shell: shell,
parameters: parameters,
operation: "shellCall",
success: false,
stdout: stdOut,
stderr: stdErr,
}
}));
taskLog("[Script] Command failed!" +
(stdOut && stdOut.length > 0 ? stdOut.join("\n") : "") +
(stdErr && stdErr.length > 0 ? "\nErrors and Warnings:\n" + stdErr.join("\n") : ""));
}
return null; // Result already set via MCPStudio.setToolResult
}
module.exports = {
shellCall
};