ssh_list_files
List files and directories on local or remote servers via SSH connections to browse directory contents and manage file systems.
Instructions
List files and directories on local or remote server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionId | Yes | SSH connection ID (use "local" for local files) | |
| remotePath | Yes | Directory path to list | |
| showHidden | No | Show hidden files |
Implementation Reference
- src/index.ts:778-833 (handler)The handler function that implements the core logic for the 'ssh_list_files' tool. It parses input parameters, handles local file listing using Node.js fs.readdir (with optional hidden file filtering), and remote listing via SSH execCommand with 'ls -l' or 'ls -la'. Returns formatted file listings or throws MCP errors on failure.private async handleSSHListFiles(args: unknown) { const params = ListFilesSchema.parse(args); try { if (params.connectionId === 'local') { // List local files const files = await fs.readdir(params.remotePath, { withFileTypes: true }); const fileList = files .filter((file) => params.showHidden || !file.name.startsWith('.')) .map((file) => ({ name: file.name, type: file.isDirectory() ? 'directory' : 'file', path: path.join(params.remotePath, file.name) })); return { content: [ { type: 'text', text: `Files in ${params.remotePath}:\n${JSON.stringify(fileList, null, 2)}`, }, ], }; } else { // List remote files const ssh = connectionPool.get(params.connectionId); if (!ssh) { throw new McpError( ErrorCode.InvalidParams, `Connection ID '${params.connectionId}' not found` ); } const lsCommand = params.showHidden ? 'ls -la' : 'ls -l'; const result = await ssh.execCommand(`${lsCommand} "${params.remotePath}"`); if (result.code !== 0) { throw new Error(`ls command failed: ${result.stderr}`); } return { content: [ { type: 'text', text: `Files in ${params.connectionId}:${params.remotePath}:\n${result.stdout}`, }, ], }; } } catch (error) { throw new McpError( ErrorCode.InternalError, `List files failed: ${error instanceof Error ? error.message : String(error)}` ); } }
- src/index.ts:88-92 (schema)Zod schema defining the input parameters for the 'ssh_list_files' tool: connectionId (string, required), remotePath (string, required), showHidden (boolean, optional with default false).const ListFilesSchema = z.object({ connectionId: z.string().describe('SSH connection ID (use "local" for local files)'), remotePath: z.string().describe('Directory path to list'), showHidden: z.boolean().default(false).describe('Show hidden files') });
- src/index.ts:295-306 (registration)Registration of the 'ssh_list_files' tool metadata in the ListToolsRequestSchema handler, including name, description, and inputSchema definition for tool discovery.name: 'ssh_list_files', description: 'List files and directories on local or remote server', inputSchema: { type: 'object', properties: { connectionId: { type: 'string', description: 'SSH connection ID (use "local" for local files)' }, remotePath: { type: 'string', description: 'Directory path to list' }, showHidden: { type: 'boolean', default: false, description: 'Show hidden files' } }, required: ['connectionId', 'remotePath'] }, },
- src/index.ts:493-495 (registration)Dispatch registration in the CallToolRequestSchema switch statement that routes 'ssh_list_files' calls to the handleSSHListFiles handler function.case 'ssh_list_files': return await this.handleSSHListFiles(args); case 'ssh_file_info':