@pyrokine/mcp-ssh 1.2.1 → 1.3.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 +2 -2
- package/README_zh.md +2 -2
- package/dist/forward-manager.js +2 -1
- package/dist/index.js +16 -1
- package/dist/tools/exec.js +6 -4
- package/package.json +3 -3
- package/src/file-ops.ts +13 -13
- package/src/forward-manager.ts +3 -2
- package/src/index.ts +17 -2
- package/src/pty-manager.ts +1 -1
- package/src/session-manager.ts +9 -9
- package/src/ssh-config.ts +13 -13
- package/src/tools/connection.ts +2 -2
- package/src/tools/exec.ts +12 -8
- package/src/tools/file.ts +5 -5
- package/src/tools/pty.ts +2 -2
- package/src/tools/utils.ts +2 -2
package/README.md
CHANGED
|
@@ -57,8 +57,8 @@ npm install -g @pyrokine/mcp-ssh
|
|
|
57
57
|
### From Source
|
|
58
58
|
|
|
59
59
|
```bash
|
|
60
|
-
git clone https://github.com/Pyrokine/claude-
|
|
61
|
-
cd claude-
|
|
60
|
+
git clone https://github.com/Pyrokine/claude-tools.git
|
|
61
|
+
cd claude-tools/mcp-ssh
|
|
62
62
|
npm install
|
|
63
63
|
npm run build
|
|
64
64
|
```
|
package/README_zh.md
CHANGED
|
@@ -57,8 +57,8 @@ npm install -g @pyrokine/mcp-ssh
|
|
|
57
57
|
### 从源码安装
|
|
58
58
|
|
|
59
59
|
```bash
|
|
60
|
-
git clone https://github.com/Pyrokine/claude-
|
|
61
|
-
cd claude-
|
|
60
|
+
git clone https://github.com/Pyrokine/claude-tools.git
|
|
61
|
+
cd claude-tools/mcp-ssh
|
|
62
62
|
npm install
|
|
63
63
|
npm run build
|
|
64
64
|
```
|
package/dist/forward-manager.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,13 +16,28 @@
|
|
|
16
16
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
17
17
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
18
18
|
import { registerConnectionTools, registerExecTools, registerFileTools, registerForwardTools, registerPtyTools, } from './tools/index.js';
|
|
19
|
-
const server = new McpServer({ name: 'ssh-mcp-pro', version: '1.
|
|
19
|
+
const server = new McpServer({ name: 'ssh-mcp-pro', version: '1.2.2' }, { capabilities: { tools: {} } });
|
|
20
20
|
registerConnectionTools(server);
|
|
21
21
|
registerExecTools(server);
|
|
22
22
|
registerFileTools(server);
|
|
23
23
|
registerPtyTools(server);
|
|
24
24
|
registerForwardTools(server);
|
|
25
25
|
async function main() {
|
|
26
|
+
// 防止父进程退出后成为孤儿进程持续占用 CPU
|
|
27
|
+
process.on('uncaughtException', (error) => {
|
|
28
|
+
if (error.code === 'EPIPE' || error.code === 'ERR_STREAM_DESTROYED') {
|
|
29
|
+
process.exit(0);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
console.error('[MCP] Uncaught exception:', error);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
process.stdin.on('end', () => process.exit(0));
|
|
39
|
+
process.stdout.on('error', () => process.exit(0));
|
|
40
|
+
process.stderr.on('error', () => process.exit(0));
|
|
26
41
|
const transport = new StdioServerTransport();
|
|
27
42
|
await server.connect(transport);
|
|
28
43
|
console.error('SSH MCP Pro server started');
|
package/dist/tools/exec.js
CHANGED
|
@@ -11,7 +11,7 @@ import { formatError, formatResult } from './utils.js';
|
|
|
11
11
|
const execSchema = z.object({
|
|
12
12
|
alias: z.string().describe('连接别名'),
|
|
13
13
|
command: z.string().describe('要执行的命令'),
|
|
14
|
-
timeout: z.number().optional().describe('超时(毫秒),默认 30000'),
|
|
14
|
+
timeout: z.number().optional().describe('超时(毫秒),默认 30000。长时间命令可设更大值如 600000(10 分钟),或用 ssh_pty_start 替代'),
|
|
15
15
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
16
16
|
env: z.record(z.string()).optional().describe('额外环境变量'),
|
|
17
17
|
pty: z.boolean().optional().describe('是否使用 PTY 模式(用于 top 等交互式命令)'),
|
|
@@ -21,7 +21,7 @@ const execAsUserSchema = z.object({
|
|
|
21
21
|
command: z.string().describe('要执行的命令'),
|
|
22
22
|
targetUser: z.string().describe('目标用户名'),
|
|
23
23
|
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
24
|
-
loadProfile: z.boolean().optional().describe('
|
|
24
|
+
loadProfile: z.boolean().optional().describe('是否加载目标用户的 shell 配置(默认 true)。profile 加载慢导致超时时设为 false'),
|
|
25
25
|
});
|
|
26
26
|
const execSudoSchema = z.object({
|
|
27
27
|
alias: z.string().describe('连接别名'),
|
|
@@ -191,7 +191,7 @@ export function registerExecTools(server) {
|
|
|
191
191
|
|
|
192
192
|
适用场景: SSH 以 root 登录,但需要以其他用户(如 caros)执行命令。
|
|
193
193
|
|
|
194
|
-
默认加载目标用户的 shell
|
|
194
|
+
默认加载目标用户的 shell 配置以获取环境变量。如果 profile 加载耗时过长导致超时,设 loadProfile=false 跳过。
|
|
195
195
|
支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)。
|
|
196
196
|
|
|
197
197
|
示例: ssh_exec_as_user(alias="server", command="whoami", targetUser="caros")`,
|
|
@@ -202,7 +202,9 @@ export function registerExecTools(server) {
|
|
|
202
202
|
inputSchema: execSudoSchema,
|
|
203
203
|
}, (args) => handleExecSudo(args));
|
|
204
204
|
server.registerTool('ssh_exec_batch', {
|
|
205
|
-
description:
|
|
205
|
+
description: `批量执行多条命令。每条命令在独立 SSH channel 中执行,环境变量不共享。
|
|
206
|
+
|
|
207
|
+
如需命令间共享环境(如 export 变量),用分号拼接为单条命令通过 ssh_exec 执行。`,
|
|
206
208
|
inputSchema: execBatchSchema,
|
|
207
209
|
}, (args) => handleExecBatch(args));
|
|
208
210
|
server.registerTool('ssh_quick_exec', {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyrokine/mcp-ssh",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A comprehensive SSH MCP Server for Claude Code",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"repository": {
|
|
27
27
|
"type": "git",
|
|
28
|
-
"url": "https://github.com/Pyrokine/claude-
|
|
28
|
+
"url": "https://github.com/Pyrokine/claude-tools.git"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
31
|
+
"@modelcontextprotocol/sdk": "^1.27.1",
|
|
32
32
|
"@xterm/headless": "^6.0.0",
|
|
33
33
|
"ssh2": "^1.17.0",
|
|
34
34
|
"zod": "^3.25.76"
|
package/src/file-ops.ts
CHANGED
|
@@ -72,7 +72,7 @@ function pipeWithProgress(
|
|
|
72
72
|
if (!settled) {
|
|
73
73
|
settled = true
|
|
74
74
|
sftp.end()
|
|
75
|
-
resolve({success: true, size: totalSize})
|
|
75
|
+
resolve({ success: true, size: totalSize })
|
|
76
76
|
}
|
|
77
77
|
})
|
|
78
78
|
|
|
@@ -118,7 +118,7 @@ export async function downloadFile(
|
|
|
118
118
|
// 确保本地目录存在
|
|
119
119
|
const localDir = path.dirname(localPath)
|
|
120
120
|
if (!fs.existsSync(localDir)) {
|
|
121
|
-
fs.mkdirSync(localDir, {recursive: true})
|
|
121
|
+
fs.mkdirSync(localDir, { recursive: true })
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
return pipeWithProgress(
|
|
@@ -143,7 +143,7 @@ export async function readFile(
|
|
|
143
143
|
// 处理空文件
|
|
144
144
|
if (actualSize === 0) {
|
|
145
145
|
sftp.end()
|
|
146
|
-
return {content: '', size: 0, truncated: false}
|
|
146
|
+
return { content: '', size: 0, truncated: false }
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
const readSize = Math.min(actualSize, maxBytes)
|
|
@@ -190,11 +190,11 @@ export async function writeFile(
|
|
|
190
190
|
const flags = append ? 'a' : 'w'
|
|
191
191
|
|
|
192
192
|
return new Promise((resolve, reject) => {
|
|
193
|
-
const writeStream = sftp.createWriteStream(remotePath, {flags})
|
|
193
|
+
const writeStream = sftp.createWriteStream(remotePath, { flags })
|
|
194
194
|
|
|
195
195
|
writeStream.on('close', () => {
|
|
196
196
|
sftp.end()
|
|
197
|
-
resolve({success: true, size: content.length})
|
|
197
|
+
resolve({ success: true, size: content.length })
|
|
198
198
|
})
|
|
199
199
|
|
|
200
200
|
writeStream.on('error', (err: Error) => {
|
|
@@ -384,7 +384,7 @@ async function syncWithRsync(
|
|
|
384
384
|
let hasLocalRsync = false
|
|
385
385
|
try {
|
|
386
386
|
const cmd = os.platform() === 'win32' ? 'where rsync' : 'which rsync'
|
|
387
|
-
execSync(cmd, {stdio: 'pipe'})
|
|
387
|
+
execSync(cmd, { stdio: 'pipe' })
|
|
388
388
|
hasLocalRsync = true
|
|
389
389
|
} catch {
|
|
390
390
|
}
|
|
@@ -513,20 +513,20 @@ async function syncWithSftp(
|
|
|
513
513
|
if (direction === 'upload') {
|
|
514
514
|
const stats = fs.statSync(localPath)
|
|
515
515
|
if (stats.isDirectory() && options.recursive !== false) {
|
|
516
|
-
const {fileCount, totalSize} = await uploadDirectory(alias, localPath, remotePath, options.exclude)
|
|
516
|
+
const { fileCount, totalSize } = await uploadDirectory(alias, localPath, remotePath, options.exclude)
|
|
517
517
|
return buildSyncResult(fileCount, totalSize, warnings)
|
|
518
518
|
}
|
|
519
|
-
const {size} = await uploadFile(alias, localPath, remotePath)
|
|
519
|
+
const { size } = await uploadFile(alias, localPath, remotePath)
|
|
520
520
|
return buildSyncResult(1, size, warnings)
|
|
521
521
|
}
|
|
522
522
|
|
|
523
523
|
// download
|
|
524
524
|
const info = await getFileInfo(alias, remotePath)
|
|
525
525
|
if (info.isDirectory && options.recursive !== false) {
|
|
526
|
-
const {fileCount, totalSize} = await downloadDirectory(alias, remotePath, localPath, options.exclude)
|
|
526
|
+
const { fileCount, totalSize } = await downloadDirectory(alias, remotePath, localPath, options.exclude)
|
|
527
527
|
return buildSyncResult(fileCount, totalSize, warnings)
|
|
528
528
|
}
|
|
529
|
-
const {size} = await downloadFile(alias, remotePath, localPath)
|
|
529
|
+
const { size } = await downloadFile(alias, remotePath, localPath)
|
|
530
530
|
return buildSyncResult(1, size, warnings)
|
|
531
531
|
} catch (err: any) {
|
|
532
532
|
return {
|
|
@@ -574,7 +574,7 @@ async function uploadDirectory(
|
|
|
574
574
|
}
|
|
575
575
|
}
|
|
576
576
|
|
|
577
|
-
return {fileCount, totalSize}
|
|
577
|
+
return { fileCount, totalSize }
|
|
578
578
|
}
|
|
579
579
|
|
|
580
580
|
/**
|
|
@@ -591,7 +591,7 @@ async function downloadDirectory(
|
|
|
591
591
|
|
|
592
592
|
// 确保本地目录存在
|
|
593
593
|
if (!fs.existsSync(localPath)) {
|
|
594
|
-
fs.mkdirSync(localPath, {recursive: true})
|
|
594
|
+
fs.mkdirSync(localPath, { recursive: true })
|
|
595
595
|
}
|
|
596
596
|
|
|
597
597
|
const items = await listDir(alias, remotePath, true)
|
|
@@ -614,7 +614,7 @@ async function downloadDirectory(
|
|
|
614
614
|
}
|
|
615
615
|
}
|
|
616
616
|
|
|
617
|
-
return {fileCount, totalSize}
|
|
617
|
+
return { fileCount, totalSize }
|
|
618
618
|
}
|
|
619
619
|
|
|
620
620
|
/**
|
package/src/forward-manager.ts
CHANGED
|
@@ -178,7 +178,8 @@ export class ForwardManager {
|
|
|
178
178
|
if (state) {
|
|
179
179
|
try {
|
|
180
180
|
state.client.removeListener('tcp connection', state.handler)
|
|
181
|
-
} catch { /* client 可能已关闭 */
|
|
181
|
+
} catch { /* client 可能已关闭 */
|
|
182
|
+
}
|
|
182
183
|
this.dispatchers.delete(alias)
|
|
183
184
|
}
|
|
184
185
|
}
|
|
@@ -228,7 +229,7 @@ export class ForwardManager {
|
|
|
228
229
|
rejectConn()
|
|
229
230
|
}
|
|
230
231
|
|
|
231
|
-
this.dispatchers.set(alias, {handler, client})
|
|
232
|
+
this.dispatchers.set(alias, { handler, client })
|
|
232
233
|
client.on('tcp connection', handler)
|
|
233
234
|
}
|
|
234
235
|
|
package/src/index.ts
CHANGED
|
@@ -25,8 +25,8 @@ import {
|
|
|
25
25
|
} from './tools/index.js'
|
|
26
26
|
|
|
27
27
|
const server = new McpServer(
|
|
28
|
-
{name: 'ssh-mcp-pro', version: '1.
|
|
29
|
-
{capabilities: {tools: {}}},
|
|
28
|
+
{ name: 'ssh-mcp-pro', version: '1.2.2' },
|
|
29
|
+
{ capabilities: { tools: {} } },
|
|
30
30
|
)
|
|
31
31
|
|
|
32
32
|
registerConnectionTools(server)
|
|
@@ -36,6 +36,21 @@ registerPtyTools(server)
|
|
|
36
36
|
registerForwardTools(server)
|
|
37
37
|
|
|
38
38
|
async function main() {
|
|
39
|
+
// 防止父进程退出后成为孤儿进程持续占用 CPU
|
|
40
|
+
process.on('uncaughtException', (error: NodeJS.ErrnoException) => {
|
|
41
|
+
if (error.code === 'EPIPE' || error.code === 'ERR_STREAM_DESTROYED') {
|
|
42
|
+
process.exit(0)
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
console.error('[MCP] Uncaught exception:', error)
|
|
46
|
+
} catch {
|
|
47
|
+
process.exit(1)
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
process.stdin.on('end', () => process.exit(0))
|
|
51
|
+
process.stdout.on('error', () => process.exit(0))
|
|
52
|
+
process.stderr.on('error', () => process.exit(0))
|
|
53
|
+
|
|
39
54
|
const transport = new StdioServerTransport()
|
|
40
55
|
await server.connect(transport)
|
|
41
56
|
console.error('SSH MCP Pro server started')
|
package/src/pty-manager.ts
CHANGED
|
@@ -51,7 +51,7 @@ export class PtyManager {
|
|
|
51
51
|
const stream = await deps.execPty(alias, command, options)
|
|
52
52
|
|
|
53
53
|
// 创建 xterm headless 终端仿真器
|
|
54
|
-
const terminal = new Terminal({rows, cols, allowProposedApi: true})
|
|
54
|
+
const terminal = new Terminal({ rows, cols, allowProposedApi: true })
|
|
55
55
|
|
|
56
56
|
const ptySession: PtySession = {
|
|
57
57
|
id: ptyId,
|
package/src/session-manager.ts
CHANGED
|
@@ -49,9 +49,9 @@ export class SessionManager {
|
|
|
49
49
|
|
|
50
50
|
constructor(persistPath?: string) {
|
|
51
51
|
this.persistPath = persistPath || path.join(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
os.homedir(),
|
|
53
|
+
'.ssh-mcp-pro',
|
|
54
|
+
'sessions.json',
|
|
55
55
|
)
|
|
56
56
|
this.ensurePersistDir()
|
|
57
57
|
}
|
|
@@ -333,7 +333,7 @@ export class SessionManager {
|
|
|
333
333
|
throw new Error(`Invalid username: ${targetUser}`)
|
|
334
334
|
}
|
|
335
335
|
|
|
336
|
-
const {loadProfile = true, ...execOpts} = options
|
|
336
|
+
const { loadProfile = true, ...execOpts } = options
|
|
337
337
|
|
|
338
338
|
const wrappedCommand = loadProfile
|
|
339
339
|
? `${this.getLoadProfileCommand()}${command}`
|
|
@@ -419,7 +419,7 @@ export class SessionManager {
|
|
|
419
419
|
localHost: string = '127.0.0.1',
|
|
420
420
|
): Promise<string> {
|
|
421
421
|
return this.forwardManager.forwardLocal(
|
|
422
|
-
{getClient: (a) => this.getSession(a).client},
|
|
422
|
+
{ getClient: (a) => this.getSession(a).client },
|
|
423
423
|
alias, localPort, remoteHost, remotePort, localHost,
|
|
424
424
|
)
|
|
425
425
|
}
|
|
@@ -432,7 +432,7 @@ export class SessionManager {
|
|
|
432
432
|
remoteHost: string = '127.0.0.1',
|
|
433
433
|
): Promise<string> {
|
|
434
434
|
return this.forwardManager.forwardRemote(
|
|
435
|
-
{getClient: (a) => this.getSession(a).client},
|
|
435
|
+
{ getClient: (a) => this.getSession(a).client },
|
|
436
436
|
alias, remotePort, localHost, localPort, remoteHost,
|
|
437
437
|
)
|
|
438
438
|
}
|
|
@@ -456,7 +456,7 @@ export class SessionManager {
|
|
|
456
456
|
options: PtyOptions,
|
|
457
457
|
): Promise<ClientChannel> {
|
|
458
458
|
const session = this.getSession(alias)
|
|
459
|
-
const fullCommand = this.buildCommand(command, session, {env: options.env, cwd: options.cwd})
|
|
459
|
+
const fullCommand = this.buildCommand(command, session, { env: options.env, cwd: options.cwd })
|
|
460
460
|
|
|
461
461
|
return new Promise((resolve, reject) => {
|
|
462
462
|
session.client.exec(fullCommand, {
|
|
@@ -478,7 +478,7 @@ export class SessionManager {
|
|
|
478
478
|
private ensurePersistDir(): void {
|
|
479
479
|
const dir = path.dirname(this.persistPath)
|
|
480
480
|
if (!fs.existsSync(dir)) {
|
|
481
|
-
fs.mkdirSync(dir, {recursive: true})
|
|
481
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
482
482
|
}
|
|
483
483
|
}
|
|
484
484
|
|
|
@@ -602,7 +602,7 @@ export class SessionManager {
|
|
|
602
602
|
options: { env?: Record<string, string>; cwd?: string },
|
|
603
603
|
): string {
|
|
604
604
|
let fullCommand = command
|
|
605
|
-
const env = {...session.config.env, ...options.env}
|
|
605
|
+
const env = { ...session.config.env, ...options.env }
|
|
606
606
|
|
|
607
607
|
if (Object.keys(env).length > 0) {
|
|
608
608
|
const validEntries = Object.entries(env).filter(([k]) => this.isValidEnvKey(k))
|
package/src/ssh-config.ts
CHANGED
|
@@ -48,17 +48,17 @@ function parseHostPort(s: string): { host: string; port?: number } {
|
|
|
48
48
|
if (rest.startsWith(':')) {
|
|
49
49
|
const parsedPort = parseInt(rest.slice(1), 10)
|
|
50
50
|
if (!isNaN(parsedPort)) {
|
|
51
|
-
return {host, port: parsedPort}
|
|
51
|
+
return { host, port: parsedPort }
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
return {host}
|
|
54
|
+
return { host }
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
// 检测裸 IPv6(多个冒号但无方括号):安全失败,当作 host-only
|
|
59
59
|
const colonCount = (s.match(/:/g) || []).length
|
|
60
60
|
if (colonCount >= 2) {
|
|
61
|
-
return {host: s}
|
|
61
|
+
return { host: s }
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
// 普通格式: host:port 或 host
|
|
@@ -68,10 +68,10 @@ function parseHostPort(s: string): { host: string; port?: number } {
|
|
|
68
68
|
const portStr = s.slice(colonIndex + 1)
|
|
69
69
|
const parsedPort = parseInt(portStr, 10)
|
|
70
70
|
if (!isNaN(parsedPort)) {
|
|
71
|
-
return {host, port: parsedPort}
|
|
71
|
+
return { host, port: parsedPort }
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
return {host: s}
|
|
74
|
+
return { host: s }
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
@@ -95,14 +95,14 @@ export function parseProxyJump(proxyJump: string): ParsedProxyJump | null {
|
|
|
95
95
|
// 解析 user@... 格式
|
|
96
96
|
const atIndex = firstJump.indexOf('@')
|
|
97
97
|
if (atIndex !== -1) {
|
|
98
|
-
user
|
|
99
|
-
const rest
|
|
100
|
-
const {host, port} = parseHostPort(rest)
|
|
101
|
-
return {user, host, port}
|
|
98
|
+
user = firstJump.slice(0, atIndex)
|
|
99
|
+
const rest = firstJump.slice(atIndex + 1)
|
|
100
|
+
const { host, port } = parseHostPort(rest)
|
|
101
|
+
return { user, host, port }
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
const {host, port} = parseHostPort(firstJump)
|
|
105
|
-
return {host, port}
|
|
104
|
+
const { host, port } = parseHostPort(firstJump)
|
|
105
|
+
return { host, port }
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
/**
|
|
@@ -187,7 +187,7 @@ export function parseSSHConfig(configPath?: string): SSHConfigHost[] {
|
|
|
187
187
|
|
|
188
188
|
// Host 行可能有多个别名/模式,用空格分隔
|
|
189
189
|
const patterns = value.split(/\s+/).filter(p => p.length > 0)
|
|
190
|
-
currentBlock = {patterns, config: {}}
|
|
190
|
+
currentBlock = { patterns, config: {} }
|
|
191
191
|
} else if (keyLower === 'match') {
|
|
192
192
|
// Match 块开始,跳过直到下一个 Host
|
|
193
193
|
inMatchBlock = true
|
|
@@ -226,7 +226,7 @@ export function parseSSHConfig(configPath?: string): SSHConfigHost[] {
|
|
|
226
226
|
// 第二遍:提取 Host * 的全局默认配置
|
|
227
227
|
for (const block of blocks) {
|
|
228
228
|
if (block.patterns.length === 1 && block.patterns[0] === '*') {
|
|
229
|
-
globalDefaults = {...block.config}
|
|
229
|
+
globalDefaults = { ...block.config }
|
|
230
230
|
break
|
|
231
231
|
}
|
|
232
232
|
}
|
package/src/tools/connection.ts
CHANGED
|
@@ -71,7 +71,7 @@ async function handleConnect(args: z.infer<typeof connectSchema>) {
|
|
|
71
71
|
const allHosts = parseSSHConfig(args.configPath)
|
|
72
72
|
const hostConfig = allHosts.find(h => h.host === args.configHost)
|
|
73
73
|
if (!hostConfig) {
|
|
74
|
-
return formatResult({success: false, error: `Host '${args.configHost}' not found in SSH config`})
|
|
74
|
+
return formatResult({ success: false, error: `Host '${args.configHost}' not found in SSH config` })
|
|
75
75
|
}
|
|
76
76
|
// 显式参数优先于 config 值
|
|
77
77
|
host = host || hostConfig.hostName || hostConfig.host
|
|
@@ -174,7 +174,7 @@ async function handleListSessions() {
|
|
|
174
174
|
async function handleReconnect(args: z.infer<typeof reconnectSchema>) {
|
|
175
175
|
try {
|
|
176
176
|
await sessionManager.reconnect(args.alias)
|
|
177
|
-
return formatResult({success: true, message: `Reconnected to ${args.alias}`})
|
|
177
|
+
return formatResult({ success: true, message: `Reconnected to ${args.alias}` })
|
|
178
178
|
} catch (error) {
|
|
179
179
|
return formatError(error)
|
|
180
180
|
}
|
package/src/tools/exec.ts
CHANGED
|
@@ -15,7 +15,8 @@ import {formatError, formatResult} from './utils.js'
|
|
|
15
15
|
const execSchema = z.object({
|
|
16
16
|
alias: z.string().describe('连接别名'),
|
|
17
17
|
command: z.string().describe('要执行的命令'),
|
|
18
|
-
timeout: z.number().optional().describe(
|
|
18
|
+
timeout: z.number().optional().describe(
|
|
19
|
+
'超时(毫秒),默认 30000。长时间命令可设更大值如 600000(10 分钟),或用 ssh_pty_start 替代'),
|
|
19
20
|
cwd: z.string().optional().describe('工作目录(可选)'),
|
|
20
21
|
env: z.record(z.string()).optional().describe('额外环境变量'),
|
|
21
22
|
pty: z.boolean().optional().describe('是否使用 PTY 模式(用于 top 等交互式命令)'),
|
|
@@ -26,7 +27,8 @@ const execAsUserSchema = z.object({
|
|
|
26
27
|
command: z.string().describe('要执行的命令'),
|
|
27
28
|
targetUser: z.string().describe('目标用户名'),
|
|
28
29
|
timeout: z.number().optional().describe('超时(毫秒)'),
|
|
29
|
-
loadProfile: z.boolean().optional().describe(
|
|
30
|
+
loadProfile: z.boolean().optional().describe(
|
|
31
|
+
'是否加载目标用户的 shell 配置(默认 true)。profile 加载慢导致超时时设为 false'),
|
|
30
32
|
})
|
|
31
33
|
|
|
32
34
|
const execSudoSchema = z.object({
|
|
@@ -102,7 +104,7 @@ async function handleExecSudo(args: z.infer<typeof execSudoSchema>) {
|
|
|
102
104
|
args.alias,
|
|
103
105
|
args.command,
|
|
104
106
|
args.sudoPassword,
|
|
105
|
-
{timeout: args.timeout},
|
|
107
|
+
{ timeout: args.timeout },
|
|
106
108
|
)
|
|
107
109
|
return formatResult(result)
|
|
108
110
|
} catch (error) {
|
|
@@ -120,7 +122,7 @@ async function handleExecBatch(args: z.infer<typeof execBatchSchema>) {
|
|
|
120
122
|
const execResult = await sessionManager.exec(
|
|
121
123
|
args.alias,
|
|
122
124
|
args.commands[i],
|
|
123
|
-
{timeout: args.timeout},
|
|
125
|
+
{ timeout: args.timeout },
|
|
124
126
|
)
|
|
125
127
|
results.push({
|
|
126
128
|
index: i,
|
|
@@ -168,7 +170,7 @@ async function handleQuickExec(args: z.infer<typeof quickExecSchema>) {
|
|
|
168
170
|
const result = await sessionManager.exec(
|
|
169
171
|
tempAlias,
|
|
170
172
|
args.command,
|
|
171
|
-
{timeout: args.timeout},
|
|
173
|
+
{ timeout: args.timeout },
|
|
172
174
|
)
|
|
173
175
|
return formatResult(result)
|
|
174
176
|
} catch (error) {
|
|
@@ -182,7 +184,7 @@ async function handleExecParallel(args: z.infer<typeof execParallelSchema>) {
|
|
|
182
184
|
try {
|
|
183
185
|
const execPromises = args.aliases.map(async (alias) => {
|
|
184
186
|
try {
|
|
185
|
-
const execResult = await sessionManager.exec(alias, args.command, {timeout: args.timeout})
|
|
187
|
+
const execResult = await sessionManager.exec(alias, args.command, { timeout: args.timeout })
|
|
186
188
|
return {
|
|
187
189
|
alias,
|
|
188
190
|
success: execResult.success,
|
|
@@ -226,7 +228,7 @@ export function registerExecTools(server: McpServer): void {
|
|
|
226
228
|
|
|
227
229
|
适用场景: SSH 以 root 登录,但需要以其他用户(如 caros)执行命令。
|
|
228
230
|
|
|
229
|
-
默认加载目标用户的 shell
|
|
231
|
+
默认加载目标用户的 shell 配置以获取环境变量。如果 profile 加载耗时过长导致超时,设 loadProfile=false 跳过。
|
|
230
232
|
支持 bash(.bashrc)、zsh(.zshrc) 及其他 shell(.profile)。
|
|
231
233
|
|
|
232
234
|
示例: ssh_exec_as_user(alias="server", command="whoami", targetUser="caros")`,
|
|
@@ -239,7 +241,9 @@ export function registerExecTools(server: McpServer): void {
|
|
|
239
241
|
}, (args) => handleExecSudo(args))
|
|
240
242
|
|
|
241
243
|
server.registerTool('ssh_exec_batch', {
|
|
242
|
-
description:
|
|
244
|
+
description: `批量执行多条命令。每条命令在独立 SSH channel 中执行,环境变量不共享。
|
|
245
|
+
|
|
246
|
+
如需命令间共享环境(如 export 变量),用分号拼接为单条命令通过 ssh_exec 执行。`,
|
|
243
247
|
inputSchema: execBatchSchema,
|
|
244
248
|
}, (args) => handleExecBatch(args))
|
|
245
249
|
|
package/src/tools/file.ts
CHANGED
|
@@ -71,7 +71,7 @@ const syncSchema = z.object({
|
|
|
71
71
|
async function handleUpload(args: z.infer<typeof uploadSchema>) {
|
|
72
72
|
try {
|
|
73
73
|
const uploadResult = await fileOps.uploadFile(args.alias, args.localPath, args.remotePath)
|
|
74
|
-
return formatResult({...uploadResult, message: `Uploaded to ${args.remotePath}`})
|
|
74
|
+
return formatResult({ ...uploadResult, message: `Uploaded to ${args.remotePath}` })
|
|
75
75
|
} catch (error) {
|
|
76
76
|
return formatError(error)
|
|
77
77
|
}
|
|
@@ -80,7 +80,7 @@ async function handleUpload(args: z.infer<typeof uploadSchema>) {
|
|
|
80
80
|
async function handleDownload(args: z.infer<typeof downloadSchema>) {
|
|
81
81
|
try {
|
|
82
82
|
const downloadResult = await fileOps.downloadFile(args.alias, args.remotePath, args.localPath)
|
|
83
|
-
return formatResult({...downloadResult, message: `Downloaded to ${args.localPath}`})
|
|
83
|
+
return formatResult({ ...downloadResult, message: `Downloaded to ${args.localPath}` })
|
|
84
84
|
} catch (error) {
|
|
85
85
|
return formatError(error)
|
|
86
86
|
}
|
|
@@ -89,7 +89,7 @@ async function handleDownload(args: z.infer<typeof downloadSchema>) {
|
|
|
89
89
|
async function handleReadFile(args: z.infer<typeof readFileSchema>) {
|
|
90
90
|
try {
|
|
91
91
|
const readResult = await fileOps.readFile(args.alias, args.remotePath, args.maxBytes)
|
|
92
|
-
return formatResult({success: true, ...readResult})
|
|
92
|
+
return formatResult({ success: true, ...readResult })
|
|
93
93
|
} catch (error) {
|
|
94
94
|
return formatError(error)
|
|
95
95
|
}
|
|
@@ -121,7 +121,7 @@ async function handleListDir(args: z.infer<typeof listDirSchema>) {
|
|
|
121
121
|
async function handleFileInfo(args: z.infer<typeof fileInfoSchema>) {
|
|
122
122
|
try {
|
|
123
123
|
const info = await fileOps.getFileInfo(args.alias, args.remotePath)
|
|
124
|
-
return formatResult({success: true, ...info})
|
|
124
|
+
return formatResult({ success: true, ...info })
|
|
125
125
|
} catch (error) {
|
|
126
126
|
return formatError(error)
|
|
127
127
|
}
|
|
@@ -130,7 +130,7 @@ async function handleFileInfo(args: z.infer<typeof fileInfoSchema>) {
|
|
|
130
130
|
async function handleMkdir(args: z.infer<typeof mkdirSchema>) {
|
|
131
131
|
try {
|
|
132
132
|
const success = await fileOps.mkdir(args.alias, args.remotePath, args.recursive)
|
|
133
|
-
return formatResult({success, path: args.remotePath})
|
|
133
|
+
return formatResult({ success, path: args.remotePath })
|
|
134
134
|
} catch (error) {
|
|
135
135
|
return formatError(error)
|
|
136
136
|
}
|
package/src/tools/pty.ts
CHANGED
|
@@ -77,7 +77,7 @@ async function handlePtyStart(args: z.infer<typeof ptyStartSchema>) {
|
|
|
77
77
|
async function handlePtyWrite(args: z.infer<typeof ptyWriteSchema>) {
|
|
78
78
|
try {
|
|
79
79
|
const success = sessionManager.ptyWrite(args.ptyId, args.data)
|
|
80
|
-
return formatResult({success, ptyId: args.ptyId})
|
|
80
|
+
return formatResult({ success, ptyId: args.ptyId })
|
|
81
81
|
} catch (error) {
|
|
82
82
|
return formatError(error)
|
|
83
83
|
}
|
|
@@ -106,7 +106,7 @@ async function handlePtyRead(args: z.infer<typeof ptyReadSchema>) {
|
|
|
106
106
|
async function handlePtyResize(args: z.infer<typeof ptyResizeSchema>) {
|
|
107
107
|
try {
|
|
108
108
|
const success = sessionManager.ptyResize(args.ptyId, args.rows, args.cols)
|
|
109
|
-
return formatResult({success, ptyId: args.ptyId})
|
|
109
|
+
return formatResult({ success, ptyId: args.ptyId })
|
|
110
110
|
} catch (error) {
|
|
111
111
|
return formatError(error)
|
|
112
112
|
}
|
package/src/tools/utils.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export function formatResult(data: unknown) {
|
|
9
9
|
return {
|
|
10
|
-
content: [{type: 'text' as const, text: JSON.stringify(data, null, 2)}],
|
|
10
|
+
content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }],
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -17,7 +17,7 @@ export function formatResult(data: unknown) {
|
|
|
17
17
|
export function formatError(error: unknown) {
|
|
18
18
|
const message = error instanceof Error ? error.message : String(error)
|
|
19
19
|
return {
|
|
20
|
-
content: [{type: 'text' as const, text: JSON.stringify({success: false, error: message}, null, 2)}],
|
|
20
|
+
content: [{ type: 'text' as const, text: JSON.stringify({ success: false, error: message }, null, 2) }],
|
|
21
21
|
isError: true,
|
|
22
22
|
}
|
|
23
23
|
}
|