@pyrokine/mcp-ssh 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/file-ops.js +106 -31
- package/dist/session-manager.d.ts +1 -0
- package/dist/session-manager.js +16 -3
- package/dist/tools/connection.js +11 -9
- package/dist/tools/exec.js +18 -7
- package/dist/tools/file.js +10 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.js +2 -1
- package/package.json +5 -5
package/dist/file-ops.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SSH File Operations - 文件操作
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { spawn } from 'child_process';
|
|
5
5
|
import { createHash } from 'crypto';
|
|
6
6
|
import * as fs from 'fs';
|
|
7
7
|
import * as os from 'os';
|
|
@@ -15,7 +15,57 @@ const S_IFREG = 0o100000;
|
|
|
15
15
|
const S_IFLNK = 0o120000;
|
|
16
16
|
// rsync 可用性缓存(per alias,连接存续期内不变,disconnect 时由 SessionManager 主动清理)
|
|
17
17
|
const RSYNC_CACHE_TTL_MS = 5 * 60_000;
|
|
18
|
+
const LOCAL_PROCESS_OUTPUT_LIMIT = 200_000;
|
|
18
19
|
const rsyncCache = new Map();
|
|
20
|
+
function appendLimitedOutput(current, chunk) {
|
|
21
|
+
if (current.length >= LOCAL_PROCESS_OUTPUT_LIMIT) {
|
|
22
|
+
return current;
|
|
23
|
+
}
|
|
24
|
+
return (current + chunk).slice(0, LOCAL_PROCESS_OUTPUT_LIMIT);
|
|
25
|
+
}
|
|
26
|
+
function runLocalProcess(command, args, timeout) {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
let stdout = '';
|
|
29
|
+
let stderr = '';
|
|
30
|
+
let settled = false;
|
|
31
|
+
let timedOut = false;
|
|
32
|
+
let killTimer;
|
|
33
|
+
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
34
|
+
const timer = setTimeout(() => {
|
|
35
|
+
timedOut = true;
|
|
36
|
+
child.kill('SIGTERM');
|
|
37
|
+
killTimer = setTimeout(() => child.kill('SIGKILL'), 5000);
|
|
38
|
+
}, timeout);
|
|
39
|
+
child.stdout.on('data', (chunk) => {
|
|
40
|
+
stdout = appendLimitedOutput(stdout, chunk.toString('utf-8'));
|
|
41
|
+
});
|
|
42
|
+
child.stderr.on('data', (chunk) => {
|
|
43
|
+
stderr = appendLimitedOutput(stderr, chunk.toString('utf-8'));
|
|
44
|
+
});
|
|
45
|
+
child.on('error', (error) => {
|
|
46
|
+
if (settled) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
settled = true;
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
if (killTimer) {
|
|
52
|
+
clearTimeout(killTimer);
|
|
53
|
+
}
|
|
54
|
+
resolve({ status: null, stdout, stderr, error, timedOut });
|
|
55
|
+
});
|
|
56
|
+
child.on('close', (status) => {
|
|
57
|
+
if (settled) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
settled = true;
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
if (killTimer) {
|
|
63
|
+
clearTimeout(killTimer);
|
|
64
|
+
}
|
|
65
|
+
resolve({ status, stdout, stderr, timedOut });
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
19
69
|
/** 由 SessionManager.disconnect 调用,防止 alias 重连后读到旧主机的判断 */
|
|
20
70
|
export function clearRsyncCache(alias) {
|
|
21
71
|
if (alias === undefined) {
|
|
@@ -25,8 +75,39 @@ export function clearRsyncCache(alias) {
|
|
|
25
75
|
rsyncCache.delete(alias);
|
|
26
76
|
}
|
|
27
77
|
}
|
|
78
|
+
function positiveIntegerEnv(name, defaultValue) {
|
|
79
|
+
const value = Number(process.env[name]);
|
|
80
|
+
return Number.isInteger(value) && value > 0 ? value : defaultValue;
|
|
81
|
+
}
|
|
28
82
|
/** SFTP 并发上限,避免单 SSH session 上太多 channel */
|
|
29
|
-
const SFTP_PARALLEL_LIMIT =
|
|
83
|
+
const SFTP_PARALLEL_LIMIT = positiveIntegerEnv('SSH_MCP_SFTP_PARALLEL', 8);
|
|
84
|
+
const SFTP_SYNC_MAX_FILES = positiveIntegerEnv('SSH_MCP_SFTP_MAX_FILES', 10_000);
|
|
85
|
+
const SFTP_SYNC_MAX_DIRECTORIES = positiveIntegerEnv('SSH_MCP_SFTP_MAX_DIRECTORIES', 2_000);
|
|
86
|
+
const SFTP_SYNC_MAX_DEPTH = positiveIntegerEnv('SSH_MCP_SFTP_MAX_DEPTH', 64);
|
|
87
|
+
const SFTP_SYNC_MAX_BYTES = positiveIntegerEnv('SSH_MCP_SFTP_MAX_BYTES', 10 * 1024 * 1024 * 1024);
|
|
88
|
+
function assertSftpTraversalBudget(state, depth, currentPath) {
|
|
89
|
+
if (depth > SFTP_SYNC_MAX_DEPTH) {
|
|
90
|
+
throw new Error(`SFTP sync traversal depth exceeded at ${currentPath} (max ${SFTP_SYNC_MAX_DEPTH})`);
|
|
91
|
+
}
|
|
92
|
+
if (state.files > SFTP_SYNC_MAX_FILES) {
|
|
93
|
+
throw new Error(`SFTP sync file count exceeded at ${currentPath} (max ${SFTP_SYNC_MAX_FILES})`);
|
|
94
|
+
}
|
|
95
|
+
if (state.directories > SFTP_SYNC_MAX_DIRECTORIES) {
|
|
96
|
+
throw new Error(`SFTP sync directory count exceeded at ${currentPath} (max ${SFTP_SYNC_MAX_DIRECTORIES})`);
|
|
97
|
+
}
|
|
98
|
+
if (state.bytes > SFTP_SYNC_MAX_BYTES) {
|
|
99
|
+
throw new Error(`SFTP sync byte budget exceeded at ${currentPath} (max ${SFTP_SYNC_MAX_BYTES})`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function recordSftpDirectory(state, depth, currentPath) {
|
|
103
|
+
state.directories++;
|
|
104
|
+
assertSftpTraversalBudget(state, depth, currentPath);
|
|
105
|
+
}
|
|
106
|
+
function recordSftpFile(state, size, depth, currentPath) {
|
|
107
|
+
state.files++;
|
|
108
|
+
state.bytes += size;
|
|
109
|
+
assertSftpTraversalBudget(state, depth, currentPath);
|
|
110
|
+
}
|
|
30
111
|
/**
|
|
31
112
|
* 简单的并发控制:把异步任务限制在 limit 个 in-flight,串行入队
|
|
32
113
|
*/
|
|
@@ -566,17 +647,9 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
566
647
|
* 通过本地执行 rsync 连接到远程(需要密钥认证或 ssh-agent)
|
|
567
648
|
*/
|
|
568
649
|
async function syncWithRsync(alias, localPath, remotePath, direction, options) {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
const cmd = os.platform() === 'win32' ? 'where rsync' : 'which rsync';
|
|
573
|
-
execSync(cmd, { stdio: 'pipe' });
|
|
574
|
-
hasLocalRsync = true;
|
|
575
|
-
}
|
|
576
|
-
catch {
|
|
577
|
-
// rsync 不可用,由 hasLocalRsync 标记处理
|
|
578
|
-
}
|
|
579
|
-
if (!hasLocalRsync) {
|
|
650
|
+
const checkCommand = os.platform() === 'win32' ? 'where' : 'which';
|
|
651
|
+
const checkResult = await runLocalProcess(checkCommand, ['rsync'], 10_000);
|
|
652
|
+
if (checkResult.status !== 0) {
|
|
580
653
|
// 本地没有 rsync,回退到 SFTP
|
|
581
654
|
return syncWithSftp(alias, localPath, remotePath, direction, options);
|
|
582
655
|
}
|
|
@@ -647,14 +720,12 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options) {
|
|
|
647
720
|
? [...args, '-e', sshCmd, '--', localPath, remoteSpec]
|
|
648
721
|
: [...args, '-e', sshCmd, '--', remoteSpec, localPath];
|
|
649
722
|
try {
|
|
650
|
-
const result =
|
|
651
|
-
encoding: 'utf-8',
|
|
652
|
-
timeout: 600000,
|
|
653
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
654
|
-
});
|
|
723
|
+
const result = await runLocalProcess('rsync', rsyncArgs, 600000);
|
|
655
724
|
if (result.error || result.status !== 0) {
|
|
656
|
-
const stderr =
|
|
657
|
-
const reason = result.
|
|
725
|
+
const stderr = result.stderr.trim();
|
|
726
|
+
const reason = result.timedOut
|
|
727
|
+
? 'timed out after 600000ms'
|
|
728
|
+
: (result.error?.message ?? `exit code ${result.status}`);
|
|
658
729
|
const fallback = await syncWithSftp(alias, localPath, remotePath, direction, options);
|
|
659
730
|
const warning = `rsync 失败原因: ${reason}${stderr ? ` | stderr: ${stderr}` : ''} | 已 fallback 到 SFTP(性能可能较差)`;
|
|
660
731
|
console.warn(`[mcp-ssh] ${warning}`);
|
|
@@ -663,7 +734,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options) {
|
|
|
663
734
|
output: fallback.output ? `${warning}\n\n${fallback.output}` : warning,
|
|
664
735
|
};
|
|
665
736
|
}
|
|
666
|
-
const output = result.stdout
|
|
737
|
+
const output = result.stdout;
|
|
667
738
|
const stats = parseRsyncStats(output);
|
|
668
739
|
const filesTransferred = stats.added + stats.updated;
|
|
669
740
|
return {
|
|
@@ -864,7 +935,7 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
|
|
|
864
935
|
};
|
|
865
936
|
}
|
|
866
937
|
if (stats.isDirectory() && options.recursive !== false) {
|
|
867
|
-
const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false);
|
|
938
|
+
const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0);
|
|
868
939
|
return buildDirectorySyncResult(result, warnings);
|
|
869
940
|
}
|
|
870
941
|
const existed = await remoteExists(alias, remotePath);
|
|
@@ -877,7 +948,11 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
|
|
|
877
948
|
// download
|
|
878
949
|
const info = await getFileInfo(alias, remotePath);
|
|
879
950
|
if (info.isDirectory && options.recursive !== false) {
|
|
880
|
-
const result = await downloadDirectory(alias, remotePath, localPath, options.exclude
|
|
951
|
+
const result = await downloadDirectory(alias, remotePath, localPath, options.exclude, undefined, {
|
|
952
|
+
files: 0,
|
|
953
|
+
directories: 0,
|
|
954
|
+
bytes: 0,
|
|
955
|
+
}, 0);
|
|
881
956
|
return buildDirectorySyncResult(result, warnings);
|
|
882
957
|
}
|
|
883
958
|
const existed = fs.existsSync(localPath);
|
|
@@ -899,13 +974,12 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
|
|
|
899
974
|
/**
|
|
900
975
|
* 递归上传目录
|
|
901
976
|
*/
|
|
902
|
-
async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false) {
|
|
977
|
+
async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0) {
|
|
903
978
|
const accumulator = new DirectoryTransferAccumulator();
|
|
904
|
-
|
|
979
|
+
recordSftpDirectory(traversal, depth, localPath);
|
|
905
980
|
const ownSftp = !sharedSftp;
|
|
906
981
|
const sftp = sharedSftp ?? (await sessionManager.getSftp(alias));
|
|
907
982
|
try {
|
|
908
|
-
// 确保远程目录存在
|
|
909
983
|
await mkdir(alias, remotePath, true);
|
|
910
984
|
const items = fs.readdirSync(localPath);
|
|
911
985
|
// 先分类:目录串行(递归会自己并发文件);文件并发上传,避免单 SSH session 上 channel 过多
|
|
@@ -926,6 +1000,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
926
1000
|
subDirItems.push(item);
|
|
927
1001
|
}
|
|
928
1002
|
else if (stats.isFile()) {
|
|
1003
|
+
recordSftpFile(traversal, stats.size, depth, itemLocalPath);
|
|
929
1004
|
fileItems.push({ name: item, stats });
|
|
930
1005
|
}
|
|
931
1006
|
}
|
|
@@ -947,7 +1022,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
947
1022
|
for (const item of subDirItems) {
|
|
948
1023
|
const itemLocalPath = path.join(localPath, item);
|
|
949
1024
|
const itemRemotePath = path.posix.join(remotePath, item);
|
|
950
|
-
const result = await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks);
|
|
1025
|
+
const result = await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1);
|
|
951
1026
|
accumulator.merge(result);
|
|
952
1027
|
}
|
|
953
1028
|
}
|
|
@@ -961,13 +1036,12 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
961
1036
|
/**
|
|
962
1037
|
* 递归下载目录
|
|
963
1038
|
*/
|
|
964
|
-
async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp) {
|
|
1039
|
+
async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0) {
|
|
965
1040
|
const accumulator = new DirectoryTransferAccumulator();
|
|
966
|
-
|
|
1041
|
+
recordSftpDirectory(traversal, depth, remotePath);
|
|
967
1042
|
const ownSftp = !sharedSftp;
|
|
968
1043
|
const sftp = sharedSftp ?? (await sessionManager.getSftp(alias));
|
|
969
1044
|
try {
|
|
970
|
-
// 确保本地目录存在
|
|
971
1045
|
if (!fs.existsSync(localPath)) {
|
|
972
1046
|
fs.mkdirSync(localPath, { recursive: true });
|
|
973
1047
|
}
|
|
@@ -984,6 +1058,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
984
1058
|
dirItems.push(item);
|
|
985
1059
|
}
|
|
986
1060
|
else if (item.isFile) {
|
|
1061
|
+
recordSftpFile(traversal, item.size, depth, item.path);
|
|
987
1062
|
fileItems.push(item);
|
|
988
1063
|
}
|
|
989
1064
|
else if (item.isSymlink) {
|
|
@@ -1007,7 +1082,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
1007
1082
|
});
|
|
1008
1083
|
for (const item of dirItems) {
|
|
1009
1084
|
const itemLocalPath = path.join(localPath, item.name);
|
|
1010
|
-
const result = await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp);
|
|
1085
|
+
const result = await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1);
|
|
1011
1086
|
accumulator.merge(result);
|
|
1012
1087
|
}
|
|
1013
1088
|
for (const item of symlinkItems) {
|
package/dist/session-manager.js
CHANGED
|
@@ -16,6 +16,7 @@ import { ForwardManager } from './forward-manager.js';
|
|
|
16
16
|
import { PtyManager } from './pty-manager.js';
|
|
17
17
|
import { sanitizeKeyError, validateKeyFile } from './security.js';
|
|
18
18
|
import { escapeShellArg, expandTilde } from './tools/utils.js';
|
|
19
|
+
import { DEFAULT_EXEC_MAX_OUTPUT_SIZE, HARD_EXEC_MAX_OUTPUT_SIZE, } from './types.js';
|
|
19
20
|
const OUTPUT_PREVIEW_CHARS = 4096;
|
|
20
21
|
class CommandError extends Error {
|
|
21
22
|
details;
|
|
@@ -36,7 +37,7 @@ export class SessionManager {
|
|
|
36
37
|
defaultKeepaliveCountMax = 3;
|
|
37
38
|
defaultTimeout = 30000; // 30秒
|
|
38
39
|
maxReconnectAttempts = 3;
|
|
39
|
-
defaultMaxOutputSize =
|
|
40
|
+
defaultMaxOutputSize = DEFAULT_EXEC_MAX_OUTPUT_SIZE;
|
|
40
41
|
reconnectDelay = 5000; // 5秒
|
|
41
42
|
constructor(persistPath) {
|
|
42
43
|
this.persistPath = persistPath || path.join(os.homedir(), '.ssh-mcp-pro', 'sessions.json');
|
|
@@ -203,7 +204,7 @@ export class SessionManager {
|
|
|
203
204
|
const session = this.getSession(alias);
|
|
204
205
|
const startTime = Date.now();
|
|
205
206
|
const timeout = options.timeout ?? this.defaultTimeout;
|
|
206
|
-
const maxOutputSize = options.maxOutputSize
|
|
207
|
+
const maxOutputSize = this.normalizeMaxOutputSize(options.maxOutputSize);
|
|
207
208
|
const fullCommand = this.buildCommand(`sudo -S ${command}`, session, options);
|
|
208
209
|
return new Promise((resolve, reject) => {
|
|
209
210
|
let settled = false;
|
|
@@ -397,10 +398,22 @@ export class SessionManager {
|
|
|
397
398
|
: undefined,
|
|
398
399
|
};
|
|
399
400
|
}
|
|
401
|
+
normalizeMaxOutputSize(value) {
|
|
402
|
+
const maxOutputSize = value ?? this.defaultMaxOutputSize;
|
|
403
|
+
if (!Number.isInteger(maxOutputSize) || maxOutputSize <= 0 || maxOutputSize > HARD_EXEC_MAX_OUTPUT_SIZE) {
|
|
404
|
+
throw new CommandError('Invalid maxOutputSize', {
|
|
405
|
+
maxOutputSize,
|
|
406
|
+
min: 1,
|
|
407
|
+
max: HARD_EXEC_MAX_OUTPUT_SIZE,
|
|
408
|
+
suggestion: '使用 1 到 HARD_EXEC_MAX_OUTPUT_SIZE 之间的整数,超大输出请重定向到远端文件后用 ssh_read_file 分块读取',
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
return maxOutputSize;
|
|
412
|
+
}
|
|
400
413
|
async execDirect(alias, command, options = {}, context = {}) {
|
|
401
414
|
const session = this.getSession(alias);
|
|
402
415
|
const startTime = Date.now();
|
|
403
|
-
const maxOutputSize = options.maxOutputSize
|
|
416
|
+
const maxOutputSize = this.normalizeMaxOutputSize(options.maxOutputSize);
|
|
404
417
|
const fullCommand = this.buildCommand(command, session, options);
|
|
405
418
|
return new Promise((resolve, reject) => {
|
|
406
419
|
const timeout = options.timeout ?? this.defaultTimeout;
|
package/dist/tools/connection.js
CHANGED
|
@@ -266,15 +266,17 @@ async function handleConfigList(args) {
|
|
|
266
266
|
// ========== Register ==========
|
|
267
267
|
export function registerConnectionTools(server) {
|
|
268
268
|
server.registerTool('ssh_connect', {
|
|
269
|
-
description:
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
-
|
|
277
|
-
-
|
|
269
|
+
description: [
|
|
270
|
+
'建立 SSH 连接并保持会话,支持密码、密钥认证,支持跳板机',
|
|
271
|
+
'',
|
|
272
|
+
'可通过 configHost 参数使用 ~/.ssh/config 中的配置,无需重复填写连接信息',
|
|
273
|
+
'支持 Host 多别名、Host * 全局默认继承、ProxyJump(user@host:port 格式)',
|
|
274
|
+
'',
|
|
275
|
+
'示例',
|
|
276
|
+
'- 使用 ssh config,传入 configHost 为 "myserver"',
|
|
277
|
+
'- 密钥认证,传入 host、user、keyPath',
|
|
278
|
+
'- 跳板机,传入 host、user、keyPath 和 jumpHost 配置',
|
|
279
|
+
].join('\n'),
|
|
278
280
|
inputSchema: connectSchema,
|
|
279
281
|
}, (args) => handleConnect(args));
|
|
280
282
|
server.registerTool('ssh_disconnect', {
|
package/dist/tools/exec.js
CHANGED
|
@@ -7,8 +7,16 @@
|
|
|
7
7
|
import { z } from 'zod';
|
|
8
8
|
import * as fileOps from '../file-ops.js';
|
|
9
9
|
import { sessionManager } from '../session-manager.js';
|
|
10
|
+
import { HARD_EXEC_MAX_OUTPUT_SIZE } from '../types.js';
|
|
10
11
|
import { escapeShellArg, formatError, formatResult } from './utils.js'; // ========== Schemas ==========
|
|
11
12
|
// ========== Schemas ==========
|
|
13
|
+
const maxOutputSizeSchema = z
|
|
14
|
+
.number()
|
|
15
|
+
.int()
|
|
16
|
+
.positive()
|
|
17
|
+
.max(HARD_EXEC_MAX_OUTPUT_SIZE)
|
|
18
|
+
.optional()
|
|
19
|
+
.describe('最大输出字符数,超过后截断并返回提示');
|
|
12
20
|
const execSchema = z.object({
|
|
13
21
|
alias: z.string().describe('连接别名'),
|
|
14
22
|
command: z.string().describe('要执行的命令'),
|
|
@@ -19,7 +27,7 @@ const execSchema = z.object({
|
|
|
19
27
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
20
28
|
env: z.record(z.string(), z.string()).optional().describe('额外环境变量'),
|
|
21
29
|
pty: z.boolean().optional().describe('是否使用 PTY 模式(用于 top 等交互式命令)'),
|
|
22
|
-
maxOutputSize:
|
|
30
|
+
maxOutputSize: maxOutputSizeSchema,
|
|
23
31
|
runAs: z.string().optional().describe('本次命令使用的目标用户,覆盖连接级 runAs'),
|
|
24
32
|
useLoginUser: z.boolean().optional().describe('设为 true 时跳过连接级 runAs,直接以登录用户执行'),
|
|
25
33
|
loadProfile: z.boolean().optional().describe('runAs 执行时是否加载目标用户 shell 配置,默认 true'),
|
|
@@ -31,7 +39,7 @@ const execAsUserSchema = z.object({
|
|
|
31
39
|
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
32
40
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
33
41
|
env: z.record(z.string(), z.string()).optional().describe('额外环境变量'),
|
|
34
|
-
maxOutputSize:
|
|
42
|
+
maxOutputSize: maxOutputSizeSchema,
|
|
35
43
|
loadProfile: z
|
|
36
44
|
.boolean()
|
|
37
45
|
.optional()
|
|
@@ -42,7 +50,7 @@ const execSudoSchema = z.object({
|
|
|
42
50
|
command: z.string().describe('要执行的命令'),
|
|
43
51
|
sudoPassword: z.string().optional().describe('sudo 密码(如果需要)'),
|
|
44
52
|
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
45
|
-
maxOutputSize:
|
|
53
|
+
maxOutputSize: maxOutputSizeSchema,
|
|
46
54
|
});
|
|
47
55
|
const execBatchSchema = z.object({
|
|
48
56
|
alias: z.string().describe('连接别名'),
|
|
@@ -74,7 +82,7 @@ const execScriptSchema = z.object({
|
|
|
74
82
|
env: z.record(z.string(), z.string()).optional().describe('额外环境变量'),
|
|
75
83
|
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
76
84
|
keepScript: z.boolean().optional().describe('是否保留远端临时脚本,默认 false'),
|
|
77
|
-
maxOutputSize:
|
|
85
|
+
maxOutputSize: maxOutputSizeSchema,
|
|
78
86
|
});
|
|
79
87
|
function pushRisk(categories, signals, category, signal) {
|
|
80
88
|
categories.add(category);
|
|
@@ -103,7 +111,7 @@ function classifyCommandRisk(command) {
|
|
|
103
111
|
if (pipeCount >= 3) {
|
|
104
112
|
pushRisk(categories, signals, 'long-running', 'long_pipeline');
|
|
105
113
|
}
|
|
106
|
-
if (/(
|
|
114
|
+
if (/(^|[^&])&(?!&)/.test(command)) {
|
|
107
115
|
pushRisk(categories, signals, 'process-control', 'background_task');
|
|
108
116
|
}
|
|
109
117
|
if (/\b(?:rm\s+-[a-zA-Z]*r|dd\s+if=|mkfs|fdisk|parted|shutdown|reboot)\b/.test(lowerCommand)) {
|
|
@@ -197,11 +205,13 @@ async function handleExecBatch(args) {
|
|
|
197
205
|
const stopOnError = args.stopOnError !== false;
|
|
198
206
|
const results = [];
|
|
199
207
|
for (let i = 0; i < args.commands.length; i++) {
|
|
208
|
+
const commandRisk = classifyCommandRisk(args.commands[i]);
|
|
200
209
|
try {
|
|
201
210
|
const execResult = await sessionManager.exec(args.alias, args.commands[i], { timeout: args.timeout });
|
|
202
211
|
results.push({
|
|
203
212
|
index: i,
|
|
204
|
-
|
|
213
|
+
commandRedacted: true,
|
|
214
|
+
commandRisk,
|
|
205
215
|
...execResult,
|
|
206
216
|
});
|
|
207
217
|
if (execResult.exitCode !== 0 && stopOnError) {
|
|
@@ -211,7 +221,8 @@ async function handleExecBatch(args) {
|
|
|
211
221
|
catch (err) {
|
|
212
222
|
results.push({
|
|
213
223
|
index: i,
|
|
214
|
-
|
|
224
|
+
commandRedacted: true,
|
|
225
|
+
commandRisk,
|
|
215
226
|
success: false,
|
|
216
227
|
error: err instanceof Error ? err.message : String(err),
|
|
217
228
|
});
|
package/dist/tools/file.js
CHANGED
|
@@ -293,7 +293,16 @@ async function handleUpload(args) {
|
|
|
293
293
|
const rename = await sessionManager.exec(args.alias, `mv -f -- ${escapeShellArg(tempRemotePath)} ${escapeShellArg(args.remotePath)}`, { timeout: 30000, useLoginUser: true, maxOutputSize: 4096 });
|
|
294
294
|
if (!rename.success) {
|
|
295
295
|
const renameError = rename.stderr || rename.stdout || `atomic rename failed with exit code ${rename.exitCode}`;
|
|
296
|
-
|
|
296
|
+
await cleanupRemotePath(args.alias, tempRemotePath).catch(() => undefined);
|
|
297
|
+
return formatResult({
|
|
298
|
+
success: false,
|
|
299
|
+
error: renameError,
|
|
300
|
+
diagnostics: {
|
|
301
|
+
local: summarizeLocalPathProbe(local),
|
|
302
|
+
remoteParent,
|
|
303
|
+
atomic: true,
|
|
304
|
+
},
|
|
305
|
+
});
|
|
297
306
|
}
|
|
298
307
|
}
|
|
299
308
|
return formatResult({
|
package/dist/types.d.ts
CHANGED
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyrokine/mcp-ssh",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A comprehensive SSH MCP Server for Claude Code",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@eslint/js": "^10.0.1",
|
|
42
|
-
"@types/node": "^25.9.
|
|
42
|
+
"@types/node": "^25.9.3",
|
|
43
43
|
"@types/ssh2": "^1.15.5",
|
|
44
|
-
"eslint": "^10.
|
|
45
|
-
"prettier": "^3.8.
|
|
44
|
+
"eslint": "^10.5.0",
|
|
45
|
+
"prettier": "^3.8.4",
|
|
46
46
|
"typescript": "^6.0.3",
|
|
47
|
-
"typescript-eslint": "^8.
|
|
47
|
+
"typescript-eslint": "^8.61.0"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": ">=18.0.0"
|