Skip to main content
Glama

execute_command

Execute shell commands on remote Linux or Windows systems via SSH, returning stdout, stderr, and exit codes for efficient remote system management.

Instructions

Run a shell command in an existing SSH session and return stdout/stderr/exitCode.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesShell command to execute
connection_idYes

Implementation Reference

  • Handler function for the 'execute_command' tool. Validates input, retrieves SSH connection from global state, executes the shell command using the wrapExec helper, optionally updates the connection's currentPath if the command was a 'cd', and returns stdout, stderr, and exitCode as JSON.
    if (name === "execute_command") { const { connection_id, command } = args as { connection_id: string; command: string }; if (!command?.trim()) throw new Error("Command cannot be empty."); const conn = connections.get(connection_id); if (!conn) throw new Error(`connection_id '${connection_id}' not found.`); const { stdout, stderr, exitCode } = await wrapExec(conn.client, command); // update PWD if the agent just cd’d somewhere if (/^cd\\s+/.test(command.trim())) { const { stdout: cwd } = await wrapExec(conn.client, "pwd"); conn.currentPath = cwd.trim(); } return { content: [ { type: "text", text: JSON.stringify({ stdout, stderr, exitCode }, null, 2), }, ], }; }
  • src/index.ts:167-180 (registration)
    Registration of the 'execute_command' tool in the ListTools response, including name, description, and input schema.
    { name: "execute_command", description: "Run a shell command in an existing SSH session and return stdout/stderr/exitCode.", inputSchema: { type: "object", required: ["connection_id", "command"], properties: { connection_id: { type: "string" }, command: { type: "string", description: "Shell command to execute" }, }, additionalProperties: false, }, },
  • Input schema for the 'execute_command' tool, defining required parameters connection_id and command.
    inputSchema: { type: "object", required: ["connection_id", "command"], properties: { connection_id: { type: "string" }, command: { type: "string", description: "Shell command to execute" }, }, additionalProperties: false, },
  • Helper function wrapExec that wraps SSH client.exec to return a Promise with stdout, stderr, and exitCode. Used by execute_command and other tools.
    function wrapExec(client, command): any { return new Promise((resolve, reject) => { let stdout = ""; let stderr = ""; client.exec(command, (err, stream) => { if (err) return reject(err); stream .on("close", (code) => { resolve({ stdout, stderr, exitCode: code }); }) .on("data", (data) => { stdout += data.toString(); }) .stderr.on("data", (data) => { stderr += data.toString(); }); }); }); }

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/vilasone455/ssh-mcp-server'

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