main.go
Overview
The main.go file serves as the entry point of an application that creates and launches a weather information agent using large language model (LLM) technology. The file primarily implements:
Creation of an LLM-based weather agent that can answer questions about city time and weather.
An HTTP server exposing this agent through the Agent-To-Agent (A2A) protocol.
A launcher which runs a remote proxy client to communicate with the weather agent server.
This file integrates multiple core components including the LLM agent configuration, A2A server setup, remote agent communication, and command-line launcher execution.
Detailed Explanation
Function: newWeatherAgent
func newWeatherAgent(ctx context.Context) agent.Agent
Purpose: Creates and configures a simple LLM agent specialized for answering questions about weather and time in cities.
Parameters:
ctx context.Context: The context for managing lifecycle and cancellation of the agent creation.
Returns: An initialized
agent.Agentinstance.Usage:
Instantiates a Gemini LLM model (
gemini-2.5-flash) using an API key from environment variables.Configures an
llmagent.Agentwith:Name:
"weather_time_agent"Description and instruction strings outlining its capability.
Tools: Includes a Google Search tool (
geminitool.GoogleSearch{}) to enhance information retrieval.
Implementation Details:
Uses Gemini model from the
geminipackage with Google GenAI client config.Wraps the model in an LLM agent (
llmagent.New) with custom instructions and tool integration.Logs fatal errors if model or agent creation fails, ensuring application does not proceed without a valid agent.
Function: startWeatherAgentServer
func startWeatherAgentServer() string
Purpose: Launches an HTTP server that exposes the weather agent over the A2A protocol.
Parameters: None.
Returns: The base URL string where the server is listening (e.g.,
"http://127.0.0.1:<port>").Usage:
Opens a TCP listener on
127.0.0.1with a dynamically assigned port (:0).Constructs a base URL using the listener's address.
Starts a goroutine to run the HTTP server.
Implementation Details:
Creates the weather agent by calling
newWeatherAgent.Defines an agent invocation path
/invoke.Builds an
a2a.AgentCarddescribing the agent's skills, preferred transport (JSON-RPC), URL, and capabilities (supports streaming).Sets up HTTP handlers:
Serves the static agent card at the well-known A2A path.
Handles JSON-RPC requests routed to
/invoke, using anExecutorthat runs the agent with an in-memory session.
Runs the HTTP server with the configured multiplexer.
Logs server start and stop events.
Concurrency: Runs the server asynchronously in a goroutine to avoid blocking the main thread.
Function: main
func main()
Purpose: Application entry point that initializes and runs the weather agent server and remote agent launcher.
Process:
Creates background context.
Calls
startWeatherAgentServerto launch the A2A HTTP server and retrieves its address.Creates a remote agent instance configured to communicate with the weather agent server via A2A.
Configures a launcher with the remote agent as its single agent loader.
Executes the launcher with command-line arguments.
Error Handling: Fatal logs on failure to create the remote agent or run the launcher.
Interaction with other components:
remoteagent.NewA2Acreates a remote proxy agent communicating over HTTP.launcher.Configandfull.NewLauncherhandle command-line invocation and agent lifecycle.
Effect: Provides a CLI that connects to the local weather agent server and routes commands/input to it.
Important Implementation Details and Algorithms
The weather agent uses a Gemini LLM model configured with Google GenAI credentials.
Integration of a Google Search tool allows the agent to retrieve fresh information beyond the LLM's training data.
The agent is exposed over the A2A protocol, which enables inter-agent communication via JSON-RPC over HTTP.
The server setup uses Go's standard HTTP server with custom routing for agent card and JSON-RPC handling.
The session service used is in-memory, meaning all session data is transient.
The launcher component supports command-line execution and provides help syntax on errors.
The agent card exposes metadata about the agent’s capabilities, facilitating discovery and invocation by clients.
Interactions with Other System Components
LLM Model (
gemini.NewModel): Provides the foundational language understanding and generation capabilities.LLM Agent (
llmagent.New): Wraps the LLM model with instructions, tools, and agent lifecycle management.Tools (
geminitool.GoogleSearch): Extends agent functionality with external search capability.A2A Protocol (
a2a,a2asrv,adka2a): Manages standardized agent communication and invocation over HTTP/JSON-RPC.Remote Agent (
remoteagent.NewA2A): Acts as a client proxy to the weather agent server for remote execution.Launcher (
launcher,full): Provides command-line interface and execution environment for running agents.Session (
session.InMemoryService): Maintains in-memory session state during agent execution.
This file primarily acts as a bridge connecting LLM agent creation, A2A server setup, and command-line launching of remote agent clients.
Usage Example
To run the weather agent server and invoke it via the CLI launcher:
GOOGLE_API_KEY=<your_api_key> go run main.go
This will:
Start the weather agent HTTP server (listening on localhost with random port).
Create a remote agent proxy connected to that server.
Launch the CLI launcher allowing commands to be sent to the weather agent.
Mermaid Diagram: File Structure and Workflow
flowchart TD
subgraph Agent Creation
A1[newWeatherAgent]
A2[Gemini Model Init]
A3[LLMAgent Init]
A2 --> A3
A3 --> A1
end
subgraph A2A Server Setup
S1[startWeatherAgentServer]
S2[TCP Listener]
S3[AgentCard Creation]
S4[HTTP Mux Setup]
S5[JSONRPC Handler]
S6[Launch HTTP Server]
S1 --> S2 --> S3 --> S4 --> S5 --> S6
A1 --> S3
end
subgraph Main Execution
M1["main()"]
M2[startWeatherAgentServer]
M3[remoteagent.NewA2A]
M4[Launcher Config]
M5[Launcher Execute]
M1 --> M2 --> M3 --> M4 --> M5
S1 --> M2
end
Summary of Key Elements
Element | Description |
|---|---|
Creates an LLM agent using Gemini model and Google Search tool for weather/time Q&A. | |
| Starts an HTTP A2A server exposing the weather agent via JSON-RPC and serving agent card metadata. |
| Launches the weather agent server, creates a remote A2A agent client, and runs the CLI launcher. |
Gemini Model | LLM model used ( |
Google Search Tool | Tool enabling agent to perform web searches for real-time info. |
A2A Protocol | Agent-To-Agent communication protocol handling remote invocation via HTTP/JSON-RPC. |
Launcher | CLI launcher interface supporting agent execution and command parsing. |
For further details on related components like LLM agent configuration, A2A protocol, remote agent communication, and launcher usage, see the following topics: