Skip to main content
Glama

mcp_call_gpu_available

Check GPU availability and performance in conda environments for PyTorch or TensorFlow, verifying Metal acceleration setup and providing benchmark comparisons.

Instructions

Check if GPU is available in torch for a specific conda environment.
Input: torch or tensorflow
if framework is not provided, it will default to torch.

Returns a detailed dictionary with the following information:
- "torch_version": PyTorch version string
- "python_version": Python version string
- "platform": Platform information string
- "processor": Processor type
- "architecture": CPU architecture
- "mps_available": True if MPS (Metal Performance Shaders) is available
- "mps_built": True if PyTorch was built with MPS support
- "mps_functional": True if MPS is functional, False otherwise
- "benchmarks": A list of benchmark results for different matrix sizes, each containing:
  - "size": Matrix size used for benchmark
  - "cpu_time": Time taken on CPU (seconds)
  - "mps_time": Time taken on MPS (seconds)
  - "speedup": Ratio of CPU time to MPS time (higher means MPS is faster)

This helps determine if GPU acceleration via Apple's Metal is properly configured
and functioning, with performance benchmarks for comparison.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
env_nameYes
frameworkNotorch

Implementation Reference

  • Primary handler for the 'mcp_call_gpu_available' tool. Decorated with @mcp.tool for registration. Dispatches to specific framework helpers based on input.
    @mcp.tool(name="mcp_call_gpu_available")
    async def mcp_call_gpu_available(env_name: str, framework: str = "torch") -> dict:
        """
        Check if GPU is available in torch for a specific conda environment.
        Input: torch or tensorflow
        if framework is not provided, it will default to torch.
    
        Returns a detailed dictionary with the following information:
        - "torch_version": PyTorch version string
        - "python_version": Python version string
        - "platform": Platform information string
        - "processor": Processor type
        - "architecture": CPU architecture
        - "mps_available": True if MPS (Metal Performance Shaders) is available
        - "mps_built": True if PyTorch was built with MPS support
        - "mps_functional": True if MPS is functional, False otherwise
        - "benchmarks": A list of benchmark results for different matrix sizes, each containing:
          - "size": Matrix size used for benchmark
          - "cpu_time": Time taken on CPU (seconds)
          - "mps_time": Time taken on MPS (seconds)
          - "speedup": Ratio of CPU time to MPS time (higher means MPS is faster)
    
        This helps determine if GPU acceleration via Apple's Metal is properly configured
        and functioning, with performance benchmarks for comparison.
        """
        if framework == "torch":
            return load_gpu_available_mac_torch(env_name)
        elif framework == "tensorflow":
            return load_gpu_available_mac_tensorflow_benchmarks(env_name)
        return {"error": "Framework not supported"}
  • Helper function that executes a PyTorch script in the target conda environment to check MPS (Apple GPU) availability, functionality, and performance benchmarks via matrix multiplication timings.
    def load_gpu_available_mac_torch(env_name: str) -> dict:
        """Get detailed information about PyTorch and MPS capabilities on Mac in the specified conda environment."""
        conda_executable = find_conda_executable()
        if not conda_executable:
            return {"error": "Conda executable not found"}
    
        # Create a temporary Python script
        with tempfile.NamedTemporaryFile(suffix=".py", mode="w+", delete=False) as f:
            f.write(
                "import torch\n"
                "import platform\n"
                "import sys\n"
                "import json\n"
                "import time\n"
                "\n"
                "info = {\n"
                "    'torch_version': torch.__version__,\n"
                "    'python_version': sys.version.split()[0],\n"
                "    'platform': platform.platform(),\n"
                "    'processor': platform.processor(),\n"
                "    'architecture': platform.machine(),\n"
                "    'mps_available': torch.backends.mps.is_available(),\n"
                "    'mps_built': torch.backends.mps.is_built(),\n"
                "    'benchmarks': []\n"
                "}\n"
                "\n"
                "# Try to get device information if MPS is available\n"
                "if torch.backends.mps.is_available():\n"
                "    try:\n"
                "        # Test MPS with a small tensor operation\n"
                "        device = torch.device('mps')\n"
                "        x = torch.ones(10, 10, device=device)\n"
                "        y = x + x\n"
                "        info['mps_functional'] = bool((y == 2).all().item())\n"
                "        \n"
                "        # GPU vs CPU benchmark\n"
                "        matrix_sizes = [5000]  # Test with different sizes\n"
                "        \n"
                "        for size in matrix_sizes:\n"
                "            benchmark = {'size': size}\n"
                "            \n"
                "            # CPU benchmark\n"
                "            a_cpu = torch.randn(size, size)\n"
                "            b_cpu = torch.randn(size, size)\n"
                "            start = time.time()\n"
                "            c_cpu = torch.matmul(a_cpu, b_cpu)\n"
                "            cpu_time = time.time() - start\n"
                "            benchmark['cpu_time'] = cpu_time\n"
                "            \n"
                "            # MPS benchmark\n"
                "            a_mps = torch.randn(size, size, device=device)\n"
                "            b_mps = torch.randn(size, size, device=device)\n"
                "            torch.mps.synchronize()\n"
                "            start = time.time()\n"
                "            c_mps = torch.matmul(a_mps, b_mps)\n"
                "            torch.mps.synchronize()\n"
                "            mps_time = time.time() - start\n"
                "            benchmark['mps_time'] = mps_time\n"
                "            \n"
                "            # Calculate speedup\n"
                "            benchmark['speedup'] = cpu_time / mps_time if mps_time > 0 else 0\n"
                "            info['benchmarks'].append(benchmark)\n"
                "        \n"
                "    except Exception as e:\n"
                "        info['mps_error'] = str(e)\n"
                "        info['mps_functional'] = False\n"
                "else:\n"
                "    # Get reason why MPS is not available\n"
                "    info['mps_not_available_reason'] = 'PyTorch not built with MPS support' "
                "if not torch.backends.mps.is_built() else 'Hardware/OS not supported'\n"
                "\n"
                "print(json.dumps(info))"
            )
            script_path = f.name
    
        try:
            # Create a clean environment without venv variables
            clean_env = os.environ.copy()
    
            # Remove virtual environment variables that might interfere
            for var in list(clean_env.keys()):
                if var.startswith("VIRTUAL_ENV") or var.startswith("PYTHONHOME"):
                    clean_env.pop(var, None)
    
            # Update PATH to remove .venv entries
            if "PATH" in clean_env:
                path_parts = clean_env["PATH"].split(os.pathsep)
                clean_path = os.pathsep.join([p for p in path_parts if ".venv" not in p])
                clean_env["PATH"] = clean_path
    
            # Execute with clean environment
            command = f"{conda_executable} run -n {env_name} python {script_path}"
            logging.debug(f"Executing: {command}")
    
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                env=clean_env,  # Use the clean environment
            )
    
            if result.returncode == 0:
                try:
                    gpu_info = json.loads(result.stdout.strip())
                    logging.info(f"PyTorch MPS check: {gpu_info}")
                    return gpu_info
                except json.JSONDecodeError:
                    error_msg = f"Failed to parse PyTorch output: {result.stdout}"
                    logging.error(error_msg)
                    return {"error": error_msg, "raw_output": result.stdout}
            else:
                error_msg = f"Failed to check PyTorch MPS availability: {result.stderr}"
                logging.error(error_msg)
                return {"error": error_msg, "returncode": result.returncode}
        finally:
            # Clean up the temporary file
            os.remove(script_path)
  • Helper function for TensorFlow GPU (MPS) availability check and benchmarks in the specified conda environment, analogous to the PyTorch version.
    def load_gpu_available_mac_tensorflow_benchmarks(env_name: str) -> dict:
        """Get detailed information about TensorFlow and MPS capabilities on Mac in the specified conda environment."""
        conda_executable = find_conda_executable()
        if not conda_executable:
            return {"error": "Conda executable not found"}
    
        # Create a temporary Python script
        with tempfile.NamedTemporaryFile(suffix=".py", mode="w+", delete=False) as f:
            f.write(
                "import tensorflow as tf\n"
                "import platform\n"
                "import sys\n"
                "import json\n"
                "import time\n"
                "import numpy as np\n"
                "\n"
                "info = {\n"
                "    'tf_version': tf.__version__,\n"
                "    'python_version': sys.version.split()[0],\n"
                "    'platform': platform.platform(),\n"
                "    'processor': platform.processor(),\n"
                "    'architecture': platform.machine(),\n"
                "    'gpu_devices': tf.config.list_physical_devices('GPU'),\n"
                "    'benchmarks': []\n"
                "}\n"
                "\n"
                "try:\n"
                "    # Check if MPS is available\n"
                "    info['mps_available'] = len(tf.config.list_physical_devices('GPU')) > 0\n"
                "    \n"
                "    if info['mps_available']:\n"
                "        # Test with matrix multiplication benchmark\n"
                "        matrix_sizes = [5000]  # Test with different sizes\n"
                "        \n"
                "        for size in matrix_sizes:\n"
                "            benchmark = {'size': size}\n"
                "            \n"
                "            # CPU benchmark\n"
                "            with tf.device('/CPU:0'):\n"
                "                a_cpu = tf.random.normal([size, size])\n"
                "                b_cpu = tf.random.normal([size, size])\n"
                "                start = time.time()\n"
                "                c_cpu = tf.matmul(a_cpu, b_cpu)\n"
                "                _ = c_cpu.numpy()  # Force execution\n"
                "                cpu_time = time.time() - start\n"
                "                benchmark['cpu_time'] = cpu_time\n"
                "            \n"
                "            # GPU/MPS benchmark\n"
                "            with tf.device('/GPU:0'):\n"
                "                a_gpu = tf.random.normal([size, size])\n"
                "                b_gpu = tf.random.normal([size, size])\n"
                "                # Warmup run\n"
                "                _ = tf.matmul(a_gpu, b_gpu).numpy()\n"
                "                \n"
                "                start = time.time()\n"
                "                c_gpu = tf.matmul(a_gpu, b_gpu)\n"
                "                _ = c_gpu.numpy()  # Force execution\n"
                "                gpu_time = time.time() - start\n"
                "                benchmark['gpu_time'] = gpu_time\n"
                "            \n"
                "            # Calculate speedup\n"
                "            benchmark['speedup'] = cpu_time / gpu_time if gpu_time > 0 else 0\n"
                "            info['benchmarks'].append(benchmark)\n"
                "            \n"
                "        info['mps_functional'] = True\n"
                "except Exception as e:\n"
                "    info['error'] = str(e)\n"
                "    info['mps_functional'] = False\n"
                "\n"
                "print(json.dumps(info))"
            )
            script_path = f.name
    
        try:
            # Create a clean environment without venv variables
            clean_env = os.environ.copy()
    
            # Remove virtual environment variables that might interfere
            for var in list(clean_env.keys()):
                if var.startswith("VIRTUAL_ENV") or var.startswith("PYTHONHOME"):
                    clean_env.pop(var, None)
    
            # Update PATH to remove .venv entries
            if "PATH" in clean_env:
                path_parts = clean_env["PATH"].split(os.pathsep)
                clean_path = os.pathsep.join([p for p in path_parts if ".venv" not in p])
                clean_env["PATH"] = clean_path
    
            # Execute with clean environment
            command = f"{conda_executable} run -n {env_name} python {script_path}"
            logging.debug(f"Executing: {command}")
    
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                env=clean_env,
            )
    
            if result.returncode == 0:
                try:
                    gpu_info = json.loads(result.stdout.strip())
                    logging.info(f"TensorFlow MPS check: {gpu_info}")
                    return gpu_info
                except json.JSONDecodeError:
                    error_msg = f"Failed to parse TensorFlow output: {result.stdout}"
                    logging.error(error_msg)
                    return {"error": error_msg, "raw_output": result.stdout}
            else:
                error_msg = f"Failed to check TensorFlow MPS availability: {result.stderr}"
                logging.error(error_msg)
                return {"error": error_msg, "returncode": result.returncode}
        finally:
            # Clean up the temporary file
            os.remove(script_path)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes the tool's behavior: it runs benchmarks, returns detailed diagnostic information, and helps determine GPU configuration status. However, it doesn't mention potential side effects, performance impact, or error conditions that might occur during execution.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured and appropriately sized. It begins with the core purpose, explains parameters, details the return structure, and concludes with the tool's value. Every sentence adds necessary information without redundancy, and the return value documentation is efficiently organized in bullet points.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a diagnostic tool with no annotations and no output schema, the description provides comprehensive information about purpose, parameters, and return values. The detailed return structure documentation compensates for the lack of output schema. However, it doesn't cover potential error cases or execution constraints that might be relevant for a system diagnostic tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description must compensate for the schema's lack of parameter documentation. It successfully explains both parameters: 'env_name' is implied through 'specific conda environment,' and 'framework' is explicitly described with its default behavior ('if framework is not provided, it will default to torch'). This adds significant value beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('Check if GPU is available') and resource ('in torch for a specific conda environment'), with specific scope about Apple's Metal acceleration. It distinguishes from sibling tools like 'mcp_call_conda_info' and 'mcp_call_mac_system_profiler' by focusing on GPU availability testing rather than general conda or system information.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context about when to use this tool ('to determine if GPU acceleration via Apple's Metal is properly configured and functioning'), but doesn't explicitly state when NOT to use it or mention specific alternatives. The framework parameter guidance ('if framework is not provided, it will default to torch') offers some usage direction.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/zhongmingyuan/mcp-my-mac'

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