Skip to main content
Glama

terminal_list_dir

Lists directory contents to navigate file systems across local, remote SSH, and GitHub environments. Use this tool to view files and folders in any accessible directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dirNo.

Implementation Reference

  • Main handler for the terminal_list_dir tool. Handles directory listing for both local (fs.readdir) and SSH (ls -la command) contexts, returning formatted content.
    async ({ dir }) => { try { if (globalConfig.context === 'ssh') { const command = `ls -la "${dir || globalConfig.ssh.appPath || '~'}"`; const result = await executeCommand(command); return { content: [{ type: 'text', text: result }], }; } else { const targetDir = dir || '.'; const files = await fs.readdir(targetDir, { withFileTypes: true }); const fileList = files.map(file => ({ name: file.name, isDirectory: file.isDirectory(), path: path.join(targetDir, file.name), })); return { content: [{ type: 'text', text: JSON.stringify(fileList, null, 2) }], }; } } catch (error) { return { content: [{ type: 'text', text: `Erro ao listar diretório: ${error.message}` }], }; } }
  • Zod schema defining the input parameters for the tool: 'dir' as optional string defaulting to current directory.
    { dir: z.string().default('.'), },
  • Registration of the terminal_list_dir tool via server.tool(), specifying name, input schema, and handler function.
    server.tool( 'terminal_list_dir', { dir: z.string().default('.'), }, async ({ dir }) => { try { if (globalConfig.context === 'ssh') { const command = `ls -la "${dir || globalConfig.ssh.appPath || '~'}"`; const result = await executeCommand(command); return { content: [{ type: 'text', text: result }], }; } else { const targetDir = dir || '.'; const files = await fs.readdir(targetDir, { withFileTypes: true }); const fileList = files.map(file => ({ name: file.name, isDirectory: file.isDirectory(), path: path.join(targetDir, file.name), })); return { content: [{ type: 'text', text: JSON.stringify(fileList, null, 2) }], }; } } catch (error) { return { content: [{ type: 'text', text: `Erro ao listar diretório: ${error.message}` }], }; } } );
  • Helper function used by the handler for executing shell commands in local or SSH contexts.
    // Função auxiliar para executar comandos async function executeCommand(command, cwd = null) { if (globalConfig.context === 'ssh') { if (!globalConfig.ssh.host || !globalConfig.ssh.username || !globalConfig.ssh.password) { throw new Error('Configuração SSH incompleta'); } const { stdout, stderr } = await execSSH( globalConfig.ssh.host, globalConfig.ssh.port, globalConfig.ssh.username, globalConfig.ssh.password, command ); return `${stdout}${stderr}`.trim(); } else { const { stdout, stderr } = await execPromise(command, { cwd: cwd || globalConfig.localWorkDir, }); return `${stdout}${stderr}`.trim(); } }
  • Low-level SSH connection and command execution helper used indirectly by the tool handler.
    // Função auxiliar para SSH async function execSSH(host, port, username, password, command) { return new Promise((resolve, reject) => { const conn = new Client(); let stdout = ''; let stderr = ''; conn.on('ready', () => { conn.exec(command, (err, stream) => { if (err) { conn.end(); return reject(err); } stream.on('close', (code) => { conn.end(); resolve({ stdout, stderr, code }); }).on('data', (data) => { stdout += data.toString(); }).stderr.on('data', (data) => { stderr += data.toString(); }); }); }).on('error', (err) => { reject(err); }).connect({ host, port: parseInt(port, 10), username, password, readyTimeout: 30000, hostVerifier: () => true }); }); }

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/mamprimauto/mcp'

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