Skip to main content
Glama

rename_file

Rename files or folders using Language Server Protocol capabilities to update references across your codebase automatically.

Instructions

Rename a file or folder using LSP rename capabilities

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
oldPathYesCurrent path of the file or folder
newPathYesNew path for the file or folder
languageNoProgramming language (typescript, javascript, python, etc.)typescript

Implementation Reference

  • Core handler function that implements the logic for renaming files or directories, using filesystem operations with LSP integration for files.
    export async function renameFile(args: RenameFileArgs, clientManager: LSPClientManager) { const { oldPath, newPath, language = 'typescript' } = args; const workspaceRoot = findWorkspaceRoot(oldPath); try { // Check if paths exist await fs.access(oldPath); const isDirectory = (await fs.stat(oldPath)).isDirectory(); if (isDirectory) { // For directories, perform file system rename and update imports await fs.rename(oldPath, newPath); // Find all files that might import from the renamed directory const references = await findDirectoryReferences(oldPath, workspaceRoot); return { content: [ { type: 'text', text: `Successfully renamed directory from ${oldPath} to ${newPath}. Found ${references.length} files that may need import updates.`, }, ], }; } else { // For files, use LSP if possible, otherwise fallback to filesystem try { const client = await clientManager.getOrCreateLSPClient(language, workspaceRoot); // Open the file in LSP const fileContent = await fs.readFile(oldPath, 'utf-8'); await clientManager.sendLSPNotification(client, 'textDocument/didOpen', { textDocument: { uri: `file://${oldPath}`, languageId: language, version: 1, text: fileContent, }, }); // Try to get workspace edit for file rename // Note: Not all LSP servers support file renaming await fs.rename(oldPath, newPath); return { content: [ { type: 'text', text: `Successfully renamed file from ${oldPath} to ${newPath}`, }, ], }; } catch (_lspError) { // Fallback to filesystem rename await fs.rename(oldPath, newPath); return { content: [ { type: 'text', text: `File renamed from ${oldPath} to ${newPath} (LSP rename not available, imports may need manual updates)`, }, ], }; } } } catch (error) { throw new Error(`Failed to rename: ${error instanceof Error ? error.message : String(error)}`); } }
  • src/server.ts:43-65 (registration)
    Tool registration in the ListTools handler, defining name 'rename_file', description, and input schema.
    { name: 'rename_file', description: 'Rename a file or folder using LSP rename capabilities', inputSchema: { type: 'object', properties: { oldPath: { type: 'string', description: 'Current path of the file or folder', }, newPath: { type: 'string', description: 'New path for the file or folder', }, language: { type: 'string', description: 'Programming language (typescript, javascript, python, etc.)', default: 'typescript', }, }, required: ['oldPath', 'newPath'], }, },
  • src/server.ts:212-213 (registration)
    Registration of the call handler for 'rename_file' tool in the switch statement of CallToolRequest.
    case 'rename_file': return await renameFile(args as unknown as RenameFileArgs, this.clientManager);
  • TypeScript interface defining the input arguments for the rename_file tool.
    export interface RenameFileArgs { oldPath: string; newPath: string; language?: string; }
  • src/server.ts:5-5 (registration)
    Import of the renameFile handler function.
    import { renameFile } from './commands/renameFile.js';

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/sminnee/lsp-mcp'

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