Skip to main content
Skip to content

Microsoft代理框架集成

在 Microsoft Agent Framework(MAF)中将 Copilot SDK 用作代理提供程序,并结合 Azure OpenAI、Anthropic 和其他提供程序来构建多代理工作流。

概述

Microsoft Agent Framework 是语义内核和 AutoGen 的统一继任者。 它提供用于生成、协调和部署 AI 代理的标准接口。 借助专用集成包,可以将 Copilot SDK 客户端包装为一流的 MAF 代理,与框架中的其他任何代理提供程序互换。

概念说明
Microsoft代理框架用于 .NET 和 Python 的单代理和多代理编排的开源框架
智能体提供程序为助手(如 Copilot、Azure OpenAI、Anthropic 等)提供支持的后端。
业务流程协调程序****在顺序、并发或切换工作流中协调智能体的 MAF 组件
A2A 协议框架支持的代理到代理通信标准

注意

MAF 集成包可用于 .NET 和 Python。 对于 TypeScript、Go、Java 和 Rust,直接使用 Copilot SDK—标准 SDK API 已经提供工具调用、流式处理和自定义代理。

先决条件

在开始之前,请确保具备:

  • 一个有效的所选语言的 Build your first Copilot-powered app
  • GitHub Copilot订阅(个人、企业或企业)
  • 已安装 Copilot CLI 或可通过 SDK 捆绑的 CLI 使用

安装

同时安装 Copilot SDK 和适用于您的编程语言的 MAF 集成包:

代码语言 navigation

.NET
dotnet add package GitHub.Copilot.SDK
dotnet add package Microsoft.Agents.AI.GitHub.Copilot --prerelease

基本用法

使用单个方法调用将 Copilot SDK 客户端包装为 MAF 代理。 生成的代理符合框架的标准接口,可在预期 MAF 代理的任何位置使用。

代码语言 navigation

.NET
using GitHub.Copilot;
using Microsoft.Agents.AI;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

// Wrap as a MAF agent
AIAgent agent = copilotClient.AsAIAgent();

// Use the standard MAF interface
string response = await agent.RunAsync("Explain how dependency injection works in ASP.NET Core");
Console.WriteLine(response);

添加自定义工具

使用自定义函数工具扩展Copilot 智能体。 当代理在 MAF 中运行时,通过标准Copilot SDK 定义的工具将自动可用。

代码语言 navigation

.NET
using GitHub.Copilot;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;

// Define a custom tool
AIFunction weatherTool = CopilotTool.DefineTool(
    (string location) => $"The weather in {location} is sunny with a high of 25°C.",
    factoryOptions: new AIFunctionFactoryOptions
    {
        Name = "GetWeather",
        Description = "Get the current weather for a given location.",
    }
);

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

// Create agent with tools
AIAgent agent = copilotClient.AsAIAgent(new AIAgentOptions
{
    Tools = new[] { weatherTool },
});

string response = await agent.RunAsync("What's the weather like in Seattle?");
Console.WriteLine(response);

还可以将 Copilot SDK 的本机工具定义与 MAF 工具一起使用:

代码语言 navigation

TypeScript
import { CopilotClient, defineTool } from "@github/copilot-sdk";

const getWeather = defineTool("GetWeather", {
    description: "Get the current weather for a given location.",
    parameters: {
        type: "object",
        properties: {
            location: { type: "string", description: "City name" },
        },
        required: ["location"],
    },
    handler: async ({ location }: { location: string }) =>
        `The weather in ${location} is sunny, 25°C.`,
});

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-5.4",
    tools: [getWeather],
    onPermissionRequest: async () => ({ kind: "approve-once" }),
});

await session.sendAndWait({ prompt: "What's the weather like in Seattle?" });

多代理工作流

MAF 集成的主要优势在于,能够在编排式工作流中将 Copilot 与其他代理提供程序结合使用。 使用框架内置的编排器创建流水线,不同的代理负责不同的步骤。

顺序工作流

逐个运行代理,将输出从一个传递到下一个:

代码语言 navigation

.NET
using GitHub.Copilot;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Orchestration;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

// Copilot agent for code review
AIAgent reviewer = copilotClient.AsAIAgent(new AIAgentOptions
{
    Instructions = "You review code for bugs, security issues, and best practices. Be thorough.",
});

// Azure OpenAI agent for generating documentation
AIAgent documentor = AIAgent.FromOpenAI(new OpenAIAgentOptions
{
    Model = "gpt-5.4",
    Instructions = "You write clear, concise documentation for code changes.",
});

// Compose in a sequential pipeline
var pipeline = new SequentialOrchestrator(new[] { reviewer, documentor });

string result = await pipeline.RunAsync(
    "Review and document this pull request: added retry logic to the HTTP client"
);
Console.WriteLine(result);

并发工作流

并行运行多个代理并聚合其结果:

代码语言 navigation

.NET
using GitHub.Copilot;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Orchestration;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

AIAgent securityReviewer = copilotClient.AsAIAgent(new AIAgentOptions
{
    Instructions = "Focus exclusively on security vulnerabilities and risks.",
});

AIAgent performanceReviewer = copilotClient.AsAIAgent(new AIAgentOptions
{
    Instructions = "Focus exclusively on performance bottlenecks and optimization opportunities.",
});

// Run both reviews concurrently
var concurrent = new ConcurrentOrchestrator(new[] { securityReviewer, performanceReviewer });

string combinedResult = await concurrent.RunAsync(
    "Analyze this database query module for issues"
);
Console.WriteLine(combinedResult);

流式响应

在构建交互式应用程序时,通过流式传输代理的响应,以显示实时输出。 MAF 集成保留了 Copilot SDK 的流式传输能力。

代码语言 navigation

.NET
using GitHub.Copilot;
using Microsoft.Agents.AI;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

AIAgent agent = copilotClient.AsAIAgent(new AIAgentOptions
{
    Streaming = true,
});

await foreach (var chunk in agent.RunStreamingAsync("Write a quicksort implementation in C#"))
{
    Console.Write(chunk);
}
Console.WriteLine();

还可以直接通过没有 MAF 的 Copilot SDK 进行流式传输:

代码语言 navigation

TypeScript
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-5.4",
    streaming: true,
    onPermissionRequest: async () => ({ kind: "approve-once" }),
});

session.on("assistant.message_delta", (event) => {
    process.stdout.write(event.data.deltaContent ?? "");
});

await session.sendAndWait({ prompt: "Write a quicksort implementation in TypeScript" });

配置参考

MAF 代理选项

财产类型说明
Instructions / instructionsstring代理的系统提示
Tools / toolsAIFunction[] / list代理可用的自定义函数工具
Streaming / streamingbool启用流式处理响应
Model / modelstring覆盖默认模型

Copilot SDK 选项(直通传递)

创建基础Copilot客户端时,所有标准 Build your first Copilot-powered app 选项仍然可用。 MAF 包装器在底层委托给 SDK:

SDK 功能MAF 支持
自定义工具 (DefineTool / AIFunctionFactory)
✅ 已与 MAF 工具合并
MCP 服务器
✅ 在 SDK 客户端上配置
自定义代理/子代理
✅ 在 Copilot 代理中可用
无限会话
✅ 在 SDK 客户端上配置
模型选择
✅ 可按智能体或每次调用重写
流媒体
✅ 完整增量事件支持

最佳做法

选择正确的集成级别

当你需要在业务流程工作流中将 Copilot 与其他提供程序组合时,使用 MAF 包装器。 如果您的应用仅使用 Copilot,那么独立的 SDK 更简单,并且可让您完全掌控:

// Standalone SDK — full control, simpler setup
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-5.4",
    onPermissionRequest: async () => ({ kind: "approve-once" }),
});
const response = await session.sendAndWait({ prompt: "Explain this code" });

使代理保持专注

生成多代理工作流时,请为每个代理提供一个具有明确说明的特定角色。 避免重叠责任:

// ❌ Too vague — overlapping roles
const agents = [
    { instructions: "Help with code" },
    { instructions: "Assist with programming" },
];

// ✅ Focused — clear separation of concerns
const agents = [
    { instructions: "Review code for security vulnerabilities. Flag SQL injection, XSS, and auth issues." },
    { instructions: "Optimize code performance. Focus on algorithmic complexity and memory usage." },
];

在业务流程级别处理错误

将智能体调用包装在错误处理中,尤其是在多智能体工作流中,其中一个智能体的故障不应阻止整个管道:

try
{
    string result = await pipeline.RunAsync("Analyze this module");
    Console.WriteLine(result);
}
catch (AgentException ex)
{
    Console.Error.WriteLine($"Agent {ex.AgentName} failed: {ex.Message}");
    // Fall back to single-agent mode or retry
}

另见