@pyrokine/mcp-ssh 1.1.2 → 1.2.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.
Files changed (43) hide show
  1. package/README.md +3 -1
  2. package/README_zh.md +3 -1
  3. package/dist/file-ops.d.ts +7 -10
  4. package/dist/file-ops.js +74 -172
  5. package/dist/forward-manager.d.ts +33 -0
  6. package/dist/forward-manager.js +185 -0
  7. package/dist/index.js +8 -953
  8. package/dist/pty-manager.d.ts +37 -0
  9. package/dist/pty-manager.js +169 -0
  10. package/dist/session-manager.d.ts +28 -86
  11. package/dist/session-manager.js +131 -441
  12. package/dist/ssh-config.d.ts +0 -4
  13. package/dist/ssh-config.js +1 -8
  14. package/dist/tools/connection.d.ts +7 -0
  15. package/dist/tools/connection.js +215 -0
  16. package/dist/tools/exec.d.ts +8 -0
  17. package/dist/tools/exec.js +221 -0
  18. package/dist/tools/file.d.ts +8 -0
  19. package/dist/tools/file.js +192 -0
  20. package/dist/tools/forward.d.ts +7 -0
  21. package/dist/tools/forward.js +116 -0
  22. package/dist/tools/index.d.ts +8 -0
  23. package/dist/tools/index.js +8 -0
  24. package/dist/tools/pty.d.ts +7 -0
  25. package/dist/tools/pty.js +178 -0
  26. package/dist/tools/utils.d.ts +22 -0
  27. package/dist/tools/utils.js +21 -0
  28. package/dist/types.d.ts +0 -7
  29. package/package.json +3 -2
  30. package/src/file-ops.ts +117 -212
  31. package/src/forward-manager.ts +257 -0
  32. package/src/index.ts +17 -1099
  33. package/src/pty-manager.ts +220 -0
  34. package/src/session-manager.ts +166 -532
  35. package/src/ssh-config.ts +1 -8
  36. package/src/tools/connection.ts +240 -0
  37. package/src/tools/exec.ts +260 -0
  38. package/src/tools/file.ts +219 -0
  39. package/src/tools/forward.ts +141 -0
  40. package/src/tools/index.ts +9 -0
  41. package/src/tools/pty.ts +203 -0
  42. package/src/tools/utils.ts +23 -0
  43. package/src/types.ts +0 -9
@@ -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'
@@ -0,0 +1,203 @@
1
+ /**
2
+ * PTY 会话工具组
3
+ *
4
+ * ssh_pty_start, ssh_pty_write, ssh_pty_read, ssh_pty_resize, ssh_pty_close, ssh_pty_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 ptyStartSchema = z.object({
15
+ alias: z.string().describe('连接别名'),
16
+ command: z.string().describe('要执行的命令'),
17
+ rows: z.number().optional().describe('终端行数,默认 24'),
18
+ cols: z.number().optional().describe('终端列数,默认 80'),
19
+ term: z.string().optional().describe('终端类型,默认 xterm-256color'),
20
+ cwd: z.string().optional().describe('工作目录'),
21
+ env: z.record(z.string()).optional().describe('环境变量'),
22
+ bufferSize: z.number().optional().describe('输出缓冲区大小(字节),默认 1MB'),
23
+ })
24
+
25
+ const ptyWriteSchema = z.object({
26
+ ptyId: z.string().describe('PTY 会话 ID'),
27
+ data: z.string().describe('要写入的数据'),
28
+ })
29
+
30
+ const ptyReadSchema = z.object({
31
+ ptyId: z.string().describe('PTY 会话 ID'),
32
+ mode: z.enum(['screen', 'raw']).optional().describe(
33
+ '输出模式:screen(当前屏幕)或 raw(原始流),默认 screen'),
34
+ clear: z.boolean()
35
+ .optional()
36
+ .describe('(仅 raw 模式) 读取后是否清空缓冲区,默认 true'),
37
+ })
38
+
39
+ const ptyResizeSchema = z.object({
40
+ ptyId: z.string().describe('PTY 会话 ID'),
41
+ rows: z.number().describe('新的行数'),
42
+ cols: z.number().describe('新的列数'),
43
+ })
44
+
45
+ const ptyCloseSchema = z.object({
46
+ ptyId: z.string().describe('PTY 会话 ID'),
47
+ })
48
+
49
+ const ptyListSchema = z.object({})
50
+
51
+ // ========== Handlers ==========
52
+
53
+ async function handlePtyStart(args: z.infer<typeof ptyStartSchema>) {
54
+ try {
55
+ const ptyId = await sessionManager.ptyStart(
56
+ args.alias,
57
+ args.command,
58
+ {
59
+ rows: args.rows,
60
+ cols: args.cols,
61
+ term: args.term,
62
+ cwd: args.cwd,
63
+ env: args.env,
64
+ bufferSize: args.bufferSize,
65
+ },
66
+ )
67
+ return formatResult({
68
+ success: true,
69
+ ptyId,
70
+ message: `PTY session started: ${args.command}`,
71
+ })
72
+ } catch (error) {
73
+ return formatError(error)
74
+ }
75
+ }
76
+
77
+ async function handlePtyWrite(args: z.infer<typeof ptyWriteSchema>) {
78
+ try {
79
+ const success = sessionManager.ptyWrite(args.ptyId, args.data)
80
+ return formatResult({success, ptyId: args.ptyId})
81
+ } catch (error) {
82
+ return formatError(error)
83
+ }
84
+ }
85
+
86
+ async function handlePtyRead(args: z.infer<typeof ptyReadSchema>) {
87
+ try {
88
+ const readResult = sessionManager.ptyRead(
89
+ args.ptyId,
90
+ {
91
+ mode: args.mode || 'screen',
92
+ clear: args.clear !== false,
93
+ },
94
+ )
95
+ return formatResult({
96
+ success: true,
97
+ ptyId: args.ptyId,
98
+ mode: args.mode || 'screen',
99
+ ...readResult,
100
+ })
101
+ } catch (error) {
102
+ return formatError(error)
103
+ }
104
+ }
105
+
106
+ async function handlePtyResize(args: z.infer<typeof ptyResizeSchema>) {
107
+ try {
108
+ const success = sessionManager.ptyResize(args.ptyId, args.rows, args.cols)
109
+ return formatResult({success, ptyId: args.ptyId})
110
+ } catch (error) {
111
+ return formatError(error)
112
+ }
113
+ }
114
+
115
+ async function handlePtyClose(args: z.infer<typeof ptyCloseSchema>) {
116
+ try {
117
+ const success = sessionManager.ptyClose(args.ptyId)
118
+ return formatResult({
119
+ success,
120
+ message: success
121
+ ? `PTY session closed: ${args.ptyId}`
122
+ : `PTY session not found: ${args.ptyId}`,
123
+ })
124
+ } catch (error) {
125
+ return formatError(error)
126
+ }
127
+ }
128
+
129
+ async function handlePtyList() {
130
+ try {
131
+ const ptySessions = sessionManager.ptyList()
132
+ return formatResult({
133
+ success: true,
134
+ count: ptySessions.length,
135
+ sessions: ptySessions,
136
+ })
137
+ } catch (error) {
138
+ return formatError(error)
139
+ }
140
+ }
141
+
142
+ // ========== Register ==========
143
+
144
+ export function registerPtyTools(server: McpServer): void {
145
+ server.registerTool('ssh_pty_start', {
146
+ description: `启动持久化 PTY 会话,支持 top、htop、tmux 等交互式命令。
147
+
148
+ 特点:
149
+ - 输出缓冲区持续收集数据
150
+ - 可通过 ssh_pty_read 轮询读取最新输出
151
+ - 可通过 ssh_pty_write 发送按键/命令
152
+
153
+ 示例:
154
+ - 启动 top: ssh_pty_start(alias="server", command="top")
155
+ - 启动 tmux: ssh_pty_start(alias="server", command="tmux new -s work")`,
156
+ inputSchema: ptyStartSchema,
157
+ }, (args) => handlePtyStart(args))
158
+
159
+ server.registerTool('ssh_pty_write', {
160
+ description: `向 PTY 写入数据(按键、命令)。
161
+
162
+ 常用控制序列:
163
+ - 回车: "\\r" 或 "\\n"
164
+ - Ctrl+C: "\\x03"
165
+ - Ctrl+D: "\\x04"
166
+ - Ctrl+Z: "\\x1a"
167
+ - 上箭头: "\\x1b[A"
168
+ - 下箭头: "\\x1b[B"
169
+
170
+ 示例:
171
+ - 发送命令: ssh_pty_write(ptyId="xxx", data="ls -la\\r")
172
+ - 退出 top: ssh_pty_write(ptyId="xxx", data="q")`,
173
+ inputSchema: ptyWriteSchema,
174
+ }, (args) => handlePtyWrite(args))
175
+
176
+ server.registerTool('ssh_pty_read', {
177
+ description: `读取 PTY 输出。
178
+
179
+ 两种模式:
180
+ - screen(默认):返回当前屏幕内容(解析后的纯文本,适合 top/btop/htop 等全屏刷新工具)
181
+ - raw:返回原始 ANSI 流(包含转义序列,适合需要完整终端数据的场景)
182
+
183
+ 示例:
184
+ - 获取 top 当前画面: ssh_pty_read(ptyId="xxx")
185
+ - 获取原始输出: ssh_pty_read(ptyId="xxx", mode="raw")`,
186
+ inputSchema: ptyReadSchema,
187
+ }, (args) => handlePtyRead(args))
188
+
189
+ server.registerTool('ssh_pty_resize', {
190
+ description: '调整 PTY 窗口大小',
191
+ inputSchema: ptyResizeSchema,
192
+ }, (args) => handlePtyResize(args))
193
+
194
+ server.registerTool('ssh_pty_close', {
195
+ description: '关闭 PTY 会话',
196
+ inputSchema: ptyCloseSchema,
197
+ }, (args) => handlePtyClose(args))
198
+
199
+ server.registerTool('ssh_pty_list', {
200
+ description: '列出所有 PTY 会话',
201
+ inputSchema: ptyListSchema,
202
+ }, () => handlePtyList())
203
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 工具共享响应格式化
3
+ */
4
+
5
+ /**
6
+ * 格式化成功响应
7
+ */
8
+ export function formatResult(data: unknown) {
9
+ return {
10
+ content: [{type: 'text' as const, text: JSON.stringify(data, null, 2)}],
11
+ }
12
+ }
13
+
14
+ /**
15
+ * 格式化错误响应
16
+ */
17
+ export function formatError(error: unknown) {
18
+ const message = error instanceof Error ? error.message : String(error)
19
+ return {
20
+ content: [{type: 'text' as const, text: JSON.stringify({success: false, error: message}, null, 2)}],
21
+ isError: true,
22
+ }
23
+ }
package/src/types.ts CHANGED
@@ -110,15 +110,6 @@ export interface PtySessionInfo {
110
110
  // 端口转发类型
111
111
  export type ForwardType = 'local' | 'remote';
112
112
 
113
- // 端口转发配置
114
- export interface PortForwardConfig {
115
- type: ForwardType;
116
- localHost: string;
117
- localPort: number;
118
- remoteHost: string;
119
- remotePort: number;
120
- }
121
-
122
113
  // 端口转发信息
123
114
  export interface PortForwardInfo {
124
115
  id: string;