build-with-ag2

AG2 Agent Template for Google Cloud

A remote template for agent-starter-pack that creates an AG2 agent deployed on Google Cloud Run with A2A protocol support.

Quick Start

uvx agent-starter-pack create my-agent \
  -a https://github.com/ag2ai/build-with-ag2/tree/main/gcp-agent-starter-pack/templates/ag2

What You Get

Architecture

This template uses custom_a2a as its base_template, which provides the “bring your own framework” A2A infrastructure. AG2’s native A2A support (A2aAgentServer) handles the protocol implementation.

Key Files

File Purpose
app/agent.py AG2 ConversableAgent with tools
app/fast_api_app.py FastAPI + AG2 A2aAgentServer
deployment/ Terraform for Cloud Run (from base template)

Local Development

Prerequisites

Set up a Google Gemini API key for AG2 to use Google Gemini models:

export GOOGLE_GEMINI_API_KEY="your-api-key"

Documentation for using Google Vertex AI with AG2 agents.

Running Locally

After generating your project:

cd my-agent
make install    # Install dependencies
make run        # Run locally on port 8000
make test       # Run tests
make playground # Interactive testing

Or run directly:

uvicorn app.fast_api_app:app --host 0.0.0.0 --port 8000

Customization

Edit app/agent.py to modify your agent:

See AG2 documentation for advanced patterns.

A2A Protocol

The agent is served via the A2A protocol, enabling:

Testing with curl

# Check agent card
curl http://localhost:8000/a2a/app/.well-known/agent-card.json

# Send a message
curl -X POST http://localhost:8000/a2a/app/ \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "message/send",
    "id": "1",
    "params": {
      "message": {
        "messageId": "msg-1",
        "role": "user",
        "parts": [{"kind": "text", "text": "What is the weather in San Francisco?"}]
      }
    }
  }'

Testing with Python A2A Client

import asyncio
from autogen.a2a import A2aRemoteAgent

async def main():
    remote = A2aRemoteAgent(name="client", url="http://localhost:8000")
    response = await remote.a_run(message="What's the weather in SF?")

    # Consume events to populate messages
    async for event in response.events:
        pass

    # Access the response messages
    if response.messages:
        print(response.messages[-1].get("content"))

asyncio.run(main())

Learn More