Principle:Microsoft Agent framework Agent Construction
| Knowledge Sources | |
|---|---|
| Domains | Agent_Architecture |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
A construction pattern that binds a chat client, system instructions, and function tools into a single executable agent entity. The Agent class is the central abstraction in the Microsoft Agent Framework, composing the minimal set of components needed to produce an autonomous, tool-using AI agent.
Description
The Agent class serves as the central abstraction in the Microsoft Agent Framework. It encapsulates the three core elements required to define a functional agent:
- Chat client (
SupportsChatGetResponse): The underlying model endpoint that handles LLM inference. This is the only required parameter and must conform to theSupportsChatGetResponse[OptionsCoT]protocol, allowing any compliant chat backend to be used interchangeably. - Instructions (
str): A system prompt string that defines the agent's persona, behavior, and constraints. Defaults to an empty string, enabling bare-bones agents that rely solely on tools or middleware for behavior. - Tools (
FunctionTool | Callable | Sequence[...]): An optional list of function tools or plain callables that the agent can invoke during execution. Tools give the agent the ability to interact with external systems, perform computations, or retrieve data.
Beyond these core components, the Agent constructor also accepts:
- Identity fields (
id,name,description): Metadata for identification and tracing within multi-agent systems. - Default options (
OptionsCoT): Model-specific configuration such as temperature and token limits, generic over the options type. - Chat message store factory: A factory for creating persistent or in-memory stores for conversation history.
- Context provider: An injectable component that supplies additional runtime context to the agent during execution.
- Middleware: A sequence of middleware components (agent-level, chat-level, or function-level) that intercept and transform behavior at various stages of the execution pipeline.
- Function invocation configuration: Fine-grained control over how tool functions are invoked.
Async Context Management
The Agent class supports async context management (async with) for lifecycle management of MCP (Model Context Protocol) tools. When an agent is used as an async context manager, it properly initializes and tears down any MCP tool servers that have been registered, ensuring clean resource management.
Factory Method
In addition to direct construction, the framework supports a factory method pattern via client.as_agent(). Any SupportsChatGetResponse client can produce an agent directly, providing a fluent API for simple use cases:
agent = client.as_agent(
instructions="You are a helpful assistant.",
tools=[get_weather],
)
Theoretical Basis
The Agent Construction principle is rooted in the composition pattern:
agent = client + instructions + tools
Rather than inheriting from a complex base class hierarchy, an agent is composed from independent, interchangeable components. This design offers several advantages:
- Loose coupling: The chat client, instructions, and tools are independent concerns. Any conforming client can be paired with any set of instructions and tools.
- Dependency injection: Middleware, context providers, and message stores are injected at construction time, enabling testability and configurability without subclassing.
- Protocol-based typing: The client parameter is typed as a protocol (
SupportsChatGetResponse) rather than a concrete class, following Python's structural subtyping conventions and enabling broad interoperability. - Generic parameterization: The
Generic[OptionsCoT]type parameter threads model-specific options through the entire agent without sacrificing type safety.
Usage
Basic Construction
from agent_framework import Agent
from agent_framework.openai import OpenAIResponsesClient
client = OpenAIResponsesClient()
agent = Agent(
client=client,
instructions="You are a helpful assistant.",
name="assistant",
tools=[get_weather],
)
Factory Method
agent = client.as_agent(
instructions="You are a helpful assistant.",
tools=[get_weather],
)
Async Context Manager for MCP Tools
async with Agent(
client=client,
instructions="You are an assistant with MCP tools.",
tools=[mcp_server_tool],
) as agent:
result = await agent.run("What is the weather?")