Skip to main content
Glama

ssh_list_files

List files and directories on local or remote servers via SSH connection to browse directory contents and manage file systems.

Instructions

List files and directories on local or remote server

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionIdYesSSH connection ID (use "local" for local files)
remotePathYesDirectory path to list
showHiddenNoShow hidden files

Implementation Reference

  • The main execution handler for the 'ssh_list_files' tool. It validates input using ListFilesSchema, handles local file listing with fs.readdir (filtering hidden files if not specified), and remote listing via SSH execCommand with 'ls -l' or 'ls -la', returning formatted file lists or raw ls output.
    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)}` ); } }
  • Zod input schema for the 'ssh_list_files' tool, defining parameters: connectionId (string, required), remotePath (string, required), showHidden (boolean, optional default false). Used for validation in the handler.
    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)
    Tool registration in the ListTools response: defines name 'ssh_list_files', description, and inputSchema matching the Zod schema.
    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-494 (registration)
    Registration of the handler in the CallToolRequestSchema switch statement, dispatching calls to handleSSHListFiles.
    case 'ssh_list_files': return await this.handleSSHListFiles(args);

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

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