@@ -164,7 +164,8 @@ async def create_tool_config_list(agent_id, tenant_id, user_id):
164164 inputs = tool .get ("inputs" ),
165165 output_type = tool .get ("output_type" ),
166166 params = param_dict ,
167- source = tool .get ("source" )
167+ source = tool .get ("source" ),
168+ usage = tool .get ("usage" )
168169 )
169170
170171 if tool .get ("source" ) == "langchain" :
@@ -251,76 +252,58 @@ async def join_minio_file_description_to_query(minio_files, query):
251252 return final_query
252253
253254
254- def filter_mcp_servers_and_tools (agent_run_info , default_mcp_url , remote_mcp_list ) :
255+ def filter_mcp_servers_and_tools (input_agent_config : AgentConfig , mcp_info_dict ) -> list :
255256 """
256- čżćť¤ MCP ćĺĄĺ¨ĺ塼ĺ
ˇďźĺŞäżçĺŽé
ç¨ĺ°ç MCP ćĺĄĺ¨
257- ćŻćĺ¤çş§ agentďźéĺ˝ćŁćĽććĺ agent ç塼ĺ
ˇ
258-
259- Args:
260- agent_run_info: AgentRunInfo 寚蹥
261- default_mcp_url: éťčޤ MCP ćĺĄĺ¨ URL
262- remote_mcp_list: čżç¨ MCP ćĺĄĺ¨ĺ襨
263-
264- Returns:
265- None (ç´ćĽäżŽćš agent_run_info 寚蹥)
257+ Filter mcp servers and tools, only keep the actual used mcp servers
258+ Support multi-level agent, recursively check all sub-agent tools
266259 """
267260 used_mcp_urls = set ()
268- has_mcp_tools = False
269-
270- # éĺ˝ćŁćĽćć agent ç塼ĺ
ˇ
271- def check_agent_tools (agent_config ):
272- nonlocal has_mcp_tools
273- # ćŁćĽĺ˝ĺ agent ç塼ĺ
ˇ
274- for tool in getattr (agent_config , "tools" , []):
275- if hasattr (tool , "source" ) and tool .source == "mcp" :
276- has_mcp_tools = True
277- # ĺŻšäş MCP 塼ĺ
ˇďźäť usage ĺ掾čˇĺ MCP ćĺĄĺ¨ĺç§°
278- if hasattr (tool , "usage" ) and tool .usage :
279- mcp_server_name = tool .usage
280- # äťčżç¨ MCP ĺ襨ä¸ćĽćžĺŻšĺşç URL
281- for remote_mcp_info in remote_mcp_list :
282- if (remote_mcp_info ["remote_mcp_server_name" ] == mcp_server_name and
283- remote_mcp_info ["status" ]):
284- used_mcp_urls .add (remote_mcp_info ["remote_mcp_server" ])
285- break
286-
287- # éĺ˝ćŁćĽĺ agent
288- for sub_agent_config in getattr (agent_config , "managed_agents" , []):
289- check_agent_tools (sub_agent_config )
290261
291- # ćŁćĽćć agent ç塼ĺ
ˇ
292- check_agent_tools (agent_run_info .agent_config )
262+ # Recursively check all agent tools
263+ def check_agent_tools (agent_config : AgentConfig ):
264+ # Check current agent tools
265+ for tool in agent_config .tools :
266+ if tool .source == "mcp" and tool .usage in mcp_info_dict :
267+ used_mcp_urls .add (mcp_info_dict [tool .usage ]["remote_mcp_server" ])
293268
294- # ĺŚćć MCP 塼ĺ
ˇä˝ć˛Ąććžĺ°ĺŻšĺşçćĺĄĺ¨ďźä˝żç¨éťčޤćĺĄĺ¨
295- if has_mcp_tools and not used_mcp_urls :
296- used_mcp_urls .add (default_mcp_url )
269+ # Recursively check sub-agent
270+ for sub_agent_config in agent_config .managed_agents :
271+ check_agent_tools (sub_agent_config )
272+
273+ # Check all agent tools
274+ check_agent_tools (input_agent_config )
297275
298- # ç´ćĽčŽžç˝Ž mcp_host 为ćžĺ°ç URL ĺ襨
299- agent_run_info .mcp_host = list (used_mcp_urls )
276+ return list (used_mcp_urls )
300277
301278
302279async def create_agent_run_info (agent_id , minio_files , query , history , authorization , language : str = 'zh' ):
303280 user_id , tenant_id = get_current_user_id (authorization )
304281
305282 final_query = await join_minio_file_description_to_query (minio_files = minio_files , query = query )
306283 model_list = await create_model_config_list (tenant_id )
284+ agent_config = await create_agent_config (agent_id = agent_id , tenant_id = tenant_id , user_id = user_id ,
285+ language = language , last_user_query = final_query )
307286
308287 remote_mcp_list = await get_remote_mcp_server_list (tenant_id = tenant_id )
309288 default_mcp_url = urljoin (config_manager .get_config ("NEXENT_MCP_SERVER" ), "sse" )
310- mcp_host = [default_mcp_url ]
311- for remote_mcp_info in remote_mcp_list :
312- if remote_mcp_info ["status" ]:
313- mcp_host .append (remote_mcp_info ["remote_mcp_server" ])
289+ remote_mcp_list .append ({
290+ "remote_mcp_server_name" : "nexent" ,
291+ "remote_mcp_server" : default_mcp_url ,
292+ "status" : True
293+ })
294+ remote_mcp_dict = {record ["remote_mcp_server_name" ]: record for record in remote_mcp_list if record ["status" ]}
295+
296+ # Filter MCP servers and tools
297+ mcp_host = filter_mcp_servers_and_tools (agent_config , remote_mcp_dict )
298+
314299
315300 agent_run_info = AgentRunInfo (
316301 query = final_query ,
317302 model_config_list = model_list ,
318303 observer = MessageObserver (lang = language ),
319- agent_config = await create_agent_config (agent_id = agent_id , tenant_id = tenant_id , user_id = user_id ,
320- language = language , last_user_query = final_query ),
304+ agent_config = agent_config ,
321305 mcp_host = mcp_host ,
322306 history = history ,
323307 stop_event = threading .Event ()
324308 )
325-
326309 return agent_run_info
0 commit comments