Skip to main content
Glama

ida_rename_local_variable

Rename a local variable in a function within the IDA database by specifying the function name, old variable name, and new variable name.

Instructions

Rename a local variable within a function in the IDA database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
function_nameYes
new_nameYes
old_nameYes

Implementation Reference

  • Core handler implementation using IDA Hex-Rays API to decompile function, locate local variable by name, and rename it with ida_hexrays.rename_lvar. Includes validation, error handling, and view refresh.
    def rename_local_variable(self, function_name: str, old_name: str, new_name: str) -> Dict[str, Any]: """Rename a local variable within a function""" return self._rename_local_variable_internal(function_name, old_name, new_name) def _rename_local_variable_internal(self, function_name: str, old_name: str, new_name: str) -> Dict[str, Any]: """Internal implementation for rename_local_variable without sync wrapper""" try: # Parameter validation if not function_name: return {"success": False, "message": "Function name cannot be empty"} if not old_name: return {"success": False, "message": "Old variable name cannot be empty"} if not new_name: return {"success": False, "message": "New variable name cannot be empty"} # Get function address func_addr: int = ida_name.get_name_ea(0, function_name) if func_addr == idaapi.BADADDR: return {"success": False, "message": f"Function '{function_name}' not found"} # Check if it's a function func: Optional[ida_funcs.func_t] = ida_funcs.get_func(func_addr) if not func: return {"success": False, "message": f"'{function_name}' is not a function"} # Check if decompiler is available if not ida_hexrays.init_hexrays_plugin(): return {"success": False, "message": "Hex-Rays decompiler is not available"} # Get decompilation result cfunc: Optional[ida_hexrays.cfunc_t] = ida_hexrays.decompile(func_addr) if not cfunc: return {"success": False, "message": f"Failed to decompile function '{function_name}'"} ida_hexrays.open_pseudocode(func_addr, 0) # Find local variable to rename found: bool = False renamed: bool = False lvar: Optional[ida_hexrays.lvar_t] = None # Iterate through all local variables lvars = cfunc.get_lvars() for i in range(lvars.size()): v = lvars[i] if v.name == old_name: lvar = v found = True break if not found: return {"success": False, "message": f"Local variable '{old_name}' not found in function '{function_name}'"} # Rename local variable if ida_hexrays.rename_lvar(cfunc.entry_ea, lvar.name, new_name): renamed = True if renamed: # Refresh view self._refresh_view_internal() return {"success": True, "message": f"Local variable renamed from '{old_name}' to '{new_name}' in function '{function_name}'"} else: return {"success": False, "message": f"Failed to rename local variable from '{old_name}' to '{new_name}', possibly due to invalid name format or other IDA restrictions"} except Exception as e: print(f"Error renaming local variable: {str(e)}") traceback.print_exc() return {"success": False, "message": str(e)}
  • Pydantic input schema/model for the tool defining required parameters: function_name, old_name, new_name.
    class RenameLocalVariable(BaseModel): function_name: str old_name: str new_name: str
  • MCP tool registration in server.list_tools(), associating name 'ida_rename_local_variable', description, and input schema.
    Tool( name=IDATools.RENAME_LOCAL_VARIABLE, description="Rename a local variable within a function in the IDA database", inputSchema=RenameLocalVariable.schema(), ),
  • MCP server tool dispatch/caller in @server.call_tool(), invoking the proxy function with parsed arguments and returning result as TextContent.
    case IDATools.RENAME_LOCAL_VARIABLE: result: str = ida_functions.rename_local_variable( arguments["function_name"], arguments["old_name"], arguments["new_name"] ) return [TextContent( type="text", text=result )]
  • Proxy helper function in IDAProFunctions class that sends the 'rename_local_variable' request to IDA plugin via socket communicator and formats the response.
    def rename_local_variable(self, function_name: str, old_name: str, new_name: str) -> str: """Rename a local variable within a function""" try: response: Dict[str, Any] = self.communicator.send_request( "rename_local_variable", {"function_name": function_name, "old_name": old_name, "new_name": new_name} ) if "error" in response: return f"Error renaming local variable from '{old_name}' to '{new_name}' in function '{function_name}': {response['error']}" success: bool = response.get("success", False) message: str = response.get("message", "") if success: return f"Successfully renamed local variable from '{old_name}' to '{new_name}' in function '{function_name}': {message}" else: return f"Failed to rename local variable from '{old_name}' to '{new_name}' in function '{function_name}': {message}" except Exception as e: self.logger.error(f"Error renaming local variable: {str(e)}", exc_info=True) return f"Error renaming local variable from '{old_name}' to '{new_name}' in function '{function_name}': {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/MxIris-Reverse-Engineering/ida-mcp-server'

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