@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,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 连接管理工具组
|
|
3
|
+
*
|
|
4
|
+
* ssh_connect, ssh_disconnect, ssh_list_sessions, ssh_reconnect, ssh_config_list
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { sessionManager } from '../session-manager.js';
|
|
8
|
+
import { parseProxyJump, parseSSHConfig } from '../ssh-config.js';
|
|
9
|
+
import { formatError, formatResult } from './utils.js';
|
|
10
|
+
// ========== Schemas ==========
|
|
11
|
+
const connectSchema = z.object({
|
|
12
|
+
configHost: z.string()
|
|
13
|
+
.optional()
|
|
14
|
+
.describe('使用 ~/.ssh/config 中的 Host 配置(推荐)'),
|
|
15
|
+
configPath: z.string().optional().describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
16
|
+
host: z.string().optional().describe('服务器地址(使用 configHost 时可省略)'),
|
|
17
|
+
user: z.string().optional().describe('用户名(使用 configHost 时可省略)'),
|
|
18
|
+
password: z.string().optional().describe('密码'),
|
|
19
|
+
keyPath: z.string().optional().describe('SSH 私钥路径'),
|
|
20
|
+
port: z.number().optional().describe('SSH 端口,默认 22'),
|
|
21
|
+
alias: z.string().optional().describe('连接别名(可选,默认使用 configHost 或 host)'),
|
|
22
|
+
env: z.record(z.string()).optional().describe('环境变量'),
|
|
23
|
+
keepaliveInterval: z.number().optional().describe('心跳间隔(毫秒),默认 30000'),
|
|
24
|
+
jumpHost: z.object({
|
|
25
|
+
host: z.string().describe('跳板机地址'),
|
|
26
|
+
user: z.string().describe('跳板机用户名'),
|
|
27
|
+
password: z.string().optional().describe('跳板机密码'),
|
|
28
|
+
keyPath: z.string().optional().describe('跳板机私钥路径'),
|
|
29
|
+
port: z.number().optional().describe('跳板机端口,默认 22'),
|
|
30
|
+
}).optional().describe('跳板机配置'),
|
|
31
|
+
});
|
|
32
|
+
const disconnectSchema = z.object({
|
|
33
|
+
alias: z.string().describe('连接别名'),
|
|
34
|
+
});
|
|
35
|
+
const listSessionsSchema = z.object({});
|
|
36
|
+
const reconnectSchema = z.object({
|
|
37
|
+
alias: z.string().describe('连接别名'),
|
|
38
|
+
});
|
|
39
|
+
const configListSchema = z.object({
|
|
40
|
+
configPath: z.string()
|
|
41
|
+
.optional()
|
|
42
|
+
.describe('SSH 配置文件路径(默认 ~/.ssh/config)'),
|
|
43
|
+
});
|
|
44
|
+
// ========== Handlers ==========
|
|
45
|
+
async function handleConnect(args) {
|
|
46
|
+
try {
|
|
47
|
+
// 解析 configHost
|
|
48
|
+
let host = args.host;
|
|
49
|
+
let user = args.user;
|
|
50
|
+
let port = args.port;
|
|
51
|
+
let keyPath = args.keyPath;
|
|
52
|
+
let jumpHostResolved;
|
|
53
|
+
if (args.configHost) {
|
|
54
|
+
const allHosts = parseSSHConfig(args.configPath);
|
|
55
|
+
const hostConfig = allHosts.find(h => h.host === args.configHost);
|
|
56
|
+
if (!hostConfig) {
|
|
57
|
+
return formatResult({ success: false, error: `Host '${args.configHost}' not found in SSH config` });
|
|
58
|
+
}
|
|
59
|
+
// 显式参数优先于 config 值
|
|
60
|
+
host = host || hostConfig.hostName || hostConfig.host;
|
|
61
|
+
user = user || hostConfig.user;
|
|
62
|
+
port = port || hostConfig.port;
|
|
63
|
+
keyPath = keyPath || hostConfig.identityFile;
|
|
64
|
+
// 解析 ProxyJump(支持 user@host:port 格式)
|
|
65
|
+
if (hostConfig.proxyJump) {
|
|
66
|
+
const parsed = parseProxyJump(hostConfig.proxyJump);
|
|
67
|
+
if (parsed) {
|
|
68
|
+
// 先尝试在 config 中查找对应的 Host
|
|
69
|
+
const jumpHostConfig = allHosts.find(h => h.host === parsed.host);
|
|
70
|
+
if (jumpHostConfig) {
|
|
71
|
+
// 使用 config 中的配置,但 parsed 的 user/port 优先
|
|
72
|
+
jumpHostResolved = {
|
|
73
|
+
host: jumpHostConfig.hostName || jumpHostConfig.host,
|
|
74
|
+
port: parsed.port || jumpHostConfig.port || 22,
|
|
75
|
+
username: parsed.user || jumpHostConfig.user || 'root',
|
|
76
|
+
privateKeyPath: jumpHostConfig.identityFile,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// 直接使用 parsed 的值
|
|
81
|
+
jumpHostResolved = {
|
|
82
|
+
host: parsed.host,
|
|
83
|
+
port: parsed.port || 22,
|
|
84
|
+
username: parsed.user || 'root',
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!host || !user) {
|
|
91
|
+
return formatResult({
|
|
92
|
+
success: false,
|
|
93
|
+
error: 'host and user are required (either directly or via configHost)',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
// 手动指定的 jumpHost 优先级高于 ProxyJump
|
|
97
|
+
const jumpHost = args.jumpHost ? {
|
|
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
|
+
} : jumpHostResolved;
|
|
104
|
+
const alias = await sessionManager.connect({
|
|
105
|
+
host,
|
|
106
|
+
port: port || 22,
|
|
107
|
+
username: user,
|
|
108
|
+
password: args.password,
|
|
109
|
+
privateKeyPath: keyPath,
|
|
110
|
+
alias: args.alias || args.configHost,
|
|
111
|
+
env: args.env,
|
|
112
|
+
keepaliveInterval: args.keepaliveInterval,
|
|
113
|
+
jumpHost,
|
|
114
|
+
});
|
|
115
|
+
return formatResult({
|
|
116
|
+
success: true,
|
|
117
|
+
alias,
|
|
118
|
+
message: `Connected to ${user}@${host}:${port || 22}${jumpHost ?
|
|
119
|
+
' via jump host' :
|
|
120
|
+
''}`,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
return formatError(error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function handleDisconnect(args) {
|
|
128
|
+
try {
|
|
129
|
+
const success = sessionManager.disconnect(args.alias);
|
|
130
|
+
return formatResult({
|
|
131
|
+
success,
|
|
132
|
+
message: success
|
|
133
|
+
? `Disconnected from ${args.alias}`
|
|
134
|
+
: `Session ${args.alias} not found`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
return formatError(error);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async function handleListSessions() {
|
|
142
|
+
try {
|
|
143
|
+
const sessions = sessionManager.listSessions();
|
|
144
|
+
return formatResult({
|
|
145
|
+
success: true,
|
|
146
|
+
count: sessions.length,
|
|
147
|
+
sessions,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
return formatError(error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async function handleReconnect(args) {
|
|
155
|
+
try {
|
|
156
|
+
await sessionManager.reconnect(args.alias);
|
|
157
|
+
return formatResult({ success: true, message: `Reconnected to ${args.alias}` });
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
return formatError(error);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async function handleConfigList(args) {
|
|
164
|
+
try {
|
|
165
|
+
const hosts = parseSSHConfig(args.configPath);
|
|
166
|
+
return formatResult({
|
|
167
|
+
success: true,
|
|
168
|
+
count: hosts.length,
|
|
169
|
+
hosts: hosts.map(h => ({
|
|
170
|
+
host: h.host,
|
|
171
|
+
hostName: h.hostName,
|
|
172
|
+
user: h.user,
|
|
173
|
+
port: h.port,
|
|
174
|
+
identityFile: h.identityFile,
|
|
175
|
+
proxyJump: h.proxyJump,
|
|
176
|
+
})),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
return formatError(error);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// ========== Register ==========
|
|
184
|
+
export function registerConnectionTools(server) {
|
|
185
|
+
server.registerTool('ssh_connect', {
|
|
186
|
+
description: `建立 SSH 连接并保持会话。支持密码、密钥认证,支持跳板机。
|
|
187
|
+
|
|
188
|
+
可通过 configHost 参数使用 ~/.ssh/config 中的配置,无需重复填写连接信息。
|
|
189
|
+
支持 Host 多别名、Host * 全局默认继承、ProxyJump(user@host:port 格式)。
|
|
190
|
+
|
|
191
|
+
示例:
|
|
192
|
+
- 使用 ssh config: ssh_connect(configHost="myserver")
|
|
193
|
+
- 密钥认证: ssh_connect(host="192.168.1.1", user="root", keyPath="/home/.ssh/id_rsa")
|
|
194
|
+
- 跳板机: ssh_connect(host="内网IP", user="root", keyPath="...", jumpHost={host:"跳板机IP", user:"root", keyPath:"..."})`,
|
|
195
|
+
inputSchema: connectSchema,
|
|
196
|
+
}, (args) => handleConnect(args));
|
|
197
|
+
server.registerTool('ssh_disconnect', {
|
|
198
|
+
description: '断开 SSH 连接',
|
|
199
|
+
inputSchema: disconnectSchema,
|
|
200
|
+
}, (args) => handleDisconnect(args));
|
|
201
|
+
server.registerTool('ssh_list_sessions', {
|
|
202
|
+
description: '列出所有活跃的 SSH 会话',
|
|
203
|
+
inputSchema: listSessionsSchema,
|
|
204
|
+
}, () => handleListSessions());
|
|
205
|
+
server.registerTool('ssh_reconnect', {
|
|
206
|
+
description: '重新连接已断开的会话',
|
|
207
|
+
inputSchema: reconnectSchema,
|
|
208
|
+
}, (args) => handleReconnect(args));
|
|
209
|
+
server.registerTool('ssh_config_list', {
|
|
210
|
+
description: `列出 ~/.ssh/config 中配置的所有 Host。
|
|
211
|
+
|
|
212
|
+
返回每个 Host 的配置信息(别名、地址、用户、端口、密钥路径等)。`,
|
|
213
|
+
inputSchema: configListSchema,
|
|
214
|
+
}, (args) => handleConfigList(args));
|
|
215
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
8
|
+
export declare function registerExecTools(server: McpServer): void;
|
|
@@ -0,0 +1,221 @@
|
|
|
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
|
+
import { z } from 'zod';
|
|
8
|
+
import { sessionManager } from '../session-manager.js';
|
|
9
|
+
import { formatError, formatResult } from './utils.js';
|
|
10
|
+
// ========== Schemas ==========
|
|
11
|
+
const execSchema = z.object({
|
|
12
|
+
alias: z.string().describe('连接别名'),
|
|
13
|
+
command: z.string().describe('要执行的命令'),
|
|
14
|
+
timeout: z.number().optional().describe('超时(毫秒),默认 30000'),
|
|
15
|
+
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
16
|
+
env: z.record(z.string()).optional().describe('额外环境变量'),
|
|
17
|
+
pty: z.boolean().optional().describe('是否使用 PTY 模式(用于 top 等交互式命令)'),
|
|
18
|
+
});
|
|
19
|
+
const execAsUserSchema = z.object({
|
|
20
|
+
alias: z.string().describe('连接别名'),
|
|
21
|
+
command: z.string().describe('要执行的命令'),
|
|
22
|
+
targetUser: z.string().describe('目标用户名'),
|
|
23
|
+
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
24
|
+
loadProfile: z.boolean().optional().describe('是否加载 .bashrc(默认 true)'),
|
|
25
|
+
});
|
|
26
|
+
const execSudoSchema = z.object({
|
|
27
|
+
alias: z.string().describe('连接别名'),
|
|
28
|
+
command: z.string().describe('要执行的命令'),
|
|
29
|
+
sudoPassword: z.string().optional().describe('sudo 密码(如果需要)'),
|
|
30
|
+
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
31
|
+
});
|
|
32
|
+
const execBatchSchema = z.object({
|
|
33
|
+
alias: z.string().describe('连接别名'),
|
|
34
|
+
commands: z.array(z.string()).describe('命令列表'),
|
|
35
|
+
stopOnError: z.boolean().optional().describe('遇到错误是否停止,默认 true'),
|
|
36
|
+
timeout: z.number().optional().describe('每条命令的超时(毫秒)'),
|
|
37
|
+
});
|
|
38
|
+
const quickExecSchema = z.object({
|
|
39
|
+
host: z.string().describe('服务器地址'),
|
|
40
|
+
user: z.string().describe('用户名'),
|
|
41
|
+
command: z.string().describe('要执行的命令'),
|
|
42
|
+
password: z.string().optional().describe('密码'),
|
|
43
|
+
keyPath: z.string().optional().describe('密钥路径'),
|
|
44
|
+
port: z.number().optional().describe('端口'),
|
|
45
|
+
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
46
|
+
});
|
|
47
|
+
const execParallelSchema = z.object({
|
|
48
|
+
aliases: z.array(z.string()).describe('连接别名列表'),
|
|
49
|
+
command: z.string().describe('要执行的命令'),
|
|
50
|
+
timeout: z.number().optional().describe('每个命令的超时(毫秒),默认 30000'),
|
|
51
|
+
});
|
|
52
|
+
// ========== Handlers ==========
|
|
53
|
+
async function handleExec(args) {
|
|
54
|
+
try {
|
|
55
|
+
const result = await sessionManager.exec(args.alias, args.command, {
|
|
56
|
+
timeout: args.timeout,
|
|
57
|
+
cwd: args.cwd,
|
|
58
|
+
env: args.env,
|
|
59
|
+
pty: args.pty,
|
|
60
|
+
});
|
|
61
|
+
return formatResult(result);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return formatError(error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function handleExecAsUser(args) {
|
|
68
|
+
try {
|
|
69
|
+
const result = await sessionManager.execAsUser(args.alias, args.command, args.targetUser, {
|
|
70
|
+
timeout: args.timeout,
|
|
71
|
+
loadProfile: args.loadProfile,
|
|
72
|
+
});
|
|
73
|
+
return formatResult(result);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return formatError(error);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function handleExecSudo(args) {
|
|
80
|
+
try {
|
|
81
|
+
const result = await sessionManager.execSudo(args.alias, args.command, args.sudoPassword, { timeout: args.timeout });
|
|
82
|
+
return formatResult(result);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
return formatError(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function handleExecBatch(args) {
|
|
89
|
+
try {
|
|
90
|
+
const stopOnError = args.stopOnError !== false;
|
|
91
|
+
const results = [];
|
|
92
|
+
for (let i = 0; i < args.commands.length; i++) {
|
|
93
|
+
try {
|
|
94
|
+
const execResult = await sessionManager.exec(args.alias, args.commands[i], { timeout: args.timeout });
|
|
95
|
+
results.push({
|
|
96
|
+
index: i,
|
|
97
|
+
command: args.commands[i],
|
|
98
|
+
...execResult,
|
|
99
|
+
});
|
|
100
|
+
if (execResult.exitCode !== 0 && stopOnError) {
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
results.push({
|
|
106
|
+
index: i,
|
|
107
|
+
command: args.commands[i],
|
|
108
|
+
success: false,
|
|
109
|
+
error: err.message,
|
|
110
|
+
});
|
|
111
|
+
if (stopOnError) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return formatResult({
|
|
117
|
+
success: results.every((r) => r.success),
|
|
118
|
+
total: args.commands.length,
|
|
119
|
+
executed: results.length,
|
|
120
|
+
results,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
return formatError(error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function handleQuickExec(args) {
|
|
128
|
+
const tempAlias = `_quick_${Date.now()}`;
|
|
129
|
+
try {
|
|
130
|
+
await sessionManager.connect({
|
|
131
|
+
host: args.host,
|
|
132
|
+
port: args.port || 22,
|
|
133
|
+
username: args.user,
|
|
134
|
+
password: args.password,
|
|
135
|
+
privateKeyPath: args.keyPath,
|
|
136
|
+
alias: tempAlias,
|
|
137
|
+
});
|
|
138
|
+
const result = await sessionManager.exec(tempAlias, args.command, { timeout: args.timeout });
|
|
139
|
+
return formatResult(result);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
return formatError(error);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
sessionManager.disconnect(tempAlias);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async function handleExecParallel(args) {
|
|
149
|
+
try {
|
|
150
|
+
const execPromises = args.aliases.map(async (alias) => {
|
|
151
|
+
try {
|
|
152
|
+
const execResult = await sessionManager.exec(alias, args.command, { timeout: args.timeout });
|
|
153
|
+
return {
|
|
154
|
+
alias,
|
|
155
|
+
success: execResult.success,
|
|
156
|
+
exitCode: execResult.exitCode,
|
|
157
|
+
stdout: execResult.stdout,
|
|
158
|
+
stderr: execResult.stderr,
|
|
159
|
+
duration: execResult.duration,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
return {
|
|
164
|
+
alias,
|
|
165
|
+
success: false,
|
|
166
|
+
error: err.message,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
const results = await Promise.all(execPromises);
|
|
171
|
+
return formatResult({
|
|
172
|
+
success: results.every(r => r.success),
|
|
173
|
+
total: args.aliases.length,
|
|
174
|
+
results,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
return formatError(error);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// ========== Register ==========
|
|
182
|
+
export function registerExecTools(server) {
|
|
183
|
+
server.registerTool('ssh_exec', {
|
|
184
|
+
description: `在远程服务器执行命令。
|
|
185
|
+
|
|
186
|
+
返回: stdout, stderr, exitCode, duration`,
|
|
187
|
+
inputSchema: execSchema,
|
|
188
|
+
}, (args) => handleExec(args));
|
|
189
|
+
server.registerTool('ssh_exec_as_user', {
|
|
190
|
+
description: `以其他用户身份执行命令(通过 su 切换)。
|
|
191
|
+
|
|
192
|
+
适用场景: SSH 以 root 登录,但需要以其他用户(如 caros)执行命令。
|
|
193
|
+
|
|
194
|
+
默认加载目标用户的 shell 配置以获取环境变量(su -c 创建非交互式 shell,不会自动执行 rc 文件)。
|
|
195
|
+
支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)。
|
|
196
|
+
|
|
197
|
+
示例: ssh_exec_as_user(alias="server", command="whoami", targetUser="caros")`,
|
|
198
|
+
inputSchema: execAsUserSchema,
|
|
199
|
+
}, (args) => handleExecAsUser(args));
|
|
200
|
+
server.registerTool('ssh_exec_sudo', {
|
|
201
|
+
description: '使用 sudo 执行命令',
|
|
202
|
+
inputSchema: execSudoSchema,
|
|
203
|
+
}, (args) => handleExecSudo(args));
|
|
204
|
+
server.registerTool('ssh_exec_batch', {
|
|
205
|
+
description: '批量执行多条命令',
|
|
206
|
+
inputSchema: execBatchSchema,
|
|
207
|
+
}, (args) => handleExecBatch(args));
|
|
208
|
+
server.registerTool('ssh_quick_exec', {
|
|
209
|
+
description: '一次性执行命令(自动连接、执行、断开)。适用于单次命令,不需要保持连接。',
|
|
210
|
+
inputSchema: quickExecSchema,
|
|
211
|
+
}, (args) => handleQuickExec(args));
|
|
212
|
+
server.registerTool('ssh_exec_parallel', {
|
|
213
|
+
description: `在多个已连接的会话上并行执行同一命令。
|
|
214
|
+
|
|
215
|
+
示例:
|
|
216
|
+
- ssh_exec_parallel(aliases=["server1", "server2"], command="uptime")
|
|
217
|
+
|
|
218
|
+
返回每个主机的执行结果。`,
|
|
219
|
+
inputSchema: execParallelSchema,
|
|
220
|
+
}, (args) => handleExecParallel(args));
|
|
221
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
8
|
+
export declare function registerFileTools(server: McpServer): void;
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
import { z } from 'zod';
|
|
8
|
+
import * as fileOps from '../file-ops.js';
|
|
9
|
+
import { formatError, formatResult } from './utils.js';
|
|
10
|
+
// ========== Schemas ==========
|
|
11
|
+
const uploadSchema = z.object({
|
|
12
|
+
alias: z.string().describe('连接别名'),
|
|
13
|
+
localPath: z.string().describe('本地文件路径'),
|
|
14
|
+
remotePath: z.string().describe('远程目标路径'),
|
|
15
|
+
});
|
|
16
|
+
const downloadSchema = z.object({
|
|
17
|
+
alias: z.string().describe('连接别名'),
|
|
18
|
+
remotePath: z.string().describe('远程文件路径'),
|
|
19
|
+
localPath: z.string().describe('本地保存路径'),
|
|
20
|
+
});
|
|
21
|
+
const readFileSchema = z.object({
|
|
22
|
+
alias: z.string().describe('连接别名'),
|
|
23
|
+
remotePath: z.string().describe('远程文件路径'),
|
|
24
|
+
maxBytes: z.number().optional().describe('最大读取字节数,默认 1MB'),
|
|
25
|
+
});
|
|
26
|
+
const writeFileSchema = z.object({
|
|
27
|
+
alias: z.string().describe('连接别名'),
|
|
28
|
+
remotePath: z.string().describe('远程文件路径'),
|
|
29
|
+
content: z.string().describe('要写入的内容'),
|
|
30
|
+
append: z.boolean().optional().describe('是否追加模式,默认覆盖'),
|
|
31
|
+
});
|
|
32
|
+
const listDirSchema = z.object({
|
|
33
|
+
alias: z.string().describe('连接别名'),
|
|
34
|
+
remotePath: z.string().describe('远程目录路径'),
|
|
35
|
+
showHidden: z.boolean().optional().describe('是否显示隐藏文件'),
|
|
36
|
+
});
|
|
37
|
+
const fileInfoSchema = z.object({
|
|
38
|
+
alias: z.string().describe('连接别名'),
|
|
39
|
+
remotePath: z.string().describe('远程路径'),
|
|
40
|
+
});
|
|
41
|
+
const mkdirSchema = z.object({
|
|
42
|
+
alias: z.string().describe('连接别名'),
|
|
43
|
+
remotePath: z.string().describe('远程目录路径'),
|
|
44
|
+
recursive: z.boolean().optional().describe('是否递归创建,默认 false'),
|
|
45
|
+
});
|
|
46
|
+
const syncSchema = z.object({
|
|
47
|
+
alias: z.string().describe('连接别名'),
|
|
48
|
+
localPath: z.string().describe('本地路径'),
|
|
49
|
+
remotePath: z.string().describe('远程路径'),
|
|
50
|
+
direction: z.enum(['upload', 'download'])
|
|
51
|
+
.describe('同步方向:upload(本地到远程)或 download(远程到本地)'),
|
|
52
|
+
delete: z.boolean().optional().describe('删除目标端多余文件(类似 rsync --delete)'),
|
|
53
|
+
dryRun: z.boolean().optional().describe('仅显示将执行的操作,不实际传输'),
|
|
54
|
+
exclude: z.array(z.string()).optional().describe('排除模式列表(支持 * 和 ? 通配符)'),
|
|
55
|
+
recursive: z.boolean().optional().describe('递归同步目录,默认 true'),
|
|
56
|
+
});
|
|
57
|
+
// ========== Handlers ==========
|
|
58
|
+
async function handleUpload(args) {
|
|
59
|
+
try {
|
|
60
|
+
const uploadResult = await fileOps.uploadFile(args.alias, args.localPath, args.remotePath);
|
|
61
|
+
return formatResult({ ...uploadResult, message: `Uploaded to ${args.remotePath}` });
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return formatError(error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function handleDownload(args) {
|
|
68
|
+
try {
|
|
69
|
+
const downloadResult = await fileOps.downloadFile(args.alias, args.remotePath, args.localPath);
|
|
70
|
+
return formatResult({ ...downloadResult, message: `Downloaded to ${args.localPath}` });
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
return formatError(error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function handleReadFile(args) {
|
|
77
|
+
try {
|
|
78
|
+
const readResult = await fileOps.readFile(args.alias, args.remotePath, args.maxBytes);
|
|
79
|
+
return formatResult({ success: true, ...readResult });
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
return formatError(error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function handleWriteFile(args) {
|
|
86
|
+
try {
|
|
87
|
+
const result = await fileOps.writeFile(args.alias, args.remotePath, args.content, args.append);
|
|
88
|
+
return formatResult(result);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
return formatError(error);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function handleListDir(args) {
|
|
95
|
+
try {
|
|
96
|
+
const files = await fileOps.listDir(args.alias, args.remotePath, args.showHidden);
|
|
97
|
+
return formatResult({
|
|
98
|
+
success: true,
|
|
99
|
+
path: args.remotePath,
|
|
100
|
+
count: files.length,
|
|
101
|
+
files,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
return formatError(error);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function handleFileInfo(args) {
|
|
109
|
+
try {
|
|
110
|
+
const info = await fileOps.getFileInfo(args.alias, args.remotePath);
|
|
111
|
+
return formatResult({ success: true, ...info });
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
return formatError(error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function handleMkdir(args) {
|
|
118
|
+
try {
|
|
119
|
+
const success = await fileOps.mkdir(args.alias, args.remotePath, args.recursive);
|
|
120
|
+
return formatResult({ success, path: args.remotePath });
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
return formatError(error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async function handleSync(args) {
|
|
127
|
+
try {
|
|
128
|
+
const syncResult = await fileOps.syncFiles(args.alias, args.localPath, args.remotePath, args.direction, {
|
|
129
|
+
delete: args.delete,
|
|
130
|
+
dryRun: args.dryRun,
|
|
131
|
+
exclude: args.exclude,
|
|
132
|
+
recursive: args.recursive,
|
|
133
|
+
});
|
|
134
|
+
return formatResult({
|
|
135
|
+
...syncResult,
|
|
136
|
+
direction: args.direction,
|
|
137
|
+
localPath: args.localPath,
|
|
138
|
+
remotePath: args.remotePath,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
return formatError(error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// ========== Register ==========
|
|
146
|
+
export function registerFileTools(server) {
|
|
147
|
+
server.registerTool('ssh_upload', {
|
|
148
|
+
description: '上传本地文件到远程服务器',
|
|
149
|
+
inputSchema: uploadSchema,
|
|
150
|
+
}, (args) => handleUpload(args));
|
|
151
|
+
server.registerTool('ssh_download', {
|
|
152
|
+
description: '从远程服务器下载文件',
|
|
153
|
+
inputSchema: downloadSchema,
|
|
154
|
+
}, (args) => handleDownload(args));
|
|
155
|
+
server.registerTool('ssh_read_file', {
|
|
156
|
+
description: '读取远程文件内容',
|
|
157
|
+
inputSchema: readFileSchema,
|
|
158
|
+
}, (args) => handleReadFile(args));
|
|
159
|
+
server.registerTool('ssh_write_file', {
|
|
160
|
+
description: '写入内容到远程文件',
|
|
161
|
+
inputSchema: writeFileSchema,
|
|
162
|
+
}, (args) => handleWriteFile(args));
|
|
163
|
+
server.registerTool('ssh_list_dir', {
|
|
164
|
+
description: '列出远程目录内容',
|
|
165
|
+
inputSchema: listDirSchema,
|
|
166
|
+
}, (args) => handleListDir(args));
|
|
167
|
+
server.registerTool('ssh_file_info', {
|
|
168
|
+
description: '获取远程文件信息(大小、权限、修改时间等)',
|
|
169
|
+
inputSchema: fileInfoSchema,
|
|
170
|
+
}, (args) => handleFileInfo(args));
|
|
171
|
+
server.registerTool('ssh_mkdir', {
|
|
172
|
+
description: '创建远程目录',
|
|
173
|
+
inputSchema: mkdirSchema,
|
|
174
|
+
}, (args) => handleMkdir(args));
|
|
175
|
+
server.registerTool('ssh_sync', {
|
|
176
|
+
description: `智能文件同步(支持目录递归)。
|
|
177
|
+
|
|
178
|
+
优先使用 rsync(如果本地和远程都安装了),否则回退到 SFTP。
|
|
179
|
+
rsync 可实现增量传输,对大目录同步效率更高。
|
|
180
|
+
|
|
181
|
+
用途:
|
|
182
|
+
- 同步本地目录到远程
|
|
183
|
+
- 从远程同步目录到本地
|
|
184
|
+
- 支持排除特定文件/目录
|
|
185
|
+
|
|
186
|
+
示例:
|
|
187
|
+
- 上传目录: ssh_sync(alias="server", localPath="/local/dir", remotePath="/remote/dir", direction="upload")
|
|
188
|
+
- 下载目录: ssh_sync(alias="server", localPath="/local/dir", remotePath="/remote/dir", direction="download")
|
|
189
|
+
- 排除文件: ssh_sync(..., exclude=["*.log", "node_modules"])`,
|
|
190
|
+
inputSchema: syncSchema,
|
|
191
|
+
}, (args) => handleSync(args));
|
|
192
|
+
}
|