- Reference Project:
mcp-ssh-session - Absolute Path:
/home/jon/Dev/ai/mcp/mcp-ssh-session - Status: Pivot complete. Logic ported and improved.
- Environment:
uv(Python 3.14+) - Command Runner:
just(seeJustfile) - Core Library:
libtmux(v0.30+ API used) - Logic:
mcp_ssh_tmux/session_manager.pyandserver.py.
- The server provides raw visual snapshots. The AI agent is responsible for interpreting state (prompts, errors, etc.).
- Hints:
server.pyappends[INFO: ...]hints to snapshots when common shell prompts or password requests are detected.
send_commandandread_fileMUST useawait asyncio.sleep(), nevertime.sleep(). The MCP server runs on an async event loop (FastMCP/uvicorn). Synchronous sleep blocks the entire event loop, making the server unresponsive to all other requests during the polling period. This was the root cause of a production bug wheresend_commandwith large timeouts (e.g., 60s fordocker service update) killed the server's ability to handle concurrentget_snapshotorlist_sessionscalls.
send_keys(literal mode) usespane.cmd("send-keys", *keys.split())to pass each token as a separate tmux argument. This lets tmux interpret key names (Enter,C-c,Tab) while still sending unrecognized tokens as literal text.- Do NOT use libtmux's
literal=True— it passes-lto tmux, which disables all key name interpretation (e.g.,"yes Enter"would type the word "Enter").
- SSH Execution: We start
sshdirectly as thewindow_shellcommand in tmux. This is more reliable than starting a shell and sending keys. - Config Resolution: We use
ssh -G <host>to resolve aliases and identity files from the user's~/.ssh/config. - BatchMode: We intentionally avoided
BatchMode=yesto allow the AI to handle interactive password/passphrase prompts visually. - Persistence:
remain-on-exitis enabled viawindow.set_option("remain-on-exit", "on"). This allows capturing final errors after a connection dies.
- Cleanup:
- The default "0" window is killed upon the first SSH connection to ensure the session can close fully when done.
TmuxSessionManager.close_windowkills the entire tmux session if the last active SSH window is closed.
- Lazy Init:
server.pyusesget_manager()for lazy initialization to avoid creating empty tmux sessions on server startup.
- Method: Uses
catandteeover the existing PTY. - Reliability: Uses unique markers (
__MCP_EOF_<uuid>__) and base64 encoding to handle binary data and special characters without shell escaping issues. - History: Commands are prefixed with a leading space to trigger
HISTCONTROL=ignorespaceand keep capture noise out of the user's shell history.
- Unit Tests:
tests/test_session_manager.pyandtests/test_validation.py. Always use themock_tmuxfixture to avoid orphaned real sessions. - Live Tests:
tests/test_live_ssh.pytests againstlocalhost. - E2E: Verified against MikroTik (RouterOS) and Debian 13 (Proxmox) environments.
- Port SSH Config & Safety Validation.
- ANSI Sanitization & Prompt Hints.
- Robust Window Renaming (user@host-id).
- File Transfer:
read_remote_fileandwrite_remote_fileimplemented. - Streaming Status:
ssh-tmux://{session_id}/snapshotMCP resource implemented. - PyPI Release: v0.1.0 published.
- Multi-Pane Layouts: Add tool to split windows for side-by-side monitoring (e.g.,
tail -fin one pane, interactive shell in another). - Port Forwarding: Add tools to manage SSH tunnels via the same tmux background process.
- Session Re-attachment: Improve
list_sessionsto allow re-associating with tmux windows created in previous server runs.
just installjust test(Runs all 15+ tests)mcp-ssh-tmux(Start the server)