list_directory
Lists all files and subdirectories in a specified directory path.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Directory path to list |
Implementation Reference
- src/mcp/directory.ts:11-36 (handler)The actual tool handler/implementation of listDirectory - reads directory contents using fs.promises.readdir with file type detection, and returns an array of {name, type, size?} objects.
export async function listDirectory(dirPath: string): Promise<Array<{ name: string; type: string; size?: number }>> { const entries = await fs.promises.readdir(dirPath, { withFileTypes: true }); return Promise.all(entries.map(async (entry) => { const entryPath = path.join(dirPath, entry.name); if (entry.isDirectory()) { return { name: entry.name, type: 'directory' }; } else if (entry.isFile()) { try { const stats = await fs.promises.stat(entryPath); return { name: entry.name, type: 'file', size: stats.size }; } catch (statError) { // Handle potential stat errors (e.g., broken symlinks) console.warn(`Could not stat file ${entryPath}:`, statError); return { name: entry.name, type: 'file', size: undefined }; // Indicate file, but size unknown } } else { return { name: entry.name, type: 'other' }; } })); } - src/mcp/directory.ts:38-58 (registration)Registration function registerDirectoryTool that registers the 'list_directory' tool with the MCP server using server.tool(), defining the 'path' string parameter and the async handler that calls listDirectory.
export function registerDirectoryTool(server: McpServer): void { server.tool( "list_directory", { path: z.string().describe("Directory path to list") }, async (params) => { // Let SDK handle errors like directory not found const dirPath = params.path; const contents = await listDirectory(dirPath); return { content: [{ type: "text", text: JSON.stringify({ path: dirPath, contents }, null, 2) }] }; } ); } - src/mcp/directory.ts:11-11 (schema)Type signature: listDirectory returns Promise<Array<{name: string; type: string; size?: number}>>
export async function listDirectory(dirPath: string): Promise<Array<{ name: string; type: string; size?: number }>> { - src/index.ts:6-6 (registration)Import of registerDirectoryTool from './mcp/directory.js'
import { registerDirectoryTool } from "./mcp/directory.js"; - src/index.ts:21-21 (registration)Call to registerDirectoryTool(server) to register the tool with the MCP server instance.
registerDirectoryTool(server);