Skip to main content
Glama
dknell

System Information MCP Server

by dknell

get_cpu_info_tool

Retrieve CPU usage metrics and hardware information from the System Information MCP Server to monitor system performance and resource utilization.

Instructions

Retrieve CPU usage and information.

Args: interval: Measurement interval in seconds (default: 1.0) per_cpu: Include per-CPU core breakdown (default: false)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
intervalNo
per_cpuNo

Implementation Reference

  • Handler function for the get_cpu_info_tool, registered via @app.tool() decorator. Delegates execution to the get_cpu_info helper function with provided parameters.
    @app.tool()
    def get_cpu_info_tool(interval: float = 1.0, per_cpu: bool = False) -> Dict[str, Any]:
        """Retrieve CPU usage and information.
    
        Args:
            interval: Measurement interval in seconds (default: 1.0)
            per_cpu: Include per-CPU core breakdown (default: false)
        """
        return get_cpu_info(interval=interval, per_cpu=per_cpu)
  • Registration of the get_cpu_info_tool via FastMCP @app.tool() decorator.
    @app.tool()
  • Core helper function implementing the CPU information retrieval logic using psutil library. Includes parameter validation, CPU stats collection, frequency, load average, and optional per-CPU breakdown. Cached with 2-second TTL.
    @cache_result("cpu_info", ttl=2)
    def get_cpu_info(interval: float = 1.0, per_cpu: bool = False) -> Dict[str, Any]:
        """Retrieve CPU usage and information."""
        try:
            # Validate parameters
            if interval <= 0:
                raise ValueError("Interval must be a positive number")
    
            # Get CPU percentage (this call blocks for the interval)
            cpu_percent = psutil.cpu_percent(interval=interval)
    
            # Get per-CPU percentages if requested
            per_cpu_percent = None
            if per_cpu:
                per_cpu_percent = psutil.cpu_percent(interval=0, percpu=True)
    
            # Get CPU counts
            cpu_count_logical = psutil.cpu_count(logical=True) or 0
            cpu_count_physical = psutil.cpu_count(logical=False) or 0
    
            # Get CPU frequency
            try:
                cpu_freq = psutil.cpu_freq()
                cpu_freq_current = safe_float(cpu_freq.current if cpu_freq else 0)
                cpu_freq_max = safe_float(cpu_freq.max if cpu_freq else 0)
            except (AttributeError, OSError):
                cpu_freq_current = 0.0
                cpu_freq_max = 0.0
    
            # Get load average (Unix-like systems only)
            try:
                if hasattr(os, 'getloadavg'):
                    load_avg = os.getloadavg()
                    load_average = [round(avg, 2) for avg in load_avg]
                else:
                    load_average = [0.0, 0.0, 0.0]
            except (AttributeError, OSError):
                load_average = [0.0, 0.0, 0.0]
    
            result = {
                "cpu_percent": round(cpu_percent, 1),
                "cpu_count_logical": cpu_count_logical,
                "cpu_count_physical": cpu_count_physical,
                "cpu_freq_current": cpu_freq_current,
                "cpu_freq_max": cpu_freq_max,
                "load_average": load_average,
            }
    
            if per_cpu_percent is not None:
                result["per_cpu_percent"] = [round(p, 1) for p in per_cpu_percent]
    
            return result
    
        except Exception as e:
            logger.error(f"Error getting CPU info: {e}")
            raise

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/dknell/mcp-system-info'

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