@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,220 @@
1
+ /**
2
+ * PTY 会话管理器
3
+ *
4
+ * 负责 PTY 会话的完整生命周期:创建、读写、调整大小、关闭
5
+ */
6
+
7
+ import xterm from '@xterm/headless'
8
+ import type {ClientChannel} from 'ssh2'
9
+ import type {PtyOptions, PtySessionInfo} from './types.js'
10
+
11
+ const Terminal = xterm.Terminal as typeof import('@xterm/headless').Terminal
12
+ type TerminalType = import('@xterm/headless').Terminal;
13
+
14
+ /** PTY 创建所需的外部依赖 */
15
+ export interface PtyDependencies {
16
+ /** 通过 alias 获取 SSH client 并执行命令 */
17
+ execPty(alias: string, command: string, options: PtyOptions): Promise<ClientChannel>;
18
+ }
19
+
20
+ interface PtySession {
21
+ id: string;
22
+ alias: string;
23
+ command: string;
24
+ stream: ClientChannel;
25
+ terminal: TerminalType;
26
+ rows: number;
27
+ cols: number;
28
+ createdAt: number;
29
+ lastReadAt: number;
30
+ rawBuffer: string;
31
+ maxBufferSize: number;
32
+ active: boolean;
33
+ }
34
+
35
+ export class PtyManager {
36
+ private sessions: Map<string, PtySession> = new Map()
37
+ private idCounter = 0
38
+ private readonly defaultBufferSize = 1024 * 1024 // 1MB
39
+
40
+ async start(
41
+ deps: PtyDependencies,
42
+ alias: string,
43
+ command: string,
44
+ options: PtyOptions = {},
45
+ ): Promise<string> {
46
+ const ptyId = this.generateId()
47
+ const rows = options.rows || 24
48
+ const cols = options.cols || 80
49
+ const maxBufferSize = options.bufferSize || this.defaultBufferSize
50
+
51
+ const stream = await deps.execPty(alias, command, options)
52
+
53
+ // 创建 xterm headless 终端仿真器
54
+ const terminal = new Terminal({rows, cols, allowProposedApi: true})
55
+
56
+ const ptySession: PtySession = {
57
+ id: ptyId,
58
+ alias,
59
+ command,
60
+ stream,
61
+ terminal,
62
+ rows,
63
+ cols,
64
+ createdAt: Date.now(),
65
+ lastReadAt: Date.now(),
66
+ rawBuffer: '',
67
+ maxBufferSize,
68
+ active: true,
69
+ }
70
+
71
+ // 监听输出数据
72
+ stream.on('data', (data: Buffer) => {
73
+ if (!ptySession.active) {
74
+ return
75
+ }
76
+ const chunk = data.toString('utf-8')
77
+ // 写入终端仿真器(解析 ANSI 序列)
78
+ terminal.write(chunk)
79
+ // 同时保留原始流(用于 raw 模式)
80
+ ptySession.rawBuffer += chunk
81
+ if (ptySession.rawBuffer.length > maxBufferSize) {
82
+ ptySession.rawBuffer = ptySession.rawBuffer.slice(-maxBufferSize)
83
+ }
84
+ })
85
+
86
+ stream.on('close', () => {
87
+ ptySession.active = false
88
+ })
89
+
90
+ stream.on('error', (err: Error) => {
91
+ ptySession.active = false
92
+ terminal.write(`\n[PTY Error: ${err.message}]`)
93
+ })
94
+
95
+ this.sessions.set(ptyId, ptySession)
96
+ return ptyId
97
+ }
98
+
99
+ write(ptyId: string, data: string): boolean {
100
+ const session = this.getSession(ptyId)
101
+ if (!session.active) {
102
+ throw new Error(`PTY session '${ptyId}' is closed`)
103
+ }
104
+ return session.stream.write(data)
105
+ }
106
+
107
+ read(
108
+ ptyId: string,
109
+ options: { mode?: 'screen' | 'raw'; clear?: boolean } = {},
110
+ ): { data: string; active: boolean; rows: number; cols: number } {
111
+ const session = this.getSession(ptyId)
112
+ const mode = options.mode || 'screen'
113
+ const clear = options.clear !== false
114
+
115
+ let data: string
116
+ if (mode === 'screen') {
117
+ data = this.getScreenContent(session.terminal)
118
+ } else {
119
+ data = session.rawBuffer
120
+ if (clear) {
121
+ session.rawBuffer = ''
122
+ }
123
+ }
124
+
125
+ session.lastReadAt = Date.now()
126
+ return {
127
+ data,
128
+ active: session.active,
129
+ rows: session.rows,
130
+ cols: session.cols,
131
+ }
132
+ }
133
+
134
+ resize(ptyId: string, rows: number, cols: number): boolean {
135
+ const session = this.getSession(ptyId)
136
+ if (!session.active) {
137
+ throw new Error(`PTY session '${ptyId}' is closed`)
138
+ }
139
+ session.stream.setWindow(rows, cols, 0, 0)
140
+ session.terminal.resize(cols, rows)
141
+ session.rows = rows
142
+ session.cols = cols
143
+ return true
144
+ }
145
+
146
+ close(ptyId: string): boolean {
147
+ const session = this.sessions.get(ptyId)
148
+ if (!session) {
149
+ return false
150
+ }
151
+ try {
152
+ session.stream.close()
153
+ } catch (e) {
154
+ console.warn(`PTY ${ptyId} stream close failed:`, (e as Error).message)
155
+ }
156
+ try {
157
+ session.terminal.dispose()
158
+ } catch (e) {
159
+ console.warn(`PTY ${ptyId} terminal dispose failed:`, (e as Error).message)
160
+ }
161
+ session.active = false
162
+ this.sessions.delete(ptyId)
163
+ return true
164
+ }
165
+
166
+ /** 关闭指定 alias 的所有 PTY 会话 */
167
+ closeByAlias(alias: string): void {
168
+ for (const [id, session] of this.sessions) {
169
+ if (session.alias === alias) {
170
+ this.close(id)
171
+ }
172
+ }
173
+ }
174
+
175
+ list(): PtySessionInfo[] {
176
+ const result: PtySessionInfo[] = []
177
+ for (const [id, pty] of this.sessions) {
178
+ result.push({
179
+ id,
180
+ alias: pty.alias,
181
+ command: pty.command,
182
+ rows: pty.rows,
183
+ cols: pty.cols,
184
+ createdAt: pty.createdAt,
185
+ lastReadAt: pty.lastReadAt,
186
+ bufferSize: pty.rawBuffer.length,
187
+ active: pty.active,
188
+ })
189
+ }
190
+ return result
191
+ }
192
+
193
+ private getSession(ptyId: string): PtySession {
194
+ const session = this.sessions.get(ptyId)
195
+ if (!session) {
196
+ throw new Error(`PTY session '${ptyId}' not found`)
197
+ }
198
+ return session
199
+ }
200
+
201
+ private generateId(): string {
202
+ return `pty_${++this.idCounter}_${Date.now()}`
203
+ }
204
+
205
+ /** 从终端仿真器获取当前屏幕内容 */
206
+ private getScreenContent(terminal: TerminalType): string {
207
+ const buffer = terminal.buffer.active
208
+ const lines: string[] = []
209
+ for (let i = 0; i < terminal.rows; i++) {
210
+ const line = buffer.getLine(i)
211
+ if (line) {
212
+ lines.push(line.translateToString(true))
213
+ }
214
+ }
215
+ while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
216
+ lines.pop()
217
+ }
218
+ return lines.join('\n')
219
+ }
220
+ }