Microsoft Agent Framework 集成 Claude Agent SDK:可构建多智能体 AI 应用

Microsoft Agent Framework 现已集成 Claude Agent SDK,让你能够构建由 Claude 完整 Agent 能力驱动的 AI Agent。这个集成将 Agent Framework 一致的 Agent 抽象,与 Claude 的强大能力结合在一起,包括文件编辑、代码执行、函数调用、流式响应、多轮对话,以及 Model Context Protocol(MCP)服务器集成——并且这些能力均可在 Python 中使用。
为什么要在 Agent Framework 中使用 Claude Agent SDK?
你当然可以单独使用 Claude Agent SDK 来构建 Agent。那么为什么还要通过 Agent Framework 来用它?关键原因如下:
- 一致的 Agent 抽象 —— Claude Agent 与框架中的其他 Agent 类型一样,实现相同的
BaseAgent接口。你可以在不重构代码的情况下切换或组合不同提供方。 - 多 Agent 工作流 —— 借助内置编排器,你可以将 Claude Agent 与其他 Agent(Azure OpenAI、OpenAI、GitHub Copilot 等)组合为顺序、并发、handoff 和群聊等工作流。
- 生态集成能力 —— 可直接接入 Agent Framework 的完整生态:声明式 Agent 定义、A2A 协议支持,以及跨提供方统一的函数工具、会话与流式处理模式。
简而言之,Agent Framework 让你把 Claude 作为更大 Agent 系统中的一个构建模块,而不只是一个独立工具。
安装 Claude Agent SDK 集成
Python
\npip install agent-framework-claude --pre\n
创建 Claude Agent
上手很简单。创建一个 ClaudeAgent,然后通过 async context manager 模式开始交互。
Python
\nfrom agent_framework_claude import ClaudeAgent\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a helpful assistant.",\n ) as agent:\n response = await agent.run("What is Microsoft Agent Framework?")\n print(response.text)\n
使用内置工具
Claude Agent SDK 提供了强大的内置工具,可用于文件操作、shell 命令等。你只需以字符串形式传入工具名即可启用。
Python
\nfrom agent_framework_claude import ClaudeAgent\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a helpful coding assistant.",\n tools=["Read", "Write", "Bash", "Glob"],\n ) as agent:\n response = await agent.run("List all Python files in the current directory")\n print(response.text)\n
添加函数工具
你可以通过自定义函数工具扩展 Agent,让它具备特定领域能力。
Python
\nfrom typing import Annotated\nfrom pydantic import Field\nfrom agent_framework_claude import ClaudeAgent\n\ndef get_weather(\n location: Annotated[str, Field(description="The location to get the weather for.")],\n) -> str:\n """Get the weather for a given location."""\n return f"The weather in {location} is sunny with a high of 25C."\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a helpful weather agent.",\n tools=[get_weather],\n ) as agent:\n response = await agent.run("What's the weather like in Seattle?")\n print(response.text)\n
流式响应
为了获得更好的用户体验,你可以在响应生成过程中实时流式输出,而不是等完整结果返回。
Python
\nfrom agent_framework_claude import ClaudeAgent\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a helpful assistant.",\n ) as agent:\n print("Agent: ", end="", flush=True)\n async for chunk in agent.run_stream("Tell me a short story."):\n if chunk.text:\n print(chunk.text, end="", flush=True)\n print()\n
多轮对话
你可以通过 thread 在多次交互中保持上下文。Claude Agent SDK 会自动管理会话恢复以保留上下文信息。
Python
\nfrom agent_framework_claude import ClaudeAgent\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a helpful assistant. Keep your answers short.",\n ) as agent:\n thread = agent.get_new_thread()\n\n # First turn\n await agent.run("My name is Alice.", thread=thread)\n\n # Second turn - agent remembers the context\n response = await agent.run("What is my name?", thread=thread)\n print(response.text) # Should mention "Alice"\n
配置权限模式
你可以通过 permission mode 控制 Agent 在文件操作和命令执行时如何处理权限请求。
Python
\nfrom agent_framework_claude import ClaudeAgent\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a coding assistant that can edit files.",\n tools=["Read", "Write", "Bash"],\n default_options={\n "permission_mode": "acceptEdits", # Auto-accept file edits\n },\n ) as agent:\n response = await agent.run("Create a hello.py file that prints 'Hello, World!'")\n print(response.text)\n
连接 MCP 服务器
Claude Agent 支持连接外部 MCP 服务器,从而让 Agent 获得更多工具和数据源访问能力。
Python
\nfrom agent_framework_claude import ClaudeAgent\n\nasync def main():\n async with ClaudeAgent(\n instructions="You are a helpful assistant with access to the filesystem.",\n default_options={\n "mcp_servers": {\n "filesystem": {\n "command": "npx",\n "args": ["-y", "@modelcontextprotocol/server-filesystem", "."],\n },\n },\n },\n ) as agent:\n response = await agent.run("List all files in the current directory using MCP")\n print(response.text)\n
在多 Agent 工作流中使用 Claude
在 Agent Framework 中使用 Claude 的一个关键优势,是可以将它与其他 Agent 组合进多 Agent 工作流。在下面这个示例里,Azure OpenAI Agent 先生成营销标语,再由 Claude Agent 进行审阅——整个过程由顺序流水线统一编排。
Python
\nimport asyncio\nfrom typing import cast\n\nfrom agent_framework import ChatMessage, Role, SequentialBuilder, WorkflowOutputEvent\nfrom agent_framework.azure import AzureOpenAIChatClient\nfrom agent_framework_claude import ClaudeAgent\nfrom azure.identity import AzureCliCredential\n\nasync def main():\n # Create an Azure OpenAI agent as a copywriter\n chat_client = AzureOpenAIChatClient(credential=AzureCliCredential())\n\n writer = chat_client.as_agent(\n instructions="You are a concise copywriter. Provide a single, punchy marketing sentence based on the prompt.",\n name="writer",\n )\n\n # Create a Claude agent as a reviewer\n reviewer = ClaudeAgent(\n instructions="You are a thoughtful reviewer. Give brief feedback on the previous assistant message.",\n name="reviewer",\n )\n\n # Build a sequential workflow: writer -> reviewer\n workflow = SequentialBuilder().participants([writer, reviewer]).build()\n\n # Run the workflow\n async for event in workflow.run_stream("Write a tagline for a budget-friendly electric bike."):\n if isinstance(event, WorkflowOutputEvent):\n messages = cast(list[ChatMessage], event.data)\n for msg in messages:\n name = msg.author_name or ("assistant" if msg.role == Role.ASSISTANT else "user")\n print(f"[{name}]: {msg.text}\n")\n\nasyncio.run(main())\n
该示例展示了:一个工作流就可以组合来自不同提供方的 Agent。你也可以将这一模式扩展到并发、handoff 和群聊工作流。
更多信息
总结
面向 Microsoft Agent Framework 的 Claude Agent SDK 集成,让开发者可以更轻松地构建充分利用 Claude 完整 Agent 能力的 AI Agent。借助 Python 中对内置工具、函数工具、流式输出、多轮对话、权限模式和 MCP 服务器的支持,你可以构建能够与代码、文件、shell 命令及外部服务交互的强大 Agent 应用。
我们非常希望听到你的反馈。如果你有建议、问题,或希望进一步交流,欢迎前往 GitHub 的讨论区与我们及社区沟通!如果你喜欢 Agent Framework,也欢迎在 GitHub 给我们点个 star。
分类
作者

高级软件工程师

