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