← All articles

Agents · October 3, 2026 · 4 min read

Connecting AI agents to cloud GPUs with MCP

Learn how to connect AI agents to high-performance cloud GPUs using the Model Context Protocol (MCP) and Podstack for on-demand, per-second billed compute.

Connecting AI agents to cloud GPUs has traditionally been a clunky process involving manual infrastructure provisioning, long-running SSH sessions, and fragile REST wrappers. For ML engineers, this friction often means agents are limited to simple API calls rather than performing heavy-duty computation like fine-tuning, video rendering, or large-scale inference.

The Model Context Protocol (MCP) changes this by providing a standardized way for LLMs to interface with external tools. By combining MCP with a high-performance GPU cloud, you can give your agents "superpowers"—allowing them to spin up H100s or A100s, execute CUDA kernels, and tear them down automatically when the task is complete.

In this guide, we’ll walk through how to build an MCP server that allows an agent to run Python-based GPU workloads on Podstack.

The Architecture: Why MCP?

The Model Context Protocol acts as a bridge between the agent (the "client") and the compute resource (the "server"). Instead of writing custom logic for every agent framework, MCP allows you to define a set of tools that any compatible agent—like Claude Desktop or a custom LangChain implementation—can understand.

When an agent needs to process an image with a specific diffusion model or run a heavy tensor calculation, it calls a tool defined in your MCP server. That server, in turn, invokes the Podstack API to launch a per-second billed GPU job.

Prerequisites

To follow along, you will need:

  • A Podstack account and an API key.
  • The Podstack API installed (pip install podstack-sdk).
  • The Python MCP SDK (pip install mcp).

First, authenticate your environment:

Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.

Building the MCP GPU Server

We will create a Python-based MCP server that exposes a tool called run_gpu_task. This tool will take Python code as input and execute it on a remote GPU container via Podstack.

import os
import subprocess
from mcp.server.fastmcp import FastMCP

# Initialize FastMCP server
mcp = FastMCP("PodstackGPU")

@mcp.tool()
def execute_gpu_code(script_contents: str, gpu_type: str = "A100"):
    """
    Executes Python code on a Podstack cloud GPU.
    Args:
        script_contents: The Python code to run on the GPU.
        gpu_type: The type of GPU required (e.g., 'A100', 'H100', 'L40').
    """
    # Create a temporary file for the script
    with open("task.py", "w") as f:
        f.write(script_contents)
    
    # Use the Podstack API to run a per-second job
    # This command uploads the script and executes it in a standard ML environment
    try:
        result = subprocess.run(
            ["podstack", "run", "--gpu", gpu_type, "--file", "task.py"],
            capture_output=True,
            text=True,
            check=True,
            env={**os.environ, "PODSTACK_API_KEY": os.getenv("PODSTACK_API_KEY")}
        )
        return {"status": "success", "output": result.stdout}
    except subprocess.CalledProcessError as e:
        return {"status": "error", "error": e.stderr}

if __name__ == "__main__":
    mcp.run()

Connecting the Agent

Once your MCP server is running, you can connect it to an agent. If you are using an agentic IDE or the Claude Desktop client, you add your server to the configuration file (usually claude_desktop_config.json):

{
  "mcpServers": {
    "podstack": {
      "command": "python",
      "args": ["/path/to/your/mcp_server.py"],
      "env": {
        "PODSTACK_API_KEY": "your_api_key_here"
      }
    }
  }
}

Now, your agent can "reason" about when it needs a GPU. If you ask the agent, "Compare the execution time of a large matrix multiplication on a CPU vs an A100," the agent will generate the PyTorch code, call your execute_gpu_code tool, and receive the real-world results from Podstack.

Practical Tips for Production

  1. Environment Syncing: Podstack allows you to define custom Docker images. If your agent needs specific libraries (like diffusers or vLLM), pre-build an image and include the --image flag in your subprocess call to minimize cold-start times.
  2. State Management: GPU jobs are often ephemeral. If your agent is performing a multi-step task (e.g., training for 10 minutes and then running inference), use Podstack's persistent volumes to store model weights between jobs.
  3. Cost Controls: Because Podstack bills per second, ensure your MCP server includes a timeout or a maximum spend limit to prevent an autonomous agent from accidentally running a massive cluster for longer than intended.
  4. Security: Always sanitize the code inputs if your MCP server is exposed beyond a local environment. Use Podstack’s isolated job environments to ensure the agent’s code cannot escape the container.

Scaling with Podstack

Using MCP with Podstack bridges the gap between LLM intelligence and raw compute power. By leveraging Podstack’s serverless inference and on-demand jobs, you don't have to manage a pool of idling GPUs. The agent requests compute, Podstack provisions it in seconds, bills you only for the duration of the execution, and releases the hardware immediately.

Whether you are building an autonomous ML researcher or a creative coding assistant, the combination of a standardized protocol like MCP and a high-performance cloud like Podstack ensures your agents are never bottlenecked by hardware availability.

Try it on Podstack

Per-second GPUs and pay-per-token models. Add $5, get $10 on your first top-up.

Start building