Skip to main content
Glama

ssh_exec

Execute remote commands securely over SSH using a private key file. Input host, command, username, and private key path for seamless server management and automation.

Instructions

Execute command over SSH using private key file path

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYes
hostYes
privateKeyPathYes
usernameYes

Implementation Reference

  • The main execution logic for the ssh_exec tool. It validates the private key path, escapes the command for safe execution, constructs an SSH command using bash -ic to load the remote shell environment, and runs it via child_process.exec with error handling and logging.
    case 'ssh_exec': {
        const args = request.params.arguments as {
            host: string;
            command: string;
            username: string;
            privateKeyPath: string;
        };
        const { host, command, username, privateKeyPath } = args;
    
        try {
            const validatedKeyPath = validatePrivateKeyPath(privateKeyPath);
            // Escape single quotes in the command for bash -ic
            const escapedCommand = command.replace(/'/g, "'\\''");
            // Wrap the command in bash -ic '...' to load shell environment
            const sshCommand = `ssh -i "${validatedKeyPath}" ${username}@${host} "bash -ic '${escapedCommand}'"`;
            console.error('Executing SSH command:', sshCommand); // Log the modified command
    
            return new Promise((resolve) => {
                // Increased maxBuffer size for potentially larger output from env loading
                exec(sshCommand, { maxBuffer: 1024 * 1024 * 5 }, (error, stdout, stderr) => {
                    if (error) {
                        // Log both stdout and stderr on error for better debugging
                        console.error(`SSH error: ${error.message}`);
                        console.error(`SSH stderr: ${stderr}`);
                        console.error(`SSH stdout (partial): ${stdout}`);
                        resolve({
                            content: [{
                                type: 'text',
                                text: `SSH command failed.\nError: ${error.message}\nstderr: ${stderr}\nstdout: ${stdout}`,
                            }],
                            isError: true,
                        });
                    } else {
                        console.log(`SSH success: ${stdout}`);
                        resolve({
                            content: [{
                                type: 'text',
                                text: stdout,
                            }],
                        });
                    }
                });
            });
        } catch (error: unknown) {
            return {
                content: [{ type: 'text', text: `Error preparing SSH command: ${error instanceof Error ? error.message : String(error)}` }],
                isError: true,
            };
        }
    }
  • src/index.ts:83-96 (registration)
    Registration of the 'ssh_exec' tool in the ListToolsRequestSchema handler, including the tool name, description, and input schema definition.
    {
        name: 'ssh_exec',
        description: 'Execute command over SSH using private key file path',
        inputSchema: {
            type: 'object',
            properties: {
                host: { type: 'string' },
                command: { type: 'string' },
                username: { type: 'string' },
                privateKeyPath: { type: 'string' },
            },
            required: ['host', 'command', 'username', 'privateKeyPath'],
        },
    },
  • Supporting helper function used by ssh_exec to validate the existence and resolve the absolute path of the SSH private key file.
    function validatePrivateKeyPath(path: string): string {
        console.error('DEBUG: Validating key path input:', path); // Log input
        if (typeof path !== 'string') {
            throw new Error('validatePrivateKeyPath received non-string input');
        }
        const resolvedPath = resolve(path);
        console.error('DEBUG: Resolved key path:', resolvedPath); // Log resolved
        if (!existsSync(resolvedPath)) {
            throw new Error(`Private key file not found at path: ${resolvedPath}`);
        }
        return resolvedPath;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions the authentication mechanism but doesn't describe execution behavior (e.g., synchronous/asynchronous, timeout handling, error propagation, output capture, or security implications). For a potentially destructive remote execution tool, this is a significant gap.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that communicates the core functionality without unnecessary words. It's appropriately sized for a tool with this complexity level and gets straight to the point.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a remote execution tool with 4 parameters, no annotations, and no output schema, the description is insufficient. It doesn't explain what happens during execution, how output is returned, error handling, security considerations, or typical use patterns. The agent would need to guess about important behavioral aspects.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage for all 4 parameters, the description doesn't add any semantic information beyond what's implied by parameter names. It mentions 'private key file path' which aligns with the privateKeyPath parameter name but doesn't explain format expectations, path resolution, or relationships between parameters like host and username.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Execute command') and resource ('over SSH using private key file path'), making the purpose immediately understandable. It doesn't distinguish from sibling tools like rsync_copy or credential management tools, but it's specific enough to understand what this tool does.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like rsync_copy or when SSH execution is appropriate. It mentions the authentication method (private key file path) but doesn't explain prerequisites, error conditions, or typical use cases.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related 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/KinoThe-Kafkaesque/ssh-mcp-server'

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