ssh_ls
List files in a remote directory over SFTP by specifying the hostname and absolute path.
Instructions
List files in a directory on a remote host via SFTP.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | SSH hostname or IP address | |
| port | No | SSH port (default: 22) | |
| username | No | SSH username (default: current user) | |
| privateKeyPath | No | Path to SSH private key | |
| password | No | SSH password. STRONGLY prefer key-based auth (privateKeyPath or ssh-agent). Passwords pass through MCP protocol frames as plaintext and may be logged by the transport or host process. | |
| path | Yes | Absolute path to the remote directory |
Implementation Reference
- src/tools.ts:122-135 (handler)Registration and handler for the ssh_ls tool. Calls the listDir helper to list files via SFTP and joins results with newlines.
server.tool( "ssh_ls", "List files in a directory on a remote host via SFTP.", { ...connectionParams, path: z.string().describe("Absolute path to the remote directory"), }, async ({ path, ...conn }) => { return connectionPool.withConnection(conn, async (client) => { const files = await listDir(client, path); return { content: [{ type: "text", text: files.join("\n") }] }; }); }, ); - src/tools.ts:122-135 (registration)Tool registration for 'ssh_ls' using server.tool() with Zod schema for connection params and path.
server.tool( "ssh_ls", "List files in a directory on a remote host via SFTP.", { ...connectionParams, path: z.string().describe("Absolute path to the remote directory"), }, async ({ path, ...conn }) => { return connectionPool.withConnection(conn, async (client) => { const files = await listDir(client, path); return { content: [{ type: "text", text: files.join("\n") }] }; }); }, ); - src/tools.ts:125-128 (schema)Input schema for ssh_ls: spreads connectionParams (host, port, username, privateKeyPath, password) and adds a required 'path' string.
{ ...connectionParams, path: z.string().describe("Absolute path to the remote directory"), }, - src/ssh.ts:485-497 (helper)The listDir helper function that uses ssh2 SFTP client to read directory entries and return filenames.
export async function listDir(client: Client, remotePath: string): Promise<string[]> { const sftp = await getSftp(client); try { return await new Promise((resolve, reject) => { sftp.readdir(remotePath, (err, list) => { if (err) return reject(err); resolve(list.map((item) => item.filename)); }); }); } finally { sftp.end(); } }