set.go

Overview

The set.go file implements an MCP (Model Context Protocol) ToolSet adapter that integrates MCP-compliant tools into the ADK agent tooling system. This adapter enables an agent, typically an LLM agent, to dynamically discover, retrieve, and invoke tools hosted on a remote MCP server transparently as native tools within the ADK framework.

The MCP ToolSet connects to an MCP server via an MCP client session, lazily established on first use, fetches the list of available MCP tools, converts them into ADK-compatible tools, and exposes them to the agent. Tool invocation requests are delegated through the MCP session to the MCP server, with results adapted back to the ADK tool output format.

This file is central to the MCP Toolset Integration subtopic and a key component of the broader Tooling System that allows agents to extend their capabilities by incorporating external MCP tools.


Key Components

Config Struct

Config encapsulates the initialization parameters for creating an MCP ToolSet:


New Function

func New(cfg Config) (tool.Toolset, error)

Creates and returns a new MCP ToolSet instance configured with the provided Config. It ensures a client exists (creating a default if needed), attaches the transport and tool filter, and returns a tool.Toolset implementation.

Usage Example

ts, err := mcptoolset.New(mcptoolset.Config{
    Transport: &mcp.CommandTransport{Command: exec.Command("myserver")},
})
if err != nil {
    // handle error
}

agent, err := llmagent.New(llmagent.Config{
    Name:    "my_agent",
    Model:   model,
    Toolsets: []tool.Toolset{ ts },
})

This example shows how to create an MCP ToolSet with a command transport and inject it into an LLM agent configuration.


set Struct

The set struct implements the tool.Toolset interface and encapsulates state and logic for managing an MCP client session and providing MCP tools to the agent.

Fields


Methods of set

Name() string

Returns the name of the toolset, "mcp_tool_set".

Description() string

Returns a human-readable description of the toolset:
"Connects to a MCP Server, retrieves MCP Tools into ADK Tools."

IsLongRunning() bool

Returns false indicating the toolset itself is not considered long-running.


Tools(ctx agent.ReadonlyContext) ([]tool.Tool, error)

Fetches MCP tools from the MCP server, converts them to ADK tool.Tool instances, and applies the optional toolFilter.

Process:

  1. Obtains an MCP client session via getSession.

  2. Uses the MCP session's ListTools API to paginate through all available MCP tools.

  3. Converts each MCP tool to an ADK tool via convertTool (defined elsewhere).

  4. Filters tools using toolFilter if provided.

  5. Accumulates and returns the list of ADK tools.

Returns:


getSession(ctx context.Context) (*mcp.ClientSession, error)

Manages the lifecycle of the MCP client session with concurrency safety.

Behavior:


Important Implementation Details


Interactions with Other System Components


Mermaid Class Diagram

classDiagram
class set {
-client: *mcp.Client
-transport: mcp.Transport
-toolFilter: tool.Predicate
-mu: sync.Mutex
-session: *mcp.ClientSession
+Name() string
+Description() string
+IsLongRunning() bool
+Tools(ctx agent.ReadonlyContext) []tool.Tool
-getSession(ctx context.Context) *mcp.ClientSession
}
class Config {
+Client: *mcp.Client
+Transport: mcp.Transport
+ToolFilter: tool.Predicate
}
set ..> Config : uses

Workflow of Tool Retrieval and Session Management

sequenceDiagram
participant Agent
participant MCP_ToolSet as ToolSet
participant MCP_Session as Session
participant MCP_Server as Server
Agent->>ToolSet: Tools(ctx)
ToolSet->>ToolSet: getSession(ctx)
alt session exists
ToolSet-->>Agent: session
else
ToolSet->>Session: Connect(transport)
Session->>Server: Establish connection
Server-->>Session: Connection established
Session-->>ToolSet: session
ToolSet-->>Agent: session
end
ToolSet->>Session: ListTools(cursor)
Session->>Server: ListTools request with cursor
Server-->>Session: Tools list + NextCursor
Session-->>ToolSet: Tools response
ToolSet->>ToolSet: convertTool each tool
ToolSet-->>Agent: []tool.Tool

This sequence shows:


References to Related Topics


This documentation covers the design, implementation, usage, and integration of the MCP ToolSet adapter implemented in set.go. It provides the necessary understanding to use and extend the MCP ToolSet within the agent tooling system.