工具概述

代理框架支援多種不同類型的工具,以擴充代理能力。 工具讓代理能與外部系統互動、執行程式碼、搜尋資料等。

Warning

工具呼叫與參數由模型產生。 在應用程式代碼產生副作用前,先驗證並授權它們。 高風險工具使用批准與最低權限執行。

工具類型

工具類型 說明
函式工具 客服人員在通話中可呼叫的自訂程式碼
程式碼解譯器 在沙盒環境中執行程式碼
檔案搜尋 搜尋上傳的檔案
網路搜尋 請上網搜尋相關資訊
託管 MCP 工具 由提供者執行時呼叫的 MCP 伺服器
本地 MCP 工具 本地運行或自訂主機上的 MCP 伺服器
Microsoft Foundry 工具箱 在 Foundry 專案中管理的已命名且有版本的代管工具配置套件
殼體工具 本地與容器化殼執行,具備環境探測與政策控制
工具類型 說明
函式工具 客服人員在通話中可呼叫的自訂程式碼
程式碼解譯器 在沙盒環境中執行程式碼
檔案搜尋 搜尋上傳的檔案
網路搜尋 請上網搜尋相關資訊
託管 MCP 工具 由提供者執行時呼叫的 MCP 伺服器
本地 MCP 工具 本地運行或自訂主機上的 MCP 伺服器
Microsoft Foundry 工具箱 在 Foundry 專案中管理的已命名且有版本的代管工具配置套件
殼體工具 本地與容器化殼執行,具備環境探測與政策控制
影像生成 Foundry / OpenAI Responses 執行時的託管影像生成
原生電腦應用 執行提供者請求的桌面或瀏覽器動作並回傳截圖
Shell OpenAI Responses 執行環境下的託管殼執行 — 與 GitHub Copilot CLI 內建的 shell/file/URL 執行工具不同
Bing 基礎 透過你自己的 Bing 搜尋資源進行網路接地——實驗性
必應自訂搜尋 Bing 限制在精選網域清單中 — 預覽
Azure AI 搜尋服務 透過 Foundry 連線查詢 Azure AI 搜尋服務 索引 — 實驗性
SharePoint SharePoint 內容中的地面答案 — 預覽
Microsoft Fabric 查詢 Fabric 資料代理 — 預覽
記憶搜尋 搜尋 Foundry 管理的記憶體儲存 — 預覽
電腦使用預覽 透過獨立的 Foundry 預覽 API 來驅動桌面或瀏覽器環境
瀏覽器自動化 透過 Azure Playwright Drive a browser — preview
代理對代理(A2A)工具 以 Foundry 代理工具的方式呼叫遠端 A2A 代理 — 預覽

備註

標示 為實驗性 或 預覽 的工具會在相關提供者頁面上記錄,並於首次使用時發布 ExperimentalWarning 。

工具類型 包裝 說明
函式工具 tool/functool 帶有 JSON 架構的型別 Go 函式,代理可呼叫
代理作為功能工具 tool/agenttool 將代理包裝成 a tool.FuncTool ,讓另一個代理可以呼叫它
本地 MCP 工具 tool/mcptool 連接 MCP 伺服器,將他們的工具暴露為 tool.FuncTool 價值
網路搜尋 tool/hostedtool.WebSearch 當支援服務支援時,宣告服務端網頁搜尋
檔案搜尋 tool/hostedtool.FileSearch 宣告提供者端檔案或向量存儲搜尋
程式碼解譯器 tool/hostedtool.CodeInterpreter 宣告提供者端程式碼執行
託管 MCP 工具 tool/hostedtool.MCPServer 宣告一個 MCP 伺服器讓提供者執行時呼叫
本地殼層工具 tool/shelltool 透過一個預設需要核准的函式工具執行本地 shell 指令

所有工具都實作以下 tool.Tool 介面:

type Tool interface {
    Name() string
    Description() string
}

功能工具還能實現 tool.FuncTool:

import "context"

type FuncTool interface {
    Tool
    Schema() any
    ReturnSchema() any
    Call(ctx context.Context, arguments string) (any, error)
}

大多數應用程式會與功能工具一同 functool.New 或 functool.MustNew 直接實作 FuncTool 。 框架使用 Go 函式簽名或結構標籤來建立暴露給模型的結構結構。

透過以下方式 agent.Config.Tools將工具傳給代理人:

a := foundryprovider.NewAgent(endpoint, token, foundryprovider.ModelDeployment(model), foundryprovider.AgentConfig{
    Instructions: "You are a helpful assistant.",
    Config: agent.Config{
        Tools: []tool.Tool{weatherTool, calculatorTool},
    },
})

或者每次遊戲新增工具:

resp, err := a.RunText(ctx, "What's the weather?", agent.WithTool(weatherTool)).Collect()

工具核准

工具審核 是一項框架功能,讓你能在模型收到結果前,透過人工決策來閘控工具調用。 它與客戶在本地調用工具的供應商合作;服務端託管工具會遵循提供者自身的審核行為。 完整模式請參閱 工具審核 頁面,包括審核如何與會話、串流及中介軟體互動。

對於 Go 來說,可以用tool.ApprovalRequiredFunc已實作的工具標記可調用工具tool.ApprovalRequiredTool,例如本地殼層工具。 核准請求與回應會透過自動呼叫中介軟體流動,因此它們會與回傳本地函式呼叫的提供者合作。

提供者支援矩陣

OpenAI 與 Azure OpenAI 供應商各自提供兩種客戶端類型——回應與聊天完成,並具備不同工具功能。 Azure OpenAI 用戶端會鏡像其 OpenAI 對應版本。 Copilot Studio 和 A2A 代理程式在遠端服務上運行,因此其能力是在遠端代理程式上設定,而非透過代理框架用戶端——它們不會被列在矩陣中。

工具類型 回應 聊天完成 鑄造廠 人為 Ollama GitHub Copilot
函式工具 ✅ ✅ ✅ ✅ ✅ ✅
程式碼解譯器 ✅ ❌ ✅ ❌ ❌ ❌
檔案搜尋 ✅ ❌ ✅ ❌ ❌ ❌
網路搜尋 ✅ ✅ ❌ ❌ ❌ ❌
託管 MCP 工具 ✅ ❌ ✅ ✅ ❌ ✅
本地 MCP 工具 ✅ ✅ ✅ ✅ ✅ ✅

備註

Responses 和 Chat Completion欄位同時適用於 OpenAI 與 Azure OpenAI——Azure 版本在工具支援上與 OpenAI 版本相同。 已棄用的 OpenAI Assistants API 已不再被官方文件記錄;關於遷移指引,請參見 語意核心遷移指南。

提供者支援矩陣

OpenAI 與 Azure OpenAI 供應商各自提供多種客戶端類型及不同工具功能。 Azure OpenAI 用戶端會鏡像其 OpenAI 對應版本。 Foundry 欄位適用於 FoundryChatClient —— 對於 FoundryAgent,工具會根據 Foundry 代理定義設定(參見「 哪些可行,哪些無效」) FoundryAgent。 Copilot Studio 和 A2A 代理程式在遠端服務上運行,因此其能力是在遠端代理程式上設定,而非透過代理框架用戶端——它們不會被列在矩陣中。

工具類型 回應 聊天完成 鑄造廠 人為 Ollama 鑄造廠地方分會 GitHub Copilot
函式工具 ✅ ✅ ✅ ✅ ⚠️¹ ⚠️¹ ✅
程式碼解譯器 ✅ ❌ ✅ ✅ ❌ ❌ ❌
檔案搜尋 ✅ ❌ ✅ ❌ ❌ ❌ ❌
網路搜尋 ✅ ✅ ✅ ✅ ❌ ❌ ❌
影像生成 ✅ ❌ ✅ ❌ ❌ ❌ ❌
原生電腦應用 ✅ ❌ ✅ ❌ ❌ ❌ ❌
託管殼牌(get_shell_tool) ✅ ❌ ❌ ❌ ❌ ❌ ❌
內建 shell / 檔案系統 / URL 擷取 ❌ ❌ ❌ ❌ ❌ ❌ ✅²
託管 MCP 工具 ✅ ❌ ✅ ✅ ❌ ❌ ✅
本地 MCP 工具 ✅ ✅ ✅ ✅ ✅ ✅ ✅
Microsoft Foundry 工具箱 ❌ ❌ ✅ ❌ ❌ ❌ ❌
Bing 接地 (實驗性) ❌ ❌ ✅ ❌ ❌ ❌ ❌
Bing 自訂搜尋 (預覽) ❌ ❌ ✅ ❌ ❌ ❌ ❌
Azure AI 搜尋服務(實驗性) ❌ ❌ ✅ ❌ ❌ ❌ ❌
SharePoint(預覽) ❌ ❌ ✅ ❌ ❌ ❌ ❌
Microsoft Fabric(預覽) ❌ ❌ ✅ ❌ ❌ ❌ ❌
記憶搜尋 (預覽) ❌ ❌ ✅ ❌ ❌ ❌ ❌
電腦使用預覽 ❌ ❌ ✅ ❌ ❌ ❌ ❌
瀏覽器自動化 (預覽) ❌ ❌ ✅ ❌ ❌ ❌ ❌
代理對代理(A2A)工具 (預覽) ❌ ❌ ✅ ❌ ❌ ❌ ❌

¹ 取決於所選的本地模型支援函式呼叫。 ² 內建於 GitHub Copilot CLI 執行環境,並由權限處理器限制。 與 OpenAI get_shell_tool的 .

備註

Responses 和 Chat Completion欄位同時適用於 OpenAI 與 Azure OpenAI——Azure 版本在工具支援上與 OpenAI 版本相同。 本地 MCP 工具可與任何支援函式工具的供應商合作。

提供者支援矩陣

Go SDK 透過 、 、 Anthropic 和 Gemini geminiproviderMicrosoft Foundry foundryproviderAzure OpenAI openaiprovider。anthropicprovider 託管 tool/hostedtool 工具是宣告:Go SDK 會將宣告發送給供應商,提供者決定該託管能力是否可用。

工具類型 鑄造廠 回應 聊天完成 人為 雙子座
函式工具 ✅ ✅ ✅ ✅ ✅
代理作為功能工具 ✅ ✅ ✅ ✅ ✅
本地 MCP 工具 ✅ ✅ ✅ ✅ ✅
網路搜尋 ✅ ✅ ✅ ❌ ✅
檔案搜尋 ✅ ✅ ❌ ❌ ✅
程式碼解譯器 ✅ ✅ ❌ ❌ ✅
託管 MCP 工具 ✅ ✅ ❌ ❌ ❌
本地殼層工具 ✅ ✅ ✅ ✅ ✅

備註

本地 MCP 工具與本地 shell 工具是從提供者角度來看的函式工具,因此遵循函式工具支援。 託管工具 hostedtool.FileSearch如 、 hostedtool.CodeInterpreter、 hostedtool.MCPServer 是由 AI 服務執行,而非由 Go 流程執行。

託管工具的可用性也取決於所選模型、部署與 API。 對於 Gemini 檔案搜尋,請提供 Gemini 檔案搜尋的儲存名稱,而非 OpenAI 向量儲存 ID。

使用代理作為函數工具

你可以將一個代理當作另一個代理的功能工具,實現代理組合和更進階的工作流程。 內部代理會被轉換成功能工具,並提供給外部代理,外部代理可視需要呼叫。

呼叫 .AsAIFunction() , AIAgent 將其轉換為可供其他代理使用的函式工具:

// Create the inner agent with its own tools
AIAgent weatherAgent = new AIProjectClient(
    new Uri("<your-foundry-project-endpoint>"),
    new DefaultAzureCredential())
     .AsAIAgent(
        model: "gpt-4o-mini",
        instructions: "You answer questions about the weather.",
        name: "WeatherAgent",
        description: "An agent that answers questions about the weather.",
        tools: [AIFunctionFactory.Create(GetWeather)]);

// Create the main agent and provide the inner agent as a function tool
AIAgent agent = new AIProjectClient(
    new Uri("<your-foundry-project-endpoint>"),
    new DefaultAzureCredential())
     .AsAIAgent(
        model: "gpt-4o-mini",
        instructions: "You are a helpful assistant.",
        tools: [weatherAgent.AsAIFunction()]);

// The main agent can now call the weather agent as a tool
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));

Warning

DefaultAzureCredential 開發方便,但在生產過程中需謹慎考量。 在生產環境中,建議使用特定的憑證(例如 ManagedIdentityCredential),以避免延遲問題、意外的憑證探測,以及備援機制帶來的安全風險。

呼叫 .as_tool() 代理將其轉換成功能工具,並提供給另一代理:

import os
from agent_framework.openai import OpenAIChatCompletionClient
from azure.identity import AzureCliCredential

# Create the inner agent with its own tools
weather_agent = OpenAIChatCompletionClient(
    model=os.environ["AZURE_OPENAI_CHAT_COMPLETION_MODEL"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
    credential=AzureCliCredential(),
).as_agent(
    name="WeatherAgent",
    description="An agent that answers questions about the weather.",
    instructions="You answer questions about the weather.",
    tools=get_weather
)

# Create the main agent and provide the inner agent as a function tool
main_agent = OpenAIChatCompletionClient(
    model=os.environ["AZURE_OPENAI_CHAT_COMPLETION_MODEL"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
    credential=AzureCliCredential(),
).as_agent(
    instructions="You are a helpful assistant.",
    tools=weather_agent.as_tool()
)

# The main agent can now call the weather agent as a tool
result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)

你也可以自訂工具名稱、描述和參數名稱:

weather_tool = weather_agent.as_tool(
    name="WeatherLookup",
    description="Look up weather information for any location",
    arg_name="query",
    arg_description="The weather query or location"
)

將一個代理人 agenttool.New 包裝成 ,使其作為另一個代理人可用 tool.FuncTool :

import (
    "github.com/microsoft/agent-framework-go/agent"
    "github.com/microsoft/agent-framework-go/provider/foundryprovider"
    "github.com/microsoft/agent-framework-go/tool"
    "github.com/microsoft/agent-framework-go/tool/agenttool"
)

weatherAgent := foundryprovider.NewAgent(endpoint, token, foundryprovider.ModelDeployment(model), foundryprovider.AgentConfig{
    Instructions: "You answer questions about the weather.",
    Config: agent.Config{
        Name:        "WeatherAgent",
        Description: "An agent that answers weather questions.",
        Tools:       []tool.Tool{weatherTool},
    },
})

mainAgent := foundryprovider.NewAgent(endpoint, token, foundryprovider.ModelDeployment(model), foundryprovider.AgentConfig{
    Instructions: "You are a helpful assistant.",
    Config: agent.Config{
        Tools: []tool.Tool{agenttool.New(weatherAgent, agenttool.Config{})},
    },
})

resp, err := mainAgent.RunText(ctx, "Should I bring an umbrella to Amsterdam?").Collect()

你也可以透過 MCP mcptool.AddTool暴露同一個包裹的代理,因為 agenttool.New 會回傳一個函式工具。

Tip

完整可執行範例請參考 代理作為函數工具範例 ,代理 作為 MCP 工具範例 。

下一步