ADK Go 入門 04 - 讓 Agent 擁有工具能力(Go)

Agent 光靠 LLM 只能產生文字,透過 Tool 才能真正執行動作:查資料庫、呼叫 API、計算數值。本篇說明如何在 Go 中定義工具並掛載到 Agent 上。


Tool vs Toolset

概念 說明 適用情境
tool.Tool 單一可呼叫函式 自訂邏輯、內建工具(如 GoogleSearch)
tool.Toolset 多個 Tool 的集合 外部系統(MCP 伺服器、API 組)

自訂 Tool 範例

ADK Go SDK 使用 functiontool.New 建立工具,透過泛型自動推斷 schema,handler 接收強型別的 struct 參數。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import (
"google.golang.org/adk/tool"
"google.golang.org/adk/tool/functiontool"
)

// 定義輸入與輸出型別(schema 自動從 struct 推斷)
type WeatherArgs struct {
City string `json:"city" jsonschema:"description=城市名稱"`
}

type WeatherResult struct {
Weather string `json:"weather"`
City string `json:"city"`
}

// 建立工具(err 須處理)
weatherTool, err := functiontool.New[WeatherArgs, WeatherResult](
functiontool.Config{
Name: "get_weather",
Description: "查詢指定城市的天氣",
},
func(ctx tool.Context, args WeatherArgs) (WeatherResult, error) {
// 實際查詢邏輯...
return WeatherResult{Weather: "晴天", City: args.City}, nil
},
)

將工具掛載到 Agent:

1
2
3
4
5
myAgent, _ := llmagent.New(llmagent.Config{
Name: "weather_agent",
Model: llm,
Tools: []tool.Tool{weatherTool},
})

工具白名單過濾(FilterToolset)

當 Toolset 提供數十個工具時,某個 Agent 可能只需要其中幾個。使用 tool.FilterToolset 限制範圍:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 假設 fullToolset 來自 MCP,包含 50 個工具
// 只讓這個 Agent 看到 3 個
filteredToolset := tool.FilterToolset(
fullToolset,
tool.StringPredicate([]string{
"get_product",
"list_products",
"update_product",
}),
)

productAgent, _ := llmagent.New(llmagent.Config{
Name: "product_agent",
Model: llm,
Toolsets: []tool.Toolset{filteredToolset},
})

⚠️ 最小權限原則:每個 Agent 只給它實際需要的工具。工具過多會讓 LLM 選錯工具,也增加安全風險。

Sub-Agent 作為 Tool

Agent 可以把另一個 Agent 包裝成 Tool,讓被呼叫的 Agent 以工具身份執行後回傳結果(而非完全移交控制權):

1
2
3
4
5
6
7
8
9
10
import "google.golang.org/adk/tool/agenttool"

// visionAgent 是另一個已建立的 Agent
visionTool := agenttool.New(visionAgent, nil)

parentAgent, _ := llmagent.New(llmagent.Config{
Name: "parent_agent",
Model: llm,
Tools: []tool.Tool{visionTool},
})

這和 transfer_to_agent(完全移交)不同,適合「呼叫後取得結果,繼續由自己處理」的場景。