@pyrokine/mcp-ssh 1.3.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tools/pty.ts DELETED
@@ -1,203 +0,0 @@
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
- }
@@ -1,23 +0,0 @@
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 DELETED
@@ -1,124 +0,0 @@
1
- /**
2
- * SSH MCP Pro - 类型定义
3
- */
4
-
5
- export interface SSHConnectionConfig {
6
- host: string;
7
- port: number;
8
- username: string;
9
- password?: string;
10
- privateKeyPath?: string;
11
- privateKey?: string;
12
- passphrase?: string;
13
- alias?: string;
14
- // 高级配置
15
- keepaliveInterval?: number; // 心跳间隔(毫秒)
16
- keepaliveCountMax?: number; // 最大心跳失败次数
17
- readyTimeout?: number; // 连接超时(毫秒)
18
- // 环境配置
19
- env?: Record<string, string>; // 环境变量
20
- lang?: string; // LANG 设置
21
- shell?: string; // Shell 类型
22
- // 跳板机
23
- jumpHost?: SSHConnectionConfig;
24
- }
25
-
26
- export interface SSHSessionInfo {
27
- alias: string;
28
- host: string;
29
- port: number;
30
- username: string;
31
- connected: boolean;
32
- connectedAt: number;
33
- lastUsedAt: number;
34
- env?: Record<string, string>;
35
- }
36
-
37
- export interface ExecOptions {
38
- timeout?: number; // 命令超时(毫秒)
39
- env?: Record<string, string>; // 额外环境变量
40
- cwd?: string; // 工作目录
41
- pty?: boolean; // 是否使用 PTY
42
- maxOutputSize?: number; // 最大输出大小(字节),默认 10MB
43
- // PTY 配置
44
- rows?: number;
45
- cols?: number;
46
- term?: string;
47
- }
48
-
49
- export interface ExecResult {
50
- success: boolean;
51
- stdout: string;
52
- stderr: string;
53
- exitCode: number;
54
- duration: number; // 执行时间(毫秒)
55
- }
56
-
57
- export interface FileInfo {
58
- name: string;
59
- path: string;
60
- size: number;
61
- isDirectory: boolean;
62
- isFile: boolean;
63
- isSymlink: boolean;
64
- permissions: string;
65
- owner: number;
66
- group: number;
67
- mtime: Date;
68
- atime: Date;
69
- }
70
-
71
- export interface TransferProgress {
72
- transferred: number;
73
- total: number;
74
- percent: number;
75
- }
76
-
77
- // 持久化存储的会话配置(不含敏感信息)
78
- export interface PersistedSession {
79
- alias: string;
80
- host: string;
81
- port: number;
82
- username: string;
83
- connectedAt: number;
84
- env?: Record<string, string>;
85
- }
86
-
87
- // PTY 会话配置
88
- export interface PtyOptions {
89
- rows?: number;
90
- cols?: number;
91
- term?: string;
92
- env?: Record<string, string>;
93
- cwd?: string;
94
- bufferSize?: number; // 输出缓冲区大小,默认 1MB
95
- }
96
-
97
- // PTY 会话信息
98
- export interface PtySessionInfo {
99
- id: string;
100
- alias: string; // SSH 连接别名
101
- command: string; // 启动命令
102
- rows: number;
103
- cols: number;
104
- createdAt: number;
105
- lastReadAt: number;
106
- bufferSize: number; // 当前缓冲区大小
107
- active: boolean;
108
- }
109
-
110
- // 端口转发类型
111
- export type ForwardType = 'local' | 'remote';
112
-
113
- // 端口转发信息
114
- export interface PortForwardInfo {
115
- id: string;
116
- alias: string; // SSH 连接别名
117
- type: ForwardType;
118
- localHost: string;
119
- localPort: number;
120
- remoteHost: string;
121
- remotePort: number;
122
- createdAt: number;
123
- active: boolean;
124
- }
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "outDir": "./dist",
7
- "rootDir": "./src",
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "skipLibCheck": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "declaration": true
13
- },
14
- "include": [
15
- "src/**/*"
16
- ],
17
- "exclude": [
18
- "node_modules",
19
- "dist"
20
- ]
21
- }