Test SSH Connection
ssh_test_connectionTest SSH connectivity to configured servers by establishing a connection and returning the remote hostname for verification.
Instructions
Attempts to connect to the configured SSH host and returns the remote hostname
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "ssh_test_connection"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:88-108 (handler)Handler function that executes the ssh_test_connection tool: connects via SSH using env vars, runs 'hostname', returns result or error.
async () => { try { const ssh = await createSshConnection(); try { const { stdout, stderr, exitCode } = await runRemoteCommand(ssh, "hostname"); ssh.end(); const details = JSON.stringify( { exitCode, stderr: stderr.trim(), stdout: stdout.trim() }, null, 2 ); return { content: [{ type: "text", text: details }] }; } finally { // Ensure connection is closed if not already ssh.end(); } } catch (error) { const message = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `SSH connection failed: ${message}` }], isError: true }; } } - src/smithery.ts:64-85 (handler)Handler function that executes the ssh_test_connection tool: connects via SSH using provided config, runs 'hostname', returns result or error.
async () => { try { const ssh = await createSshConnection(config); try { const { stdout, stderr, exitCode } = await runRemoteCommand(ssh, "hostname"); ssh.end(); return { content: [ { type: "text", text: JSON.stringify({ exitCode, stdout: stdout.trim(), stderr: stderr.trim() }, null, 2), }, ], }; } finally { ssh.end(); } } catch (error) { const message = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `SSH connection failed: ${message}` }], isError: true }; } } - src/index.ts:81-109 (registration)Registration of the ssh_test_connection tool in the main MCP server in index.ts
server.registerTool( "ssh_test_connection", { title: "Test SSH Connection", description: "Attempts to connect to the configured SSH host and returns the remote hostname", inputSchema: {}, }, async () => { try { const ssh = await createSshConnection(); try { const { stdout, stderr, exitCode } = await runRemoteCommand(ssh, "hostname"); ssh.end(); const details = JSON.stringify( { exitCode, stderr: stderr.trim(), stdout: stdout.trim() }, null, 2 ); return { content: [{ type: "text", text: details }] }; } finally { // Ensure connection is closed if not already ssh.end(); } } catch (error) { const message = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `SSH connection failed: ${message}` }], isError: true }; } } ); - src/smithery.ts:57-86 (registration)Registration of the ssh_test_connection tool in the smithery module
server.registerTool( "ssh_test_connection", { title: "Test SSH Connection", description: "Attempts to connect and returns the remote hostname", inputSchema: {}, }, async () => { try { const ssh = await createSshConnection(config); try { const { stdout, stderr, exitCode } = await runRemoteCommand(ssh, "hostname"); ssh.end(); return { content: [ { type: "text", text: JSON.stringify({ exitCode, stdout: stdout.trim(), stderr: stderr.trim() }, null, 2), }, ], }; } finally { ssh.end(); } } catch (error) { const message = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `SSH connection failed: ${message}` }], isError: true }; } } ); - src/index.ts:32-53 (helper)Helper function to create SSH connection using environment variables.
async function createSshConnection(): Promise<SSHClient> { const env = getEnv(); const ssh = new SSHClient(); const config: ConnectConfig = { host: env.SSH_HOST, port: Number(env.SSH_PORT), username: env.SSH_USERNAME, password: env.SSH_PASSWORD, readyTimeout: 15000, tryKeyboard: false, algorithms: { // Keep defaults; allow host key algo negotiation modern-first }, }; await new Promise<void>((resolve, reject) => { ssh .on("ready", () => resolve()) .on("error", (err: Error) => reject(err)) .connect(config); }); return ssh; }