什么是 Microsoft Agent Framework?AutoGen 继任者的企业级 Agent 框架全解析

30 秒快速回答:Microsoft Agent Framework(微软 Agent 框架,简称 MAF)是微软于 2026 年 4 月 3 日发布的 1.0 正式版企业级 AI Agent 开发框架,由原 Semantic Kernel 与 AutoGen 的同一团队合并打造,是两者的直接继任者(Semantic Kernel 与 AutoGen 均已进入维护模式)。它用一套 API 覆盖 Python、.NET(Go 公共预览)三种语言,把 AutoGen 简洁的多智能体编排抽象与 Semantic Kernel 的企业级能力(会话状态管理、类型安全、中间件、遥测)合二为一,并新增了基于图的 Workflow 引擎,为单 Agent 与多 Agent 协作提供显式、可恢复、可观测的编排能力。

核心价值一句话:如果你之前纠结”选 AutoGen 的灵活还是 Semantic Kernel 的稳”,微软已经替你做了决定——Agent Framework 把两者的优势装进同一个 SDK,让团队从原型到生产不必再”二选一”。

一、为什么需要 Microsoft Agent Framework?一场持续两年的框架之争

过去两年,微软内部同时维护着两个 Agent 框架,开发者被迫站队:

框架 定位 强项 短板
Semantic Kernel 企业级、.NET 优先 会话状态管理、类型安全、中间件、遥测、广泛的模型与 Embedding 支持 多智能体编排像”后装”的,不够顺手
AutoGen 研究级、Python 优先 简洁的 Agent 抽象、研究级多智能体编排模式 生产环境缺状态管理、可观测性、长时任务支撑

这种分裂让企业在技术选型上反复摇摆。2026 年 4 月 3 日,微软发布 Agent Framework 1.0,宣布同一团队把两者合并为单一 SDK——官方原话是”结合 AutoGen 的简单抽象与 Semantic Kernel 的企业级特性,并在此基础上新增基于图的工作流“。这不是包装层或兼容层,而是同一个团队打造的新一代产品,并提供了从 Semantic Kernel 与 AutoGen 各自的迁移指南。

补充一点架构事实:框架官方文档把能力划分为 Agents(单个代理)、Harness(开箱即用的增强代理)、Workflows(图式工作流) 三大层,底层共享模型客户端、Agent 会话(状态管理)、上下文提供器(记忆)、中间件与 MCP 客户端等基础构建块。

二、快速上手:安装与最小 Agent

Python 安装只需要一条命令(注意:框架不会自动加载 .env 文件,需要自己调用 load_dotenv() 或直接在 shell 中设置环境变量):

pip install agent-framework
# 如果连接 Azure AI Foundry,再装:
pip install agent-framework-foundry azure-identity

最小 Agent 示例(连接 Azure AI Foundry):

import asyncio
from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential

async def main():
    client = FoundryChatClient(
        project_endpoint="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project",
        model="gpt-5.4-mini",
        credential=AzureCliCredential(),   # 先执行 az login
    )
    agent = client.as_agent(
        name="HelloAgent",
        instructions="You are a friendly assistant. Keep your answers brief.",
    )
    result = await agent.run("What is the largest city in France?")
    print(f"Agent: {result}")

asyncio.run(main())

对应的 .NET 写法同样简洁:

using System;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;

AIAgent agent = new AIProjectClient(
    new Uri("https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project"),
    new AzureCliCredential())
    .AsAIAgent(
        model: "gpt-5.4-mini",
        instructions: "You are a friendly assistant. Keep your answers brief.");

Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));

框架对模型供应商非常开放:Microsoft Foundry、Azure OpenAI、OpenAI、Anthropic Claude、Google Gemini、Ollama 等都能通过同一套抽象接入,方便做多模型兜底策略而不是绑定单一厂商。

三、给 Agent 加工具与编排:@tool 与 Sequential 工作流

单 Agent 没有工具就只是聊天机器人。框架用 @tool 装饰器把普通 Python 函数变成类型安全的工具,并原生支持 MCP(Model Context Protocol)客户端集成:

import asyncio
from agent_framework import Agent, tool
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    weather_data = {
        "Paris": "18°C, partly cloudy",
        "Tokyo": "22°C, sunny",
        "New York": "15°C, rainy",
    }
    return weather_data.get(city, f"Weather data not available for {city}")

async def main():
    client = FoundryChatClient(
        project_endpoint="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project",
        model="gpt-5.4-mini",
        credential=AzureCliCredential(),
    )
    agent = client.as_agent(
        name="WeatherAgent",
        instructions="You help users check the weather. Use the get_weather tool.",
        tools=[get_weather],
    )
    result = await agent.run("What's the weather like in Tokyo?")
    print(f"Agent: {result}")

asyncio.run(main())

多 Agent 场景中,Workflow 引擎把 Agent 与普通函数连成显式的图结构,支持顺序、并发、交接、群聊等模式,还自带流式输出、检查点(checkpointing)与人工介入(human-in-the-loop)。下面是经典的两 Agent 顺序工作流(研究 → 写作):

import asyncio
from agent_framework import SequentialBuilder
from agent_framework_foundry import FoundryChatClient
from azure.identity import DefaultAzureCredential

async def main():
    client = FoundryChatClient(
        endpoint="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project",
        model="gpt-5-mini",
        credential=DefaultAzureCredential(),
    )
    researcher = client.create_agent(
        name="researcher",
        instructions="Collect the key facts on the topic as a short bullet list.",
    )
    writer = client.create_agent(
        name="writer",
        instructions="Turn the bullet list into a short, friendly summary for IT admins.",
    )

    # researcher 先跑,输出自动成为 writer 的输入
    workflow = SequentialBuilder().participants([researcher, writer]).build()
    result = await workflow.run("What is the Model Context Protocol?")
    print(result.get_outputs()[-1])

asyncio.run(main())

四、关键选型决策:Agent vs Workflow,以及五种编排模式

官方文档给了一张”先想清楚再动手”的决策表:

用 Agent(自治代理)当…… 用 Workflow(工作流)当……
任务是开放式或对话式的 流程有明确定义的步骤
需要自主使用工具与规划 需要显式控制执行顺序
单个 LLM 调用(可能带工具)就够 多个 Agent 或函数必须协同

文档还附带一句直白的建议:”如果写一个函数就能搞定,就别用 AI Agent。”大量生产事故恰恰源于把确定性的五步业务流程做成了话痨自治代理,然后花几个月和自造的不确定性搏斗。

框架内置五种编排模式,各有适用场景与成本特征:

模式 用途 你需要付出的成本
单 Agent + 工具 一个好工具能搞定的事,永远先检查这个 无(优先考虑)
Sequential(顺序) 任务有固定先后顺序(草稿→审阅→定稿) 延迟随步骤累加
Concurrent(并发) 想要多个独立视角并行出结果 token 随 Agent 数倍增
Group chat(群聊) Agent 之间必须相互回应、讨论 每轮都会广播上下文,token 消耗大
Handoff(交接) 把任务完整交给专家,原 Agent 不再收回 路由质量取决于各 Agent 的描述
Magentic(经理制) 开放式复杂问题,经理 Agent 规划、派活、跟踪、复盘 token 成本最不可预测

其中 Magentic 模式基于 AutoGen 时代的 Magentic-One 研究,是能力最强也最难控成本的模式;官方建议务必设置最大轮数与最大停滞次数上限,涉及高价值生产系统时开启人工计划审批。另外所有模式都支持 human-in-the-loop——把某个工具标记为 approval_mode="always_require",工作流就会暂停等待人工批准。

五、和 LangGraph / CrewAI 怎么比?

如果你已经在用或正在评估 LangGraph、CrewAI,可以按团队技术栈与生产诉求做决策:

维度 Microsoft Agent Framework LangGraph CrewAI
出身 微软官方,AutoGen + Semantic Kernel 合并 LangChain 生态 独立开源团队
支持语言 Python、.NET(Go 预览) Python(有 JS 版) Python(近期有 JS 版)
编排方式 图式 Workflow:sequential/concurrent/handoff/group chat/Magentic 图(StateGraph)编排,细粒度状态控制 角色化 Crew 编排,上手快
企业级能力 会话状态、类型安全、中间件、OpenTelemetry 遥测、检查点、人工介入 依赖 LangChain 生态,持久化需额外组件 偏上层封装,底层控制弱
模型生态 Foundry/Azure OpenAI/OpenAI/Anthropic/Bedrock/Gemini/Ollama 广泛(LangChain 集成) 广泛
协议支持 MCP、A2A、AG-UI 原生支持 MCP 支持 MCP 支持
最佳场景 微软技术栈团队、需要长期维护与治理的生产 Agent 深度定制图逻辑、已有 LangChain 技术栈 快速搭建角色化多 Agent 演示

关键判断:如果你已经在微软生态(Azure / Foundry)里,或需要长期支持(LTS)与治理能力,MAF 是默认选择;如果你深度依赖 LangChain 生态、需要极细粒度控制图状态,LangGraph 依然是合理选项;如果只是想快速验证多角色协作的 Demo,CrewAI 上手成本最低。

总结与延伸阅读

核心要点回顾:

  1. Microsoft Agent Framework 是 AutoGen 与 Semantic Kernel 的官方继任者,2026-04-03 发布 1.0,两个前身均进入维护模式,新项目应从它起步;
  2. 三大能力层:Agents(单 Agent)、Harness(增强代理)、Workflows(图式工作流),底层共享状态管理、中间件、MCP 客户端;
  3. 选型第一原则:能写函数就别用 Agent;开放式任务用 Agent,确定流程用 Workflow;
  4. 五种编排模式里 Sequential 最适合起步,Magentic 最强但成本最不可控,务必设上限并开启人工审批;
  5. 框架原生支持 MCP 与 A2A,Python 侧不自动加载 .env,需要自己处理环境变量。

延伸阅读建议:想理解 MAF 中的 MCP 集成,先读本站《什么是 MCP 协议?一文读懂 AI 的 USB 接口标准》;做多 Agent 系统的可以接着看《什么是多智能体协作?AI Agent 如何组队干活》;编排工具链方面推荐《怎么用 LangGraph?Agent 开发框架完全指南》;Agent 的能力组装可参考《什么是 Agent Skills?AI Agent 的模块化能力系统全解析》。想深入研究,直接读官方 Overview 文档与《Migration Guide from AutoGen / Semantic Kernel》两份迁移指南。