@pyrokine/mcp-ssh 2.4.3 → 3.1.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/README.md +257 -49
- package/README_zh.md +251 -68
- package/dist/command-risk.d.ts +2 -1
- package/dist/command-risk.js +238 -87
- package/dist/directory-verification.d.ts +7 -3
- package/dist/directory-verification.js +302 -208
- package/dist/file-mode.d.ts +6 -0
- package/dist/file-mode.js +71 -0
- package/dist/file-ops.d.ts +38 -7
- package/dist/file-ops.js +822 -293
- package/dist/forward-manager.d.ts +2 -0
- package/dist/forward-manager.js +19 -10
- package/dist/openssh.d.ts +51 -0
- package/dist/openssh.js +528 -0
- package/dist/operation-manager.d.ts +4 -4
- package/dist/operation-manager.js +28 -3
- package/dist/pty-manager.d.ts +5 -13
- package/dist/pty-manager.js +47 -15
- package/dist/remote-platform.d.ts +13 -0
- package/dist/remote-platform.js +132 -0
- package/dist/security.js +12 -2
- package/dist/session-manager.d.ts +31 -15
- package/dist/session-manager.js +483 -120
- package/dist/ssh-config.d.ts +3 -0
- package/dist/ssh-config.js +48 -20
- package/dist/tools/connection.js +132 -3
- package/dist/tools/exec.d.ts +1 -0
- package/dist/tools/exec.js +108 -31
- package/dist/tools/file.js +406 -129
- package/dist/tools/operation.js +1 -1
- package/dist/tools/pty.js +5 -3
- package/dist/tools/utils.js +2 -2
- package/dist/types.d.ts +63 -2
- package/dist/utf8.d.ts +1 -0
- package/dist/utf8.js +10 -0
- package/dist/windows-acl.d.ts +4 -0
- package/dist/windows-acl.js +109 -0
- package/package.json +4 -3
package/dist/ssh-config.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export interface SSHConfigHost {
|
|
|
13
13
|
user?: string;
|
|
14
14
|
port?: number;
|
|
15
15
|
identityFile?: string;
|
|
16
|
+
identityAgent?: string;
|
|
16
17
|
proxyJump?: string;
|
|
17
18
|
}
|
|
18
19
|
/** ProxyJump 解析结果 */
|
|
@@ -27,6 +28,8 @@ export interface ParsedProxyJump {
|
|
|
27
28
|
* 注意:只解析第一跳,不支持逗号分隔的多跳链路
|
|
28
29
|
*/
|
|
29
30
|
export declare function parseProxyJump(proxyJump: string): ParsedProxyJump | null;
|
|
31
|
+
/** Parse SSH config values without treating Windows path separators as escapes. */
|
|
32
|
+
export declare function parseConfigValue(value: string): string[];
|
|
30
33
|
/**
|
|
31
34
|
* 解析 SSH config 文件
|
|
32
35
|
* 支持 Host 多别名和 Host * 继承
|
package/dist/ssh-config.js
CHANGED
|
@@ -89,28 +89,52 @@ export function parseProxyJump(proxyJump) {
|
|
|
89
89
|
const { host, port } = parseHostPort(firstJump);
|
|
90
90
|
return { host, port };
|
|
91
91
|
}
|
|
92
|
-
/**
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
let
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
92
|
+
/** Parse SSH config values without treating Windows path separators as escapes. */
|
|
93
|
+
export function parseConfigValue(value) {
|
|
94
|
+
const tokens = [];
|
|
95
|
+
let token = '';
|
|
96
|
+
let quote;
|
|
97
|
+
let started = false;
|
|
98
|
+
for (let i = 0; i < value.length; ++i) {
|
|
99
|
+
const char = value[i];
|
|
100
|
+
if (char === '\\' && i + 1 < value.length && /[\s"']/.test(value[i + 1])) {
|
|
101
|
+
token += value[++i];
|
|
102
|
+
started = true;
|
|
103
|
+
}
|
|
104
|
+
else if (char === '"' || char === "'") {
|
|
105
|
+
if (quote === char) {
|
|
106
|
+
quote = undefined;
|
|
107
|
+
}
|
|
108
|
+
else if (!quote && !started) {
|
|
109
|
+
quote = char;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
token += char;
|
|
113
|
+
}
|
|
114
|
+
started = true;
|
|
105
115
|
}
|
|
106
|
-
else if (
|
|
107
|
-
|
|
116
|
+
else if (char === '#' && !quote && !started) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
else if (/\s/.test(char) && !quote) {
|
|
120
|
+
if (started) {
|
|
121
|
+
tokens.push(token);
|
|
122
|
+
token = '';
|
|
123
|
+
started = false;
|
|
124
|
+
}
|
|
108
125
|
}
|
|
109
|
-
else
|
|
110
|
-
|
|
126
|
+
else {
|
|
127
|
+
token += char;
|
|
128
|
+
started = true;
|
|
111
129
|
}
|
|
112
130
|
}
|
|
113
|
-
|
|
131
|
+
if (quote) {
|
|
132
|
+
throw new Error('ssh-config: unclosed quote');
|
|
133
|
+
}
|
|
134
|
+
if (started) {
|
|
135
|
+
tokens.push(token);
|
|
136
|
+
}
|
|
137
|
+
return tokens;
|
|
114
138
|
}
|
|
115
139
|
/**
|
|
116
140
|
* 解析 SSH config 文件
|
|
@@ -144,7 +168,8 @@ export function parseSSHConfig(configPath) {
|
|
|
144
168
|
}
|
|
145
169
|
const { key, rawValue } = match.groups;
|
|
146
170
|
const keyLower = key.toLowerCase();
|
|
147
|
-
const
|
|
171
|
+
const values = parseConfigValue(rawValue);
|
|
172
|
+
const value = values.join(' ');
|
|
148
173
|
if (keyLower === 'host') {
|
|
149
174
|
// Host 块开始,结束 Match 块
|
|
150
175
|
inMatchBlock = false;
|
|
@@ -153,7 +178,7 @@ export function parseSSHConfig(configPath) {
|
|
|
153
178
|
blocks.push(currentBlock);
|
|
154
179
|
}
|
|
155
180
|
// Host 行可能有多个别名/模式,用空格分隔
|
|
156
|
-
const patterns =
|
|
181
|
+
const patterns = values.filter((p) => p.length > 0);
|
|
157
182
|
currentBlock = { patterns, config: {} };
|
|
158
183
|
}
|
|
159
184
|
else if (keyLower === 'match') {
|
|
@@ -182,6 +207,9 @@ export function parseSSHConfig(configPath) {
|
|
|
182
207
|
case 'identityfile':
|
|
183
208
|
currentBlock.config.identityFile = expandTilde(value);
|
|
184
209
|
break;
|
|
210
|
+
case 'identityagent':
|
|
211
|
+
currentBlock.config.identityAgent = expandTilde(value);
|
|
212
|
+
break;
|
|
185
213
|
case 'proxyjump':
|
|
186
214
|
currentBlock.config.proxyJump = value;
|
|
187
215
|
break;
|
package/dist/tools/connection.js
CHANGED
|
@@ -8,10 +8,19 @@ import * as os from 'os';
|
|
|
8
8
|
import * as path from 'path';
|
|
9
9
|
import { z } from 'zod';
|
|
10
10
|
import { sessionManager } from '../session-manager.js';
|
|
11
|
+
import { OpenSshError, resolveOpenSshConfig } from '../openssh.js';
|
|
11
12
|
import { parseProxyJump, parseSSHConfig } from '../ssh-config.js';
|
|
12
13
|
import { sshPortSchema } from './schema.js';
|
|
13
14
|
import { expandTilde, formatError, formatResult } from './utils.js';
|
|
15
|
+
const remotePlatformSchema = z.enum(['posix', 'windows']);
|
|
16
|
+
const commandShellSchema = z.enum(['posix', 'cmd', 'powershell']);
|
|
17
|
+
const remotePathStyleSchema = z.enum(['posix', 'win32', 'sftp-posix']);
|
|
18
|
+
const rsyncProfileSchema = z.enum(['posix', 'native-win32', 'msys2', 'cygwin']);
|
|
14
19
|
const templateJumpHostSchema = z.object({
|
|
20
|
+
agent: z.string().optional(),
|
|
21
|
+
remotePlatform: remotePlatformSchema.optional(),
|
|
22
|
+
commandShell: commandShellSchema.optional(),
|
|
23
|
+
remotePathStyle: remotePathStyleSchema.optional(),
|
|
15
24
|
host: z.string(),
|
|
16
25
|
user: z.string(),
|
|
17
26
|
password: z.string().optional(),
|
|
@@ -20,6 +29,9 @@ const templateJumpHostSchema = z.object({
|
|
|
20
29
|
readyTimeout: z.number().int().positive().max(600000).optional(),
|
|
21
30
|
});
|
|
22
31
|
const connectionTemplateSchema = z.object({
|
|
32
|
+
backend: z.enum(['ssh2', 'openssh']).optional(),
|
|
33
|
+
controlPath: z.string().optional(),
|
|
34
|
+
configPath: z.string().optional(),
|
|
23
35
|
configHost: z.string().optional(),
|
|
24
36
|
host: z.string().optional(),
|
|
25
37
|
user: z.string().optional(),
|
|
@@ -27,6 +39,12 @@ const connectionTemplateSchema = z.object({
|
|
|
27
39
|
password: z.string().optional(),
|
|
28
40
|
keyPath: z.string().optional(),
|
|
29
41
|
privateKeyPath: z.string().optional(),
|
|
42
|
+
agent: z.string().optional(),
|
|
43
|
+
remotePlatform: remotePlatformSchema.optional(),
|
|
44
|
+
commandShell: commandShellSchema.optional(),
|
|
45
|
+
remotePathStyle: remotePathStyleSchema.optional(),
|
|
46
|
+
rsyncExecutable: z.string().optional(),
|
|
47
|
+
rsyncProfile: rsyncProfileSchema.optional(),
|
|
30
48
|
port: sshPortSchema.optional(),
|
|
31
49
|
alias: z.string().optional(),
|
|
32
50
|
env: z.record(z.string(), z.string()).optional(),
|
|
@@ -87,6 +105,11 @@ function mergeEnv(templateEnv, argsEnv) {
|
|
|
87
105
|
}
|
|
88
106
|
// ========== Schemas ==========
|
|
89
107
|
const connectSchema = z.object({
|
|
108
|
+
backend: z
|
|
109
|
+
.enum(['ssh2', 'openssh'])
|
|
110
|
+
.optional()
|
|
111
|
+
.describe('连接后端,默认 ssh2;openssh 仅复用用户已有 master,失败不新建连接'),
|
|
112
|
+
controlPath: z.string().optional().describe('OpenSSH master 的 socket 路径,默认取 ssh -G 的 ControlPath'),
|
|
90
113
|
template: z.string().optional().describe('连接模板名,从 SSH_MCP_TEMPLATES 或 ~/.mcp-ssh/templates.json 读取'),
|
|
91
114
|
configHost: z.string().optional().describe('使用 ~/.ssh/config 中的 Host 配置(推荐)'),
|
|
92
115
|
configPath: z.string().optional().describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
@@ -94,6 +117,12 @@ const connectSchema = z.object({
|
|
|
94
117
|
user: z.string().optional().describe('用户名(使用 configHost 时可省略)'),
|
|
95
118
|
password: z.string().optional().describe('密码'),
|
|
96
119
|
keyPath: z.string().optional().describe('SSH 私钥路径'),
|
|
120
|
+
agent: z.string().optional().describe('SSH agent socket 或 Windows OpenSSH named pipe 路径'),
|
|
121
|
+
remotePlatform: remotePlatformSchema.optional().describe('远端平台,默认 posix;Windows 须明确设置 windows'),
|
|
122
|
+
commandShell: commandShellSchema.optional().describe('远端命令 shell,POSIX 默认 posix,Windows 默认 powershell'),
|
|
123
|
+
remotePathStyle: remotePathStyleSchema.optional().describe('远端 SFTP 路径格式,Windows 默认 win32'),
|
|
124
|
+
rsyncExecutable: z.string().optional().describe('远端 rsync 可执行文件路径'),
|
|
125
|
+
rsyncProfile: rsyncProfileSchema.optional().describe('rsync 环境:posix、native-win32、msys2 或 cygwin'),
|
|
97
126
|
port: sshPortSchema.optional().describe('SSH 端口,默认 22'),
|
|
98
127
|
alias: z.string().optional().describe('连接别名(可选,默认使用 configHost 或 host)'),
|
|
99
128
|
env: z.record(z.string(), z.string()).optional().describe('环境变量'),
|
|
@@ -113,6 +142,10 @@ const connectSchema = z.object({
|
|
|
113
142
|
user: z.string().describe('跳板机用户名'),
|
|
114
143
|
password: z.string().optional().describe('跳板机密码'),
|
|
115
144
|
keyPath: z.string().optional().describe('跳板机私钥路径'),
|
|
145
|
+
agent: z.string().optional().describe('跳板机 SSH agent socket 路径'),
|
|
146
|
+
remotePlatform: remotePlatformSchema.optional(),
|
|
147
|
+
commandShell: commandShellSchema.optional(),
|
|
148
|
+
remotePathStyle: remotePathStyleSchema.optional(),
|
|
116
149
|
port: sshPortSchema.optional().describe('跳板机端口,默认 22'),
|
|
117
150
|
readyTimeout: z
|
|
118
151
|
.number()
|
|
@@ -129,6 +162,11 @@ const disconnectSchema = z.object({
|
|
|
129
162
|
alias: z.string().describe('连接别名'),
|
|
130
163
|
});
|
|
131
164
|
const sessionFieldSchema = z.enum([
|
|
165
|
+
'backend',
|
|
166
|
+
'masterReused',
|
|
167
|
+
'identityStatus',
|
|
168
|
+
'masterPid',
|
|
169
|
+
'observedLogin',
|
|
132
170
|
'alias',
|
|
133
171
|
'identity',
|
|
134
172
|
'runAs',
|
|
@@ -140,6 +178,9 @@ const sessionFieldSchema = z.enum([
|
|
|
140
178
|
'authMethod',
|
|
141
179
|
'connectedAt',
|
|
142
180
|
'hasJumpHost',
|
|
181
|
+
'remotePlatform',
|
|
182
|
+
'commandShell',
|
|
183
|
+
'remotePathStyle',
|
|
143
184
|
]);
|
|
144
185
|
const listSessionsSchema = z.object({
|
|
145
186
|
detail: z.boolean().optional().describe('返回不含 keyPath 的连接详情'),
|
|
@@ -155,12 +196,24 @@ const configListSchema = z.object({
|
|
|
155
196
|
async function handleConnect(args) {
|
|
156
197
|
try {
|
|
157
198
|
const template = getTemplate(args.template);
|
|
199
|
+
const backend = args.backend ?? template.backend ?? 'ssh2';
|
|
200
|
+
const configPath = args.configPath ?? template.configPath;
|
|
201
|
+
let controlPath = args.controlPath ?? template.controlPath;
|
|
158
202
|
const configHost = args.configHost ?? template.configHost;
|
|
159
203
|
let host = args.host ?? template.host;
|
|
160
204
|
let user = args.user ?? template.user ?? template.username;
|
|
161
205
|
let port = args.port ?? template.port;
|
|
162
206
|
let keyPath = args.keyPath ?? template.keyPath ?? template.privateKeyPath;
|
|
163
207
|
const password = args.password ?? template.password;
|
|
208
|
+
let agent = args.agent ?? template.agent;
|
|
209
|
+
const remotePlatform = args.remotePlatform ?? template.remotePlatform ?? 'posix';
|
|
210
|
+
const commandShell = args.commandShell ?? template.commandShell ?? (remotePlatform === 'windows' ? 'powershell' : 'posix');
|
|
211
|
+
if ((remotePlatform === 'windows') !== (commandShell !== 'posix')) {
|
|
212
|
+
throw new Error('commandShell must match remotePlatform');
|
|
213
|
+
}
|
|
214
|
+
const remotePathStyle = args.remotePathStyle ?? template.remotePathStyle ?? (remotePlatform === 'windows' ? 'win32' : 'posix');
|
|
215
|
+
const rsyncExecutable = args.rsyncExecutable ?? template.rsyncExecutable;
|
|
216
|
+
const rsyncProfile = args.rsyncProfile ?? template.rsyncProfile;
|
|
164
217
|
const requestedAlias = args.alias ?? template.alias ?? configHost;
|
|
165
218
|
const runAs = args.runAs ?? template.runAs;
|
|
166
219
|
const defaultEnv = mergeEnv(template.defaultEnv, args.defaultEnv);
|
|
@@ -168,8 +221,53 @@ async function handleConnect(args) {
|
|
|
168
221
|
const keepaliveInterval = args.keepaliveInterval ?? template.keepaliveInterval;
|
|
169
222
|
const readyTimeout = args.readyTimeout ?? template.readyTimeout;
|
|
170
223
|
let jumpHostResolved;
|
|
171
|
-
if (
|
|
172
|
-
|
|
224
|
+
if (backend === 'openssh') {
|
|
225
|
+
if (password !== undefined ||
|
|
226
|
+
keyPath !== undefined ||
|
|
227
|
+
agent !== undefined ||
|
|
228
|
+
args.jumpHost ||
|
|
229
|
+
template.jumpHost) {
|
|
230
|
+
throw new OpenSshError('OpenSSH master reuse does not accept new authentication or jumpHost parameters', {
|
|
231
|
+
code: 'unsupported_backend',
|
|
232
|
+
backend,
|
|
233
|
+
capability: 'new_authentication',
|
|
234
|
+
retryable: false,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
if (remotePlatform !== 'posix' ||
|
|
238
|
+
commandShell !== 'posix' ||
|
|
239
|
+
remotePathStyle !== 'posix' ||
|
|
240
|
+
(rsyncProfile && rsyncProfile !== 'posix')) {
|
|
241
|
+
throw new OpenSshError('OpenSSH master reuse requires a POSIX remote shell and path style', {
|
|
242
|
+
code: 'unsupported_backend',
|
|
243
|
+
backend,
|
|
244
|
+
capability: 'platform',
|
|
245
|
+
retryable: false,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const resolved = await resolveOpenSshConfig({
|
|
249
|
+
configHost,
|
|
250
|
+
configPath,
|
|
251
|
+
host,
|
|
252
|
+
username: user,
|
|
253
|
+
port,
|
|
254
|
+
controlPath,
|
|
255
|
+
readyTimeout,
|
|
256
|
+
});
|
|
257
|
+
host = resolved.host;
|
|
258
|
+
user = resolved.username;
|
|
259
|
+
port = resolved.port;
|
|
260
|
+
controlPath = resolved.controlPath;
|
|
261
|
+
}
|
|
262
|
+
else if (controlPath !== undefined) {
|
|
263
|
+
throw new OpenSshError('controlPath requires backend=openssh', {
|
|
264
|
+
code: 'invalid_openssh_config',
|
|
265
|
+
backend,
|
|
266
|
+
retryable: false,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
else if (configHost) {
|
|
270
|
+
const allHosts = parseSSHConfig(configPath);
|
|
173
271
|
const hostConfig = allHosts.find((h) => h.host === configHost);
|
|
174
272
|
if (!hostConfig) {
|
|
175
273
|
return formatResult({
|
|
@@ -184,6 +282,11 @@ async function handleConnect(args) {
|
|
|
184
282
|
user = user ?? hostConfig.user;
|
|
185
283
|
port = port ?? hostConfig.port;
|
|
186
284
|
keyPath = keyPath ?? hostConfig.identityFile;
|
|
285
|
+
// IdentityAgent 指定的 agent 优先于进程环境,仅在未显式传参时使用
|
|
286
|
+
const configAgent = hostConfig.identityAgent;
|
|
287
|
+
if (agent === undefined && configAgent && (!keyPath || configAgent === 'none')) {
|
|
288
|
+
agent = configAgent;
|
|
289
|
+
}
|
|
187
290
|
// 解析 ProxyJump(支持 user@host:port 格式)
|
|
188
291
|
if (hostConfig.proxyJump) {
|
|
189
292
|
const parsed = parseProxyJump(hostConfig.proxyJump);
|
|
@@ -205,6 +308,9 @@ async function handleConnect(args) {
|
|
|
205
308
|
port: parsed.port ?? jumpHostConfig.port ?? 22,
|
|
206
309
|
username: jumpUser,
|
|
207
310
|
privateKeyPath: jumpHostConfig.identityFile,
|
|
311
|
+
agent: jumpHostConfig.identityAgent === 'none' || !jumpHostConfig.identityFile
|
|
312
|
+
? jumpHostConfig.identityAgent
|
|
313
|
+
: undefined,
|
|
208
314
|
};
|
|
209
315
|
}
|
|
210
316
|
else {
|
|
@@ -240,6 +346,10 @@ async function handleConnect(args) {
|
|
|
240
346
|
username: requestedJumpHost.user,
|
|
241
347
|
password: requestedJumpHost.password,
|
|
242
348
|
privateKeyPath: requestedJumpHost.keyPath,
|
|
349
|
+
agent: requestedJumpHost.agent,
|
|
350
|
+
remotePlatform: requestedJumpHost.remotePlatform,
|
|
351
|
+
commandShell: requestedJumpHost.commandShell,
|
|
352
|
+
remotePathStyle: requestedJumpHost.remotePathStyle,
|
|
243
353
|
readyTimeout: requestedJumpHost.readyTimeout ?? readyTimeout,
|
|
244
354
|
}
|
|
245
355
|
: jumpHostResolved;
|
|
@@ -252,11 +362,19 @@ async function handleConnect(args) {
|
|
|
252
362
|
.filter((session) => session.identity === identity && session.alias !== finalAlias && session.connected)
|
|
253
363
|
.map((session) => ({ alias: session.alias, runAs: session.runAs, connectedAt: session.connectedAt }));
|
|
254
364
|
const alias = await sessionManager.connect({
|
|
365
|
+
backend,
|
|
366
|
+
controlPath,
|
|
255
367
|
host,
|
|
256
368
|
port: finalPort,
|
|
257
369
|
username: user,
|
|
258
370
|
password,
|
|
259
371
|
privateKeyPath: keyPath,
|
|
372
|
+
agent,
|
|
373
|
+
remotePlatform,
|
|
374
|
+
commandShell,
|
|
375
|
+
remotePathStyle,
|
|
376
|
+
rsyncExecutable,
|
|
377
|
+
rsyncProfile,
|
|
260
378
|
alias: finalAlias,
|
|
261
379
|
template: args.template,
|
|
262
380
|
runAs,
|
|
@@ -269,11 +387,20 @@ async function handleConnect(args) {
|
|
|
269
387
|
const connected = sessionManager.getSessionConnectionInfo(alias);
|
|
270
388
|
return formatResult({
|
|
271
389
|
success: true,
|
|
390
|
+
backend: connected.backend,
|
|
391
|
+
masterReused: connected.masterReused,
|
|
392
|
+
identityStatus: connected.identityStatus,
|
|
393
|
+
masterPid: connected.masterPid,
|
|
394
|
+
observedLogin: connected.observedLogin,
|
|
272
395
|
alias: connected.alias,
|
|
273
396
|
reused,
|
|
274
397
|
identity: connected.identity,
|
|
275
398
|
loginUser: connected.loginUser,
|
|
276
399
|
runAs: connected.runAs,
|
|
400
|
+
remotePlatform: connected.remotePlatform,
|
|
401
|
+
commandShell: connected.commandShell,
|
|
402
|
+
remotePathStyle: connected.remotePathStyle,
|
|
403
|
+
keyAclStatus: connected.keyAclStatus,
|
|
277
404
|
host: connected.host,
|
|
278
405
|
port: connected.port,
|
|
279
406
|
defaultEnvKeys: connected.defaultEnvKeys,
|
|
@@ -342,6 +469,7 @@ async function handleConfigList(args) {
|
|
|
342
469
|
user: h.user,
|
|
343
470
|
port: h.port,
|
|
344
471
|
identityFile: h.identityFile,
|
|
472
|
+
identityAgent: h.identityAgent,
|
|
345
473
|
proxyJump: h.proxyJump,
|
|
346
474
|
})),
|
|
347
475
|
});
|
|
@@ -354,7 +482,8 @@ async function handleConfigList(args) {
|
|
|
354
482
|
export function registerConnectionTools(server) {
|
|
355
483
|
server.registerTool('ssh_connect', {
|
|
356
484
|
description: [
|
|
357
|
-
'建立 SSH
|
|
485
|
+
'建立 SSH 会话,默认 ssh2 支持密码、密钥和跳板机;backend=openssh 仅复用用户已有主连接,失败不新建连接或重新认证',
|
|
486
|
+
'OpenSSH 后端沿用用户 master 的信任,支持 POSIX 命令、后台操作和 rsync 同步;断开 alias 保留用户 master,未适配能力明确报错',
|
|
358
487
|
'',
|
|
359
488
|
'可通过 configHost 参数使用 ~/.ssh/config 中的配置,无需重复填写连接信息;模板会在连接前校验端口和嵌套 jumpHost 字段',
|
|
360
489
|
'支持 Host 多别名、Host * 全局默认继承、ProxyJump(user@host:port 格式)',
|
package/dist/tools/exec.d.ts
CHANGED
|
@@ -5,4 +5,5 @@
|
|
|
5
5
|
* ssh_exec_batch, ssh_quick_exec, ssh_exec_parallel
|
|
6
6
|
*/
|
|
7
7
|
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
8
|
+
export declare function windowsScriptContent(script: string, interpreter: string): string;
|
|
8
9
|
export declare function registerExecTools(server: McpServer): void;
|
package/dist/tools/exec.js
CHANGED
|
@@ -9,7 +9,10 @@ import { z } from 'zod';
|
|
|
9
9
|
import { classifyCommandRisk } from '../command-risk.js';
|
|
10
10
|
import * as fileOps from '../file-ops.js';
|
|
11
11
|
import { sessionManager } from '../session-manager.js';
|
|
12
|
+
import { OpenSshError } from '../openssh.js';
|
|
13
|
+
import { encodedPowershell, quotePowershell, sftpPath } from '../remote-platform.js';
|
|
12
14
|
import { HARD_EXEC_MAX_OUTPUT_SIZE } from '../types.js';
|
|
15
|
+
import { windowsTemporaryPathScript } from '../windows-acl.js';
|
|
13
16
|
import { sshPortSchema } from './schema.js';
|
|
14
17
|
import { escapeShellArg, formatError, formatResult } from './utils.js'; // ========== Schemas ==========
|
|
15
18
|
// ========== Schemas ==========
|
|
@@ -19,7 +22,7 @@ const maxOutputSizeSchema = z
|
|
|
19
22
|
.positive()
|
|
20
23
|
.max(HARD_EXEC_MAX_OUTPUT_SIZE)
|
|
21
24
|
.optional()
|
|
22
|
-
.describe('
|
|
25
|
+
.describe('stdout/stderr 各自的 UTF-8 字节上限,超过后截断并返回提示');
|
|
23
26
|
const execSchema = z.object({
|
|
24
27
|
alias: z.string().describe('连接别名'),
|
|
25
28
|
command: z.string().describe('要执行的命令'),
|
|
@@ -70,6 +73,9 @@ const quickExecSchema = z.object({
|
|
|
70
73
|
command: z.string().describe('要执行的命令'),
|
|
71
74
|
password: z.string().optional().describe('密码'),
|
|
72
75
|
keyPath: z.string().optional().describe('密钥路径'),
|
|
76
|
+
agent: z.string().optional().describe('SSH agent socket 或 Windows named pipe'),
|
|
77
|
+
remotePlatform: z.enum(['posix', 'windows']).optional().describe('远端平台,默认 posix'),
|
|
78
|
+
commandShell: z.enum(['posix', 'cmd', 'powershell']).optional().describe('远端命令 shell'),
|
|
73
79
|
port: sshPortSchema.optional().describe('SSH 端口,默认 22'),
|
|
74
80
|
readyTimeout: z.number().int().positive().max(600000).optional().describe('等待 SSH ready 的超时,默认 30000'),
|
|
75
81
|
timeout: z.number().optional().describe('命令执行超时(毫秒),默认 30000'),
|
|
@@ -98,7 +104,7 @@ const execScriptSchema = z.object({
|
|
|
98
104
|
runAs: z.string().optional().describe('本次脚本使用的目标用户'),
|
|
99
105
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
100
106
|
env: z.record(z.string(), z.string()).optional().describe('额外环境变量'),
|
|
101
|
-
timeout: z.number().optional().describe('
|
|
107
|
+
timeout: z.number().optional().describe('上传、准备和执行各阶段的超时(毫秒)'),
|
|
102
108
|
keepScript: z.boolean().optional().describe('是否保留远端临时脚本,默认 false'),
|
|
103
109
|
maxOutputSize: maxOutputSizeSchema,
|
|
104
110
|
});
|
|
@@ -115,7 +121,10 @@ async function handleExec(args) {
|
|
|
115
121
|
useLoginUser: args.useLoginUser,
|
|
116
122
|
loadProfile: args.loadProfile,
|
|
117
123
|
});
|
|
118
|
-
return formatResult({
|
|
124
|
+
return formatResult({
|
|
125
|
+
...result,
|
|
126
|
+
commandRisk: classifyCommandRisk(args.command, args.timeout, sessionManager.getCommandShell(args.alias)),
|
|
127
|
+
});
|
|
119
128
|
}
|
|
120
129
|
catch (error) {
|
|
121
130
|
return formatError(error);
|
|
@@ -123,6 +132,7 @@ async function handleExec(args) {
|
|
|
123
132
|
}
|
|
124
133
|
async function handleExecAsUser(args) {
|
|
125
134
|
try {
|
|
135
|
+
sessionManager.requirePosixPrivilege(args.alias);
|
|
126
136
|
if (args.targetUser) {
|
|
127
137
|
const result = await sessionManager.execAsUser(args.alias, args.command, args.targetUser, {
|
|
128
138
|
timeout: args.timeout,
|
|
@@ -131,7 +141,10 @@ async function handleExecAsUser(args) {
|
|
|
131
141
|
maxOutputSize: args.maxOutputSize,
|
|
132
142
|
loadProfile: args.loadProfile,
|
|
133
143
|
});
|
|
134
|
-
return formatResult({
|
|
144
|
+
return formatResult({
|
|
145
|
+
...result,
|
|
146
|
+
commandRisk: classifyCommandRisk(args.command, args.timeout, sessionManager.getCommandShell(args.alias)),
|
|
147
|
+
});
|
|
135
148
|
}
|
|
136
149
|
const result = await sessionManager.exec(args.alias, args.command, {
|
|
137
150
|
timeout: args.timeout,
|
|
@@ -140,7 +153,10 @@ async function handleExecAsUser(args) {
|
|
|
140
153
|
maxOutputSize: args.maxOutputSize,
|
|
141
154
|
loadProfile: args.loadProfile,
|
|
142
155
|
});
|
|
143
|
-
return formatResult({
|
|
156
|
+
return formatResult({
|
|
157
|
+
...result,
|
|
158
|
+
commandRisk: classifyCommandRisk(args.command, args.timeout, sessionManager.getCommandShell(args.alias)),
|
|
159
|
+
});
|
|
144
160
|
}
|
|
145
161
|
catch (error) {
|
|
146
162
|
return formatError(error);
|
|
@@ -166,7 +182,7 @@ async function handleExecBatch(args) {
|
|
|
166
182
|
const stopOnError = args.stopOnError !== false;
|
|
167
183
|
const results = [];
|
|
168
184
|
for (let i = 0; i < args.commands.length; i++) {
|
|
169
|
-
const commandRisk = classifyCommandRisk(args.commands[i], args.timeout);
|
|
185
|
+
const commandRisk = classifyCommandRisk(args.commands[i], args.timeout, sessionManager.getCommandShell(args.alias));
|
|
170
186
|
try {
|
|
171
187
|
const execResult = await sessionManager.exec(args.alias, args.commands[i], { timeout: args.timeout });
|
|
172
188
|
results.push({
|
|
@@ -212,6 +228,9 @@ async function handleQuickExec(args) {
|
|
|
212
228
|
username: args.user,
|
|
213
229
|
password: args.password,
|
|
214
230
|
privateKeyPath: args.keyPath,
|
|
231
|
+
agent: args.agent,
|
|
232
|
+
remotePlatform: args.remotePlatform,
|
|
233
|
+
commandShell: args.commandShell,
|
|
215
234
|
alias: tempAlias,
|
|
216
235
|
readyTimeout: args.readyTimeout,
|
|
217
236
|
});
|
|
@@ -270,17 +289,10 @@ async function handleExecParallel(args) {
|
|
|
270
289
|
return formatError(error);
|
|
271
290
|
}
|
|
272
291
|
}
|
|
273
|
-
async function cleanupRemoteScript(alias, remotePath) {
|
|
292
|
+
async function cleanupRemoteScript(alias, remotePath, timeout) {
|
|
274
293
|
try {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
useLoginUser: true,
|
|
278
|
-
maxOutputSize: 4096,
|
|
279
|
-
});
|
|
280
|
-
if (result.success) {
|
|
281
|
-
return undefined;
|
|
282
|
-
}
|
|
283
|
-
return result.stderr || result.stdout || `cleanup failed with exit code ${result.exitCode}`;
|
|
294
|
+
await fileOps.removeRemoteFile(alias, remotePath, timeout);
|
|
295
|
+
return undefined;
|
|
284
296
|
}
|
|
285
297
|
catch (error) {
|
|
286
298
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -298,6 +310,13 @@ function resolveScriptUsers(args) {
|
|
|
298
310
|
loginUser: session?.username,
|
|
299
311
|
};
|
|
300
312
|
}
|
|
313
|
+
export function windowsScriptContent(script, interpreter) {
|
|
314
|
+
const bom = String.fromCharCode(0xfeff);
|
|
315
|
+
if (interpreter.toLowerCase().startsWith('cmd')) {
|
|
316
|
+
return `@chcp 65001>nul || exit /b 1\r\n${script.startsWith(bom) ? script.slice(1) : script}`;
|
|
317
|
+
}
|
|
318
|
+
return script.startsWith(bom) ? script : bom + script;
|
|
319
|
+
}
|
|
301
320
|
async function prepareRemoteScript(alias, remotePath, effectiveUser, loginUser, timeout) {
|
|
302
321
|
const quotedPath = escapeShellArg(remotePath);
|
|
303
322
|
const ownershipCommand = effectiveUser && effectiveUser !== loginUser ? `chown ${escapeShellArg(effectiveUser)} ${quotedPath} && ` : '';
|
|
@@ -313,22 +332,75 @@ async function prepareRemoteScript(alias, remotePath, effectiveUser, loginUser,
|
|
|
313
332
|
}
|
|
314
333
|
async function handleExecScript(args) {
|
|
315
334
|
let remotePath;
|
|
335
|
+
let remoteSftpPath;
|
|
336
|
+
let ownsRemotePath = false;
|
|
337
|
+
let executionStarted = false;
|
|
316
338
|
try {
|
|
317
|
-
|
|
339
|
+
if (sessionManager.getOpenSshConnection(args.alias))
|
|
340
|
+
throw new OpenSshError('The OpenSSH backend does not support temporary script uploads; use ssh_exec for inline commands', {
|
|
341
|
+
code: 'unsupported_backend',
|
|
342
|
+
backend: 'openssh',
|
|
343
|
+
capability: 'script_upload',
|
|
344
|
+
retryable: false,
|
|
345
|
+
});
|
|
346
|
+
const windows = sessionManager.getRemotePlatform(args.alias) === 'windows';
|
|
347
|
+
const interpreter = args.interpreter ?? (windows ? sessionManager.getCommandShell(args.alias) : 'bash');
|
|
318
348
|
const { effectiveUser, loginUser } = resolveScriptUsers(args);
|
|
349
|
+
if (windows && (args.targetUser || args.runAs || effectiveUser)) {
|
|
350
|
+
sessionManager.requirePosixPrivilege(args.alias);
|
|
351
|
+
}
|
|
319
352
|
validateScriptUser(effectiveUser);
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
353
|
+
if (windows && !['powershell', 'powershell.exe', 'cmd', 'cmd.exe'].includes(interpreter.toLowerCase())) {
|
|
354
|
+
throw new Error('Windows script interpreter must be cmd or powershell');
|
|
355
|
+
}
|
|
356
|
+
if (windows) {
|
|
357
|
+
const suffix = interpreter.toLowerCase().startsWith('cmd') ? '.cmd' : '.ps1';
|
|
358
|
+
const name = `mcp-ssh-script-${randomUUID()}${suffix}`;
|
|
359
|
+
const tempResult = await sessionManager.exec(args.alias, encodedPowershell(windowsTemporaryPathScript(name)), {
|
|
360
|
+
timeout: args.timeout,
|
|
361
|
+
useLoginUser: true,
|
|
362
|
+
maxOutputSize: 4096,
|
|
363
|
+
});
|
|
364
|
+
if (!tempResult.success) {
|
|
365
|
+
throw new Error(tempResult.stderr.includes('privacy_not_enforced_on_windows')
|
|
366
|
+
? 'privacy_not_enforced_on_windows: remote temporary directory ACL was not verified'
|
|
367
|
+
: `Remote Windows temporary directory lookup failed: ${tempResult.stderr || tempResult.stdout}`);
|
|
368
|
+
}
|
|
369
|
+
remotePath = tempResult.stdout.trim();
|
|
370
|
+
if (!remotePath ||
|
|
371
|
+
/[\r\n\0]/.test(remotePath) ||
|
|
372
|
+
!(remotePath.endsWith(`\\${name}`) || remotePath.endsWith(`/${name}`))) {
|
|
373
|
+
throw new Error('Remote Windows temporary directory lookup failed');
|
|
374
|
+
}
|
|
375
|
+
remoteSftpPath = sftpPath(remotePath, sessionManager.getRemotePathStyle(args.alias));
|
|
376
|
+
await fileOps.prepareWindowsRemotePrivateFile(args.alias, remoteSftpPath, args.timeout, remotePath);
|
|
377
|
+
ownsRemotePath = true;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
const tempResult = await sessionManager.exec(args.alias, 'mktemp /tmp/mcp-ssh-script.XXXXXX', {
|
|
381
|
+
timeout: args.timeout,
|
|
382
|
+
useLoginUser: true,
|
|
383
|
+
maxOutputSize: 4096,
|
|
384
|
+
});
|
|
385
|
+
remotePath = tempResult.stdout.trim();
|
|
386
|
+
if (!tempResult.success || remotePath.length === 0) {
|
|
387
|
+
return formatResult({ success: false, error: tempResult.stderr || 'mktemp failed' });
|
|
388
|
+
}
|
|
389
|
+
ownsRemotePath = true;
|
|
390
|
+
remoteSftpPath = remotePath;
|
|
391
|
+
}
|
|
392
|
+
if (!remoteSftpPath) {
|
|
393
|
+
throw new Error('Remote temporary script path was not created');
|
|
394
|
+
}
|
|
395
|
+
await fileOps.writeFile(args.alias, remoteSftpPath, windows ? windowsScriptContent(args.script, interpreter) : args.script, false, args.timeout);
|
|
396
|
+
if (!windows) {
|
|
397
|
+
await prepareRemoteScript(args.alias, remotePath, effectiveUser, loginUser, args.timeout);
|
|
328
398
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
399
|
+
const command = windows
|
|
400
|
+
? interpreter.toLowerCase().startsWith('cmd')
|
|
401
|
+
? `cmd.exe /D /C "${remotePath.replace(/"/g, '""')}"`
|
|
402
|
+
: encodedPowershell(`& ${quotePowershell(remotePath)}; if (-not $?) { exit 1 }`)
|
|
403
|
+
: `${escapeShellArg(interpreter)} ${escapeShellArg(remotePath)}`;
|
|
332
404
|
const execOptions = {
|
|
333
405
|
timeout: args.timeout,
|
|
334
406
|
cwd: args.cwd,
|
|
@@ -336,10 +408,13 @@ async function handleExecScript(args) {
|
|
|
336
408
|
maxOutputSize: args.maxOutputSize,
|
|
337
409
|
runAs: args.targetUser ?? args.runAs,
|
|
338
410
|
};
|
|
411
|
+
executionStarted = true;
|
|
339
412
|
const result = args.targetUser
|
|
340
413
|
? await sessionManager.execAsUser(args.alias, command, args.targetUser, execOptions)
|
|
341
414
|
: await sessionManager.exec(args.alias, command, execOptions);
|
|
342
|
-
const cleanupWarning =
|
|
415
|
+
const cleanupWarning = remoteSftpPath && ownsRemotePath && args.keepScript !== true
|
|
416
|
+
? await cleanupRemoteScript(args.alias, remoteSftpPath, args.timeout)
|
|
417
|
+
: undefined;
|
|
343
418
|
return formatResult({
|
|
344
419
|
...result,
|
|
345
420
|
remotePath: args.keepScript ? remotePath : undefined,
|
|
@@ -348,7 +423,9 @@ async function handleExecScript(args) {
|
|
|
348
423
|
});
|
|
349
424
|
}
|
|
350
425
|
catch (error) {
|
|
351
|
-
const cleanupWarning =
|
|
426
|
+
const cleanupWarning = remoteSftpPath && ownsRemotePath && (args.keepScript !== true || !executionStarted)
|
|
427
|
+
? await cleanupRemoteScript(args.alias, remoteSftpPath, args.timeout)
|
|
428
|
+
: undefined;
|
|
352
429
|
if (cleanupWarning) {
|
|
353
430
|
return formatResult({
|
|
354
431
|
success: false,
|
|
@@ -402,7 +479,7 @@ export function registerExecTools(server) {
|
|
|
402
479
|
inputSchema: execParallelSchema,
|
|
403
480
|
}, (args) => handleExecParallel(args));
|
|
404
481
|
server.registerTool('ssh_exec_script', {
|
|
405
|
-
description: '
|
|
482
|
+
description: '上传远端临时脚本并执行,默认执行后清理脚本;timeout 同时用于上传、准备和执行阶段',
|
|
406
483
|
inputSchema: execScriptSchema,
|
|
407
484
|
}, (args) => handleExecScript(args));
|
|
408
485
|
}
|