list-directory
Retrieve and display the contents of an FTP directory by specifying the remote path using this tool, simplifying FTP server navigation and file management.
Instructions
List contents of an FTP directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| remotePath | Yes | Path of the directory on the FTP server |
Implementation Reference
- src/index.ts:32-62 (handler)Handler function implementing the list-directory tool: calls FTP client to list directory, formats output with sizes and types, returns formatted text response or error.async ({ remotePath }) => { try { const listing = await ftpClient.listDirectory(remotePath); // Format the output const formatted = listing.map((item) => `${item.type === "directory" ? "[DIR]" : "[FILE]"} ${item.name} ${item.type === "file" ? `(${formatSize(item.size)})` : ""} - ${item.modifiedDate}` ).join("\n"); const summary = `Total: ${listing.length} items (${listing.filter(i => i.type === "directory").length} directories, ${listing.filter(i => i.type === "file").length} files)`; return { content: [ { type: "text", text: `Directory listing for: ${remotePath}\n\n${formatted}\n\n${summary}` } ] }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error listing directory: ${error instanceof Error ? error.message : String(error)}` } ] }; } }
- src/index.ts:29-31 (schema)Input schema for list-directory tool defining 'remotePath' parameter using Zod.{ remotePath: z.string().describe("Path of the directory on the FTP server"), },
- src/index.ts:26-63 (registration)Registration of the 'list-directory' tool on the MCP server with name, description, input schema, and handler.server.tool( "list-directory", "List contents of an FTP directory", { remotePath: z.string().describe("Path of the directory on the FTP server"), }, async ({ remotePath }) => { try { const listing = await ftpClient.listDirectory(remotePath); // Format the output const formatted = listing.map((item) => `${item.type === "directory" ? "[DIR]" : "[FILE]"} ${item.name} ${item.type === "file" ? `(${formatSize(item.size)})` : ""} - ${item.modifiedDate}` ).join("\n"); const summary = `Total: ${listing.length} items (${listing.filter(i => i.type === "directory").length} directories, ${listing.filter(i => i.type === "file").length} files)`; return { content: [ { type: "text", text: `Directory listing for: ${remotePath}\n\n${formatted}\n\n${summary}` } ] }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error listing directory: ${error instanceof Error ? error.message : String(error)}` } ] }; } } );
- src/ftp-client.ts:54-70 (helper)FtpClient class method that connects to FTP server, lists directory using basic-ftp Client.list(), parses and returns structured file list.async listDirectory(remotePath: string): Promise<Array<{name: string, type: string, size: number, modifiedDate: string}>> { try { await this.connect(); const list = await this.client.list(remotePath); await this.disconnect(); return list.map(item => ({ name: item.name, type: item.type === 1 ? "file" : item.type === 2 ? "directory" : "other", size: item.size, modifiedDate: item.modifiedAt ? item.modifiedAt.toISOString() : "" })); } catch (error) { console.error("List directory error:", error); throw new Error(`Failed to list directory: ${error instanceof Error ? error.message : String(error)}`); } }