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({
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden for behavioral disclosure. While 'Check if a file or directory exists' implies a read-only, non-destructive operation, it doesn't explicitly state this or address potential behaviors like error handling (e.g., what happens with invalid paths), performance characteristics, or return format. For a tool with no annotation coverage, this leaves significant gaps in understanding how it behaves beyond its basic purpose.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose without any wasted words. It directly answers 'what does this tool do?' in a compact form, making it easy for an agent to parse and understand quickly. Every word earns its place by contributing to clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the lack of annotations and output schema, the description is incomplete for effective tool use. It doesn't explain what the tool returns (e.g., a boolean, an error message, or structured data), how errors are handled, or any constraints like path format requirements. For a basic utility tool with no structured behavioral hints, this leaves the agent guessing about important operational details.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with both parameters ('path' and 'type') well-documented in the schema itself. The description adds no additional meaning about parameters beyond implying the tool checks existence based on a path and optionally a type. Since the schema already provides clear descriptions and enum values for 'type', the description meets the baseline without compensating further.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('Check') and resource ('file or directory'), making it immediately understandable. However, it doesn't explicitly differentiate this existence check from similar operations like 'file_stat' (which provides detailed metadata) or 'read_file' (which attempts to read content), leaving room for ambiguity about when to choose this simpler check over more comprehensive alternatives.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With siblings like 'file_stat' (which also reveals existence through metadata) and 'read_file' (which fails if a file doesn't exist), the agent receives no hints about preferring this lightweight existence check for simple validation versus using other tools that might serve dual purposes. No context about prerequisites or exclusions is mentioned.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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