1313from typing import Callable
1414from typing import Literal
1515from openai import NOT_GIVEN , NotGiven
16+ from .mcp import MCPServer
17+ import asyncio
1618
1719
1820class Agent :
1921
2022 def __init__ (
2123 self ,
22- name : str ,
2324 llm : LLMBase ,
25+ name : str = 'Assistant' ,
2426 functions : list [Callable ] = None ,
2527 tool_choice : str | NotGiven | Literal ['required' , 'auto' , 'none' ] = NOT_GIVEN ,
2628 parallel_tool_calls : bool | NotGiven = NOT_GIVEN ,
27- instruction : str | Callable [[ContextVariables ], str ] | Callable [[], str ] = 'You are a helpful assistant.'
29+ instruction : str | Callable [[ContextVariables ], str ] | Callable [[], str ] = 'You are a helpful assistant.' ,
30+ mcp_server : list [MCPServer ] = None ,
31+ mcp_impl : Literal ['function_call' ] = 'function_call'
2832 ) -> None :
2933 """ 智能体类
3034
31- Args: name (str): 名称
32- llm (LLMBase): 大模型
33- functions: (list[Callable], optional): 工具函数. Defaults to None.
34- parallel_tool_calls: (bool, optional): 允许工具并行调用. Defaults to False.
35- tool_choice: (Literal['required', 'auto', 'none'] | NotGiven, optional). 强制使用工具函数, 选择模式或提供函数名称. Defaults to NOT_GIVEN.
36- instruction (str | Callable[[ContextVariables], str] | Callable[[], str], optional): 指令. Defaults to 'You are a helpful assistant.'.
35+ Args:
36+ llm (LLMBase): 大模型
37+ name (str, optional): 名称. Defaults to 'Assistant'.
38+ functions: (list[Callable], optional): 工具函数. Defaults to None.
39+ parallel_tool_calls: (bool, optional): 允许工具并行调用. Defaults to False.
40+ tool_choice: (Literal['required', 'auto', 'none'] | NotGiven, optional). 强制使用工具函数, 选择模式或提供函数名称. Defaults to NOT_GIVEN.
41+ instruction (str | Callable[[ContextVariables], str] | Callable[[], str], optional): 指令. Defaults to 'You are a helpful assistant.'.
42+ mcp_server: (list[MCPServer], optional): MCP 服务器. Defaults to None.
43+ mcp_impl: (Literal['function_call'] | NotGiven, optional): MCP 协议实现方式, 目前只支持 'function_call'. Defaults to 'function_call'.
3744 """
38- self .name = name
3945 self .llm = llm
46+ self .name = name
4047 self .instruction = instruction
4148
42- self .tools : list [ChatCompletionToolParam ] = []
43- self .tool_functions : dict [str , Callable ] = {}
49+ self .tools : list [ChatCompletionToolParam ] = [] # for LLM
50+
51+ self .tool_functions : dict [str , Callable ] = {} # for local function call
52+ self .mcp_functions : dict [str , MCPServer ] = {} # for remote function call
53+
4454 self .parallel_tool_calls = parallel_tool_calls
4555 self .use_context_variables : dict [str , str ] = {} # 使用了上下文变量的函数以及相应的形参名称
4656 self .use_agent_variables : dict [str , str ] = {} # 使用了Agent变量的函数以及相应的形参名称
@@ -59,6 +69,23 @@ def __init__(
5969 self .tool_choice = tool_choice
6070
6171 self .messages : list [ChatCompletionMessageParam ] = []
72+ self .mcp_server = mcp_server
73+
74+ async def initialize (self ):
75+ """ 等待初始化智能体
76+ """
77+ for server in self .mcp_server :
78+ tools = await server .list_tools ()
79+ for tool in tools :
80+ self .tools .append ({
81+ 'type' : 'function' ,
82+ 'function' : {
83+ 'name' : tool .name ,
84+ 'description' : tool .description ,
85+ 'parameters' : tool .inputSchema
86+ }
87+ }) # 注意不能使用 add_tools 方法
88+ self .mcp_functions [tool .name ] = server
6289
6390 def chat (self , message : str = None ) -> ChatCompletionMessage :
6491 """ Agent 多轮对话
@@ -123,11 +150,13 @@ def add_tool_call_message(self, tool_content: str, tool_call_id: str) -> None:
123150 }
124151 self .messages .append (message )
125152
126- def tool (self , function : Callable ) -> Callable :
153+ def tool (self ) -> Callable :
127154 """ 标记一个外部工具函数
128155 """
129- self .add_tool_functions (function )
130- return function
156+ def wrapper (function : Callable ) -> Callable :
157+ self .add_tool_functions (function )
158+ return function
159+ return wrapper
131160
132161 def add_tools (self , * tools : 'Tool' ) -> 'Agent' :
133162 """ 添加外部工具
0 commit comments