@pyrokine/mcp-ssh 1.1.0 → 1.1.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/README.md +81 -72
- package/README_zh.md +81 -73
- package/dist/file-ops.d.ts +7 -10
- package/dist/file-ops.js +94 -176
- package/dist/index.js +8 -938
- package/dist/session-manager.d.ts +72 -67
- package/dist/session-manager.js +228 -240
- package/dist/ssh-config.d.ts +0 -4
- package/dist/ssh-config.js +3 -10
- package/dist/tools/connection.d.ts +7 -0
- package/dist/tools/connection.js +215 -0
- package/dist/tools/exec.d.ts +8 -0
- package/dist/tools/exec.js +221 -0
- package/dist/tools/file.d.ts +8 -0
- package/dist/tools/file.js +192 -0
- package/dist/tools/forward.d.ts +7 -0
- package/dist/tools/forward.js +116 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/pty.d.ts +7 -0
- package/dist/tools/pty.js +178 -0
- package/dist/tools/utils.d.ts +22 -0
- package/dist/tools/utils.js +21 -0
- package/dist/types.d.ts +0 -7
- package/package.json +3 -2
- package/src/file-ops.ts +526 -596
- package/src/index.ts +25 -1084
- package/src/session-manager.ts +938 -945
- package/src/ssh-config.ts +183 -190
- package/src/tools/connection.ts +240 -0
- package/src/tools/exec.ts +260 -0
- package/src/tools/file.ts +219 -0
- package/src/tools/forward.ts +141 -0
- package/src/tools/index.ts +9 -0
- package/src/tools/pty.ts +203 -0
- package/src/tools/utils.ts +23 -0
- package/src/types.ts +84 -93
- package/tsconfig.json +7 -2
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 命令执行工具组
|
|
3
|
+
*
|
|
4
|
+
* ssh_exec, ssh_exec_as_user, ssh_exec_sudo,
|
|
5
|
+
* ssh_exec_batch, ssh_quick_exec, ssh_exec_parallel
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
9
|
+
import {z} from 'zod'
|
|
10
|
+
import {sessionManager} from '../session-manager.js'
|
|
11
|
+
import {formatError, formatResult} from './utils.js'
|
|
12
|
+
|
|
13
|
+
// ========== Schemas ==========
|
|
14
|
+
|
|
15
|
+
const execSchema = z.object({
|
|
16
|
+
alias: z.string().describe('连接别名'),
|
|
17
|
+
command: z.string().describe('要执行的命令'),
|
|
18
|
+
timeout: z.number().optional().describe('超时(毫秒),默认 30000'),
|
|
19
|
+
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
20
|
+
env: z.record(z.string()).optional().describe('额外环境变量'),
|
|
21
|
+
pty: z.boolean().optional().describe('是否使用 PTY 模式(用于 top 等交互式命令)'),
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const execAsUserSchema = z.object({
|
|
25
|
+
alias: z.string().describe('连接别名'),
|
|
26
|
+
command: z.string().describe('要执行的命令'),
|
|
27
|
+
targetUser: z.string().describe('目标用户名'),
|
|
28
|
+
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
29
|
+
loadProfile: z.boolean().optional().describe('是否加载 .bashrc(默认 true)'),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const execSudoSchema = z.object({
|
|
33
|
+
alias: z.string().describe('连接别名'),
|
|
34
|
+
command: z.string().describe('要执行的命令'),
|
|
35
|
+
sudoPassword: z.string().optional().describe('sudo 密码(如果需要)'),
|
|
36
|
+
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const execBatchSchema = z.object({
|
|
40
|
+
alias: z.string().describe('连接别名'),
|
|
41
|
+
commands: z.array(z.string()).describe('命令列表'),
|
|
42
|
+
stopOnError: z.boolean().optional().describe('遇到错误是否停止,默认 true'),
|
|
43
|
+
timeout: z.number().optional().describe('每条命令的超时(毫秒)'),
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const quickExecSchema = z.object({
|
|
47
|
+
host: z.string().describe('服务器地址'),
|
|
48
|
+
user: z.string().describe('用户名'),
|
|
49
|
+
command: z.string().describe('要执行的命令'),
|
|
50
|
+
password: z.string().optional().describe('密码'),
|
|
51
|
+
keyPath: z.string().optional().describe('密钥路径'),
|
|
52
|
+
port: z.number().optional().describe('端口'),
|
|
53
|
+
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const execParallelSchema = z.object({
|
|
57
|
+
aliases: z.array(z.string()).describe('连接别名列表'),
|
|
58
|
+
command: z.string().describe('要执行的命令'),
|
|
59
|
+
timeout: z.number().optional().describe('每个命令的超时(毫秒),默认 30000'),
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// ========== Handlers ==========
|
|
63
|
+
|
|
64
|
+
async function handleExec(args: z.infer<typeof execSchema>) {
|
|
65
|
+
try {
|
|
66
|
+
const result = await sessionManager.exec(
|
|
67
|
+
args.alias,
|
|
68
|
+
args.command,
|
|
69
|
+
{
|
|
70
|
+
timeout: args.timeout,
|
|
71
|
+
cwd: args.cwd,
|
|
72
|
+
env: args.env,
|
|
73
|
+
pty: args.pty,
|
|
74
|
+
},
|
|
75
|
+
)
|
|
76
|
+
return formatResult(result)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return formatError(error)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function handleExecAsUser(args: z.infer<typeof execAsUserSchema>) {
|
|
83
|
+
try {
|
|
84
|
+
const result = await sessionManager.execAsUser(
|
|
85
|
+
args.alias,
|
|
86
|
+
args.command,
|
|
87
|
+
args.targetUser,
|
|
88
|
+
{
|
|
89
|
+
timeout: args.timeout,
|
|
90
|
+
loadProfile: args.loadProfile,
|
|
91
|
+
},
|
|
92
|
+
)
|
|
93
|
+
return formatResult(result)
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return formatError(error)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function handleExecSudo(args: z.infer<typeof execSudoSchema>) {
|
|
100
|
+
try {
|
|
101
|
+
const result = await sessionManager.execSudo(
|
|
102
|
+
args.alias,
|
|
103
|
+
args.command,
|
|
104
|
+
args.sudoPassword,
|
|
105
|
+
{timeout: args.timeout},
|
|
106
|
+
)
|
|
107
|
+
return formatResult(result)
|
|
108
|
+
} catch (error) {
|
|
109
|
+
return formatError(error)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function handleExecBatch(args: z.infer<typeof execBatchSchema>) {
|
|
114
|
+
try {
|
|
115
|
+
const stopOnError = args.stopOnError !== false
|
|
116
|
+
const results: any[] = []
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < args.commands.length; i++) {
|
|
119
|
+
try {
|
|
120
|
+
const execResult = await sessionManager.exec(
|
|
121
|
+
args.alias,
|
|
122
|
+
args.commands[i],
|
|
123
|
+
{timeout: args.timeout},
|
|
124
|
+
)
|
|
125
|
+
results.push({
|
|
126
|
+
index: i,
|
|
127
|
+
command: args.commands[i],
|
|
128
|
+
...execResult,
|
|
129
|
+
})
|
|
130
|
+
if (execResult.exitCode !== 0 && stopOnError) {
|
|
131
|
+
break
|
|
132
|
+
}
|
|
133
|
+
} catch (err: any) {
|
|
134
|
+
results.push({
|
|
135
|
+
index: i,
|
|
136
|
+
command: args.commands[i],
|
|
137
|
+
success: false,
|
|
138
|
+
error: err.message,
|
|
139
|
+
})
|
|
140
|
+
if (stopOnError) {
|
|
141
|
+
break
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return formatResult({
|
|
147
|
+
success: results.every((r) => r.success),
|
|
148
|
+
total: args.commands.length,
|
|
149
|
+
executed: results.length,
|
|
150
|
+
results,
|
|
151
|
+
})
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return formatError(error)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function handleQuickExec(args: z.infer<typeof quickExecSchema>) {
|
|
158
|
+
const tempAlias = `_quick_${Date.now()}`
|
|
159
|
+
try {
|
|
160
|
+
await sessionManager.connect({
|
|
161
|
+
host: args.host,
|
|
162
|
+
port: args.port || 22,
|
|
163
|
+
username: args.user,
|
|
164
|
+
password: args.password,
|
|
165
|
+
privateKeyPath: args.keyPath,
|
|
166
|
+
alias: tempAlias,
|
|
167
|
+
})
|
|
168
|
+
const result = await sessionManager.exec(
|
|
169
|
+
tempAlias,
|
|
170
|
+
args.command,
|
|
171
|
+
{timeout: args.timeout},
|
|
172
|
+
)
|
|
173
|
+
return formatResult(result)
|
|
174
|
+
} catch (error) {
|
|
175
|
+
return formatError(error)
|
|
176
|
+
} finally {
|
|
177
|
+
sessionManager.disconnect(tempAlias)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function handleExecParallel(args: z.infer<typeof execParallelSchema>) {
|
|
182
|
+
try {
|
|
183
|
+
const execPromises = args.aliases.map(async (alias) => {
|
|
184
|
+
try {
|
|
185
|
+
const execResult = await sessionManager.exec(alias, args.command, {timeout: args.timeout})
|
|
186
|
+
return {
|
|
187
|
+
alias,
|
|
188
|
+
success: execResult.success,
|
|
189
|
+
exitCode: execResult.exitCode,
|
|
190
|
+
stdout: execResult.stdout,
|
|
191
|
+
stderr: execResult.stderr,
|
|
192
|
+
duration: execResult.duration,
|
|
193
|
+
}
|
|
194
|
+
} catch (err: any) {
|
|
195
|
+
return {
|
|
196
|
+
alias,
|
|
197
|
+
success: false,
|
|
198
|
+
error: err.message,
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
const results = await Promise.all(execPromises)
|
|
204
|
+
return formatResult({
|
|
205
|
+
success: results.every(r => r.success),
|
|
206
|
+
total: args.aliases.length,
|
|
207
|
+
results,
|
|
208
|
+
})
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return formatError(error)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ========== Register ==========
|
|
215
|
+
|
|
216
|
+
export function registerExecTools(server: McpServer): void {
|
|
217
|
+
server.registerTool('ssh_exec', {
|
|
218
|
+
description: `在远程服务器执行命令。
|
|
219
|
+
|
|
220
|
+
返回: stdout, stderr, exitCode, duration`,
|
|
221
|
+
inputSchema: execSchema,
|
|
222
|
+
}, (args) => handleExec(args))
|
|
223
|
+
|
|
224
|
+
server.registerTool('ssh_exec_as_user', {
|
|
225
|
+
description: `以其他用户身份执行命令(通过 su 切换)。
|
|
226
|
+
|
|
227
|
+
适用场景: SSH 以 root 登录,但需要以其他用户(如 caros)执行命令。
|
|
228
|
+
|
|
229
|
+
默认加载目标用户的 shell 配置以获取环境变量(su -c 创建非交互式 shell,不会自动执行 rc 文件)。
|
|
230
|
+
支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)。
|
|
231
|
+
|
|
232
|
+
示例: ssh_exec_as_user(alias="server", command="whoami", targetUser="caros")`,
|
|
233
|
+
inputSchema: execAsUserSchema,
|
|
234
|
+
}, (args) => handleExecAsUser(args))
|
|
235
|
+
|
|
236
|
+
server.registerTool('ssh_exec_sudo', {
|
|
237
|
+
description: '使用 sudo 执行命令',
|
|
238
|
+
inputSchema: execSudoSchema,
|
|
239
|
+
}, (args) => handleExecSudo(args))
|
|
240
|
+
|
|
241
|
+
server.registerTool('ssh_exec_batch', {
|
|
242
|
+
description: '批量执行多条命令',
|
|
243
|
+
inputSchema: execBatchSchema,
|
|
244
|
+
}, (args) => handleExecBatch(args))
|
|
245
|
+
|
|
246
|
+
server.registerTool('ssh_quick_exec', {
|
|
247
|
+
description: '一次性执行命令(自动连接、执行、断开)。适用于单次命令,不需要保持连接。',
|
|
248
|
+
inputSchema: quickExecSchema,
|
|
249
|
+
}, (args) => handleQuickExec(args))
|
|
250
|
+
|
|
251
|
+
server.registerTool('ssh_exec_parallel', {
|
|
252
|
+
description: `在多个已连接的会话上并行执行同一命令。
|
|
253
|
+
|
|
254
|
+
示例:
|
|
255
|
+
- ssh_exec_parallel(aliases=["server1", "server2"], command="uptime")
|
|
256
|
+
|
|
257
|
+
返回每个主机的执行结果。`,
|
|
258
|
+
inputSchema: execParallelSchema,
|
|
259
|
+
}, (args) => handleExecParallel(args))
|
|
260
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文件操作工具组
|
|
3
|
+
*
|
|
4
|
+
* ssh_upload, ssh_download, ssh_read_file, ssh_write_file,
|
|
5
|
+
* ssh_list_dir, ssh_file_info, ssh_mkdir, ssh_sync
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
9
|
+
import {z} from 'zod'
|
|
10
|
+
import * as fileOps from '../file-ops.js'
|
|
11
|
+
import {formatError, formatResult} from './utils.js'
|
|
12
|
+
|
|
13
|
+
// ========== Schemas ==========
|
|
14
|
+
|
|
15
|
+
const uploadSchema = z.object({
|
|
16
|
+
alias: z.string().describe('连接别名'),
|
|
17
|
+
localPath: z.string().describe('本地文件路径'),
|
|
18
|
+
remotePath: z.string().describe('远程目标路径'),
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const downloadSchema = z.object({
|
|
22
|
+
alias: z.string().describe('连接别名'),
|
|
23
|
+
remotePath: z.string().describe('远程文件路径'),
|
|
24
|
+
localPath: z.string().describe('本地保存路径'),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const readFileSchema = z.object({
|
|
28
|
+
alias: z.string().describe('连接别名'),
|
|
29
|
+
remotePath: z.string().describe('远程文件路径'),
|
|
30
|
+
maxBytes: z.number().optional().describe('最大读取字节数,默认 1MB'),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const writeFileSchema = z.object({
|
|
34
|
+
alias: z.string().describe('连接别名'),
|
|
35
|
+
remotePath: z.string().describe('远程文件路径'),
|
|
36
|
+
content: z.string().describe('要写入的内容'),
|
|
37
|
+
append: z.boolean().optional().describe('是否追加模式,默认覆盖'),
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const listDirSchema = z.object({
|
|
41
|
+
alias: z.string().describe('连接别名'),
|
|
42
|
+
remotePath: z.string().describe('远程目录路径'),
|
|
43
|
+
showHidden: z.boolean().optional().describe('是否显示隐藏文件'),
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const fileInfoSchema = z.object({
|
|
47
|
+
alias: z.string().describe('连接别名'),
|
|
48
|
+
remotePath: z.string().describe('远程路径'),
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const mkdirSchema = z.object({
|
|
52
|
+
alias: z.string().describe('连接别名'),
|
|
53
|
+
remotePath: z.string().describe('远程目录路径'),
|
|
54
|
+
recursive: z.boolean().optional().describe('是否递归创建,默认 false'),
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const syncSchema = z.object({
|
|
58
|
+
alias: z.string().describe('连接别名'),
|
|
59
|
+
localPath: z.string().describe('本地路径'),
|
|
60
|
+
remotePath: z.string().describe('远程路径'),
|
|
61
|
+
direction: z.enum(['upload', 'download'])
|
|
62
|
+
.describe('同步方向:upload(本地到远程)或 download(远程到本地)'),
|
|
63
|
+
delete: z.boolean().optional().describe('删除目标端多余文件(类似 rsync --delete)'),
|
|
64
|
+
dryRun: z.boolean().optional().describe('仅显示将执行的操作,不实际传输'),
|
|
65
|
+
exclude: z.array(z.string()).optional().describe('排除模式列表(支持 * 和 ? 通配符)'),
|
|
66
|
+
recursive: z.boolean().optional().describe('递归同步目录,默认 true'),
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// ========== Handlers ==========
|
|
70
|
+
|
|
71
|
+
async function handleUpload(args: z.infer<typeof uploadSchema>) {
|
|
72
|
+
try {
|
|
73
|
+
const uploadResult = await fileOps.uploadFile(args.alias, args.localPath, args.remotePath)
|
|
74
|
+
return formatResult({...uploadResult, message: `Uploaded to ${args.remotePath}`})
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return formatError(error)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function handleDownload(args: z.infer<typeof downloadSchema>) {
|
|
81
|
+
try {
|
|
82
|
+
const downloadResult = await fileOps.downloadFile(args.alias, args.remotePath, args.localPath)
|
|
83
|
+
return formatResult({...downloadResult, message: `Downloaded to ${args.localPath}`})
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return formatError(error)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function handleReadFile(args: z.infer<typeof readFileSchema>) {
|
|
90
|
+
try {
|
|
91
|
+
const readResult = await fileOps.readFile(args.alias, args.remotePath, args.maxBytes)
|
|
92
|
+
return formatResult({success: true, ...readResult})
|
|
93
|
+
} catch (error) {
|
|
94
|
+
return formatError(error)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function handleWriteFile(args: z.infer<typeof writeFileSchema>) {
|
|
99
|
+
try {
|
|
100
|
+
const result = await fileOps.writeFile(args.alias, args.remotePath, args.content, args.append)
|
|
101
|
+
return formatResult(result)
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return formatError(error)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function handleListDir(args: z.infer<typeof listDirSchema>) {
|
|
108
|
+
try {
|
|
109
|
+
const files = await fileOps.listDir(args.alias, args.remotePath, args.showHidden)
|
|
110
|
+
return formatResult({
|
|
111
|
+
success: true,
|
|
112
|
+
path: args.remotePath,
|
|
113
|
+
count: files.length,
|
|
114
|
+
files,
|
|
115
|
+
})
|
|
116
|
+
} catch (error) {
|
|
117
|
+
return formatError(error)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function handleFileInfo(args: z.infer<typeof fileInfoSchema>) {
|
|
122
|
+
try {
|
|
123
|
+
const info = await fileOps.getFileInfo(args.alias, args.remotePath)
|
|
124
|
+
return formatResult({success: true, ...info})
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return formatError(error)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function handleMkdir(args: z.infer<typeof mkdirSchema>) {
|
|
131
|
+
try {
|
|
132
|
+
const success = await fileOps.mkdir(args.alias, args.remotePath, args.recursive)
|
|
133
|
+
return formatResult({success, path: args.remotePath})
|
|
134
|
+
} catch (error) {
|
|
135
|
+
return formatError(error)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function handleSync(args: z.infer<typeof syncSchema>) {
|
|
140
|
+
try {
|
|
141
|
+
const syncResult = await fileOps.syncFiles(
|
|
142
|
+
args.alias,
|
|
143
|
+
args.localPath,
|
|
144
|
+
args.remotePath,
|
|
145
|
+
args.direction,
|
|
146
|
+
{
|
|
147
|
+
delete: args.delete,
|
|
148
|
+
dryRun: args.dryRun,
|
|
149
|
+
exclude: args.exclude,
|
|
150
|
+
recursive: args.recursive,
|
|
151
|
+
},
|
|
152
|
+
)
|
|
153
|
+
return formatResult({
|
|
154
|
+
...syncResult,
|
|
155
|
+
direction: args.direction,
|
|
156
|
+
localPath: args.localPath,
|
|
157
|
+
remotePath: args.remotePath,
|
|
158
|
+
})
|
|
159
|
+
} catch (error) {
|
|
160
|
+
return formatError(error)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ========== Register ==========
|
|
165
|
+
|
|
166
|
+
export function registerFileTools(server: McpServer): void {
|
|
167
|
+
server.registerTool('ssh_upload', {
|
|
168
|
+
description: '上传本地文件到远程服务器',
|
|
169
|
+
inputSchema: uploadSchema,
|
|
170
|
+
}, (args) => handleUpload(args))
|
|
171
|
+
|
|
172
|
+
server.registerTool('ssh_download', {
|
|
173
|
+
description: '从远程服务器下载文件',
|
|
174
|
+
inputSchema: downloadSchema,
|
|
175
|
+
}, (args) => handleDownload(args))
|
|
176
|
+
|
|
177
|
+
server.registerTool('ssh_read_file', {
|
|
178
|
+
description: '读取远程文件内容',
|
|
179
|
+
inputSchema: readFileSchema,
|
|
180
|
+
}, (args) => handleReadFile(args))
|
|
181
|
+
|
|
182
|
+
server.registerTool('ssh_write_file', {
|
|
183
|
+
description: '写入内容到远程文件',
|
|
184
|
+
inputSchema: writeFileSchema,
|
|
185
|
+
}, (args) => handleWriteFile(args))
|
|
186
|
+
|
|
187
|
+
server.registerTool('ssh_list_dir', {
|
|
188
|
+
description: '列出远程目录内容',
|
|
189
|
+
inputSchema: listDirSchema,
|
|
190
|
+
}, (args) => handleListDir(args))
|
|
191
|
+
|
|
192
|
+
server.registerTool('ssh_file_info', {
|
|
193
|
+
description: '获取远程文件信息(大小、权限、修改时间等)',
|
|
194
|
+
inputSchema: fileInfoSchema,
|
|
195
|
+
}, (args) => handleFileInfo(args))
|
|
196
|
+
|
|
197
|
+
server.registerTool('ssh_mkdir', {
|
|
198
|
+
description: '创建远程目录',
|
|
199
|
+
inputSchema: mkdirSchema,
|
|
200
|
+
}, (args) => handleMkdir(args))
|
|
201
|
+
|
|
202
|
+
server.registerTool('ssh_sync', {
|
|
203
|
+
description: `智能文件同步(支持目录递归)。
|
|
204
|
+
|
|
205
|
+
优先使用 rsync(如果本地和远程都安装了),否则回退到 SFTP。
|
|
206
|
+
rsync 可实现增量传输,对大目录同步效率更高。
|
|
207
|
+
|
|
208
|
+
用途:
|
|
209
|
+
- 同步本地目录到远程
|
|
210
|
+
- 从远程同步目录到本地
|
|
211
|
+
- 支持排除特定文件/目录
|
|
212
|
+
|
|
213
|
+
示例:
|
|
214
|
+
- 上传目录: ssh_sync(alias="server", localPath="/local/dir", remotePath="/remote/dir", direction="upload")
|
|
215
|
+
- 下载目录: ssh_sync(alias="server", localPath="/local/dir", remotePath="/remote/dir", direction="download")
|
|
216
|
+
- 排除文件: ssh_sync(..., exclude=["*.log", "node_modules"])`,
|
|
217
|
+
inputSchema: syncSchema,
|
|
218
|
+
}, (args) => handleSync(args))
|
|
219
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 端口转发工具组
|
|
3
|
+
*
|
|
4
|
+
* ssh_forward_local, ssh_forward_remote, ssh_forward_close, ssh_forward_list
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
8
|
+
import {z} from 'zod'
|
|
9
|
+
import {sessionManager} from '../session-manager.js'
|
|
10
|
+
import {formatError, formatResult} from './utils.js'
|
|
11
|
+
|
|
12
|
+
// ========== Schemas ==========
|
|
13
|
+
|
|
14
|
+
const forwardLocalSchema = z.object({
|
|
15
|
+
alias: z.string().describe('连接别名'),
|
|
16
|
+
localPort: z.number().describe('本地监听端口'),
|
|
17
|
+
remoteHost: z.string().describe('远程目标主机'),
|
|
18
|
+
remotePort: z.number().describe('远程目标端口'),
|
|
19
|
+
localHost: z.string().optional().describe('本地监听地址,默认 127.0.0.1'),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const forwardRemoteSchema = z.object({
|
|
23
|
+
alias: z.string().describe('连接别名'),
|
|
24
|
+
remotePort: z.number().describe('远程监听端口'),
|
|
25
|
+
localHost: z.string().describe('本地目标地址'),
|
|
26
|
+
localPort: z.number().describe('本地目标端口'),
|
|
27
|
+
remoteHost: z.string().optional().describe('远程监听地址,默认 127.0.0.1'),
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const forwardCloseSchema = z.object({
|
|
31
|
+
forwardId: z.string().describe('端口转发 ID'),
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const forwardListSchema = z.object({})
|
|
35
|
+
|
|
36
|
+
// ========== Handlers ==========
|
|
37
|
+
|
|
38
|
+
async function handleForwardLocal(args: z.infer<typeof forwardLocalSchema>) {
|
|
39
|
+
try {
|
|
40
|
+
const forwardId = await sessionManager.forwardLocal(
|
|
41
|
+
args.alias,
|
|
42
|
+
args.localPort,
|
|
43
|
+
args.remoteHost,
|
|
44
|
+
args.remotePort,
|
|
45
|
+
args.localHost || '127.0.0.1',
|
|
46
|
+
)
|
|
47
|
+
return formatResult({
|
|
48
|
+
success: true,
|
|
49
|
+
forwardId,
|
|
50
|
+
type: 'local',
|
|
51
|
+
message: `Local forward: ${args.localHost ||
|
|
52
|
+
'127.0.0.1'}:${args.localPort} -> ${args.remoteHost}:${args.remotePort}`,
|
|
53
|
+
})
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return formatError(error)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function handleForwardRemote(args: z.infer<typeof forwardRemoteSchema>) {
|
|
60
|
+
try {
|
|
61
|
+
const forwardId = await sessionManager.forwardRemote(
|
|
62
|
+
args.alias,
|
|
63
|
+
args.remotePort,
|
|
64
|
+
args.localHost,
|
|
65
|
+
args.localPort,
|
|
66
|
+
args.remoteHost || '127.0.0.1',
|
|
67
|
+
)
|
|
68
|
+
return formatResult({
|
|
69
|
+
success: true,
|
|
70
|
+
forwardId,
|
|
71
|
+
type: 'remote',
|
|
72
|
+
message: `Remote forward: ${args.remoteHost ||
|
|
73
|
+
'127.0.0.1'}:${args.remotePort} -> ${args.localHost}:${args.localPort}`,
|
|
74
|
+
})
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return formatError(error)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function handleForwardClose(args: z.infer<typeof forwardCloseSchema>) {
|
|
81
|
+
try {
|
|
82
|
+
const success = sessionManager.forwardClose(args.forwardId)
|
|
83
|
+
return formatResult({
|
|
84
|
+
success,
|
|
85
|
+
message: success
|
|
86
|
+
? `Forward closed: ${args.forwardId}`
|
|
87
|
+
: `Forward not found: ${args.forwardId}`,
|
|
88
|
+
})
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return formatError(error)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function handleForwardList() {
|
|
95
|
+
try {
|
|
96
|
+
const forwards = sessionManager.forwardList()
|
|
97
|
+
return formatResult({
|
|
98
|
+
success: true,
|
|
99
|
+
count: forwards.length,
|
|
100
|
+
forwards,
|
|
101
|
+
})
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return formatError(error)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ========== Register ==========
|
|
108
|
+
|
|
109
|
+
export function registerForwardTools(server: McpServer): void {
|
|
110
|
+
server.registerTool('ssh_forward_local', {
|
|
111
|
+
description: `创建本地端口转发(类似 ssh -L)。
|
|
112
|
+
|
|
113
|
+
本地监听指定端口,将连接转发到远程主机。
|
|
114
|
+
|
|
115
|
+
用途:访问远程内网服务
|
|
116
|
+
示例:ssh_forward_local(alias="server", localPort=8080, remoteHost="10.0.0.1", remotePort=80)
|
|
117
|
+
效果:访问本地 localhost:8080 会转发到远程内网的 10.0.0.1:80`,
|
|
118
|
+
inputSchema: forwardLocalSchema,
|
|
119
|
+
}, (args) => handleForwardLocal(args))
|
|
120
|
+
|
|
121
|
+
server.registerTool('ssh_forward_remote', {
|
|
122
|
+
description: `创建远程端口转发(类似 ssh -R)。
|
|
123
|
+
|
|
124
|
+
远程监听指定端口,将连接转发到本地。
|
|
125
|
+
|
|
126
|
+
用途:将本地服务暴露到远程
|
|
127
|
+
示例:ssh_forward_remote(alias="server", remotePort=8080, localHost="127.0.0.1", localPort=3000)
|
|
128
|
+
效果:远程访问 localhost:8080 会转发到本地的 127.0.0.1:3000`,
|
|
129
|
+
inputSchema: forwardRemoteSchema,
|
|
130
|
+
}, (args) => handleForwardRemote(args))
|
|
131
|
+
|
|
132
|
+
server.registerTool('ssh_forward_close', {
|
|
133
|
+
description: '关闭端口转发',
|
|
134
|
+
inputSchema: forwardCloseSchema,
|
|
135
|
+
}, (args) => handleForwardClose(args))
|
|
136
|
+
|
|
137
|
+
server.registerTool('ssh_forward_list', {
|
|
138
|
+
description: '列出所有端口转发',
|
|
139
|
+
inputSchema: forwardListSchema,
|
|
140
|
+
}, () => handleForwardList())
|
|
141
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具模块导出
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {registerConnectionTools} from './connection.js'
|
|
6
|
+
export {registerExecTools} from './exec.js'
|
|
7
|
+
export {registerFileTools} from './file.js'
|
|
8
|
+
export {registerPtyTools} from './pty.js'
|
|
9
|
+
export {registerForwardTools} from './forward.js'
|