ssh-licco 0.2.1__tar.gz

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MCP Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,512 @@
1
+ Metadata-Version: 2.4
2
+ Name: ssh-licco
3
+ Version: 0.2.1
4
+ Summary: SSH Model Context Protocol Server - Enable SSH functionality for AI models
5
+ Author-email: SSH LICCO Team <contact@ssh-licco.dev>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Echoqili/ssh-licco
8
+ Project-URL: Repository, https://github.com/Echoqili/ssh-licco
9
+ Keywords: mcp,ssh,model-context-protocol,ai,security,devops
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Requires-Python: <3.14,>=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: mcp>=1.0.0
20
+ Requires-Dist: asyncssh>=2.17.0
21
+ Requires-Dist: pydantic>=2.0.0
22
+ Requires-Dist: pydantic-settings>=2.0.0
23
+ Provides-Extra: dev
24
+ Requires-Dist: pytest>=7.4.0; extra == "dev"
25
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
26
+ Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
27
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
28
+ Requires-Dist: mypy>=1.7.0; extra == "dev"
29
+ Dynamic: license-file
30
+
31
+ # 🚀 SSH LICCO
32
+
33
+ [![PyPI version](https://badge.fury.io/py/ssh-licco.svg)](https://badge.fury.io/py/ssh-licco)
34
+ [![Python 3.10-3.13](https://img.shields.io/badge/python-3.10--3.13-blue.svg)](https://www.python.org/downloads/)
35
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
36
+
37
+ **让 AI 帮你操作服务器!**
38
+
39
+ SSH LICCO 是一个基于 Model Context Protocol (MCP) 的 SSH 服务器,让 AI 助手(Trae、Cursor、Claude Desktop 等)能够直接连接和管理你的 SSH 服务器。通过自然语言对话,AI 可以帮你执行命令、管理文件、查看日志、部署应用等。
40
+
41
+ ## ✨ 核心特性
42
+
43
+ - 🎯 **自然语言控制** - 直接用对话方式操作服务器
44
+ - 🔐 **多种认证方式** - 支持密码、密钥、Agent 转发
45
+ - 🔗 **长连接支持** - 自动保活,避免频繁连接导致账户锁定
46
+ - 📦 **多客户端支持** - Paramiko、Fabric、AsyncSSH 可插拔
47
+ - 🛡️ **完善的异常处理** - 统一的异常体系和错误处理
48
+ - 📊 **会话管理** - 支持多个并发 SSH 会话
49
+ - 📁 **SFTP 文件传输** - 上传、下载、列出目录
50
+ - 🔑 **密钥管理** - 生成、保存和管理 SSH 密钥对
51
+ - 📝 **详细日志记录** - 支持多级日志和文件输出
52
+ - 🚀 **高性能异步** - 基于 asyncio 的异步架构
53
+ - 🔌 **连接池** - 高性能连接复用
54
+ - 📊 **批量执行** - 多主机并行命令执行
55
+ - 📝 **审计日志** - 完整操作审计记录
56
+
57
+ ### 技术栈
58
+ - Python 3.10 - 3.13
59
+ - MCP SDK 1.0+
60
+ - AsyncSSH 2.17.0+
61
+ - Pydantic 2.0+
62
+ - asyncio 异步架构
63
+
64
+ ## 📦 安装
65
+
66
+ ### 方式一:pip 安装(推荐)
67
+
68
+ ```bash
69
+ pip install ssh-licco
70
+ ```
71
+
72
+ ### 方式二:从源码安装
73
+
74
+ ```bash
75
+ git clone https://github.com/Echoqili/ssh-licco.git
76
+ cd ssh-licco
77
+ pip install -e .
78
+ ```
79
+
80
+ ### 开发环境安装
81
+
82
+ ```bash
83
+ git clone https://github.com/Echoqili/ssh-licco.git
84
+ cd ssh-licco
85
+ pip install -e ".[dev]"
86
+ ```
87
+
88
+ **Python 版本要求:**
89
+ - ✅ Python 3.10, 3.11, 3.12, 3.13
90
+ - ❌ Python 3.9 及以下版本不支持
91
+ - ❌ Python 3.14 及以上版本未测试
92
+
93
+ ## 🚀 快速开始
94
+
95
+ ### 1. 配置 MCP 服务器
96
+
97
+ #### 在 Trae / Cursor 中使用
98
+
99
+ 打开设置 → MCP → 添加新服务器:
100
+
101
+ ```json
102
+ {
103
+ "mcpServers": {
104
+ "ssh": {
105
+ "command": "ssh-licco"
106
+ }
107
+ }
108
+ }
109
+ ```
110
+
111
+ #### 在 Claude Desktop 中使用
112
+
113
+ 配置文件位置:
114
+ - **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
115
+ - **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
116
+ - **Linux**: `~/.config/Claude/claude_desktop_config.json`
117
+
118
+ ```json
119
+ {
120
+ "mcpServers": {
121
+ "ssh": {
122
+ "command": "ssh-licco"
123
+ }
124
+ }
125
+ }
126
+ ```
127
+
128
+ ### 2. 配置 SSH 主机(可选但推荐)
129
+
130
+ #### 方式 1:使用配置文件
131
+
132
+ ```bash
133
+ cp config/hosts.json.example config/hosts.json
134
+ ```
135
+
136
+ 编辑 `config/hosts.json`:
137
+
138
+ ```json
139
+ {
140
+ "ssh_hosts": [
141
+ {
142
+ "name": "生产服务器",
143
+ "host": "192.168.1.100",
144
+ "port": 22,
145
+ "username": "root",
146
+ "password": "your_password",
147
+ "timeout": 30
148
+ },
149
+ {
150
+ "name": "测试服务器",
151
+ "host": "192.168.1.101",
152
+ "port": 2222,
153
+ "username": "admin",
154
+ "password": "test123"
155
+ }
156
+ ]
157
+ }
158
+ ```
159
+
160
+ #### 方式 2:直接在 MCP 配置中指定
161
+
162
+ ```json
163
+ {
164
+ "mcpServers": {
165
+ "ssh": {
166
+ "command": "ssh-licco",
167
+ "env": {
168
+ "SSH_HOST": "192.168.1.100",
169
+ "SSH_USER": "root",
170
+ "SSH_PASSWORD": "your_password",
171
+ "SSH_KEEPALIVE_INTERVAL": "30",
172
+ "SSH_SESSION_TIMEOUT": "7200",
173
+ "SSH_CLIENT_TYPE": "asyncssh"
174
+ }
175
+ }
176
+ }
177
+ }
178
+ ```
179
+
180
+ ### 3. 开始使用
181
+
182
+ 重启 AI 应用后,直接用自然语言对话:
183
+
184
+ **示例 1:查看服务器状态**
185
+ ```
186
+ 帮我看看服务器的负载情况
187
+ ```
188
+
189
+ **示例 2:执行命令**
190
+ ```
191
+ 在服务器上执行 docker ps 查看运行中的容器
192
+ ```
193
+
194
+ **示例 3:管理文件**
195
+ ```
196
+ 列出 /var/log 目录下的所有文件
197
+ ```
198
+
199
+ **示例 4:安装软件**
200
+ ```
201
+ 在服务器上安装 nginx
202
+ ```
203
+
204
+ **示例 5:上传文件**
205
+ ```
206
+ 把本地的 config.yaml 上传到服务器的 /etc 目录
207
+ ```
208
+
209
+ ## 🔥 长连接功能(避免账户锁定)
210
+
211
+ 频繁连接 SSH 服务器可能导致账户被锁定。SSH LICCO 默认启用长连接和自动保活:
212
+
213
+ - **自动保活**:每 30 秒发送心跳包,保持连接活跃
214
+ - **持久会话**:默认保持 2 小时,避免频繁重连
215
+ - **可配置**:根据需求调整保活间隔和会话时长
216
+
217
+ ### 配置参数
218
+
219
+ ```json
220
+ {
221
+ "mcpServers": {
222
+ "ssh": {
223
+ "command": "ssh-licco",
224
+ "env": {
225
+ "SSH_HOST": "43.143.207.242",
226
+ "SSH_USER": "root",
227
+ "SSH_PASSWORD": "your-password",
228
+ "SSH_KEEPALIVE_INTERVAL": "30",
229
+ "SSH_SESSION_TIMEOUT": "7200"
230
+ }
231
+ }
232
+ }
233
+ }
234
+ ```
235
+
236
+ ## 🔌 连接池(高性能)
237
+
238
+ SSH LICCO 支持连接池功能,提供高效的连接复用:
239
+
240
+ - **连接复用**:避免频繁建立连接的开销
241
+ - **健康检查**:自动检测并回收无效连接
242
+ - **线程安全**:支持并发访问
243
+ - **配置灵活**:可调整最大连接数、空闲时间等
244
+
245
+ ### 连接池配置
246
+
247
+ ```python
248
+ from ssh_mcp import ConnectionConfig, PoolConfig, ConnectionPool
249
+
250
+ # 配置连接池
251
+ pool_config = PoolConfig(
252
+ min_size=1, # 最小连接数
253
+ max_size=10, # 最大连接数
254
+ max_idle_time=300, # 最大空闲时间(秒)
255
+ max_use_count=100, # 单连接最大使用次数
256
+ acquire_timeout=30 # 获取连接超时(秒)
257
+ )
258
+
259
+ # 创建连接池
260
+ config = ConnectionConfig(
261
+ host="192.168.1.100",
262
+ username="admin",
263
+ private_key_path="~/.ssh/id_rsa"
264
+ )
265
+ pool = ConnectionPool(config, pool_config)
266
+ pool.initialize()
267
+
268
+ # 使用连接池
269
+ with pool.acquire() as client:
270
+ result = client.execute_command("ls -la")
271
+ ```
272
+
273
+ ## 📊 批量执行(多主机管理)
274
+
275
+ 支持批量并行执行命令,管理多台服务器:
276
+
277
+ - **并行执行**:多主机同时执行命令
278
+ - **失败隔离**:单主机异常不影响其他主机
279
+ - **进度回调**:实时反馈执行进度
280
+ - **异步支持**:AsyncBatchExecutor 实现高并发
281
+
282
+ ### 批量执行示例
283
+
284
+ ```python
285
+ from ssh_mcp import (
286
+ ConnectionConfig,
287
+ BatchExecutor,
288
+ AsyncBatchExecutor
289
+ )
290
+
291
+ # 配置多台主机
292
+ hosts = [
293
+ ConnectionConfig(host="192.168.1.100", username="admin", private_key_path="~/.ssh/id_rsa"),
294
+ ConnectionConfig(host="192.168.1.101", username="admin", private_key_path="~/.ssh/id_rsa"),
295
+ ConnectionConfig(host="192.168.1.102", username="admin", private_key_path="~/.ssh/id_rsa"),
296
+ ]
297
+
298
+ # 同步批量执行
299
+ executor = BatchExecutor(hosts, max_workers=10)
300
+ result = executor.execute("uptime")
301
+
302
+ print(f"成功: {result.success_count}, 失败: {result.failed_count}")
303
+ for host_result in result.results:
304
+ print(f"{host_result.host}: {host_result.stdout}")
305
+
306
+ # 异步批量执行
307
+ async_executor = AsyncBatchExecutor(hosts, max_concurrent=50)
308
+ result = await async_executor.execute("df -h")
309
+ ```
310
+
311
+ ## 📝 审计日志
312
+
313
+ 完整的操作审计日志,满足合规要求:
314
+
315
+ - **结构化日志**:JSON 格式便于分析
316
+ - **操作记录**:连接、执行命令、文件传输
317
+ - **认证审计**:成功/失败认证记录
318
+
319
+ ### 使用审计日志
320
+
321
+ ```python
322
+ from ssh_mcp import get_audit_logger, AuditEventType
323
+
324
+ # 初始化审计日志
325
+ audit = get_audit_logger("logs/audit.log")
326
+
327
+ # 记录连接事件
328
+ audit.log_connect(
329
+ username="admin",
330
+ host="192.168.1.100",
331
+ port=22,
332
+ client_type="asyncssh",
333
+ session_id="session-123"
334
+ )
335
+
336
+ # 记录命令执行
337
+ audit.log_command(
338
+ username="admin",
339
+ host="192.168.1.100",
340
+ command="ls -la",
341
+ return_code=0,
342
+ stdout_length=1024,
343
+ stderr_length=0,
344
+ session_id="session-123"
345
+ )
346
+ ```
347
+
348
+ 详细配置说明见 [CONFIG_GUIDE.md](CONFIG_GUIDE.md)
349
+
350
+ ## 🛠️ 可用工具
351
+
352
+ | 工具 | 说明 | 示例 |
353
+ |------|------|------|
354
+ | `ssh_config` | 配置 SSH 连接信息 | 配置服务器地址、用户名、密码 |
355
+ | `ssh_login` | 使用保存的配置登录 | 登录后执行命令 |
356
+ | `ssh_connect` | 直接连接 SSH | 支持预配置主机或即时连接 |
357
+ | `ssh_execute` | 执行命令 | 在会话中执行任意命令 |
358
+ | `ssh_disconnect` | 断开连接 | 关闭指定会话 |
359
+ | `ssh_list_sessions` | 列出活跃会话 | 查看所有 SSH 会话 |
360
+ | `ssh_generate_key` | 生成 SSH 密钥 | 创建 RSA 或 ED25519 密钥对 |
361
+ | `ssh_file_transfer` | SFTP 文件传输 | 上传、下载、列出目录 |
362
+
363
+ 详细使用说明见 [USAGE.md](USAGE.md)
364
+
365
+ ## 🏗️ 架构设计
366
+
367
+ SSH LICCO 采用分层架构设计,遵循最佳实践:
368
+
369
+ ### 核心模块
370
+
371
+ ```
372
+ ssh_mcp/
373
+ ├── clients/ # SSH 客户端层(可插拔)
374
+ │ ├── interface.py # 抽象接口定义
375
+ │ ├── paramiko_client.py # Paramiko 实现
376
+ │ ├── additional_clients.py # 其他客户端实现
377
+ │ └── factory.py # 客户端工厂
378
+ ├── service.py # 业务服务层
379
+ ├── session_manager.py # 会话管理
380
+ ├── connection_config.py # 配置模型
381
+ ├── connection_pool.py # 连接池管理
382
+ ├── batch_executor.py # 批量执行器
383
+ ├── audit_logger.py # 审计日志
384
+ ├── exceptions.py # 统一异常体系
385
+ ├── logging_config.py # 日志管理
386
+ └── server.py # MCP 服务端
387
+ ```
388
+
389
+ ### 设计模式
390
+
391
+ - **工厂模式**:SSHClientFactory 动态创建客户端
392
+ - **策略模式**:支持多种 SSH 客户端实现
393
+ - **单例模式**:全局 SSHService 实例
394
+ - **上下文管理器**:自动连接/断开管理
395
+
396
+ ### 客户端支持
397
+
398
+ 当前仅支持 **AsyncSSH** 客户端:
399
+
400
+ | 客户端 | 类型 | 特点 | 安装 |
401
+ |--------|------|------|------|
402
+ | **AsyncSSH** | 异步 | 高并发性能,异步架构,默认且唯一 | `pip install asyncssh` |
403
+
404
+ **说明:** 为了简化维护和优化性能,本项目目前仅支持 AsyncSSH 客户端。AsyncSSH 提供:
405
+ - ✅ 异步架构,高并发性能
406
+ - ✅ 低资源占用
407
+ - ✅ 现代化的 API 设计
408
+ - ✅ 活跃的社区支持
409
+
410
+ ### 配置客户端类型
411
+
412
+ 当前默认且仅支持 **AsyncSSH** 客户端,无需额外配置。
413
+
414
+ 如需指定(兼容性):
415
+
416
+ ```json
417
+ {
418
+ "mcpServers": {
419
+ "ssh": {
420
+ "command": "ssh-licco",
421
+ "env": {
422
+ "SSH_CLIENT_TYPE": "asyncssh"
423
+ }
424
+ }
425
+ }
426
+ }
427
+ ```
428
+
429
+ 详见 [🔌 客户端类型配置指南](docs/CLIENT_TYPES.md)
430
+
431
+ ## 🔒 安全注意事项
432
+
433
+ ⚠️ **重要提示:**
434
+
435
+ 1. **密码安全** - 密码仅本地存储,建议使用后清除配置文件
436
+ 2. **不要分享** - 不要在公开场合分享服务器密码
437
+ 3. **密钥认证** - 优先使用 SSH 密钥认证而非密码
438
+ 4. **普通用户** - 尽量使用普通用户而非 root 用户
439
+ 5. **文件权限** - 确保 `config/hosts.json` 文件权限设置为 600
440
+ 6. **环境变量** - 使用环境变量存储敏感信息更安全
441
+
442
+ ## 📚 文档
443
+
444
+ - [📖 使用指南](USAGE.md) - 详细的工具使用说明
445
+ - [⚙️ 配置指南](CONFIG_GUIDE.md) - 完整的配置选项说明
446
+ - [🐳 Docker 超时问题](DOCKER_MCP_TIMEOUT.md) - Docker 构建相关问题
447
+ - [📝 API 参考](docs/API_REFERENCE.md) - API 接口文档
448
+ - [❓ 故障排除](docs/TROUBLESHOOTING.md) - 常见问题和解决方案
449
+
450
+ ## 💻 开发
451
+
452
+ ### 运行测试
453
+
454
+ ```bash
455
+ pytest
456
+ ```
457
+
458
+ ### 代码检查
459
+
460
+ ```bash
461
+ # 代码格式化
462
+ ruff check ssh_mcp
463
+
464
+ # 类型检查
465
+ mypy ssh_mcp
466
+ ```
467
+
468
+ ### 构建文档
469
+
470
+ ```bash
471
+ cd docs
472
+ mkdocs build
473
+ ```
474
+
475
+ ## 🤝 贡献
476
+
477
+ 欢迎贡献代码、报告问题或提出建议!
478
+
479
+ 1. Fork 本仓库
480
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
481
+ 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
482
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
483
+ 5. 开启 Pull Request
484
+
485
+ 详见 [CONTRIBUTING.md](docs/CONTRIBUTING.md)
486
+
487
+ ## 📄 许可证
488
+
489
+ 本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
490
+
491
+ ## 🙏 致谢
492
+
493
+ - [MCP SDK](https://github.com/modelcontextprotocol/python-sdk) - Model Context Protocol
494
+ - [Paramiko](https://github.com/paramiko/paramiko) - SSH 库
495
+ - [Fabric](https://github.com/fabric/fabric) - 高级 SSH 库
496
+ - [AsyncSSH](https://github.com/ronf/asyncssh) - 异步 SSH 库
497
+
498
+ ## 📬 联系方式
499
+
500
+ - **项目地址**: https://github.com/Echoqili/ssh-licco
501
+ - **问题反馈**: https://github.com/Echoqili/ssh-licco/issues
502
+ - **PyPI**: https://pypi.org/project/ssh-licco/
503
+
504
+ ---
505
+
506
+ <div align="center">
507
+
508
+ **Made with ❤️ by SSH LICCO Team**
509
+
510
+ [![Star History Chart](https://api.star-history.com/svg?repos=Echoqili/ssh-licco&type=Date)](https://star-history.com/#Echoqili/ssh-licco&Date)
511
+
512
+ </div>