CrewAI Integration

Use MachineMarket from CrewAI by creating @tool decorated functions that call the API directly with requests.

Prerequisites

  • Python 3.10+
  • crewai and requests
terminal
pip install crewai requests

Create the tool

tools.py
import json
import requests
from crewai.tools import tool

BASE_URL = "https://api.machinemarket.ai"


@tool("Spawn MachineMarket Server")
def spawn_server(
    tier: str,
    template: str,
    duration: str,
    tx_hash: str,
    wallet_address: str,
    region: str = "fsn1",
) -> str:
    """Provision a VPS server with MachineMarket.
    tier: Nano, Small, Medium, or Large
    template: base, node, python, or agent
    duration: 1h, 24h, 7d, or 30d
    tx_hash: USDC payment transaction hash on Base
    wallet_address: Sender wallet address
    region: Deployment region (default: fsn1)
    """
    response = requests.post(
        f"{BASE_URL}/v1/spawn",
        json={
            "tier": tier,
            "template": template,
            "duration": duration,
            "region": region,
            "tx_hash": tx_hash,
            "wallet_address": wallet_address,
        },
    )
    return json.dumps(response.json(), indent=2)


@tool("Get MachineMarket Pricing")
def get_pricing() -> str:
    """Get available VPS pricing tiers and durations from MachineMarket."""
    response = requests.get(f"{BASE_URL}/v1/pricing")
    return json.dumps(response.json(), indent=2)


@tool("Get MachineMarket Instance")
def get_instance(instance_id: str) -> str:
    """Get details of a MachineMarket VPS instance by ID."""
    response = requests.get(f"{BASE_URL}/v1/instances/{instance_id}")
    return json.dumps(response.json(), indent=2)


@tool("Destroy MachineMarket Server")
def destroy_server(instance_id: str) -> str:
    """Immediately destroy a MachineMarket VPS instance."""
    response = requests.delete(f"{BASE_URL}/v1/instances/{instance_id}")
    return json.dumps(response.json(), indent=2)

Create the agent and crew

main.py
from crewai import Agent, Task, Crew
from tools import spawn_server, get_pricing, get_instance, destroy_server

# Create an agent with MachineMarket tools
infra_agent = Agent(
    role="Infrastructure Manager",
    goal="Provision and manage VPS servers using MachineMarket",
    backstory="You are an AI agent that manages cloud infrastructure. "
    "You can provision servers by paying with USDC on Base.",
    tools=[spawn_server, get_pricing, get_instance, destroy_server],
    verbose=True,
)

# Define a task
provision_task = Task(
    description=(
        "Check pricing for a Small tier Node.js server for 24 hours. "
        "Then provision it using tx_hash 0xabc...def "
        "from wallet 0x1234...5678."
    ),
    expected_output="Instance ID, IP address, and SSH credentials",
    agent=infra_agent,
)

# Run the crew
crew = Crew(
    agents=[infra_agent],
    tasks=[provision_task],
    verbose=True,
)

result = crew.kickoff()
print(result)
iNote
CrewAI tools use raw HTTP calls instead of the TypeScript SDK. The API is the same — just JSON over HTTP.

Next steps