Skip to main content
Glama
PWalaGov

Enhanced Directory Context MCP Server

by PWalaGov

rename_file

Rename or move files within directories by specifying old and new paths, with optional overwrite protection for existing files.

Instructions

Rename or move a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
old_pathYesCurrent file path
new_pathYesNew file path
overwriteNoOverwrite if destination exists

Implementation Reference

  • The core handler function implementing the rename_file tool. It destructures arguments, resolves full paths relative to working directory, validates source existence and handles destination overwrite option, ensures target directory exists, performs the rename using fs.rename(), and returns a success message or throws an MCP error on failure.
    async handleRenameFile(args) { const { old_path, new_path, overwrite = false } = args; const oldFullPath = path.resolve(this.workingDirectory, old_path); const newFullPath = path.resolve(this.workingDirectory, new_path); try { // Check if source exists await fs.access(oldFullPath); // Check if destination exists try { await fs.access(newFullPath); if (!overwrite) { throw new Error('Destination file already exists. Set overwrite=true to replace it.'); } } catch (error) { // Destination doesn't exist, which is fine } // Ensure destination directory exists const destDir = path.dirname(newFullPath); await fs.mkdir(destDir, { recursive: true }); // Rename/move file await fs.rename(oldFullPath, newFullPath); return { content: [ { type: 'text', text: `File renamed/moved: ${old_path} → ${new_path}`, }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Failed to rename file: ${error.message}`); } }
  • server.js:295-317 (registration)
    Tool registration entry in the ListToolsRequestSchema handler's tools array. Defines the tool name, description, and input schema for MCP clients to discover and validate calls.
    { name: 'rename_file', description: 'Rename or move a file', inputSchema: { type: 'object', properties: { old_path: { type: 'string', description: 'Current file path', }, new_path: { type: 'string', description: 'New file path', }, overwrite: { type: 'boolean', description: 'Overwrite if destination exists', default: false, }, }, required: ['old_path', 'new_path'], }, },
  • JSON Schema defining the input parameters for the rename_file tool: old_path (required string), new_path (required string), overwrite (optional boolean with default false).
    inputSchema: { type: 'object', properties: { old_path: { type: 'string', description: 'Current file path', }, new_path: { type: 'string', description: 'New file path', }, overwrite: { type: 'boolean', description: 'Overwrite if destination exists', default: false, }, }, required: ['old_path', 'new_path'], },
  • server.js:485-486 (registration)
    Dispatch case in the CallToolRequestSchema handler's switch statement that routes 'rename_file' tool calls to the handleRenameFile method.
    case 'rename_file': return await this.handleRenameFile(args);

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/PWalaGov/File-Control-MCP'

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