@pyrokine/mcp-ssh 1.2.2 → 2.0.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 +55 -5
- package/README_zh.md +56 -10
- package/dist/file-ops.d.ts +8 -3
- package/dist/file-ops.js +363 -115
- package/dist/forward-manager.d.ts +11 -1
- package/dist/forward-manager.js +76 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +44 -2
- package/dist/pty-manager.d.ts +7 -0
- package/dist/pty-manager.js +38 -1
- package/dist/security.d.ts +24 -0
- package/dist/security.js +81 -0
- package/dist/session-manager.d.ts +20 -7
- package/dist/session-manager.js +225 -114
- package/dist/ssh-config.js +25 -14
- package/dist/tools/connection.js +26 -29
- package/dist/tools/exec.js +26 -16
- package/dist/tools/file.js +9 -5
- package/dist/tools/forward.js +19 -15
- package/dist/tools/pty.js +6 -10
- package/dist/tools/utils.d.ts +10 -0
- package/dist/tools/utils.js +22 -0
- package/package.json +56 -42
- package/src/file-ops.ts +0 -662
- package/src/forward-manager.ts +0 -258
- package/src/index.ts +0 -47
- package/src/pty-manager.ts +0 -220
- package/src/session-manager.ts +0 -661
- package/src/ssh-config.ts +0 -257
- package/src/tools/connection.ts +0 -240
- package/src/tools/exec.ts +0 -260
- package/src/tools/file.ts +0 -219
- package/src/tools/forward.ts +0 -141
- package/src/tools/index.ts +0 -9
- package/src/tools/pty.ts +0 -203
- package/src/tools/utils.ts +0 -23
- package/src/types.ts +0 -124
- package/tsconfig.json +0 -21
package/dist/tools/connection.js
CHANGED
|
@@ -9,9 +9,7 @@ import { parseProxyJump, parseSSHConfig } from '../ssh-config.js';
|
|
|
9
9
|
import { formatError, formatResult } from './utils.js';
|
|
10
10
|
// ========== Schemas ==========
|
|
11
11
|
const connectSchema = z.object({
|
|
12
|
-
configHost: z.string()
|
|
13
|
-
.optional()
|
|
14
|
-
.describe('使用 ~/.ssh/config 中的 Host 配置(推荐)'),
|
|
12
|
+
configHost: z.string().optional().describe('使用 ~/.ssh/config 中的 Host 配置(推荐)'),
|
|
15
13
|
configPath: z.string().optional().describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
16
14
|
host: z.string().optional().describe('服务器地址(使用 configHost 时可省略)'),
|
|
17
15
|
user: z.string().optional().describe('用户名(使用 configHost 时可省略)'),
|
|
@@ -21,13 +19,16 @@ const connectSchema = z.object({
|
|
|
21
19
|
alias: z.string().optional().describe('连接别名(可选,默认使用 configHost 或 host)'),
|
|
22
20
|
env: z.record(z.string()).optional().describe('环境变量'),
|
|
23
21
|
keepaliveInterval: z.number().optional().describe('心跳间隔(毫秒),默认 30000'),
|
|
24
|
-
jumpHost: z
|
|
22
|
+
jumpHost: z
|
|
23
|
+
.object({
|
|
25
24
|
host: z.string().describe('跳板机地址'),
|
|
26
25
|
user: z.string().describe('跳板机用户名'),
|
|
27
26
|
password: z.string().optional().describe('跳板机密码'),
|
|
28
27
|
keyPath: z.string().optional().describe('跳板机私钥路径'),
|
|
29
28
|
port: z.number().optional().describe('跳板机端口,默认 22'),
|
|
30
|
-
})
|
|
29
|
+
})
|
|
30
|
+
.optional()
|
|
31
|
+
.describe('跳板机配置'),
|
|
31
32
|
});
|
|
32
33
|
const disconnectSchema = z.object({
|
|
33
34
|
alias: z.string().describe('连接别名'),
|
|
@@ -37,9 +38,7 @@ const reconnectSchema = z.object({
|
|
|
37
38
|
alias: z.string().describe('连接别名'),
|
|
38
39
|
});
|
|
39
40
|
const configListSchema = z.object({
|
|
40
|
-
configPath: z.string()
|
|
41
|
-
.optional()
|
|
42
|
-
.describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
41
|
+
configPath: z.string().optional().describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
43
42
|
});
|
|
44
43
|
// ========== Handlers ==========
|
|
45
44
|
async function handleConnect(args) {
|
|
@@ -52,7 +51,7 @@ async function handleConnect(args) {
|
|
|
52
51
|
let jumpHostResolved;
|
|
53
52
|
if (args.configHost) {
|
|
54
53
|
const allHosts = parseSSHConfig(args.configPath);
|
|
55
|
-
const hostConfig = allHosts.find(h => h.host === args.configHost);
|
|
54
|
+
const hostConfig = allHosts.find((h) => h.host === args.configHost);
|
|
56
55
|
if (!hostConfig) {
|
|
57
56
|
return formatResult({ success: false, error: `Host '${args.configHost}' not found in SSH config` });
|
|
58
57
|
}
|
|
@@ -66,7 +65,7 @@ async function handleConnect(args) {
|
|
|
66
65
|
const parsed = parseProxyJump(hostConfig.proxyJump);
|
|
67
66
|
if (parsed) {
|
|
68
67
|
// 先尝试在 config 中查找对应的 Host
|
|
69
|
-
const jumpHostConfig = allHosts.find(h => h.host === parsed.host);
|
|
68
|
+
const jumpHostConfig = allHosts.find((h) => h.host === parsed.host);
|
|
70
69
|
if (jumpHostConfig) {
|
|
71
70
|
// 使用 config 中的配置,但 parsed 的 user/port 优先
|
|
72
71
|
jumpHostResolved = {
|
|
@@ -94,13 +93,15 @@ async function handleConnect(args) {
|
|
|
94
93
|
});
|
|
95
94
|
}
|
|
96
95
|
// 手动指定的 jumpHost 优先级高于 ProxyJump
|
|
97
|
-
const jumpHost = args.jumpHost
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
96
|
+
const jumpHost = args.jumpHost
|
|
97
|
+
? {
|
|
98
|
+
host: args.jumpHost.host,
|
|
99
|
+
port: args.jumpHost.port || 22,
|
|
100
|
+
username: args.jumpHost.user,
|
|
101
|
+
password: args.jumpHost.password,
|
|
102
|
+
privateKeyPath: args.jumpHost.keyPath,
|
|
103
|
+
}
|
|
104
|
+
: jumpHostResolved;
|
|
104
105
|
const alias = await sessionManager.connect({
|
|
105
106
|
host,
|
|
106
107
|
port: port || 22,
|
|
@@ -115,9 +116,7 @@ async function handleConnect(args) {
|
|
|
115
116
|
return formatResult({
|
|
116
117
|
success: true,
|
|
117
118
|
alias,
|
|
118
|
-
message: `Connected to ${user}@${host}:${port || 22}${jumpHost ?
|
|
119
|
-
' via jump host' :
|
|
120
|
-
''}`,
|
|
119
|
+
message: `Connected to ${user}@${host}:${port || 22}${jumpHost ? ' via jump host' : ''}`,
|
|
121
120
|
});
|
|
122
121
|
}
|
|
123
122
|
catch (error) {
|
|
@@ -129,9 +128,7 @@ async function handleDisconnect(args) {
|
|
|
129
128
|
const success = sessionManager.disconnect(args.alias);
|
|
130
129
|
return formatResult({
|
|
131
130
|
success,
|
|
132
|
-
message: success
|
|
133
|
-
? `Disconnected from ${args.alias}`
|
|
134
|
-
: `Session ${args.alias} not found`,
|
|
131
|
+
message: success ? `Disconnected from ${args.alias}` : `Session ${args.alias} not found`,
|
|
135
132
|
});
|
|
136
133
|
}
|
|
137
134
|
catch (error) {
|
|
@@ -166,7 +163,7 @@ async function handleConfigList(args) {
|
|
|
166
163
|
return formatResult({
|
|
167
164
|
success: true,
|
|
168
165
|
count: hosts.length,
|
|
169
|
-
hosts: hosts.map(h => ({
|
|
166
|
+
hosts: hosts.map((h) => ({
|
|
170
167
|
host: h.host,
|
|
171
168
|
hostName: h.hostName,
|
|
172
169
|
user: h.user,
|
|
@@ -183,10 +180,10 @@ async function handleConfigList(args) {
|
|
|
183
180
|
// ========== Register ==========
|
|
184
181
|
export function registerConnectionTools(server) {
|
|
185
182
|
server.registerTool('ssh_connect', {
|
|
186
|
-
description: `建立 SSH
|
|
183
|
+
description: `建立 SSH 连接并保持会话,支持密码、密钥认证,支持跳板机
|
|
187
184
|
|
|
188
|
-
可通过 configHost 参数使用 ~/.ssh/config
|
|
189
|
-
支持 Host 多别名、Host * 全局默认继承、ProxyJump(user@host:port
|
|
185
|
+
可通过 configHost 参数使用 ~/.ssh/config 中的配置,无需重复填写连接信息
|
|
186
|
+
支持 Host 多别名、Host * 全局默认继承、ProxyJump(user@host:port 格式)
|
|
190
187
|
|
|
191
188
|
示例:
|
|
192
189
|
- 使用 ssh config: ssh_connect(configHost="myserver")
|
|
@@ -207,9 +204,9 @@ export function registerConnectionTools(server) {
|
|
|
207
204
|
inputSchema: reconnectSchema,
|
|
208
205
|
}, (args) => handleReconnect(args));
|
|
209
206
|
server.registerTool('ssh_config_list', {
|
|
210
|
-
description: `列出 ~/.ssh/config 中配置的所有 Host
|
|
207
|
+
description: `列出 ~/.ssh/config 中配置的所有 Host
|
|
211
208
|
|
|
212
|
-
返回每个 Host
|
|
209
|
+
返回每个 Host 的配置信息(别名、地址、用户、端口、密钥路径等)`,
|
|
213
210
|
inputSchema: configListSchema,
|
|
214
211
|
}, (args) => handleConfigList(args));
|
|
215
212
|
}
|
package/dist/tools/exec.js
CHANGED
|
@@ -6,12 +6,15 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { z } from 'zod';
|
|
8
8
|
import { sessionManager } from '../session-manager.js';
|
|
9
|
-
import { formatError, formatResult } from './utils.js';
|
|
9
|
+
import { formatError, formatResult } from './utils.js'; // ========== Schemas ==========
|
|
10
10
|
// ========== Schemas ==========
|
|
11
11
|
const execSchema = z.object({
|
|
12
12
|
alias: z.string().describe('连接别名'),
|
|
13
13
|
command: z.string().describe('要执行的命令'),
|
|
14
|
-
timeout: z
|
|
14
|
+
timeout: z
|
|
15
|
+
.number()
|
|
16
|
+
.optional()
|
|
17
|
+
.describe('超时(毫秒),默认 30000,长时间命令可设更大值如 600000(10 分钟),或用 ssh_pty_start 替代'),
|
|
15
18
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
16
19
|
env: z.record(z.string()).optional().describe('额外环境变量'),
|
|
17
20
|
pty: z.boolean().optional().describe('是否使用 PTY 模式(用于 top 等交互式命令)'),
|
|
@@ -21,7 +24,10 @@ const execAsUserSchema = z.object({
|
|
|
21
24
|
command: z.string().describe('要执行的命令'),
|
|
22
25
|
targetUser: z.string().describe('目标用户名'),
|
|
23
26
|
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
24
|
-
loadProfile: z
|
|
27
|
+
loadProfile: z
|
|
28
|
+
.boolean()
|
|
29
|
+
.optional()
|
|
30
|
+
.describe('是否加载目标用户的 shell 配置(默认 true),profile 加载慢导致超时时设为 false'),
|
|
25
31
|
});
|
|
26
32
|
const execSudoSchema = z.object({
|
|
27
33
|
alias: z.string().describe('连接别名'),
|
|
@@ -78,7 +84,9 @@ async function handleExecAsUser(args) {
|
|
|
78
84
|
}
|
|
79
85
|
async function handleExecSudo(args) {
|
|
80
86
|
try {
|
|
81
|
-
const result = await sessionManager.execSudo(args.alias, args.command, args.sudoPassword, {
|
|
87
|
+
const result = await sessionManager.execSudo(args.alias, args.command, args.sudoPassword, {
|
|
88
|
+
timeout: args.timeout,
|
|
89
|
+
});
|
|
82
90
|
return formatResult(result);
|
|
83
91
|
}
|
|
84
92
|
catch (error) {
|
|
@@ -106,7 +114,7 @@ async function handleExecBatch(args) {
|
|
|
106
114
|
index: i,
|
|
107
115
|
command: args.commands[i],
|
|
108
116
|
success: false,
|
|
109
|
-
error: err.message,
|
|
117
|
+
error: err instanceof Error ? err.message : String(err),
|
|
110
118
|
});
|
|
111
119
|
if (stopOnError) {
|
|
112
120
|
break;
|
|
@@ -163,13 +171,13 @@ async function handleExecParallel(args) {
|
|
|
163
171
|
return {
|
|
164
172
|
alias,
|
|
165
173
|
success: false,
|
|
166
|
-
error: err.message,
|
|
174
|
+
error: err instanceof Error ? err.message : String(err),
|
|
167
175
|
};
|
|
168
176
|
}
|
|
169
177
|
});
|
|
170
178
|
const results = await Promise.all(execPromises);
|
|
171
179
|
return formatResult({
|
|
172
|
-
success: results.every(r => r.success),
|
|
180
|
+
success: results.every((r) => r.success),
|
|
173
181
|
total: args.aliases.length,
|
|
174
182
|
results,
|
|
175
183
|
});
|
|
@@ -181,18 +189,18 @@ async function handleExecParallel(args) {
|
|
|
181
189
|
// ========== Register ==========
|
|
182
190
|
export function registerExecTools(server) {
|
|
183
191
|
server.registerTool('ssh_exec', {
|
|
184
|
-
description:
|
|
192
|
+
description: `在远程服务器执行命令
|
|
185
193
|
|
|
186
194
|
返回: stdout, stderr, exitCode, duration`,
|
|
187
195
|
inputSchema: execSchema,
|
|
188
196
|
}, (args) => handleExec(args));
|
|
189
197
|
server.registerTool('ssh_exec_as_user', {
|
|
190
|
-
description: `以其他用户身份执行命令(通过 su
|
|
198
|
+
description: `以其他用户身份执行命令(通过 su 切换)
|
|
191
199
|
|
|
192
|
-
适用场景: SSH 以 root 登录,但需要以其他用户(如 caros
|
|
200
|
+
适用场景: SSH 以 root 登录,但需要以其他用户(如 caros)执行命令
|
|
193
201
|
|
|
194
|
-
默认加载目标用户的 shell
|
|
195
|
-
支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)
|
|
202
|
+
默认加载目标用户的 shell 配置以获取环境变量,如果 profile 加载耗时过长导致超时,设 loadProfile=false 跳过
|
|
203
|
+
支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)
|
|
196
204
|
|
|
197
205
|
示例: ssh_exec_as_user(alias="server", command="whoami", targetUser="caros")`,
|
|
198
206
|
inputSchema: execAsUserSchema,
|
|
@@ -202,20 +210,22 @@ export function registerExecTools(server) {
|
|
|
202
210
|
inputSchema: execSudoSchema,
|
|
203
211
|
}, (args) => handleExecSudo(args));
|
|
204
212
|
server.registerTool('ssh_exec_batch', {
|
|
205
|
-
description:
|
|
213
|
+
description: `批量执行多条命令,每条命令在独立 SSH channel 中执行,环境变量不共享
|
|
214
|
+
|
|
215
|
+
如需命令间共享环境(如 export 变量),用分号拼接为单条命令通过 ssh_exec 执行`,
|
|
206
216
|
inputSchema: execBatchSchema,
|
|
207
217
|
}, (args) => handleExecBatch(args));
|
|
208
218
|
server.registerTool('ssh_quick_exec', {
|
|
209
|
-
description: '
|
|
219
|
+
description: '一次性执行命令(自动连接、执行、断开),适用于单次命令,不需要保持连接',
|
|
210
220
|
inputSchema: quickExecSchema,
|
|
211
221
|
}, (args) => handleQuickExec(args));
|
|
212
222
|
server.registerTool('ssh_exec_parallel', {
|
|
213
|
-
description:
|
|
223
|
+
description: `在多个已连接的会话上并行执行同一命令
|
|
214
224
|
|
|
215
225
|
示例:
|
|
216
226
|
- ssh_exec_parallel(aliases=["server1", "server2"], command="uptime")
|
|
217
227
|
|
|
218
|
-
|
|
228
|
+
返回每个主机的执行结果`,
|
|
219
229
|
inputSchema: execParallelSchema,
|
|
220
230
|
}, (args) => handleExecParallel(args));
|
|
221
231
|
}
|
package/dist/tools/file.js
CHANGED
|
@@ -47,12 +47,15 @@ const syncSchema = z.object({
|
|
|
47
47
|
alias: z.string().describe('连接别名'),
|
|
48
48
|
localPath: z.string().describe('本地路径'),
|
|
49
49
|
remotePath: z.string().describe('远程路径'),
|
|
50
|
-
direction: z.enum(['upload', 'download'])
|
|
51
|
-
.describe('同步方向:upload(本地到远程)或 download(远程到本地)'),
|
|
50
|
+
direction: z.enum(['upload', 'download']).describe('同步方向:upload(本地到远程)或 download(远程到本地)'),
|
|
52
51
|
delete: z.boolean().optional().describe('删除目标端多余文件(类似 rsync --delete)'),
|
|
53
52
|
dryRun: z.boolean().optional().describe('仅显示将执行的操作,不实际传输'),
|
|
54
53
|
exclude: z.array(z.string()).optional().describe('排除模式列表(支持 * 和 ? 通配符)'),
|
|
55
54
|
recursive: z.boolean().optional().describe('递归同步目录,默认 true'),
|
|
55
|
+
followSymlinks: z
|
|
56
|
+
.boolean()
|
|
57
|
+
.optional()
|
|
58
|
+
.describe('upload 时是否跟随本地 symlink,默认 false(跳过 symlink 并 warn,避免上传链接目标内容)'),
|
|
56
59
|
});
|
|
57
60
|
// ========== Handlers ==========
|
|
58
61
|
async function handleUpload(args) {
|
|
@@ -130,6 +133,7 @@ async function handleSync(args) {
|
|
|
130
133
|
dryRun: args.dryRun,
|
|
131
134
|
exclude: args.exclude,
|
|
132
135
|
recursive: args.recursive,
|
|
136
|
+
followSymlinks: args.followSymlinks,
|
|
133
137
|
});
|
|
134
138
|
return formatResult({
|
|
135
139
|
...syncResult,
|
|
@@ -173,10 +177,10 @@ export function registerFileTools(server) {
|
|
|
173
177
|
inputSchema: mkdirSchema,
|
|
174
178
|
}, (args) => handleMkdir(args));
|
|
175
179
|
server.registerTool('ssh_sync', {
|
|
176
|
-
description:
|
|
180
|
+
description: `智能文件同步(支持目录递归)
|
|
177
181
|
|
|
178
|
-
优先使用 rsync(如果本地和远程都安装了),否则回退到 SFTP
|
|
179
|
-
rsync
|
|
182
|
+
优先使用 rsync(如果本地和远程都安装了),否则回退到 SFTP
|
|
183
|
+
rsync 可实现增量传输,对大目录同步效率更高
|
|
180
184
|
|
|
181
185
|
用途:
|
|
182
186
|
- 同步本地目录到远程
|
package/dist/tools/forward.js
CHANGED
|
@@ -7,19 +7,26 @@ import { z } from 'zod';
|
|
|
7
7
|
import { sessionManager } from '../session-manager.js';
|
|
8
8
|
import { formatError, formatResult } from './utils.js';
|
|
9
9
|
// ========== Schemas ==========
|
|
10
|
+
// 仅允许 loopback 地址监听,防止把端口暴露到外网
|
|
11
|
+
const loopbackHostSchema = z
|
|
12
|
+
.string()
|
|
13
|
+
.refine((s) => s === '127.0.0.1' || s === '::1' || s === 'localhost', {
|
|
14
|
+
message: '仅支持 loopback 地址(127.0.0.1 / ::1 / localhost),禁止 0.0.0.0 等公网监听',
|
|
15
|
+
})
|
|
16
|
+
.default('127.0.0.1');
|
|
10
17
|
const forwardLocalSchema = z.object({
|
|
11
18
|
alias: z.string().describe('连接别名'),
|
|
12
19
|
localPort: z.number().describe('本地监听端口'),
|
|
13
20
|
remoteHost: z.string().describe('远程目标主机'),
|
|
14
21
|
remotePort: z.number().describe('远程目标端口'),
|
|
15
|
-
localHost:
|
|
22
|
+
localHost: loopbackHostSchema.describe('本地监听地址,仅允许 loopback(127.0.0.1 / ::1 / localhost),默认 127.0.0.1'),
|
|
16
23
|
});
|
|
17
24
|
const forwardRemoteSchema = z.object({
|
|
18
25
|
alias: z.string().describe('连接别名'),
|
|
19
26
|
remotePort: z.number().describe('远程监听端口'),
|
|
20
27
|
localHost: z.string().describe('本地目标地址'),
|
|
21
28
|
localPort: z.number().describe('本地目标端口'),
|
|
22
|
-
remoteHost:
|
|
29
|
+
remoteHost: loopbackHostSchema.describe('远程监听地址,仅允许 loopback(127.0.0.1 / ::1 / localhost),默认 127.0.0.1'),
|
|
23
30
|
});
|
|
24
31
|
const forwardCloseSchema = z.object({
|
|
25
32
|
forwardId: z.string().describe('端口转发 ID'),
|
|
@@ -28,13 +35,13 @@ const forwardListSchema = z.object({});
|
|
|
28
35
|
// ========== Handlers ==========
|
|
29
36
|
async function handleForwardLocal(args) {
|
|
30
37
|
try {
|
|
31
|
-
const forwardId = await sessionManager.forwardLocal(args.alias, args.localPort, args.remoteHost, args.remotePort, args.localHost
|
|
38
|
+
const { forwardId, localPort } = await sessionManager.forwardLocal(args.alias, args.localPort, args.remoteHost, args.remotePort, args.localHost);
|
|
32
39
|
return formatResult({
|
|
33
40
|
success: true,
|
|
34
41
|
forwardId,
|
|
35
42
|
type: 'local',
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
localPort,
|
|
44
|
+
message: `Local forward: ${args.localHost}:${localPort} -> ${args.remoteHost}:${args.remotePort}`,
|
|
38
45
|
});
|
|
39
46
|
}
|
|
40
47
|
catch (error) {
|
|
@@ -43,13 +50,12 @@ async function handleForwardLocal(args) {
|
|
|
43
50
|
}
|
|
44
51
|
async function handleForwardRemote(args) {
|
|
45
52
|
try {
|
|
46
|
-
const forwardId = await sessionManager.forwardRemote(args.alias, args.remotePort, args.localHost, args.localPort, args.remoteHost
|
|
53
|
+
const forwardId = await sessionManager.forwardRemote(args.alias, args.remotePort, args.localHost, args.localPort, args.remoteHost);
|
|
47
54
|
return formatResult({
|
|
48
55
|
success: true,
|
|
49
56
|
forwardId,
|
|
50
57
|
type: 'remote',
|
|
51
|
-
message: `Remote forward: ${args.remoteHost
|
|
52
|
-
'127.0.0.1'}:${args.remotePort} -> ${args.localHost}:${args.localPort}`,
|
|
58
|
+
message: `Remote forward: ${args.remoteHost}:${args.remotePort} -> ${args.localHost}:${args.localPort}`,
|
|
53
59
|
});
|
|
54
60
|
}
|
|
55
61
|
catch (error) {
|
|
@@ -61,9 +67,7 @@ async function handleForwardClose(args) {
|
|
|
61
67
|
const success = sessionManager.forwardClose(args.forwardId);
|
|
62
68
|
return formatResult({
|
|
63
69
|
success,
|
|
64
|
-
message: success
|
|
65
|
-
? `Forward closed: ${args.forwardId}`
|
|
66
|
-
: `Forward not found: ${args.forwardId}`,
|
|
70
|
+
message: success ? `Forward closed: ${args.forwardId}` : `Forward not found: ${args.forwardId}`,
|
|
67
71
|
});
|
|
68
72
|
}
|
|
69
73
|
catch (error) {
|
|
@@ -86,9 +90,9 @@ async function handleForwardList() {
|
|
|
86
90
|
// ========== Register ==========
|
|
87
91
|
export function registerForwardTools(server) {
|
|
88
92
|
server.registerTool('ssh_forward_local', {
|
|
89
|
-
description: `创建本地端口转发(类似 ssh -L
|
|
93
|
+
description: `创建本地端口转发(类似 ssh -L)
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
本地监听指定端口,将连接转发到远程主机
|
|
92
96
|
|
|
93
97
|
用途:访问远程内网服务
|
|
94
98
|
示例:ssh_forward_local(alias="server", localPort=8080, remoteHost="10.0.0.1", remotePort=80)
|
|
@@ -96,9 +100,9 @@ export function registerForwardTools(server) {
|
|
|
96
100
|
inputSchema: forwardLocalSchema,
|
|
97
101
|
}, (args) => handleForwardLocal(args));
|
|
98
102
|
server.registerTool('ssh_forward_remote', {
|
|
99
|
-
description: `创建远程端口转发(类似 ssh -R
|
|
103
|
+
description: `创建远程端口转发(类似 ssh -R)
|
|
100
104
|
|
|
101
|
-
|
|
105
|
+
远程监听指定端口,将连接转发到本地
|
|
102
106
|
|
|
103
107
|
用途:将本地服务暴露到远程
|
|
104
108
|
示例:ssh_forward_remote(alias="server", remotePort=8080, localHost="127.0.0.1", localPort=3000)
|
package/dist/tools/pty.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
import { sessionManager } from '../session-manager.js';
|
|
8
|
-
import { formatError, formatResult } from './utils.js';
|
|
8
|
+
import { formatError, formatResult } from './utils.js'; // ========== Schemas ==========
|
|
9
9
|
// ========== Schemas ==========
|
|
10
10
|
const ptyStartSchema = z.object({
|
|
11
11
|
alias: z.string().describe('连接别名'),
|
|
@@ -24,9 +24,7 @@ const ptyWriteSchema = z.object({
|
|
|
24
24
|
const ptyReadSchema = z.object({
|
|
25
25
|
ptyId: z.string().describe('PTY 会话 ID'),
|
|
26
26
|
mode: z.enum(['screen', 'raw']).optional().describe('输出模式:screen(当前屏幕)或 raw(原始流),默认 screen'),
|
|
27
|
-
clear: z.boolean()
|
|
28
|
-
.optional()
|
|
29
|
-
.describe('(仅 raw 模式) 读取后是否清空缓冲区,默认 true'),
|
|
27
|
+
clear: z.boolean().optional().describe('(仅 raw 模式) 读取后是否清空缓冲区,默认 true'),
|
|
30
28
|
});
|
|
31
29
|
const ptyResizeSchema = z.object({
|
|
32
30
|
ptyId: z.string().describe('PTY 会话 ID'),
|
|
@@ -98,9 +96,7 @@ async function handlePtyClose(args) {
|
|
|
98
96
|
const success = sessionManager.ptyClose(args.ptyId);
|
|
99
97
|
return formatResult({
|
|
100
98
|
success,
|
|
101
|
-
message: success
|
|
102
|
-
? `PTY session closed: ${args.ptyId}`
|
|
103
|
-
: `PTY session not found: ${args.ptyId}`,
|
|
99
|
+
message: success ? `PTY session closed: ${args.ptyId}` : `PTY session not found: ${args.ptyId}`,
|
|
104
100
|
});
|
|
105
101
|
}
|
|
106
102
|
catch (error) {
|
|
@@ -123,7 +119,7 @@ async function handlePtyList() {
|
|
|
123
119
|
// ========== Register ==========
|
|
124
120
|
export function registerPtyTools(server) {
|
|
125
121
|
server.registerTool('ssh_pty_start', {
|
|
126
|
-
description: `启动持久化 PTY 会话,支持 top、htop、tmux
|
|
122
|
+
description: `启动持久化 PTY 会话,支持 top、htop、tmux 等交互式命令
|
|
127
123
|
|
|
128
124
|
特点:
|
|
129
125
|
- 输出缓冲区持续收集数据
|
|
@@ -136,7 +132,7 @@ export function registerPtyTools(server) {
|
|
|
136
132
|
inputSchema: ptyStartSchema,
|
|
137
133
|
}, (args) => handlePtyStart(args));
|
|
138
134
|
server.registerTool('ssh_pty_write', {
|
|
139
|
-
description: `向 PTY
|
|
135
|
+
description: `向 PTY 写入数据(按键、命令)
|
|
140
136
|
|
|
141
137
|
常用控制序列:
|
|
142
138
|
- 回车: "\\r" 或 "\\n"
|
|
@@ -152,7 +148,7 @@ export function registerPtyTools(server) {
|
|
|
152
148
|
inputSchema: ptyWriteSchema,
|
|
153
149
|
}, (args) => handlePtyWrite(args));
|
|
154
150
|
server.registerTool('ssh_pty_read', {
|
|
155
|
-
description: `读取 PTY
|
|
151
|
+
description: `读取 PTY 输出
|
|
156
152
|
|
|
157
153
|
两种模式:
|
|
158
154
|
- screen(默认):返回当前屏幕内容(解析后的纯文本,适合 top/btop/htop 等全屏刷新工具)
|
package/dist/tools/utils.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 工具共享响应格式化
|
|
3
3
|
*/
|
|
4
|
+
/**
|
|
5
|
+
* 展开路径开头的 ~ 为用户 home 目录
|
|
6
|
+
*
|
|
7
|
+
* 仅支持 ~ 与 ~/...,不支持 ~user 形式
|
|
8
|
+
*/
|
|
9
|
+
export declare function expandTilde(p: string): string;
|
|
4
10
|
/**
|
|
5
11
|
* 格式化成功响应
|
|
6
12
|
*/
|
|
@@ -20,3 +26,7 @@ export declare function formatError(error: unknown): {
|
|
|
20
26
|
}[];
|
|
21
27
|
isError: boolean;
|
|
22
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* 转义 shell 参数(使用单引号方式,防止命令注入)
|
|
31
|
+
*/
|
|
32
|
+
export declare function escapeShellArg(s: string): string;
|
package/dist/tools/utils.js
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 工具共享响应格式化
|
|
3
3
|
*/
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
/**
|
|
7
|
+
* 展开路径开头的 ~ 为用户 home 目录
|
|
8
|
+
*
|
|
9
|
+
* 仅支持 ~ 与 ~/...,不支持 ~user 形式
|
|
10
|
+
*/
|
|
11
|
+
export function expandTilde(p) {
|
|
12
|
+
if (p === '~') {
|
|
13
|
+
return os.homedir();
|
|
14
|
+
}
|
|
15
|
+
if (p.startsWith('~/')) {
|
|
16
|
+
return path.join(os.homedir(), p.slice(2));
|
|
17
|
+
}
|
|
18
|
+
return p;
|
|
19
|
+
}
|
|
4
20
|
/**
|
|
5
21
|
* 格式化成功响应
|
|
6
22
|
*/
|
|
@@ -19,3 +35,9 @@ export function formatError(error) {
|
|
|
19
35
|
isError: true,
|
|
20
36
|
};
|
|
21
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* 转义 shell 参数(使用单引号方式,防止命令注入)
|
|
40
|
+
*/
|
|
41
|
+
export function escapeShellArg(s) {
|
|
42
|
+
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,44 +1,58 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "@pyrokine/mcp-ssh",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A comprehensive SSH MCP Server for Claude Code",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"mcp-ssh": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"start": "node dist/index.js",
|
|
13
|
+
"dev": "tsc && node dist/index.js",
|
|
14
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
15
|
+
"prepublishOnly": "npm run build",
|
|
16
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
17
|
+
"lint:fix": "eslint \"src/**/*.ts\" --fix",
|
|
18
|
+
"format": "prettier --write src/",
|
|
19
|
+
"format:check": "prettier --check src/"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"mcp",
|
|
23
|
+
"ssh",
|
|
24
|
+
"claude",
|
|
25
|
+
"remote",
|
|
26
|
+
"automation"
|
|
27
|
+
],
|
|
28
|
+
"author": "pyro",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/Pyrokine/claude-tools.git"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
36
|
+
"@xterm/headless": "^6.0.0",
|
|
37
|
+
"ssh2": "^1.17.0",
|
|
38
|
+
"zod": "^3.25.76"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@eslint/js": "^9.39.4",
|
|
42
|
+
"@types/node": "^25.0.9",
|
|
43
|
+
"@types/ssh2": "^1.15.5",
|
|
44
|
+
"eslint": "9.26.0",
|
|
45
|
+
"prettier": "^3.8.3",
|
|
46
|
+
"typescript": "^5.9.3",
|
|
47
|
+
"typescript-eslint": "^8.59.0"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18.0.0"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist",
|
|
54
|
+
"README.md",
|
|
55
|
+
"README_zh.md",
|
|
56
|
+
"LICENSE"
|
|
57
|
+
]
|
|
44
58
|
}
|