check_directory
Verify directory existence and display contents to validate file structure and locate files within specified paths.
Instructions
Check if a directory exists and list its contents
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Directory path to check |
Implementation Reference
- src/index.ts:451-487 (handler)The handler for the 'check_directory' tool. Resolves the input path, checks if it is a directory using fs.stat, lists its contents with fs.readdir if it is a directory, and returns appropriate text content blocks for success or error cases.case "check_directory": { const { path: dirPath } = args as { path: string }; const resolvedPath = resolvePath(dirPath); try { const stats = await fs.stat(resolvedPath); if (!stats.isDirectory()) { return { content: [ { type: "text", text: `${resolvedPath} exists but is not a directory`, }, ], }; } const files = await fs.readdir(resolvedPath); return { content: [ { type: "text", text: `Directory ${resolvedPath} exists and contains:\n${files.join("\n")}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Directory ${resolvedPath} does not exist`, }, ], }; } }
- src/index.ts:211-224 (registration)Registration of the 'check_directory' tool in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: "check_directory", description: "Check if a directory exists and list its contents", inputSchema: { type: "object", properties: { path: { type: "string", description: "Directory path to check", }, }, required: ["path"], }, },
- src/index.ts:214-223 (schema)Input schema definition for the 'check_directory' tool, specifying a required 'path' string parameter.inputSchema: { type: "object", properties: { path: { type: "string", description: "Directory path to check", }, }, required: ["path"], },
- src/index.ts:26-31 (helper)Helper function 'resolvePath' used by the check_directory handler to resolve user-provided paths, handling '~' expansion and absolute resolution.function resolvePath(inputPath: string): string { if (inputPath.startsWith("~")) { return path.join(os.homedir(), inputPath.slice(1)); } return path.resolve(inputPath); }