Skip to main content
Glama
PSPDFKit

Nutrient DWS MCP Server

by PSPDFKit

directory_tree

Generate a visual directory tree structure from any specified path to help users navigate and understand file organization within the Nutrient DWS MCP Server environment.

Instructions

Returns the directory tree of a given path. All paths are resolved relative to root directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesThe 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

  • Main handler function performDirectoryTreeCall that resolves the path, builds the directory tree using helper, and returns a formatted JSON response or error.
    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)}`)
      }
    }
  • Zod schema defining the input arguments for the directory_tree tool, specifically the 'path' parameter.
    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-98 (registration)
    Tool registration in the MCP server using server.tool(), providing name 'directory_tree', description, input schema, and handler function.
      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),
      )
    }
  • Recursive helper function that builds the directory tree structure by reading entries, handling files and subdirectories concurrently, filtering out inaccessible items.
    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
      }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PSPDFKit/nutrient-dws-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server