ssh_list_files
List files in a directory on a remote server via an active SSH connection. Specify the connection ID and remote path to retrieve the directory contents quickly and efficiently.
Instructions
List files in a directory on the remote server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionId | Yes | ID of an active SSH connection | |
| remotePath | Yes | Path to the directory on the remote server |
Implementation Reference
- src/index.ts:565-619 (handler)The main handler function for the 'ssh_list_files' tool. It validates the connection, initializes an SFTP session, lists the files in the specified remote directory using sftp.readdir(), processes the file list with details like name, type, size, and modification time, and returns a formatted JSON response.private async handleSSHListFiles(params: any) { const { connectionId, remotePath } = params; // Check if the connection exists if (!this.connections.has(connectionId)) { return { content: [{ type: "text", text: `No active SSH connection with ID: ${connectionId}` }], isError: true }; } const { conn } = this.connections.get(connectionId)!; try { // Get SFTP client const sftp: any = await new Promise((resolve, reject) => { conn.sftp((err: Error | undefined, sftp: any) => { if (err) { reject(new Error(`Failed to initialize SFTP: ${err.message}`)); } else { resolve(sftp); } }); }); // List files const files: any = await new Promise((resolve, reject) => { sftp.readdir(remotePath, (err: Error | undefined, list: any[]) => { if (err) { reject(new Error(`Failed to list files: ${err.message}`)); } else { resolve(list); } }); }); const fileList = files.map((file: any) => ({ filename: file.filename, isDirectory: (file.attrs.mode & 16384) === 16384, size: file.attrs.size, lastModified: new Date(file.attrs.mtime * 1000).toISOString() })); return { content: [{ type: "text", text: `Files in ${remotePath}:\n\n${JSON.stringify(fileList, null, 2)}` }] }; } catch (error: any) { return { content: [{ type: "text", text: `Failed to list files: ${error.message}` }], isError: true }; }
- src/index.ts:141-157 (schema)Input schema definition for the 'ssh_list_files' tool registered in the server's capabilities.ssh_list_files: { description: "List files in a directory on the remote server", inputSchema: { type: "object", properties: { connectionId: { type: "string", description: "ID of an active SSH connection" }, remotePath: { type: "string", description: "Path to the directory on the remote server" } }, required: ["connectionId", "remotePath"] } },
- src/index.ts:284-285 (registration)Registration/dispatch point in the tool call handler switch statement that routes 'ssh_list_files' requests to the specific handler method.case 'ssh_list_files': return this.handleSSHListFiles(request.params.arguments);
- src/index.ts:244-253 (schema)Input schema for 'ssh_list_files' exposed in the ListToolsRequestSchema handler.name: 'ssh_list_files', description: 'List files in a directory on the remote server', inputSchema: { type: 'object', properties: { connectionId: { type: 'string', description: 'ID of an active SSH connection' }, remotePath: { type: 'string', description: 'Path to the directory on the remote server' } }, required: ['connectionId', 'remotePath'] }