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
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | Shell command to execute | |
| connection_id | Yes |
Implementation Reference
- src/index.ts:395-418 (handler)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, }, },
- src/index.ts:171-179 (schema)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, },
- src/index.ts:93-111 (helper)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(); }); }); }); }