Skip to main content
Glama

fs_list

Lists directory contents on remote servers via SSH to view files and folders for navigation and management.

Instructions

Lists directory contents

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSSH session ID
pathYesDirectory path to list
pageNoPage number for pagination
limitNoMaximum items per page (default: 100)

Implementation Reference

  • The actual implementation of the listDirectory function, which handles the logic for listing contents of a directory on an SSH session.
    export async function listDirectory(
      sessionId: string,
      path: string,
      page?: number,
      limit: number = 100
    ): Promise<DirListResult> {
      logger.debug('Listing directory', { sessionId, path, page, limit });
      
      const session = sessionManager.getSession(sessionId);
      if (!session) {
        throw new Error(`Session ${sessionId} not found or expired`);
      }
      
      try {
        const fileList = await session.sftp.list(path);
        
        // Convert to our DirEntry format
        const entries: DirEntry[] = fileList.map((item: any) => {
          let type: DirEntry['type'] = 'other';
          if (item.type === 'd') {
            type = 'directory';
          } else if (item.type === '-') {
            type = 'file';
          } else if (item.type === 'l') {
            type = 'symlink';
          }
          
          return {
            name: item.name,
            type,
            size: item.size,
            mtime: new Date(item.modifyTime),
            mode: item.rights ? parseInt(item.rights.toString(), 8) : undefined
          };
        });
        
        // Apply pagination if requested
        if (page !== undefined) {
          const startIndex = page * limit;
          const endIndex = startIndex + limit;
          const paginatedEntries = entries.slice(startIndex, endIndex);
          
          const hasMore = endIndex < entries.length;
          const nextToken = hasMore ? String(page + 1) : undefined;
          
          logger.debug('Directory listed with pagination', {
            sessionId,
            path,
            total: entries.length,
            page,
            returned: paginatedEntries.length,
            hasMore
          });
          
          return {
            entries: paginatedEntries,
            nextToken
          };
        }
        
        logger.debug('Directory listed', { sessionId, path, count: entries.length });
        return { entries };
      } catch (error) {
        logger.error('Failed to list directory', { sessionId, path, error });
        throw wrapError(
          error,
          ErrorCode.EFS,
          `Failed to list directory ${path}. Check if the directory exists and is readable.`
        );
      }
    }
  • src/mcp.ts:447-452 (registration)
    Registration and call handling for the "fs_list" tool in the SSH MCP server.
    case 'fs_list': {
      const params = FSListSchema.parse(args);
      const result = await listDirectory(params.sessionId, params.path, params.page, params.limit);
      logger.info('Directory listed', { sessionId: params.sessionId, path: params.path });
      return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
    }
  • Input validation schema for the "fs_list" tool using zod.
    export const FSListSchema = z.object({
      sessionId: z.string().min(1),
      path: z.string().min(1),
      page: z.number().min(0).optional(),
      limit: z.number().min(1).max(1000).optional().default(100)
    });

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/oaslananka/mcp-ssh-tool'

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