mcp-ssh-session 0.1.0__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,37 @@
1
+ name: Publish package to PyPI
2
+
3
+ on:
4
+ release:
5
+ types:
6
+ - published
7
+ workflow_dispatch:
8
+
9
+ jobs:
10
+ publish:
11
+ name: Build and publish
12
+ runs-on: ubuntu-latest
13
+ environment:
14
+ name: pypi
15
+ url: https://pypi.org/project/mcp-ssh-session/
16
+ permissions:
17
+ id-token: write
18
+ contents: read
19
+ steps:
20
+ - name: Check out repository
21
+ uses: actions/checkout@v4
22
+
23
+ - name: Set up Python
24
+ uses: actions/setup-python@v5
25
+ with:
26
+ python-version: "3.11"
27
+
28
+ - name: Install build tooling
29
+ run: python -m pip install --upgrade pip build
30
+
31
+ - name: Build distributions
32
+ run: python -m build
33
+
34
+ - name: Publish to PyPI
35
+ uses: pypa/gh-action-pypi-publish@release/v1
36
+ with:
37
+ packages-dir: dist
@@ -0,0 +1,55 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ .env
25
+ .venv
26
+ env/
27
+ venv/
28
+ ENV/
29
+ env.bak/
30
+ venv.bak/
31
+
32
+ # IDE
33
+ .vscode/
34
+ .idea/
35
+ *.swp
36
+ *.swo
37
+ *~
38
+
39
+ # OS
40
+ .DS_Store
41
+ .DS_Store?
42
+ ._*
43
+ .Spotlight-V100
44
+ .Trashes
45
+ ehthumbs.db
46
+ Thumbs.db
47
+
48
+ # Testing
49
+ .pytest_cache/
50
+ .coverage
51
+ htmlcov/
52
+ .tox/
53
+
54
+ # Local test files
55
+ test_local.py
@@ -0,0 +1,198 @@
1
+ # MCP SSH Session
2
+
3
+ An MCP (Model Context Protocol) server that enables AI agents to establish and manage persistent SSH sessions, allowing them to execute commands on remote hosts while maintaining connection state.
4
+
5
+ ## Overview
6
+
7
+ This MCP server provides tools for AI agents to:
8
+ - Establish persistent SSH connections to remote hosts
9
+ - Execute commands within existing sessions
10
+ - Manage multiple concurrent SSH sessions
11
+ - Track and close active connections
12
+
13
+ ## Features
14
+
15
+ - **Persistent Sessions**: SSH connections are reused across multiple command executions, reducing connection overhead
16
+ - **SSH Config Support**: Automatically reads and uses settings from `~/.ssh/config`, including aliases, hosts, ports, users, and identity files
17
+ - **Multi-host Support**: Manage connections to multiple hosts simultaneously
18
+ - **Automatic Reconnection**: Dead connections are detected and automatically re-established
19
+ - **Thread-safe**: Safe for concurrent operations
20
+ - **Flexible Authentication**: Supports both password and key-based authentication
21
+ - **Network Device Support**: Automatic enable mode handling for routers and switches (Cisco, Juniper, etc.)
22
+ - **Sudo Support**: Automatic password handling for sudo commands on Unix/Linux hosts
23
+ - **Interactive Shell Handling**: Detects and responds to password prompts automatically
24
+
25
+ ## Installation
26
+
27
+ Using `uvx`:
28
+
29
+ ```bash
30
+ uvx mcp-ssh-session
31
+ ```
32
+
33
+ Using `uv`:
34
+
35
+ ```bash
36
+ uv venv
37
+ source .venv/bin/activate # On Windows: .venv\Scripts\activate
38
+ uv pip install -e .
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ### Running the Server
44
+
45
+ ```bash
46
+ uvx mcp-ssh-session
47
+ ```
48
+
49
+ ### Available Tools
50
+
51
+ #### `execute_command`
52
+ Execute a command on an SSH host using a persistent session.
53
+
54
+ The `host` parameter can be either a hostname/IP or an SSH config alias. If an SSH config alias is provided, configuration will be read from `~/.ssh/config`.
55
+
56
+ **Parameters:**
57
+ - `host` (str, required): Hostname, IP address, or SSH config alias (e.g., "myserver")
58
+ - `command` (str, required): Command to execute
59
+ - `username` (str, optional): SSH username (will use SSH config or current user if not provided)
60
+ - `password` (str, optional): Password for authentication
61
+ - `key_filename` (str, optional): Path to SSH private key file (will use SSH config if not provided)
62
+ - `port` (int, optional): SSH port (will use SSH config or default 22 if not provided)
63
+ - `enable_password` (str, optional): Password for enable mode on network devices (routers/switches)
64
+ - `enable_command` (str, optional): Command to enter enable mode (default: "enable")
65
+ - `sudo_password` (str, optional): Password for sudo commands on Unix/Linux hosts
66
+ - `timeout` (int, optional): Timeout in seconds for command execution (default: 30)
67
+
68
+ **Returns:** Command output including exit status, stdout, and stderr
69
+
70
+ **Examples:**
71
+
72
+ Using SSH config alias:
73
+ ```python
74
+ # If ~/.ssh/config has an entry for "myserver"
75
+ execute_command(
76
+ host="myserver",
77
+ command="ls -la /var/log"
78
+ )
79
+ ```
80
+
81
+ Using explicit parameters:
82
+ ```python
83
+ execute_command(
84
+ host="example.com",
85
+ username="user",
86
+ command="ls -la /var/log",
87
+ key_filename="~/.ssh/id_rsa"
88
+ )
89
+ ```
90
+
91
+ Network device with enable mode (Cisco router/switch):
92
+ ```python
93
+ execute_command(
94
+ host="router.example.com",
95
+ username="admin",
96
+ password="ssh_password",
97
+ enable_password="enable_password",
98
+ command="show running-config"
99
+ )
100
+ ```
101
+
102
+ Unix/Linux host with sudo:
103
+ ```python
104
+ execute_command(
105
+ host="server.example.com",
106
+ username="user",
107
+ sudo_password="user_password",
108
+ command="systemctl restart nginx" # Auto-prefixed with 'sudo'
109
+ )
110
+ ```
111
+
112
+ Custom enable command (Juniper):
113
+ ```python
114
+ execute_command(
115
+ host="juniper-switch",
116
+ username="admin",
117
+ password="ssh_password",
118
+ enable_password="root_password",
119
+ enable_command="su -",
120
+ command="show configuration"
121
+ )
122
+ ```
123
+
124
+ #### `list_sessions`
125
+ List all active SSH sessions.
126
+
127
+ **Returns:** List of active sessions in format `username@host:port`
128
+
129
+ #### `close_session`
130
+ Close a specific SSH session.
131
+
132
+ **Parameters:**
133
+ - `host` (str): Hostname or IP address
134
+ - `username` (str): SSH username
135
+ - `port` (int, default=22): SSH port
136
+
137
+ #### `close_all_sessions`
138
+ Close all active SSH sessions.
139
+
140
+ ## Project Structure
141
+
142
+ ```
143
+ mcp-ssh-session/
144
+ ├── mcp_ssh_session/
145
+ │ ├── __init__.py
146
+ │ ├── __main__.py # Entry point
147
+ │ ├── server.py # MCP server and tool definitions
148
+ │ └── session_manager.py # SSH session management logic
149
+ ├── pyproject.toml
150
+ └── CLAUDE.md
151
+ ```
152
+
153
+ ## Architecture
154
+
155
+ ### SSHSessionManager
156
+ The core session management class that:
157
+ - Loads and parses `~/.ssh/config` on initialization
158
+ - Resolves SSH config aliases to actual connection parameters
159
+ - Maintains a dictionary of active SSH connections keyed by `username@host:port`
160
+ - Provides thread-safe access to sessions via a threading lock
161
+ - Automatically detects and removes dead connections
162
+ - Reuses existing connections when possible
163
+ - Falls back to sensible defaults when config is not available
164
+ - Handles interactive prompts for enable mode and sudo authentication
165
+ - Tracks enable mode state per session to avoid re-authentication
166
+ - Uses `exec_command()` for standard SSH hosts and `invoke_shell()` for interactive scenarios
167
+
168
+ ### MCP Server
169
+ Built with FastMCP, exposing SSH functionality as MCP tools that can be called by AI agents.
170
+
171
+ ## Security Considerations
172
+
173
+ - Uses Paramiko's `AutoAddPolicy` for host key verification (accepts new host keys automatically)
174
+ - Passwords and keys are handled in memory only
175
+ - Sessions are properly closed when no longer needed
176
+ - Thread-safe operations prevent race conditions
177
+
178
+ ## Dependencies
179
+
180
+ - `fastmcp`: MCP server framework
181
+ - `paramiko>=3.4.0`: SSH protocol implementation
182
+
183
+ ## Development
184
+
185
+ The project uses Python 3.10+ and is structured as a standard Python package.
186
+
187
+ ### Key Components
188
+
189
+ - **[server.py](mcp_ssh_session/server.py)**: MCP tool definitions and request handling
190
+ - **[session_manager.py](mcp_ssh_session/session_manager.py)**: Core SSH session lifecycle management
191
+
192
+ ## License
193
+
194
+ Distributed under the MIT License. See `LICENSE` for details.
195
+
196
+ ## Contributing
197
+
198
+ [Add contribution guidelines]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jon
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,230 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcp-ssh-session
3
+ Version: 0.1.0
4
+ Summary: MCP server for managing persistent SSH sessions
5
+ Project-URL: Homepage, https://github.com/devnullvoid/mcp-ssh-session
6
+ Project-URL: Repository, https://github.com/devnullvoid/mcp-ssh-session
7
+ Project-URL: Issues, https://github.com/devnullvoid/mcp-ssh-session/issues
8
+ Author: Jon
9
+ License: MIT License
10
+
11
+ Copyright (c) 2024 Jon
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+ License-File: LICENSE
31
+ Keywords: cli,fastmcp,mcp,ssh
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Environment :: Console
34
+ Classifier: Intended Audience :: Developers
35
+ Classifier: License :: OSI Approved :: MIT License
36
+ Classifier: Operating System :: OS Independent
37
+ Classifier: Programming Language :: Python
38
+ Classifier: Programming Language :: Python :: 3
39
+ Classifier: Programming Language :: Python :: 3.10
40
+ Classifier: Programming Language :: Python :: 3.11
41
+ Classifier: Programming Language :: Python :: 3.12
42
+ Classifier: Topic :: System :: Networking
43
+ Classifier: Topic :: System :: Systems Administration
44
+ Requires-Python: >=3.10
45
+ Requires-Dist: fastmcp
46
+ Requires-Dist: paramiko>=3.4.0
47
+ Description-Content-Type: text/markdown
48
+
49
+ # MCP SSH Session
50
+
51
+ An MCP (Model Context Protocol) server that enables AI agents to establish and manage persistent SSH sessions.
52
+
53
+ ## Features
54
+
55
+ - **Persistent Sessions**: SSH connections are reused across multiple command executions
56
+ - **SSH Config Support**: Automatically reads and uses settings from `~/.ssh/config`
57
+ - **Multi-host Support**: Manage connections to multiple hosts simultaneously
58
+ - **Automatic Reconnection**: Dead connections are detected and automatically re-established
59
+ - **Thread-safe**: Safe for concurrent operations
60
+ - **Network Device Support**: Automatic enable mode handling for routers and switches
61
+ - **Sudo Support**: Automatic password handling for sudo commands on Unix/Linux hosts
62
+ - **File Operations**: Safe helpers to read and write remote files over SFTP
63
+
64
+ ## Installation
65
+
66
+ ### Using `uvx`
67
+
68
+ ```bash
69
+ uvx mcp-ssh-session
70
+ ```
71
+
72
+ ### Using Claude Code
73
+
74
+ Add to your `~/.claude.json`:
75
+
76
+ ```json
77
+ {
78
+ "mcpServers": {
79
+ "ssh-session": {
80
+ "type": "stdio",
81
+ "command": "uvx",
82
+ "args": ["mcp-ssh-session"],
83
+ "env": {}
84
+ }
85
+ }
86
+ }
87
+ ```
88
+
89
+ ### Using MCP Inspector
90
+
91
+ ```bash
92
+ npx @modelcontextprotocol/inspector uvx mcp-ssh-session
93
+ ```
94
+
95
+ ### Development Installation
96
+
97
+ ```bash
98
+ uv venv
99
+ source .venv/bin/activate
100
+ uv pip install -e .
101
+ ```
102
+
103
+ ## Usage
104
+
105
+ ### Available Tools
106
+
107
+ #### `execute_command`
108
+ Execute a command on an SSH host using a persistent session.
109
+
110
+ **Using SSH config alias:**
111
+ ```json
112
+ {
113
+ "host": "myserver",
114
+ "command": "uptime"
115
+ }
116
+ ```
117
+
118
+ **Using explicit parameters:**
119
+ ```json
120
+ {
121
+ "host": "example.com",
122
+ "username": "user",
123
+ "command": "ls -la",
124
+ "key_filename": "~/.ssh/id_rsa",
125
+ "port": 22
126
+ }
127
+ ```
128
+
129
+ **Network device with enable mode:**
130
+ ```json
131
+ {
132
+ "host": "router.example.com",
133
+ "username": "admin",
134
+ "password": "ssh_password",
135
+ "enable_password": "enable_password",
136
+ "command": "show running-config"
137
+ }
138
+ ```
139
+
140
+ **Unix/Linux with sudo:**
141
+ ```json
142
+ {
143
+ "host": "server.example.com",
144
+ "username": "user",
145
+ "sudo_password": "user_password",
146
+ "command": "systemctl restart nginx"
147
+ }
148
+ ```
149
+
150
+ #### `list_sessions`
151
+ List all active SSH sessions.
152
+
153
+ #### `close_session`
154
+ Close a specific SSH session.
155
+
156
+ ```json
157
+ {
158
+ "host": "myserver"
159
+ }
160
+ ```
161
+
162
+ #### `close_all_sessions`
163
+ Close all active SSH sessions.
164
+
165
+ #### `read_file`
166
+ Read the contents of a remote file via SFTP.
167
+
168
+ ```json
169
+ {
170
+ "host": "myserver",
171
+ "remote_path": "/etc/nginx/nginx.conf",
172
+ "max_bytes": 131072
173
+ }
174
+ ```
175
+
176
+ - Uses persistent SSH sessions and opens short-lived SFTP channels
177
+ - Enforces a 2 MB maximum per request (configurable per call up to that limit)
178
+ - Returns truncated notice when the content size exceeds the requested limit
179
+
180
+ #### `write_file`
181
+ Write text content to a remote file via SFTP.
182
+
183
+ ```json
184
+ {
185
+ "host": "myserver",
186
+ "remote_path": "/tmp/app.env",
187
+ "content": "DEBUG=true\n",
188
+ "append": true,
189
+ "make_dirs": true
190
+ }
191
+ ```
192
+
193
+ - Content larger than 2 MB is rejected for safety
194
+ - Optional `append` mode to add to existing files
195
+ - Optional `make_dirs` flag will create missing parent directories
196
+ - Supports `permissions` to set octal file modes after write (e.g., `420` for `0644`)
197
+
198
+ ## SSH Config Support
199
+
200
+ The server automatically reads `~/.ssh/config` and supports:
201
+ - Host aliases
202
+ - Hostname mappings
203
+ - Port configurations
204
+ - User specifications
205
+ - IdentityFile settings
206
+
207
+ Example `~/.ssh/config`:
208
+ ```
209
+ Host myserver
210
+ HostName example.com
211
+ User myuser
212
+ Port 2222
213
+ IdentityFile ~/.ssh/id_rsa
214
+ ```
215
+
216
+ Then simply use:
217
+ ```json
218
+ {
219
+ "host": "myserver",
220
+ "command": "uptime"
221
+ }
222
+ ```
223
+
224
+ ## Documentation
225
+
226
+ See [CLAUDE.md](CLAUDE.md) for detailed documentation.
227
+
228
+ ## License
229
+
230
+ Distributed under the MIT License. See `LICENSE` for details.