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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to check | |
| type | No | Type to check for |
Implementation Reference
- src/tools/metadata.ts:114-196 (handler)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}`, }), }, ], }; } } - src/tools/metadata.ts:458-469 (registration)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); } ); - src/types.ts:250-250 (schema)Input schema definition for file_exists tool.
export const FileExistsInputSchema = z.object({