ssh_list_files
List files and directories on a remote server via SSH, with an option to show detailed information like permissions and file sizes.
Instructions
List files and directories on the remote server
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| remotePath | No | Remote directory path to list | . |
| connectionId | No | Connection ID to use | default |
| detailed | No | Show detailed file information (permissions, size, etc.) |
Implementation Reference
- index.js:776-848 (handler)The `handleSSHListFiles` async function that executes the tool logic. It accepts remotePath, connectionId, and detailed args, establishes an SFTP connection, reads the remote directory, and formats output in either detailed (permissions/size/modified time) or simple (directories/files) format.
async handleSSHListFiles(args) { const { remotePath = '.', connectionId = 'default', detailed = false } = args; const conn = this.connections.get(connectionId); if (!conn) { throw new Error(`No active connection found for ID: ${connectionId}`); } try { const sftp = await new Promise((resolve, reject) => { conn.sftp((err, sftp) => { if (err) { return reject(new Error(`SFTP error: ${err.message}`)); } resolve(sftp); }); }); const list = await new Promise((resolve, reject) => { sftp.readdir(remotePath, (err, list) => { if (err) { return reject(new Error(`Failed to list directory: ${err.message}`)); } resolve(list); }); }); let output = `Directory listing for: ${remotePath}\n\n`; if (detailed) { output += 'Permissions Size Modified Name\n'; output += '-'.repeat(60) + '\n'; list.forEach(item => { const isDir = item.attrs.isDirectory() ? 'd' : '-'; const perms = item.attrs.mode ? (item.attrs.mode & parseInt('777', 8)).toString(8).padStart(3, '0') : '???'; const size = item.attrs.size ? item.attrs.size.toString().padStart(8) : '???'; const mtime = item.attrs.mtime ? new Date(item.attrs.mtime * 1000).toISOString() : 'Unknown'; output += `${isDir}${perms} ${size} ${mtime} ${item.filename}\n`; }); } else { const dirs = list.filter(item => item.attrs.isDirectory()).map(item => item.filename + '/'); const files = list.filter(item => !item.attrs.isDirectory()).map(item => item.filename); if (dirs.length > 0) { output += 'Directories:\n'; dirs.forEach(dir => output += ` ${dir}\n`); output += '\n'; } if (files.length > 0) { output += 'Files:\n'; files.forEach(file => output += ` ${file}\n`); } if (dirs.length === 0 && files.length === 0) { output += 'Directory is empty'; } } return { content: [ { type: 'text', text: output, }, ], }; } catch (error) { throw error; } } - index.js:254-277 (schema)Tool registration entry defining inputSchema for ssh_list_files with three optional properties: remotePath (string, default '.'), connectionId (string, default 'default'), and detailed (boolean, default false).
{ name: 'ssh_list_files', description: 'List files and directories on the remote server', inputSchema: { type: 'object', properties: { remotePath: { type: 'string', description: 'Remote directory path to list', default: '.', }, connectionId: { type: 'string', description: 'Connection ID to use', default: 'default', }, detailed: { type: 'boolean', description: 'Show detailed file information (permissions, size, etc.)', default: false, }, }, }, }, - index.js:302-305 (registration)The case statement in the CallToolRequestSchema handler that routes the 'ssh_list_files' tool name to the handleSSHListFiles method.
case 'ssh_list_files': return await this.handleSSHListFiles(args); default: throw new Error(`Unknown tool: ${name}`);