工具概述

代理框架支持许多不同类型的工具,用于扩展代理功能。 工具允许代理与外部系统交互、执行代码、搜索数据等。

工具类型

工具类型 说明
函数工具 代理可以在对话期间调用的自定义代码
代码解释器 在沙盒环境中执行代码
文件搜索 搜索上传的文件
Web 搜索 在 Web 上搜索信息
托管 MCP 工具 提供程序运行时调用的 MCP 服务器
本地 MCP 工具 在本地或自定义主机上运行的 MCP 服务器
Microsoft Foundry 工具箱 在 Foundry 项目中管理的已命名、带版本的托管工具配置包
Shell 工具 使用环境探测和策略控制执行本地和容器化 shell
工具类型 说明
函数工具 代理可以在对话期间调用的自定义代码
代码解释器 在沙盒环境中执行代码
文件搜索 搜索上传的文件
Web 搜索 在 Web 上搜索信息
托管 MCP 工具 提供程序运行时调用的 MCP 服务器
本地 MCP 工具 在本地或自定义主机上运行的 MCP 服务器
Microsoft Foundry 工具箱 在 Foundry 项目中管理的已命名、带版本的托管工具配置包
Shell 工具 使用环境探测和策略控制执行本地和容器化 shell
图像生成 Foundry/OpenAI 响应运行时上的托管映像生成
Shell OpenAI 响应运行时上的托管 shell 执行 - 不同于 GitHub Copilot CLI 的内置 shell/文件/URL 运行时工具
必应Grounding 使用必应搜索资源通过自己的地面进行 Web 地面 - 实验性
必应自定义搜索 必应限制为特选域列表 - 预览
Azure AI 搜索 通过 Foundry 连接查询Azure AI 搜索索引 - 实验性
SharePoint SharePoint内容中的基本答案 — 预览
Microsoft Fabric 查询Fabric数据代理 — 预览版
内存搜索 搜索 Foundry 托管的内存存储 - 预览版
计算机应用 驱动桌面或浏览器环境 — 预览版
浏览器自动化 通过 playwright Azure 驱动浏览器 — 预览版
代理对代理(A2A)工具 从 Foundry 代理调用远程 A2A 代理作为工具 — 预览版

注释

标记为 实验 性或 预览的工具 记录在相关提供程序页面上,并在过程中首次发出 ExperimentalWarning 它们。

工具类型 Package 说明
函数工具 tool/functool 具有代理可以调用的 JSON 架构的类型化 Go 函数
代理即函数工具 tool/agenttool 将代理包装为一个 tool.FuncTool 代理,以便另一个代理可以调用它
本地 MCP 工具 tool/mcptool 连接到 MCP 服务器并将其工具公开为 tool.FuncTool 值
Web 搜索 tool/hostedtool.WebSearch 当支持服务支持它时声明提供程序端 Web 搜索
文件搜索 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的工具,例如本地 shell 工具。 审批请求和响应通过工具自动调用中间件流动,因此它们与返回本地函数调用的提供程序一起工作。

提供程序支持矩阵

OpenAI 和 Azure OpenAI 提供程序分别提供两种客户端类型(响应和聊天完成),具有不同的工具功能。 Azure OpenAI 客户端镜像其 OpenAI 等效项。 Copilot Studio 和 A2A 代理在远程服务上运行,以便其功能在远程代理上配置,而不是通过 Agent Framework 客户端进行配置, 它们不会列在矩阵中。

工具类型 反应 聊天完成 代工厂 Anthropic Ollama GitHub Copilot
函数工具 ✅ ✅ ✅ ✅ ✅ ✅
代码解释器 ✅ ❌ ✅ ❌ ❌ ❌
文件搜索 ✅ ❌ ✅ ❌ ❌ ❌
Web 搜索 ✅ ✅ ❌ ❌ ❌ ❌
托管 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 代理在远程服务上运行,以便其功能在远程代理上配置,而不是通过 Agent Framework 客户端进行配置, 它们不会列在矩阵中。

工具类型 反应 聊天完成 代工厂 Anthropic Ollama Foundry Local GitHub Copilot
函数工具 ✅ ✅ ✅ ✅ ⚠️¹ ⚠️¹ ✅
代码解释器 ✅ ❌ ✅ ✅ ❌ ❌ ❌
文件搜索 ✅ ❌ ✅ ❌ ❌ ❌ ❌
Web 搜索 ✅ ✅ ✅ ✅ ❌ ❌ ❌
图像生成 ✅ ❌ ✅ ❌ ❌ ❌ ❌
托管 Shell (get_shell_tool) ✅ ❌ ❌ ❌ ❌ ❌ ❌
内置 shell/文件系统/URL 提取 ❌ ❌ ❌ ❌ ❌ ❌ ✅²
托管 MCP 工具 ✅ ❌ ✅ ✅ ❌ ❌ ✅
本地 MCP 工具 ✅ ✅ ✅ ✅ ✅ ✅ ✅
Microsoft Foundry 工具箱 ❌ ❌ ✅ ❌ ❌ ❌ ❌
必应地面 (实验) ❌ ❌ ✅ ❌ ❌ ❌ ❌
必应自定义搜索 (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌
Azure AI 搜索 (实验) ❌ ❌ ✅ ❌ ❌ ❌ ❌
SharePoint (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌
Microsoft Fabric (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌
内存搜索 (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌
计算机使用 (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌
浏览器自动化 (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌
代理到代理 (A2A) 工具 (预览版) ❌ ❌ ✅ ❌ ❌ ❌ ❌

¹ 取决于所选的本地模型支持函数调用。 ー 内置于 GitHub Copilot CLI 运行时中,由权限处理程序封闭。 与 OpenAI 不同的 get_shell_tool图面。

注释

Responses 和 Chat Completion 列同时适用于 OpenAI 和 Azure OpenAI ,Azure变体与 OpenAI 对应项镜像相同的工具支持。 本地 MCP 工具适用于支持函数工具的任何提供程序。

提供程序支持矩阵

Go SDK 通过 foundryprovider OpenAI 和 OpenAI/Azure OpenAI openaiprovider公开Microsoft Foundry。 托管工具是 tool/hostedtool 声明:Go SDK 将其发送到提供程序,提供程序决定该托管功能是否可用。

工具类型 代工厂 反应 聊天完成 Anthropic
函数工具 ✅ ✅ ✅ ✅
代理即函数工具 ✅ ✅ ✅ ✅
本地 MCP 工具 ✅ ✅ ✅ ✅
Web 搜索 ✅ ✅ ✅ ❌
文件搜索 ❌ ✅ ❌ ❌
代码解释器 ✅ ✅ ❌ ❌
托管 MCP 工具 ❌ ✅ ❌ ❌
本地壳层工具 ✅ ✅ ✅ ✅

注释

从提供程序的角度来看,本地 MCP 工具和本地 shell 工具是函数工具,因此它们遵循函数工具支持。 托管工具(如 hostedtool.FileSearchAI hostedtool.CodeInterpreterhostedtool.MCPServer 服务)执行,而不是由 Go 进程执行。

使用代理作为函数工具

可以将代理用作另一个代理的函数工具,从而启用代理组合和更高级的工作流。 内部代理将转换为函数工具,并提供给外部代理,然后可以根据需要调用它。

.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 工具示例 。

后续步骤