directory_tree
Generate a directory tree for a specified path, resolving relative paths to the root directory or sandbox, enabling efficient document and file management within the Nutrient DWS MCP Server.
Instructions
Returns the directory tree of a given path. All paths are resolved relative to root directory.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The path to the directory to analyze to find documents or files a user has referenced. Resolves to sandbox path if enabled, otherwise resolves to the local file system. |
Implementation Reference
- src/fs/directoryTree.ts:20-34 (handler)Executes the directory tree tool by resolving the path, building the tree structure using buildDirectoryTree, and returning it as a JSON string in a success response.export async function performDirectoryTreeCall(directoryPath: string): Promise<CallToolResult> { try { const validPath = await resolveReadDirectoryPath(directoryPath) const treeData = await buildDirectoryTree('root', validPath) if (treeData) { return createSuccessResponse(JSON.stringify(treeData.children, null, 2)) } else { return createErrorResponse( `Cannot read the directory tree, make sure to allow this application access to ${validPath}`, ) } } catch (error) { return createErrorResponse(`Error: ${error instanceof Error ? error.message : String(error)}`) } }
- src/schemas.ts:3-9 (schema)Zod schema defining the input arguments for the directory_tree tool: a path string.export const DirectoryTreeArgsSchema = z.object({ path: z .string() .describe( 'The path to the directory to analyze to find documents or files a user has referenced. Resolves to sandbox path if enabled, otherwise resolves to the local file system.', ), })
- src/index.ts:92-97 (registration)Registers the 'directory_tree' tool on the MCP server with name, description, input schema, and handler that calls performDirectoryTreeCall.server.tool( 'directory_tree', 'Returns the directory tree of a given path. All paths are resolved relative to root directory.', DirectoryTreeArgsSchema.shape, async ({ path }) => performDirectoryTreeCall(path), )
- src/fs/directoryTree.ts:39-80 (helper)Recursive helper function that constructs the TreeEntry structure for the directory tree by reading fs entries concurrently.async function buildDirectoryTree(name: string, directoryPath: string): Promise<TreeEntry | null> { try { const entries = await fs.promises.readdir(directoryPath, { withFileTypes: true }) const promises = entries.map(async (entry) => { let entryData: TreeEntry | null const subPath = path.join(entry.parentPath, entry.name) if (entry.isDirectory()) { entryData = await buildDirectoryTree(entry.name, subPath) } else if (entry.isFile()) { try { const fd = await fs.promises.open(subPath, 'r') await fd.close() entryData = { name: entry.name, path: subPath, type: 'file', } } catch { // If the file is not readable, it should be included in the tree entryData = null } } else { // If the item is not a file or a directory, it should not be included in the tree entryData = null } return entryData }) // Run the build Tree node for all the children concurrently const results = await Promise.all(promises) return { name: name, path: directoryPath, type: 'directory', children: results.filter((res) => res !== null), } } catch { // This is for the case where the folder doesn't allow us to list it's content return null } }