Skip to main content
Glama
mfangtao
by mfangtao

execute_ssh_command

Run commands on remote servers using SSH with support for password and private key authentication. Simplifies remote server management through the Model Context Protocol (MCP).

Instructions

Execute command on remote server via SSH

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYes
connectionYes

Implementation Reference

  • The core handler function that creates an SSH client connection using ssh2, executes the provided command, captures stdout, stderr, and exit code, and resolves with SSHCommandResult.
    private executeSSHCommand( config: SSHConnectionConfig, command: string ): Promise<SSHCommandResult> { return new Promise((resolve, reject) => { const conn = new Client(); let stdout = ''; let stderr = ''; let code: number | null = null; conn.on('ready', () => { conn.exec(command, (err, stream) => { if (err) { conn.end(); return reject(err); } stream .on('close', (exitCode: number) => { code = exitCode; conn.end(); resolve({ stdout, stderr, code }); }) .on('data', (data: string) => { stdout += data; }) .stderr.on('data', (data: string) => { stderr += data; }); }); }).on('error', (err) => { reject(err); }); const connectOptions: any = { host: config.host, port: config.port || 22, username: config.username, }; if (config.password) { connectOptions.password = config.password; } else if (config.privateKey) { connectOptions.privateKey = config.privateKey; } conn.connect(connectOptions); }); }
  • The MCP CallTool request handler that validates the tool name 'execute_ssh_command', extracts arguments, calls executeSSHCommand, and formats the response content based on success or failure.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => { if (request.params.name !== 'execute_ssh_command') { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } if (!request.params.arguments) { throw new McpError(ErrorCode.InvalidParams, 'Missing arguments'); } const { connection, command } = request.params.arguments as { connection: SSHConnectionConfig; command: string; }; if (!connection || !command) { throw new McpError(ErrorCode.InvalidParams, 'Missing connection or command'); } const result = await this.executeSSHCommand(connection, command); if (result.code !== 0) { return { content: [ { type: 'text', text: `Command failed with code ${result.code}\nSTDERR: ${result.stderr}`, }, ], isError: true, }; } return { content: [ { type: 'text', text: result.stdout, }, ], }; });
  • The tool definition including name, description, and detailed inputSchema for SSH connection configuration and command.
    { name: 'execute_ssh_command', description: 'Execute command on remote server via SSH', inputSchema: { type: 'object', properties: { connection: { type: 'object', properties: { host: { type: 'string' }, port: { type: 'number', default: 22 }, username: { type: 'string' }, password: { type: 'string' }, privateKey: { type: 'string' }, }, required: ['host', 'username'], }, command: { type: 'string' }, }, required: ['connection', 'command'], }, },
  • src/index.ts:47-72 (registration)
    Registers the 'execute_ssh_command' tool by handling ListToolsRequestSchema and returning the tool list.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'execute_ssh_command', description: 'Execute command on remote server via SSH', inputSchema: { type: 'object', properties: { connection: { type: 'object', properties: { host: { type: 'string' }, port: { type: 'number', default: 22 }, username: { type: 'string' }, password: { type: 'string' }, privateKey: { type: 'string' }, }, required: ['host', 'username'], }, command: { type: 'string' }, }, required: ['connection', 'command'], }, }, ], }));
  • TypeScript interface defining the structure of the SSH command execution result.
    interface SSHCommandResult { stdout: string; stderr: string; code: number | null; }
  • TypeScript interface defining the SSH connection configuration.
    interface SSHConnectionConfig { host: string; port?: number; username: string; password?: string; privateKey?: string; }

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

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