11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
33
4- """Fluent builder for constructing Agent instances with chainable configuration."""
4+ """Fluent builder for constructing ChatAgent instances with chainable configuration."""
55
66from collections .abc import Callable , MutableMapping , Sequence
77from typing import Any , Literal
88
99from agent_framework import (
10- Agent ,
11- AgentMiddleware ,
12- BaseChatClient ,
13- ChatMiddleware ,
10+ AggregateContextProvider ,
11+ ChatAgent ,
12+ ChatClientProtocol ,
13+ ChatMessageStoreProtocol ,
1414 ContextProvider ,
15- FunctionTool ,
15+ Middleware ,
1616 ToolMode ,
17+ ToolProtocol ,
1718)
1819from pydantic import BaseModel
1920
2223
2324
2425class AgentBuilder :
25- """Fluent builder for creating Agent instances with a chainable API.
26+ """Fluent builder for creating ChatAgent instances with a chainable API.
2627
2728 This class provides two ways to create agents:
2829 1. Fluent API with method chaining (recommended for readability)
@@ -58,7 +59,7 @@ class AgentBuilder:
5859 )
5960 """
6061
61- def __init__ (self , chat_client : BaseChatClient ):
62+ def __init__ (self , chat_client : ChatClientProtocol ):
6263 """Initialize the builder with a chat client.
6364
6465 Args:
@@ -69,15 +70,14 @@ def __init__(self, chat_client: BaseChatClient):
6970 self ._id : str | None = None
7071 self ._name : str | None = None
7172 self ._description : str | None = None
72- self ._chat_message_store_factory : Callable [[], Any ] | None = None
73+ self ._chat_message_store_factory : (
74+ Callable [[], ChatMessageStoreProtocol ] | None
75+ ) = None
7376 self ._conversation_id : str | None = None
74- self ._context_providers : ContextProvider | list [ContextProvider ] | None = None
75- self ._middleware : (
76- AgentMiddleware
77- | ChatMiddleware
78- | list [AgentMiddleware | ChatMiddleware ]
79- | None
77+ self ._context_providers : (
78+ ContextProvider | list [ContextProvider ] | AggregateContextProvider | None
8079 ) = None
80+ self ._middleware : Middleware | list [Middleware ] | None = None
8181 self ._frequency_penalty : float | None = None
8282 self ._logit_bias : dict [str | int , float ] | None = None
8383 self ._max_tokens : int | None = None
@@ -93,10 +93,10 @@ def __init__(self, chat_client: BaseChatClient):
9393 ToolMode | Literal ["auto" , "required" , "none" ] | dict [str , Any ] | None
9494 ) = "auto"
9595 self ._tools : (
96- FunctionTool
96+ ToolProtocol
9797 | Callable [..., Any ]
9898 | MutableMapping [str , Any ]
99- | Sequence [FunctionTool | Callable [..., Any ] | MutableMapping [str , Any ]]
99+ | Sequence [ToolProtocol | Callable [..., Any ] | MutableMapping [str , Any ]]
100100 | None
101101 ) = None
102102 self ._top_p : float | None = None
@@ -178,10 +178,10 @@ def with_max_tokens(self, max_tokens: int) -> "AgentBuilder":
178178
179179 def with_tools (
180180 self ,
181- tools : FunctionTool
181+ tools : ToolProtocol
182182 | Callable [..., Any ]
183183 | MutableMapping [str , Any ]
184- | Sequence [FunctionTool | Callable [..., Any ] | MutableMapping [str , Any ]],
184+ | Sequence [ToolProtocol | Callable [..., Any ] | MutableMapping [str , Any ]],
185185 ) -> "AgentBuilder" :
186186 """Set the tools available to the agent.
187187
@@ -210,8 +210,7 @@ def with_tool_choice(
210210 return self
211211
212212 def with_middleware (
213- self ,
214- middleware : AgentMiddleware | ChatMiddleware | list [AgentMiddleware | ChatMiddleware ],
213+ self , middleware : Middleware | list [Middleware ]
215214 ) -> "AgentBuilder" :
216215 """Set middleware for request/response processing.
217216
@@ -226,7 +225,9 @@ def with_middleware(
226225
227226 def with_context_providers (
228227 self ,
229- context_providers : ContextProvider | list [ContextProvider ],
228+ context_providers : ContextProvider
229+ | list [ContextProvider ]
230+ | AggregateContextProvider ,
230231 ) -> "AgentBuilder" :
231232 """Set context providers for additional conversation context.
232233
@@ -384,7 +385,7 @@ def with_store(self, store: bool) -> "AgentBuilder":
384385 return self
385386
386387 def with_message_store_factory (
387- self , factory : Callable [[], Any ]
388+ self , factory : Callable [[], ChatMessageStoreProtocol ]
388389 ) -> "AgentBuilder" :
389390 """Set the message store factory.
390391
@@ -421,11 +422,11 @@ def with_kwargs(self, **kwargs: Any) -> "AgentBuilder":
421422 self ._kwargs .update (kwargs )
422423 return self
423424
424- def build (self ) -> Agent :
425- """Build and return the configured Agent .
425+ def build (self ) -> ChatAgent :
426+ """Build and return the configured ChatAgent .
426427
427428 Returns:
428- Agent : Configured agent instance ready for use
429+ ChatAgent : Configured agent instance ready for use
429430
430431 Example:
431432 .. code-block:: python
@@ -441,7 +442,7 @@ def build(self) -> Agent:
441442 async with agent:
442443 response = await agent.run("Hello!")
443444 """
444- return Agent (
445+ return ChatAgent (
445446 chat_client = self ._chat_client ,
446447 instructions = self ._instructions ,
447448 id = self ._id ,
@@ -476,10 +477,14 @@ def create_agent_by_agentinfo(
476477 agent_info : AgentInfo ,
477478 * ,
478479 id : str | None = None ,
479- chat_message_store_factory : Callable [[], Any ] | None = None ,
480+ chat_message_store_factory : Callable [[], ChatMessageStoreProtocol ]
481+ | None = None ,
480482 conversation_id : str | None = None ,
481- context_providers : ContextProvider | list [ContextProvider ] | None = None ,
482- middleware : AgentMiddleware | ChatMiddleware | list [AgentMiddleware | ChatMiddleware ] | None = None ,
483+ context_providers : ContextProvider
484+ | list [ContextProvider ]
485+ | AggregateContextProvider
486+ | None = None ,
487+ middleware : Middleware | list [Middleware ] | None = None ,
483488 frequency_penalty : float | None = None ,
484489 logit_bias : dict [str | int , float ] | None = None ,
485490 max_tokens : int | None = None ,
@@ -495,20 +500,20 @@ def create_agent_by_agentinfo(
495500 | Literal ["auto" , "required" , "none" ]
496501 | dict [str , Any ]
497502 | None = "auto" ,
498- tools : FunctionTool
503+ tools : ToolProtocol
499504 | Callable [..., Any ]
500505 | MutableMapping [str , Any ]
501- | Sequence [FunctionTool | Callable [..., Any ] | MutableMapping [str , Any ]]
506+ | Sequence [ToolProtocol | Callable [..., Any ] | MutableMapping [str , Any ]]
502507 | None = None ,
503508 top_p : float | None = None ,
504509 user : str | None = None ,
505510 additional_chat_options : dict [str , Any ] | None = None ,
506511 ** kwargs : Any ,
507- ) -> Agent :
512+ ) -> ChatAgent :
508513 """Create an agent using AgentInfo configuration with full parameter support.
509514
510515 This method creates a chat client from the service configuration and then
511- creates a Agent with the specified parameters. Agent name, description,
516+ creates a ChatAgent with the specified parameters. Agent name, description,
512517 and instructions are taken from AgentInfo but can be overridden via kwargs.
513518
514519 Args:
@@ -538,7 +543,7 @@ def create_agent_by_agentinfo(
538543 **kwargs: Additional keyword arguments
539544
540545 Returns:
541- Agent : Configured agent instance ready for use
546+ ChatAgent : Configured agent instance ready for use
542547
543548 Example:
544549 .. code-block:: python
@@ -606,16 +611,20 @@ def create_agent_by_agentinfo(
606611
607612 @staticmethod
608613 def create_agent (
609- chat_client : BaseChatClient ,
614+ chat_client : ChatClientProtocol ,
610615 instructions : str | None = None ,
611616 * ,
612617 id : str | None = None ,
613618 name : str | None = None ,
614619 description : str | None = None ,
615- chat_message_store_factory : Callable [[], Any ] | None = None ,
620+ chat_message_store_factory : Callable [[], ChatMessageStoreProtocol ]
621+ | None = None ,
616622 conversation_id : str | None = None ,
617- context_providers : ContextProvider | list [ContextProvider ] | None = None ,
618- middleware : AgentMiddleware | ChatMiddleware | list [AgentMiddleware | ChatMiddleware ] | None = None ,
623+ context_providers : ContextProvider
624+ | list [ContextProvider ]
625+ | AggregateContextProvider
626+ | None = None ,
627+ middleware : Middleware | list [Middleware ] | None = None ,
619628 frequency_penalty : float | None = None ,
620629 logit_bias : dict [str | int , float ] | None = None ,
621630 max_tokens : int | None = None ,
@@ -631,19 +640,19 @@ def create_agent(
631640 | Literal ["auto" , "required" , "none" ]
632641 | dict [str , Any ]
633642 | None = "auto" ,
634- tools : FunctionTool
643+ tools : ToolProtocol
635644 | Callable [..., Any ]
636645 | MutableMapping [str , Any ]
637- | Sequence [FunctionTool | Callable [..., Any ] | MutableMapping [str , Any ]]
646+ | Sequence [ToolProtocol | Callable [..., Any ] | MutableMapping [str , Any ]]
638647 | None = None ,
639648 top_p : float | None = None ,
640649 user : str | None = None ,
641650 additional_chat_options : dict [str , Any ] | None = None ,
642651 ** kwargs : Any ,
643- ) -> Agent :
652+ ) -> ChatAgent :
644653 """Create a Chat Client Agent.
645654
646- Factory method that creates a Agent instance with the specified configuration.
655+ Factory method that creates a ChatAgent instance with the specified configuration.
647656 The agent uses a chat client to interact with language models and supports tools
648657 (MCP tools, callable functions), context providers, middleware, and both streaming
649658 and non-streaming responses.
@@ -677,7 +686,7 @@ def create_agent(
677686 **kwargs: Additional keyword arguments
678687
679688 Returns:
680- Agent : Configured chat agent instance that can be used directly or with async context manager
689+ ChatAgent : Configured chat agent instance that can be used directly or with async context manager
681690
682691 Examples:
683692 Non-streaming example (from azure_response_client_basic.py):
@@ -752,10 +761,10 @@ def create_agent(
752761
753762 Note:
754763 When the agent has MCP tools or needs proper resource cleanup, use it with
755- ``async with`` to ensure proper initialization and cleanup via the Agent 's
764+ ``async with`` to ensure proper initialization and cleanup via the ChatAgent 's
756765 async context manager protocol.
757766 """
758- return Agent (
767+ return ChatAgent (
759768 chat_client = chat_client ,
760769 instructions = instructions ,
761770 id = id ,
0 commit comments