Skip to main content
Glama

file_stat

Retrieve detailed statistics for files or directories, including size, permissions, and modification timestamps, to analyze and manage file system information.

Instructions

Get file or directory statistics

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to file or directory

Implementation Reference

  • Implementation of the file_stat logic which retrieves file stats and returns them as a tool result.
    async function fileStatImpl(input: FileStatInput): Promise<ToolResult> {
      try {
        const absolutePath = path.resolve(input.path);
    
        const stats = await fs.stat(absolutePath);
        const lstat = await fs.lstat(absolutePath);
    
        const result: FileStat = {
          path: absolutePath,
          name: path.basename(absolutePath),
          size: stats.size,
          isFile: stats.isFile(),
          isDirectory: stats.isDirectory(),
          isSymlink: lstat.isSymbolicLink(),
          created: stats.birthtime.toISOString(),
          modified: stats.mtime.toISOString(),
          accessed: stats.atime.toISOString(),
        };
    
        // Add formatted size for convenience
        const formattedResult = {
          ...result,
          sizeFormatted: formatBytes(stats.size),
        };
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(formattedResult, null, 2),
            },
          ],
        };
      } catch (error) {
        const err = error as NodeJS.ErrnoException;
    
        if (err.code === 'ENOENT') {
          return {
            isError: true,
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  code: 'FILE_NOT_FOUND',
                  message: `Path not found: ${input.path}`,
                }),
              },
            ],
          };
        }
    
        if (err.code === 'EACCES') {
          return {
            isError: true,
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  code: 'PERMISSION_DENIED',
                  message: `Permission denied: ${input.path}`,
                }),
              },
            ],
          };
        }
    
        return {
          isError: true,
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                code: 'UNKNOWN_ERROR',
                message: `Error getting stats: ${err.message}`,
              }),
            },
          ],
        };
      }
    }
  • Registration of the 'file_stat' tool with the MCP server.
    server.tool(
      'file_stat',
      'Get file or directory statistics',
      {
        path: z.string().describe('Path to file or directory'),
      },
      async (args) => {
        const input = FileStatInputSchema.parse(args);
        return await fileStatImpl(input);
      }
    );
  • Zod schema definition for file_stat tool input validation.
    export const FileStatInputSchema = z.object({
      path: z.string().describe('Path to file or directory'),
    });

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/mcp-tool-shop-org/mcp-file-forge'

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