@baitong-dev/bash-mcp 0.0.2 → 0.0.3
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/index.d.ts +8 -1
- package/dist/index.js +46 -32
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +364 -0
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Environment variables to inherit by default, if an environment is not explicitly given.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_INHERITED_ENV_VARS: string[];
|
|
6
|
+
/**
|
|
7
|
+
* Returns a default environment object including only environment variables deemed safe to inherit.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDefaultEnvs(): {};
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.DEFAULT_INHERITED_ENV_VARS = void 0;
|
|
8
|
+
exports.getDefaultEnvs = getDefaultEnvs;
|
|
7
9
|
/**
|
|
8
|
-
* Bash MCP Server
|
|
10
|
+
* Bash MCP Server
|
|
9
11
|
*/
|
|
10
12
|
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
11
13
|
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
@@ -20,7 +22,7 @@ const child_process_1 = require("child_process");
|
|
|
20
22
|
const bash_txt_1 = __importDefault(require("../bash.txt"));
|
|
21
23
|
const mcp_helpers_1 = require("@baitong-dev/mcp-helpers");
|
|
22
24
|
const MCP_NAME = 'Bash MCP';
|
|
23
|
-
const MCP_VERSION = '0.0.
|
|
25
|
+
const MCP_VERSION = '0.0.3';
|
|
24
26
|
const parser = (0, mcp_helpers_1.lazy)(async () => {
|
|
25
27
|
const treePath = (0, mcp_helpers_1.resolveWasm)(web_tree_sitter_wasm_1.default);
|
|
26
28
|
await web_tree_sitter_1.Parser.init({
|
|
@@ -81,13 +83,6 @@ const SUDO_REJECTION_MESSAGE = '[REJECTED] sudo commands cannot be executed. Ple
|
|
|
81
83
|
// Default max_output
|
|
82
84
|
const DEFAULT_MAX_OUTPUT = 30000;
|
|
83
85
|
const DEFAULT_TIMEOUT_MS = 2 * 60 * 1000;
|
|
84
|
-
// Environment variables that cannot be overridden (hardened mode only)
|
|
85
|
-
const PROTECTED_ENV_VARS = new Set([
|
|
86
|
-
'LD_PRELOAD',
|
|
87
|
-
'LD_LIBRARY_PATH',
|
|
88
|
-
'DYLD_INSERT_LIBRARIES',
|
|
89
|
-
'DYLD_LIBRARY_PATH'
|
|
90
|
-
]);
|
|
91
86
|
/**
|
|
92
87
|
* Check if a command contains sudo
|
|
93
88
|
* Matches: sudo at start, after semicolon, after &&, after ||, after |, after $(, after backtick
|
|
@@ -97,23 +92,43 @@ function containsSudo(command) {
|
|
|
97
92
|
return /(?:^|[;&|`$()]\s*)sudo(?:\s|$)/m.test(command);
|
|
98
93
|
}
|
|
99
94
|
/**
|
|
100
|
-
*
|
|
101
|
-
* Only active in hardened mode. Passes everything through when hardened mode is off.
|
|
95
|
+
* Environment variables to inherit by default, if an environment is not explicitly given.
|
|
102
96
|
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
97
|
+
exports.DEFAULT_INHERITED_ENV_VARS = process.platform === 'win32'
|
|
98
|
+
? [
|
|
99
|
+
'APPDATA',
|
|
100
|
+
'HOMEDRIVE',
|
|
101
|
+
'HOMEPATH',
|
|
102
|
+
'LOCALAPPDATA',
|
|
103
|
+
'PATH',
|
|
104
|
+
'PROCESSOR_ARCHITECTURE',
|
|
105
|
+
'SYSTEMDRIVE',
|
|
106
|
+
'SYSTEMROOT',
|
|
107
|
+
'TEMP',
|
|
108
|
+
'TMPDIR',
|
|
109
|
+
'USERNAME',
|
|
110
|
+
'USERPROFILE',
|
|
111
|
+
'PROGRAMFILES'
|
|
112
|
+
]
|
|
113
|
+
: /* list inspired by the default env inheritance of sudo */
|
|
114
|
+
['HOME', 'LOGNAME', 'PATH', 'SHELL', 'TERM', 'USER'];
|
|
115
|
+
/**
|
|
116
|
+
* Returns a default environment object including only environment variables deemed safe to inherit.
|
|
117
|
+
*/
|
|
118
|
+
function getDefaultEnvs() {
|
|
119
|
+
const env = {};
|
|
120
|
+
for (const key of exports.DEFAULT_INHERITED_ENV_VARS) {
|
|
121
|
+
const value = process.env[key];
|
|
122
|
+
if (value === undefined) {
|
|
123
|
+
continue;
|
|
111
124
|
}
|
|
112
|
-
|
|
113
|
-
|
|
125
|
+
if (value.startsWith('()')) {
|
|
126
|
+
// Skip functions, which are a security risk.
|
|
127
|
+
continue;
|
|
114
128
|
}
|
|
129
|
+
env[key] = value;
|
|
115
130
|
}
|
|
116
|
-
return
|
|
131
|
+
return env;
|
|
117
132
|
}
|
|
118
133
|
/**
|
|
119
134
|
* Middle-truncate output to preserve beginning and end
|
|
@@ -192,7 +207,6 @@ async function executeBash(options) {
|
|
|
192
207
|
const cwd = options.cwd || DEFAULT_CWD;
|
|
193
208
|
const timeout = options.timeout || DEFAULT_TIMEOUT_MS;
|
|
194
209
|
const maxOutput = options.maxOutput || DEFAULT_MAX_OUTPUT;
|
|
195
|
-
const { filtered: safeEnv, rejected: rejectedVars } = filterEnvVars(options.env);
|
|
196
210
|
if (containsSudo(command)) {
|
|
197
211
|
throw Error(`${SUDO_REJECTION_MESSAGE}$ ${command}`);
|
|
198
212
|
}
|
|
@@ -265,9 +279,8 @@ async function executeBash(options) {
|
|
|
265
279
|
}
|
|
266
280
|
}
|
|
267
281
|
const mergedEnv = {
|
|
268
|
-
...
|
|
269
|
-
...
|
|
270
|
-
...(0, mcp_helpers_1.getBinaryEnvs)()
|
|
282
|
+
...getDefaultEnvs(),
|
|
283
|
+
...(0, mcp_helpers_1.getBundledBinaryEnvs)()
|
|
271
284
|
};
|
|
272
285
|
const proc = (0, child_process_1.spawn)(command, {
|
|
273
286
|
shell: getBashPath(),
|
|
@@ -309,9 +322,6 @@ async function executeBash(options) {
|
|
|
309
322
|
if (timedOut) {
|
|
310
323
|
resultMetadata.push(`bash tool terminated command after exceeding timeout ${timeout} ms`);
|
|
311
324
|
}
|
|
312
|
-
if (rejectedVars.length > 0) {
|
|
313
|
-
resultMetadata.push(`bash tool blocked env vars: ${rejectedVars.join(', ')}`);
|
|
314
|
-
}
|
|
315
325
|
if (resultMetadata.length > 0) {
|
|
316
326
|
output += '\n\n<bash_metadata>\n' + resultMetadata.join('\n') + '\n</bash_metadata>';
|
|
317
327
|
}
|
|
@@ -322,9 +332,13 @@ async function executeBash(options) {
|
|
|
322
332
|
};
|
|
323
333
|
}
|
|
324
334
|
server.registerTool('bash', {
|
|
325
|
-
description:
|
|
326
|
-
|
|
327
|
-
|
|
335
|
+
description: JSON.stringify({
|
|
336
|
+
...getDefaultEnvs(),
|
|
337
|
+
...(0, mcp_helpers_1.getBundledBinaryEnvs)()
|
|
338
|
+
}, null, 2) +
|
|
339
|
+
bash_txt_1.default.replaceAll('${directory}', DEFAULT_CWD)
|
|
340
|
+
.replaceAll('${maxLines}', String(2000))
|
|
341
|
+
.replaceAll('${maxBytes}', String(50 * 1024)),
|
|
328
342
|
inputSchema: bashInputSchema
|
|
329
343
|
}, async (args) => {
|
|
330
344
|
try {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
/**
|
|
3
|
+
* Environment variables to inherit by default, if an environment is not explicitly given.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_INHERITED_ENV_VARS: string[];
|
|
6
|
+
/**
|
|
7
|
+
* Returns a default environment object including only environment variables deemed safe to inherit.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDefaultEnvs(): {};
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.DEFAULT_INHERITED_ENV_VARS = void 0;
|
|
8
|
+
exports.getDefaultEnvs = getDefaultEnvs;
|
|
9
|
+
/**
|
|
10
|
+
* Bash MCP Server v1.0
|
|
11
|
+
*/
|
|
12
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
13
|
+
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
14
|
+
const path_1 = __importDefault(require("path"));
|
|
15
|
+
const zod_1 = __importDefault(require("zod"));
|
|
16
|
+
const web_tree_sitter_1 = require("web-tree-sitter");
|
|
17
|
+
// @ts-ignore wasm
|
|
18
|
+
const web_tree_sitter_wasm_1 = __importDefault(require("web-tree-sitter/web-tree-sitter.wasm"));
|
|
19
|
+
// @ts-ignore wasm
|
|
20
|
+
const tree_sitter_bash_wasm_1 = __importDefault(require("tree-sitter-bash/tree-sitter-bash.wasm"));
|
|
21
|
+
const child_process_1 = require("child_process");
|
|
22
|
+
const bash_txt_1 = __importDefault(require("../bash.txt"));
|
|
23
|
+
const mcp_helpers_1 = require("@baitong-dev/mcp-helpers");
|
|
24
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
25
|
+
const MCP_NAME = 'Bash MCP';
|
|
26
|
+
const MCP_VERSION = package_json_1.default.version;
|
|
27
|
+
const parser = (0, mcp_helpers_1.lazy)(async () => {
|
|
28
|
+
const treePath = (0, mcp_helpers_1.resolveWasm)(web_tree_sitter_wasm_1.default);
|
|
29
|
+
await web_tree_sitter_1.Parser.init({
|
|
30
|
+
locateFile() {
|
|
31
|
+
return treePath;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const bashPath = (0, mcp_helpers_1.resolveWasm)(tree_sitter_bash_wasm_1.default);
|
|
35
|
+
const bashLanguage = await web_tree_sitter_1.Language.load(bashPath);
|
|
36
|
+
const p = new web_tree_sitter_1.Parser();
|
|
37
|
+
p.setLanguage(bashLanguage);
|
|
38
|
+
return p;
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Get the path to the bash executable
|
|
42
|
+
* @returns The path to the bash executable
|
|
43
|
+
*/
|
|
44
|
+
const getBashPath = (0, mcp_helpers_1.lazy)(() => {
|
|
45
|
+
const shell = process.env.SHELL;
|
|
46
|
+
if (shell)
|
|
47
|
+
return shell;
|
|
48
|
+
// Fallback: check common Git Bash paths directly
|
|
49
|
+
if (process.platform === 'win32') {
|
|
50
|
+
const bash = (0, mcp_helpers_1.findGitBash)((0, mcp_helpers_1.getBundledBinaryPath)('bash'));
|
|
51
|
+
if (bash)
|
|
52
|
+
return bash;
|
|
53
|
+
throw new Error('Git Bash not found');
|
|
54
|
+
// return process.env.COMSPEC || "cmd.exe"
|
|
55
|
+
}
|
|
56
|
+
if (process.platform === 'darwin')
|
|
57
|
+
return '/bin/zsh';
|
|
58
|
+
const bash = Bun.which('bash');
|
|
59
|
+
if (bash)
|
|
60
|
+
return bash;
|
|
61
|
+
return '/bin/sh';
|
|
62
|
+
});
|
|
63
|
+
const server = new mcp_js_1.McpServer({
|
|
64
|
+
name: MCP_NAME,
|
|
65
|
+
version: MCP_VERSION
|
|
66
|
+
}, {
|
|
67
|
+
capabilities: {
|
|
68
|
+
logging: {}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
// =============================================================================
|
|
72
|
+
// Constants
|
|
73
|
+
// =============================================================================
|
|
74
|
+
// Default working directory
|
|
75
|
+
const DEFAULT_CWD = path_1.default.join(mcp_helpers_1.MCP_HOME_DIR, 'workspace');
|
|
76
|
+
// Grace period before SIGKILL (ms)
|
|
77
|
+
const SIGKILL_TIMEOUT_MS = 5000;
|
|
78
|
+
// Reserved characters for truncation ellipsis
|
|
79
|
+
const TRUNCATION_ELLIPSIS_RESERVE = 50;
|
|
80
|
+
// Exit code for timeout (shell convention)
|
|
81
|
+
const EXIT_CODE_TIMEOUT = 124;
|
|
82
|
+
// Sudo rejection message
|
|
83
|
+
const SUDO_REJECTION_MESSAGE = '[REJECTED] sudo commands cannot be executed. Please STOP and ask human user to run it for you!\n';
|
|
84
|
+
// Default max_output
|
|
85
|
+
const DEFAULT_MAX_OUTPUT = 30000;
|
|
86
|
+
const DEFAULT_TIMEOUT_MS = 2 * 60 * 1000;
|
|
87
|
+
/**
|
|
88
|
+
* Check if a command contains sudo
|
|
89
|
+
* Matches: sudo at start, after semicolon, after &&, after ||, after |, after $(, after backtick
|
|
90
|
+
*/
|
|
91
|
+
function containsSudo(command) {
|
|
92
|
+
// Match sudo as a standalone command (not part of another word like "pseudocode")
|
|
93
|
+
return /(?:^|[;&|`$()]\s*)sudo(?:\s|$)/m.test(command);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Environment variables to inherit by default, if an environment is not explicitly given.
|
|
97
|
+
*/
|
|
98
|
+
exports.DEFAULT_INHERITED_ENV_VARS = process.platform === 'win32'
|
|
99
|
+
? [
|
|
100
|
+
'APPDATA',
|
|
101
|
+
'HOMEDRIVE',
|
|
102
|
+
'HOMEPATH',
|
|
103
|
+
'LOCALAPPDATA',
|
|
104
|
+
'PATH',
|
|
105
|
+
'PROCESSOR_ARCHITECTURE',
|
|
106
|
+
'SYSTEMDRIVE',
|
|
107
|
+
'SYSTEMROOT',
|
|
108
|
+
'TEMP',
|
|
109
|
+
'TMPDIR',
|
|
110
|
+
'USERNAME',
|
|
111
|
+
'USERPROFILE',
|
|
112
|
+
'PROGRAMFILES'
|
|
113
|
+
]
|
|
114
|
+
: /* list inspired by the default env inheritance of sudo */
|
|
115
|
+
['HOME', 'LOGNAME', 'PATH', 'SHELL', 'TERM', 'USER'];
|
|
116
|
+
/**
|
|
117
|
+
* Returns a default environment object including only environment variables deemed safe to inherit.
|
|
118
|
+
*/
|
|
119
|
+
function getDefaultEnvs() {
|
|
120
|
+
const env = {};
|
|
121
|
+
for (const key of exports.DEFAULT_INHERITED_ENV_VARS) {
|
|
122
|
+
const value = process.env[key];
|
|
123
|
+
if (value === undefined) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (value.startsWith('()')) {
|
|
127
|
+
// Skip functions, which are a security risk.
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
env[key] = value;
|
|
131
|
+
}
|
|
132
|
+
return env;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Middle-truncate output to preserve beginning and end
|
|
136
|
+
*/
|
|
137
|
+
function middleTruncate(text, maxLength) {
|
|
138
|
+
if (text.length <= maxLength)
|
|
139
|
+
return text;
|
|
140
|
+
const halfLength = Math.floor((maxLength - TRUNCATION_ELLIPSIS_RESERVE) / 2);
|
|
141
|
+
const start = text.slice(0, halfLength);
|
|
142
|
+
const end = text.slice(-halfLength);
|
|
143
|
+
const truncatedBytes = text.length - maxLength;
|
|
144
|
+
return `${start}\n\n... [truncated ${truncatedBytes} characters] ...\n\n${end}`;
|
|
145
|
+
}
|
|
146
|
+
// =============================================================================
|
|
147
|
+
// Tool Definitions
|
|
148
|
+
// =============================================================================
|
|
149
|
+
const bashInputSchema = zod_1.default.object({
|
|
150
|
+
command: zod_1.default.string().min(1).describe('The command to execute'),
|
|
151
|
+
cwd: zod_1.default
|
|
152
|
+
.string()
|
|
153
|
+
.optional()
|
|
154
|
+
.describe(`The working directory to run the command in. Must be in ${DEFAULT_CWD}. Defaults to ${DEFAULT_CWD}. Use this instead of 'cd' commands.`), // 需要在DEFAULT_CWD下
|
|
155
|
+
timeout: zod_1.default
|
|
156
|
+
.number()
|
|
157
|
+
.min(1)
|
|
158
|
+
.max(600000)
|
|
159
|
+
.optional()
|
|
160
|
+
.describe(`Timeout in milliseconds (optional, default ${DEFAULT_TIMEOUT_MS}, max 600000)`),
|
|
161
|
+
description: zod_1.default
|
|
162
|
+
.string()
|
|
163
|
+
.optional()
|
|
164
|
+
.describe('Clear, concise description of what this command does in 5-10 words. Examples:\nInput: ls\nOutput: Lists files in current directory\n\nInput: git status\nOutput: Shows working tree status\n\nInput: npm install\nOutput: Installs package dependencies\n\nInput: mkdir foo\nOutput: Creates directory "foo"'),
|
|
165
|
+
env: zod_1.default
|
|
166
|
+
.record(zod_1.default.string(), zod_1.default.string())
|
|
167
|
+
.optional()
|
|
168
|
+
.describe('Additional environment variables to set'),
|
|
169
|
+
maxOutput: zod_1.default
|
|
170
|
+
.number()
|
|
171
|
+
.min(1)
|
|
172
|
+
.max(1000000)
|
|
173
|
+
.optional()
|
|
174
|
+
.describe(`Maximum output length before middle-truncation (default ${DEFAULT_MAX_OUTPUT})`)
|
|
175
|
+
});
|
|
176
|
+
async function killTree(proc, opts) {
|
|
177
|
+
const pid = proc.pid;
|
|
178
|
+
if (!pid || opts?.exited?.())
|
|
179
|
+
return;
|
|
180
|
+
if (process.platform === 'win32') {
|
|
181
|
+
await new Promise(resolve => {
|
|
182
|
+
const killer = (0, child_process_1.spawn)('taskkill', ['/pid', String(pid), '/f', '/t'], { stdio: 'ignore' });
|
|
183
|
+
killer.once('exit', () => resolve());
|
|
184
|
+
killer.once('error', () => resolve());
|
|
185
|
+
});
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
process.kill(-pid, 'SIGTERM');
|
|
190
|
+
await Bun.sleep(SIGKILL_TIMEOUT_MS);
|
|
191
|
+
if (!opts?.exited?.()) {
|
|
192
|
+
process.kill(-pid, 'SIGKILL');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (_e) {
|
|
196
|
+
proc.kill('SIGTERM');
|
|
197
|
+
await Bun.sleep(SIGKILL_TIMEOUT_MS);
|
|
198
|
+
if (!opts?.exited?.()) {
|
|
199
|
+
proc.kill('SIGKILL');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Execute a bash command and return result
|
|
205
|
+
*/
|
|
206
|
+
async function executeBash(options) {
|
|
207
|
+
const command = options.command.trim();
|
|
208
|
+
const cwd = options.cwd || DEFAULT_CWD;
|
|
209
|
+
const timeout = options.timeout || DEFAULT_TIMEOUT_MS;
|
|
210
|
+
const maxOutput = options.maxOutput || DEFAULT_MAX_OUTPUT;
|
|
211
|
+
if (containsSudo(command)) {
|
|
212
|
+
throw Error(`${SUDO_REJECTION_MESSAGE}$ ${command}`);
|
|
213
|
+
}
|
|
214
|
+
if (!(0, mcp_helpers_1.isSubdirectory)(DEFAULT_CWD, cwd, {
|
|
215
|
+
includeSelf: true
|
|
216
|
+
})) {
|
|
217
|
+
throw Error(`"${cwd}" must be in ${DEFAULT_CWD}`);
|
|
218
|
+
}
|
|
219
|
+
if (!(await (0, mcp_helpers_1.isDir)(cwd))) {
|
|
220
|
+
throw Error(`"${cwd}" must be a directory`);
|
|
221
|
+
}
|
|
222
|
+
const tree = await parser().then(p => p.parse(command));
|
|
223
|
+
if (!tree) {
|
|
224
|
+
throw new Error('Failed to parse command');
|
|
225
|
+
}
|
|
226
|
+
const directories = new Set();
|
|
227
|
+
// if (!Instance.containsPath(cwd)) directories.add(cwd)
|
|
228
|
+
const patterns = new Set();
|
|
229
|
+
// const always = new Set<string>()
|
|
230
|
+
let output = '';
|
|
231
|
+
for (const node of tree.rootNode.descendantsOfType('command')) {
|
|
232
|
+
if (!node)
|
|
233
|
+
continue;
|
|
234
|
+
// Get full command text including redirects if present
|
|
235
|
+
let commandText = node.parent?.type === 'redirected_statement' ? node.parent.text : node.text;
|
|
236
|
+
const commands = [];
|
|
237
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
238
|
+
const child = node.child(i);
|
|
239
|
+
if (!child)
|
|
240
|
+
continue;
|
|
241
|
+
if (child.type !== 'command_name' &&
|
|
242
|
+
child.type !== 'word' &&
|
|
243
|
+
child.type !== 'string' &&
|
|
244
|
+
child.type !== 'raw_string' &&
|
|
245
|
+
child.type !== 'concatenation') {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
commands.push(child.text);
|
|
249
|
+
}
|
|
250
|
+
// not an exhaustive list, but covers most common cases
|
|
251
|
+
if (['cd', 'rm', 'cp', 'mv', 'mkdir', 'touch', 'chmod', 'chown', 'cat'].includes(commands[0])) {
|
|
252
|
+
for (const arg of commands.slice(1)) {
|
|
253
|
+
if (arg.startsWith('-') || (commands[0] === 'chmod' && arg.startsWith('+'))) {
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
const resolved = await Bun.$ `realpath ${arg}`
|
|
257
|
+
.cwd(cwd)
|
|
258
|
+
.quiet()
|
|
259
|
+
.nothrow()
|
|
260
|
+
.text()
|
|
261
|
+
.then(x => x.trim());
|
|
262
|
+
if (resolved) {
|
|
263
|
+
// Git Bash on Windows returns Unix-style paths like /c/Users/...
|
|
264
|
+
const normalized = process.platform === 'win32' && resolved.match(/^\/[a-z]\//)
|
|
265
|
+
? (0, mcp_helpers_1.gitBashToWindowsPath)(resolved)
|
|
266
|
+
: resolved;
|
|
267
|
+
if (!(0, mcp_helpers_1.isSubdirectory)(DEFAULT_CWD, normalized, { includeSelf: true })) {
|
|
268
|
+
const dir = (await (0, mcp_helpers_1.isDir)(normalized)) ? normalized : path_1.default.dirname(normalized);
|
|
269
|
+
directories.add(dir);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
}
|
|
276
|
+
// cd covered by above check
|
|
277
|
+
if (commands.length && commands[0] !== 'cd') {
|
|
278
|
+
patterns.add(commandText);
|
|
279
|
+
// always.add(BashArity.prefix(command).join(' ') + ' *')
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const mergedEnv = {
|
|
283
|
+
...getDefaultEnvs(),
|
|
284
|
+
...(0, mcp_helpers_1.getBundledBinaryEnvs)()
|
|
285
|
+
};
|
|
286
|
+
const proc = (0, child_process_1.spawn)(command, {
|
|
287
|
+
shell: getBashPath(),
|
|
288
|
+
cwd,
|
|
289
|
+
env: {
|
|
290
|
+
...mergedEnv
|
|
291
|
+
},
|
|
292
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
293
|
+
detached: process.platform !== 'win32'
|
|
294
|
+
});
|
|
295
|
+
const append = (chunk) => {
|
|
296
|
+
output += chunk.toString();
|
|
297
|
+
};
|
|
298
|
+
proc.stdout?.on('data', append);
|
|
299
|
+
proc.stderr?.on('data', append);
|
|
300
|
+
let timedOut = false;
|
|
301
|
+
let exited = false;
|
|
302
|
+
const kill = () => killTree(proc, { exited: () => exited });
|
|
303
|
+
const timeoutTimer = setTimeout(() => {
|
|
304
|
+
timedOut = true;
|
|
305
|
+
void kill();
|
|
306
|
+
}, timeout + 100);
|
|
307
|
+
await new Promise((resolve, reject) => {
|
|
308
|
+
const cleanup = () => {
|
|
309
|
+
clearTimeout(timeoutTimer);
|
|
310
|
+
};
|
|
311
|
+
proc.once('exit', () => {
|
|
312
|
+
exited = true;
|
|
313
|
+
cleanup();
|
|
314
|
+
resolve();
|
|
315
|
+
});
|
|
316
|
+
proc.once('error', error => {
|
|
317
|
+
exited = true;
|
|
318
|
+
cleanup();
|
|
319
|
+
reject(error);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
const resultMetadata = [];
|
|
323
|
+
if (timedOut) {
|
|
324
|
+
resultMetadata.push(`bash tool terminated command after exceeding timeout ${timeout} ms`);
|
|
325
|
+
}
|
|
326
|
+
if (resultMetadata.length > 0) {
|
|
327
|
+
output += '\n\n<bash_metadata>\n' + resultMetadata.join('\n') + '\n</bash_metadata>';
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
output: middleTruncate(output, maxOutput),
|
|
331
|
+
exitCode: proc.exitCode,
|
|
332
|
+
description: options.description
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
server.registerTool('bash', {
|
|
336
|
+
description: JSON.stringify({
|
|
337
|
+
...getDefaultEnvs(),
|
|
338
|
+
...(0, mcp_helpers_1.getBundledBinaryEnvs)()
|
|
339
|
+
}, null, 2) +
|
|
340
|
+
bash_txt_1.default.replaceAll('${directory}', DEFAULT_CWD)
|
|
341
|
+
.replaceAll('${maxLines}', String(2000))
|
|
342
|
+
.replaceAll('${maxBytes}', String(50 * 1024)),
|
|
343
|
+
inputSchema: bashInputSchema
|
|
344
|
+
}, async (args) => {
|
|
345
|
+
try {
|
|
346
|
+
const result = await executeBash({ ...args });
|
|
347
|
+
return {
|
|
348
|
+
content: [{ type: 'text', text: JSON.stringify(result) }],
|
|
349
|
+
structuredContent: result
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
return {
|
|
354
|
+
isError: true,
|
|
355
|
+
content: [{ type: 'text', text: error instanceof Error ? error.message : String(error) }]
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
// =============================================================================
|
|
360
|
+
// Start Server
|
|
361
|
+
// =============================================================================
|
|
362
|
+
const transport = new stdio_js_1.StdioServerTransport();
|
|
363
|
+
server.connect(transport);
|
|
364
|
+
console.error(`${MCP_NAME} Server v${MCP_VERSION} running`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baitong-dev/bash-mcp",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"bin": {
|
|
6
6
|
"@baitong-dev/bash-mcp": "./dist/index.js"
|
|
@@ -20,7 +20,10 @@
|
|
|
20
20
|
"tree-sitter-bash": "^0.25.1",
|
|
21
21
|
"web-tree-sitter": "^0.26.5",
|
|
22
22
|
"zod": "^4.3.4",
|
|
23
|
-
"@baitong-dev/mcp-helpers": "0.0.
|
|
23
|
+
"@baitong-dev/mcp-helpers": "0.0.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"typescript": "^5.9.2"
|
|
24
27
|
},
|
|
25
28
|
"publishConfig": {
|
|
26
29
|
"access": "public",
|