ida_get_global_variable_by_name
Retrieve detailed information about a global variable by specifying its name using the IDA database via the MCP server.
Instructions
Get information about a global variable by name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| variable_name | Yes |
Implementation Reference
- Primary handler for getting global variable by name: resolves name to address and invokes internal address-based retrievaldef get_global_variable_by_name(self, variable_name: str) -> Dict[str, Any]: """Get global variable information by its name""" try: # Get variable address var_addr: int = ida_name.get_name_ea(0, variable_name) if var_addr == idaapi.BADADDR: return {"error": f"Global variable '{variable_name}' not found"} # Call internal implementation result = self._get_global_variable_by_address_internal(var_addr) # If successful, add variable name to result if "error" not in result and "variable_info" in result: # Parse the JSON string back to dict to modify it var_info = json.loads(result["variable_info"]) var_info["name"] = variable_name # Convert back to JSON string result["variable_info"] = json.dumps(var_info, indent=2) return result except Exception as e: print(f"Error getting global variable by name: {str(e)}") traceback.print_exc() return {"error": str(e)}
- Helper function containing core IDA API logic to extract detailed global variable information including name, address, segment, type, size, value, and string content if applicabledef _get_global_variable_by_address_internal(self, address: int) -> Dict[str, Any]: """Internal implementation for get_global_variable_by_address without sync wrapper""" try: # Verify address is valid if address == idaapi.BADADDR: return {"error": f"Invalid address: {hex(address)}"} # Get variable name if available variable_name = ida_name.get_name(address) if not variable_name: variable_name = f"unnamed_{hex(address)}" # Get variable segment segment: Optional[ida_segment.segment_t] = ida_segment.getseg(address) if not segment: return {"error": f"No segment found for address {hex(address)}"} segment_name: str = ida_segment.get_segm_name(segment) segment_class: str = ida_segment.get_segm_class(segment) # Get variable type tinfo = idaapi.tinfo_t() guess_type: bool = idaapi.guess_tinfo(tinfo, address) type_str: str = tinfo.get_type_name() if guess_type else "unknown" # Try to get variable value size: int = ida_bytes.get_item_size(address) if size <= 0: size = 8 # Default to 8 bytes # Read data based on size value: Optional[int] = None if size == 1: value = ida_bytes.get_byte(address) elif size == 2: value = ida_bytes.get_word(address) elif size == 4: value = ida_bytes.get_dword(address) elif size == 8: value = ida_bytes.get_qword(address) # Build variable info var_info: Dict[str, Any] = { "name": variable_name, "address": hex(address), "segment": segment_name, "segment_class": segment_class, "type": type_str, "size": size, "value": hex(value) if value is not None else "N/A" } # If it's a string, try to read string content if ida_bytes.is_strlit(ida_bytes.get_flags(address)): str_value = idc.get_strlit_contents(address, -1, 0) if str_value: try: var_info["string_value"] = str_value.decode('utf-8', errors='replace') except: var_info["string_value"] = str(str_value) return {"variable_info": json.dumps(var_info, indent=2)} except Exception as e: print(f"Error getting global variable by address: {str(e)}") traceback.print_exc() return {"error": str(e)}
- src/mcp_server_ida/server.py:369-399 (handler)MCP server-side proxy handler that sends socket request to IDA plugin and formats the response for the tooldef get_global_variable_by_name(self, variable_name: str) -> str: """Get global variable information by its name""" try: response: Dict[str, Any] = self.communicator.send_request( "get_global_variable_by_name", {"variable_name": variable_name} ) if "error" in response: return f"Error retrieving global variable '{variable_name}': {response['error']}" variable_info: Any = response.get("variable_info") # Verify variable_info is string type if variable_info is None: return f"Error: No variable info returned for '{variable_name}'" if not isinstance(variable_info, str): self.logger.warning(f"Variable info type is not string but {type(variable_info).__name__}, attempting conversion") try: # If it's a dictionary, convert to JSON string first if isinstance(variable_info, dict): variable_info = json.dumps(variable_info, indent=2) else: variable_info = str(variable_info) except Exception as e: return f"Error: Failed to convert variable info to string: {str(e)}" return f"Global variable '{variable_name}':\n{variable_info}" except Exception as e: self.logger.error(f"Error getting global variable: {str(e)}", exc_info=True) return f"Error retrieving global variable '{variable_name}': {str(e)}"
- src/mcp_server_ida/server.py:30-31 (schema)Pydantic input schema model defining the required 'variable_name' parameterclass GetGlobalVariableByName(BaseModel): variable_name: str
- src/mcp_server_ida/server.py:938-942 (registration)MCP tool registration in list_tools() with name, description, and input schemaTool( name=IDATools.GET_GLOBAL_VARIABLE_BY_NAME, description="Get information about a global variable by name", inputSchema=GetGlobalVariableByName.schema(), ),