Skip to main content
Glama

get_jvm_info

Retrieve JVM basic information by providing a process ID to monitor Java application performance and diagnostics.

Instructions

获取JVM基础信息

        Args:
            pid (str): 进程ID,使用字符串形式(如:"12345")。
                支持十进制和十六进制格式。
                空字符串将返回错误信息。

        Returns:
            Dict: 包含JVM信息的字典,包含以下字段:
                - raw_output (str): 原始输出
                - timestamp (float): 时间戳
                - success (bool): 是否成功
                - error (Optional[str]): 错误信息
        

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pidNo

Implementation Reference

  • The primary handler for the get_jvm_info tool. Validates PID input, executes JinfoCommand to run the jinfo utility on the JVM process, and returns structured results including raw output, timestamp, success flag, and any errors.
    @self.mcp.tool()
    def get_jvm_info(pid: str = "") -> Dict:
        """获取JVM基础信息
    
        Args:
            pid (str): 进程ID,使用字符串形式(如:"12345")。
                支持十进制和十六进制格式。
                空字符串将返回错误信息。
    
        Returns:
            Dict: 包含JVM信息的字典,包含以下字段:
                - raw_output (str): 原始输出
                - timestamp (float): 时间戳
                - success (bool): 是否成功
                - error (Optional[str]): 错误信息
        """
        try:
            validated_pid = self._validate_and_convert_id(pid if pid else None, "process ID")
            if validated_pid is None:
                return {
                    "raw_output": "",
                    "timestamp": time.time(),
                    "success": False,
                    "error": "Invalid process ID"
                }
        except ValueError as e:
            return {
                "raw_output": "",
                "timestamp": time.time(),
                "success": False,
                "error": str(e)
            }
        
        cmd = JinfoCommand(self.executor, JinfoFormatter())
        result = cmd.execute(str(validated_pid))
        return {
            "raw_output": result.get('output', ''),
            "timestamp": time.time(),
            "success": result.get('success', False),
            "error": result.get('error')
        }
  • Helper class JinfoCommand that constructs the jinfo shell command for retrieving JVM flags, system properties, or all info from a given PID. Used by the get_jvm_info handler.
    class JinfoCommand(BaseCommand):
        """Jinfo命令实现"""
    
        def __init__(self, executor, formatter):
            super().__init__(executor, formatter)
            self.timeout = 30
    
        def get_command(self, pid: str, option: JinfoOption = JinfoOption.ALL, *args, **kwargs) -> str:
            if option == JinfoOption.FLAGS:
                return f'jinfo -flags {pid}'
            elif option == JinfoOption.SYSPROPS:
                return f'jinfo -sysprops {pid}'
            else:
                return f'jinfo {pid}'
  • Helper class JinfoFormatter that formats the output of jinfo commands into a dictionary with success status, output, execution time, and timestamp. Passed to JinfoCommand.
    class JinfoFormatter(OutputFormatter):
        """Jinfo输出格式化器(仅文本输出)"""
    
        def format(self, result: CommandResult) -> Dict[str, Any]:
            if not result.success:
                return {
                    "success": False,
                    "error": result.error,
                    "timestamp": result.timestamp.isoformat()
                    }
            return {
                "success": True,
                "output": result.output,
                "execution_time": result.execution_time,
                "timestamp": result.timestamp.isoformat()
                }
  • The @self.mcp.tool() decorator registers the get_jvm_info function as an MCP tool within the JvmMcpServer class.
    @self.mcp.tool()
  • Utility method used by get_jvm_info to validate and convert PID from string (dec/hex) to integer.
    def _validate_and_convert_id(self, value: Union[int, str, None], param_name: str = "ID") -> Optional[int]:
        """
        验证并转换ID参数,支持int和str类型的数字参数
        
        Args:
            value: 要转换的值,可以是int、str或None
            param_name: 参数名称,用于错误信息
            
        Returns:
            转换后的整数值,如果输入为None则返回None
            
        Raises:
            ValueError: 如果无法转换为有效的整数
        """
        if value is None:
            return None
            
        if isinstance(value, int):
            return value
            
        if isinstance(value, str):
            # 去除前后空白字符
            value = value.strip()
            if not value:
                return None
                
            try:
                # 支持十六进制格式(如0x2c03)和十进制格式
                if value.lower().startswith('0x'):
                    return int(value, 16)
                else:
                    return int(value)
            except ValueError:
                raise ValueError(f"Invalid {param_name}: '{value}' cannot be converted to integer")
        
        raise ValueError(f"Invalid {param_name} type: expected int, str or None, got {type(value)}")

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/xzq-xu/jvm-mcp-server'

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