Skip to main content
Glama

ssh_test_connection

Test SSH connectivity to configured servers by attempting connection and returning remote hostname for verification.

Instructions

Attempts to connect to the configured SSH host and returns the remote hostname

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Inline handler function that tests the SSH connection by executing the 'hostname' command and returning the result or error details.
    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 }; } } );
  • Schema definition for the ssh_test_connection tool, including title, description, and empty input schema.
    { title: "Test SSH Connection", description: "Attempts to connect to the configured SSH host and returns the remote hostname", inputSchema: {}, },
  • src/index.ts:81-109 (registration)
    Registration of the ssh_test_connection tool on the MCP server using registerTool, including schema and handler.
    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 }; } } );
  • Helper function to create and connect an SSH client 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; }
  • Helper function to execute a remote command over SSH and capture stdout, stderr, exit code.
    async function runRemoteCommand(ssh: SSHClient, command: string): Promise<{ exitCode: number; stdout: string; stderr: string }>{ return await new Promise((resolve, reject) => { ssh.exec(command, (err: Error | undefined, stream: ClientChannel) => { if (err) return reject(err); let stdout = ""; let stderr = ""; stream .on("close", (code: number) => { resolve({ exitCode: code ?? 0, stdout, stderr }); }) .on("data", (data: Buffer) => { stdout += data.toString(); }) .stderr.on("data", (data: Buffer) => { stderr += data.toString(); }); }); }); }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/lgariv/ssh-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server