Skip to main content
Glama

safe_rename

Rename Python symbols across files with type safety. Use apply=True to execute changes after verification.

Instructions

Rename symbol across project. Set apply=True to execute.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
lineYes
columnYes
new_nameYes
applyNo

Implementation Reference

  • The 'safe_rename' tool implementation which renames a symbol in a file and optionally applies the changes. It is decorated with @mcp.tool().
    async def safe_rename(
        file_path: str, line: int, column: int, new_name: str, apply: bool = False
    ) -> str:
        """Rename symbol across project. Set apply=True to execute."""
        client = _get_client()
    
        path = Path(file_path).resolve()
        if not path.exists():
            return _error(f"File not found: {file_path}")
    
        try:
            await client.open_document(path)
    
            workspace_edit = await client.rename_symbol(
                path, line - 1, column - 1, new_name
            )
    
            if not workspace_edit:
                return _not_found(f"Cannot rename at {path.name}:{line}:{column}")
    
            all_edits = workspace_edit.get_all_edits()
            total_edits = sum(len(e) for e in all_edits.values())
    
            if not apply:
                preview = []
                for uri, edits in all_edits.items():
                    edit_path = _uri_to_path(uri)
                    for e in edits:
                        text_preview = e.new_text[:80].replace("\n", "\\n") if e.new_text else "(delete)"
                        preview.append({
                            "file": edit_path.name,
                            "line": e.range.start.line + 1,
                            "column": e.range.start.character + 1,
                            "new_text": text_preview
                        })
                return _ok({
                    "preview": True,
                    "new_name": new_name,
                    "edits_count": total_edits,
                    "files_count": len(all_edits),
                    "edits": preview
                })
    
            applied_files = []
            for uri, edits in all_edits.items():
                file_to_edit = _uri_to_path(uri)
                if file_to_edit.exists():
                    new_content = _apply_edits_to_file(file_to_edit, edits)
                    file_to_edit.write_text(new_content, encoding="utf-8")
                    applied_files.append(file_to_edit.name)
    
            return _ok({
                "applied": True,
                "new_name": new_name,
                "modified_files": applied_files
            })
    
        except Exception as e:
            return _error(str(e))

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/qinsehm1128/mcp-ty'

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