main.go
Overview
The main.go file demonstrates the integration of Model Context Protocol (MCP) tools with an Agent Development Kit (ADK) agent. It provides two modes to run MCP tools:
Local in-memory MCP server mode: Defines and registers a Go function (
GetWeather) as an MCP tool served by an in-memory MCP server.GitHub remote MCP server mode: Connects to GitHub’s remote MCP server using an OAuth2-authenticated HTTP client.
The program creates a Large Language Model (LLM) agent configured with the MCP toolset, then launches the agent for interactive use. The main responsibilities covered include setting up MCP transports, creating and configuring the LLM model and agent, and handling graceful shutdown on interrupt signals.
Package and Imports
mainpackage defines the executable entry point.Imports include MCP SDK, ADK agent tooling, Gemini LLM model, OAuth2 for authentication, and standard Go libraries for context, logging, OS signal handling, and formatting.
Types
Input
type Input struct {
City string `json:"city" jsonschema:"city name"`
}
Represents the input schema for the
GetWeatherMCP tool.Field:
City— the name of the city to query weather for.Tags enable JSON marshaling and JSON Schema generation for tool interface definition.
Output
type Output struct {
WeatherSummary string `json:"weather_summary" jsonschema:"weather summary in the given city"`
}
Represents the output schema for the
GetWeatherMCP tool.Field:
WeatherSummary— a textual summary of the weather.Also supports JSON marshaling and JSON Schema generation.
Functions
GetWeather
func GetWeather(ctx context.Context, req *mcp.CallToolRequest, input Input) (*mcp.CallToolResult, Output, error)
Purpose: Implements the MCP tool logic that returns weather information for a given city.
Parameters:
ctx context.Context: Standard Go context for cancellation and deadlines.req *mcp.CallToolRequest: The MCP request metadata.input Input: The deserialized input parameters for the tool.
Returns:
*mcp.CallToolResult: Currentlynilas no complex result metadata is returned.Output: The weather summary result.error: Standard error if any occurs (alwaysnilhere).
Behavior: Returns a fixed sunny weather summary string incorporating the city name.
Usage: Registered as an MCP tool function in the local MCP server.
localMCPTransport
func localMCPTransport(ctx context.Context) mcp.Transport
Purpose: Creates an in-memory MCP transport that runs a local MCP server serving the
GetWeathertool.Parameters:
ctx context.Context: Context for managing server lifecycle.
Returns:
mcp.Transportusable by clients to call the in-memory MCP server.Implementation details:
Calls
mcp.NewInMemoryTransports()to create paired client and server transports.Creates an MCP server instance with versioning and name.
Registers the
get_weathertool backed byGetWeatherfunction.Connects the server to the server transport and logs fatal on error.
Returns the client transport for use by MCP clients.
Important: Runs server synchronously and blocks on error; suitable for demos or tests.
githubMCPTransport
func githubMCPTransport(ctx context.Context) mcp.Transport
Purpose: Creates an MCP transport that connects to GitHub’s remote MCP server.
Parameters:
ctx context.Context: Context for HTTP client lifecycle.
Returns:
mcp.Transportconfigured with OAuth2 authenticated HTTP client.Implementation details:
Obtains GitHub Personal Access Token (PAT) from environment variable
GITHUB_PAT.Uses
oauth2.StaticTokenSourceto create a token source.Wraps HTTP client with OAuth2 transport.
Returns an
mcp.StreamableClientTransportpointed at GitHub MCP endpoint.
Usage: Enables MCP client calls to GitHub-hosted MCP server.
main
func main()
Purpose: Entry point that initializes and runs the MCP-enabled LLM agent.
Key steps:
Creates a cancellable context that listens for OS interrupt signals (Ctrl+C).
Instantiates a Gemini LLM model (
gemini-2.5-flash) using Google API key fromGOOGLE_API_KEYenvironment variable.Determines MCP transport mode based on
AGENT_MODEenvironment variable ("github"or default"local").Creates an MCP toolset with the selected transport.
Creates an LLM agent (
llmagent) named"helper_agent"configured with:The Gemini model.
A description and instruction to behave as a helpful assistant.
The MCP toolset as its toolset.
Configures an ADK launcher with the single agent loader.
Executes the launcher passing in command-line arguments.
Logs fatal errors if any step fails.
Usage: Runs an interactive or scripted agent session that can invoke MCP tools either locally or remotely.
Integration Points:
Uses
gemini.NewModelfor LLM integration (80562 - LLM Integration and Agents).Uses
mcptoolset.Newto create MCP tool adapters (80577 - MCP Toolset Integration).Uses
llmagent.Newto create the language model agent (80574 - LLM Agent Configuration).Uses
launcherandfull.Launcherfor command line agent launch (80564 - REST API and Web Launchers).
Implementation Details and Algorithms
MCP Tool Registration: The in-memory MCP server registers tools by associating tool metadata with Go functions implementing the tool logic. The server uses MCP protocol to route tool calls.
Transport Selection: Runtime selects between a local in-memory MCP server and GitHub remote MCP server based on environment variables. This showcases flexibility in deployment.
Agent Composition: The LLM agent is composed with a toolset wrapping MCP tools, enabling the agent to call external tools exposed via MCP.
Signal Handling: Uses
signal.NotifyContextfor graceful shutdown on Ctrl+C, allowing cleanup and orderly termination during agent execution.Error Handling: Critical setup errors cause immediate termination with log messages for troubleshooting.
Interactions with Other System Components
Model (Gemini): Uses the Gemini LLM model from
google.golang.org/genaifor natural language understanding and generation.MCP SDK: Utilizes
github.com/modelcontextprotocol/go-sdk/mcpfor defining and communicating with MCP tools and servers.Agent Development Kit (ADK): Integrates with ADK components (
agent,llmagent,tool,launcher) for implementing and launching AI agents.OAuth2: Uses
golang.org/x/oauth2for authenticating HTTP clients to GitHub MCP server.Environment Variables:
GOOGLE_API_KEYfor Gemini model authentication.GITHUB_PATfor GitHub personal access token.AGENT_MODEto select MCP transport mode (localorgithub).
Usage Example
Run locally with in-memory MCP server (default):
export GOOGLE_API_KEY="your-google-api-key"
go run main.go
Run connecting to GitHub MCP server:
export GOOGLE_API_KEY="your-google-api-key"
export GITHUB_PAT="your-github-pat"
export AGENT_MODE="github"
go run main.go
Agent will start and accept commands, using the get_weather tool implemented either locally or remotely.
Diagram: File Structure and Function Relationships
flowchart TD
A[main] --> B[Gemini Model Init]
A --> C[Select MCP Transport]
C -->|AGENT_MODE=local| D[localMCPTransport]
C -->|AGENT_MODE=github| E[githubMCPTransport]
D --> F[In-memory MCP Server]
F --> G[Register GetWeather Tool]
E --> H[GitHub MCP Remote Server]
A --> I[Create MCP Toolset]
I --> J[mcptoolset.New]
A --> K[Create llmagent]
K --> L[Use Gemini Model]
K --> I
A --> M[Launcher Execute]
This documentation describes the purpose, architecture, and detailed function of the main.go file, including how it implements MCP tool integration with an LLM agent and supports different MCP transport modes. The provided diagram illustrates the key initialization and composition steps of the program.