Skip to main content
Glama

ssh_list_files

List files in a directory on a remote server via SSH connection. Specify connection ID and remote path to view directory contents.

Instructions

List files in a directory on the remote server

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionIdYesID of an active SSH connection
remotePathYesPath to the directory on the remote server

Implementation Reference

  • The main handler function that executes the ssh_list_files tool logic: validates the SSH connection, initializes SFTP client, lists directory contents using readdir, formats file information (name, type, size, modified time), and returns the result.
    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
        };
      }
  • Input schema definition for the ssh_list_files tool in the server's capabilities declaration.
    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 of the tool handler in the switch statement within the CallToolRequestSchema request handler, dispatching calls to handleSSHListFiles.
    case 'ssh_list_files':
      return this.handleSSHListFiles(request.params.arguments);

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/mixelpixx/SSH-MCP'

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