ssh_connect
Establish a secure read-only SSH connection to a remote machine for safe command execution. Authenticate using host, username, port, and optional private key or password.
Instructions
Establish SSH connection to a remote machine (read-only access only).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | Remote host IP address or hostname | |
| username | Yes | SSH username | |
| port | No | SSH port (default: 22) | |
| key_filename | No | Path to private key file (recommended) | |
| password | No | SSH password (fallback if no key) |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- ssh_readonly_fastmcp.py:55-101 (handler)The ssh_connect tool handler function. Uses paramiko to establish an SSH connection to a remote host. Accepts host, username, port (default 22), key_filename, and password. Creates an SSHClient, connects via key or password, stores the client in a global dict keyed by 'username@host:port', and returns a success or failure message.
@mcp.tool() def ssh_connect( host: str, username: str, port: int = 22, key_filename: str = None, password: str = None ) -> str: """ Establish SSH connection to a remote machine (read-only access only). Args: host: Remote host IP address or hostname username: SSH username port: SSH port (default: 22) key_filename: Path to private key file (recommended) password: SSH password (fallback if no key) Returns: Connection status message """ try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if key_filename: client.connect(host, port=port, username=username, key_filename=key_filename, timeout=10) elif password: client.connect(host, port=port, username=username, password=password, timeout=10) else: raise ValueError("Either key_filename or password must be provided") # Store client with connection identifier connection_id = f"{username}@{host}:{port}" ssh_clients[connection_id] = { 'client': client, 'host': host, 'username': username, 'port': port } return f"Successfully connected to {connection_id}\nRead-only access enabled." except Exception as e: return f"Connection failed: {str(e)}" - ssh_readonly_fastmcp.py:55-62 (registration)Registration of ssh_connect as an MCP tool via the @mcp.tool() decorator, along with its type-annotated parameter schema (host: str, username: str, port: int = 22, key_filename: str = None, password: str = None).
@mcp.tool() def ssh_connect( host: str, username: str, port: int = 22, key_filename: str = None, password: str = None ) -> str: - ssh_readonly_fastmcp_mcast.py:169-215 (handler)Duplicate implementation of ssh_connect in the multicast-discovery variant of the server. Same logic using paramiko, same parameter schema, same global storage pattern.
@mcp.tool() def ssh_connect( host: str, username: str, port: int = 22, key_filename: str = None, password: str = None ) -> str: """ Establish SSH connection to a remote machine (read-only access only). Args: host: Remote host IP address or hostname username: SSH username port: SSH port (default: 22) key_filename: Path to private key file (recommended) password: SSH password (fallback if no key) Returns: Connection status message """ try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if key_filename: client.connect(host, port=port, username=username, key_filename=key_filename, timeout=10) elif password: client.connect(host, port=port, username=username, password=password, timeout=10) else: raise ValueError("Either key_filename or password must be provided") # Store client with connection identifier connection_id = f"{username}@{host}:{port}" ssh_clients[connection_id] = { 'client': client, 'host': host, 'username': username, 'port': port } return f"Successfully connected to {connection_id}\nRead-only access enabled." except Exception as e: return f"Connection failed: {str(e)}" - ssh_readonly_fastmcp_mcast.py:169-176 (registration)Registration of ssh_connect via @mcp.tool() decorator in the multicast discovery server variant.
@mcp.tool() def ssh_connect( host: str, username: str, port: int = 22, key_filename: str = None, password: str = None ) -> str: