delete_file
Remove a file from the specified path using the MCP-Delete server. Supports both relative and absolute paths to ensure accurate and safe file deletion.
Instructions
Delete a file at the specified path (supports both relative and absolute paths)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the file to delete (relative to working directory or absolute) |
Implementation Reference
- src/index.ts:61-110 (handler)Handler for the delete_file tool. Resolves the input path to possible locations, checks if the file exists, deletes it using fs.promises.unlink, and returns success or throws appropriate MCP errors.case "delete_file": { const inputPath = String(request.params.arguments?.path); if (!inputPath) { throw new McpError( ErrorCode.InvalidParams, "File path is required" ); } // Try multiple potential paths const pathsToTry = [ inputPath, // Original path isAbsolute(inputPath) ? inputPath : resolve(process.cwd(), inputPath), // Relative to process.cwd() isAbsolute(inputPath) ? inputPath : resolve('c:/mcpnfo', inputPath), // Relative to mcpnfo ]; // Try each path let fileFound = false; let foundPath = ''; for (const path of pathsToTry) { if (existsSync(path)) { fileFound = true; foundPath = path; break; } } if (!fileFound) { throw new McpError( ErrorCode.InvalidParams, `File not found: ${inputPath}\nTried paths:\n${pathsToTry.join('\n')}` ); } try { await unlink(foundPath); return { content: [{ type: "text", text: `Successfully deleted file: ${inputPath}` }] }; } catch (err) { const error = err as Error; throw new McpError( ErrorCode.InternalError, `Failed to delete file ${inputPath}: ${error.message}\nTried paths:\n${pathsToTry.join('\n')}` ); } }
- src/index.ts:37-50 (registration)Registers the "delete_file" tool in the ListTools response, including its name, description, and input schema.{ name: "delete_file", description: "Delete a file at the specified path (supports both relative and absolute paths)", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the file to delete (relative to working directory or absolute)" } }, required: ["path"] } }
- src/index.ts:40-49 (schema)Input schema definition for the delete_file tool, specifying a required 'path' string property.inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the file to delete (relative to working directory or absolute)" } }, required: ["path"] }