Skip to main content
Glama

file_exists

Check if a file or directory exists at a specified path. Verify existence of specific types like files, directories, or any item to confirm availability before operations.

Instructions

Check if a file or directory exists

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to check
typeNoType to check for

Implementation Reference

  • Implementation of the file_exists tool logic.
    async function fileExistsImpl(input: FileExistsInput): Promise<ToolResult> {
      try {
        const absolutePath = path.resolve(input.path);
    
        try {
          const stats = await fs.stat(absolutePath);
    
          // Check type if specified
          if (input.type === 'file' && !stats.isFile()) {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({
                    exists: false,
                    path: absolutePath,
                    reason: 'Path exists but is not a file',
                    actualType: stats.isDirectory() ? 'directory' : 'other',
                  }),
                },
              ],
            };
          }
    
          if (input.type === 'directory' && !stats.isDirectory()) {
            return {
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({
                    exists: false,
                    path: absolutePath,
                    reason: 'Path exists but is not a directory',
                    actualType: stats.isFile() ? 'file' : 'other',
                  }),
                },
              ],
            };
          }
    
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  exists: true,
                  path: absolutePath,
                  isFile: stats.isFile(),
                  isDirectory: stats.isDirectory(),
                }),
              },
            ],
          };
        } catch {
          return {
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  exists: false,
                  path: absolutePath,
                }),
              },
            ],
          };
        }
      } catch (error) {
        const err = error as Error;
    
        return {
          isError: true,
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                code: 'UNKNOWN_ERROR',
                message: `Error checking existence: ${err.message}`,
              }),
            },
          ],
        };
      }
    }
  • Registration of the file_exists tool in the MCP server.
    server.tool(
      'file_exists',
      'Check if a file or directory exists',
      {
        path: z.string().describe('Path to check'),
        type: z.enum(['file', 'directory', 'any']).optional().describe('Type to check for'),
      },
      async (args) => {
        const input = FileExistsInputSchema.parse(args);
        return await fileExistsImpl(input);
      }
    );
  • Input schema definition for file_exists tool.
    export const FileExistsInputSchema = z.object({

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