Skip to main content
Glama

ssh_list_files

List files and directories on local or remote servers via SSH. Specify the connection ID and directory path to retrieve detailed file listings, with an option to include hidden files.

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 handler function implementing the core logic for the 'ssh_list_files' tool. Handles both local file listing using fs.readdir and remote listing using SSH 'ls' command.
    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 validating parameters of the ssh_list_files tool.
    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 in the ListToolsRequestHandler response, including name, description, and input 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:494-494 (registration)
    Dispatch case in CallToolRequestHandler switch statement that routes 'ssh_list_files' calls to its handler function.
    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/widjis/mcp-ssh'

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