1919from services .memory_config_service import build_memory_context
2020from services .image_service import get_vlm_model
2121from database .agent_db import search_agent_info_by_agent_id , query_sub_agents_id_list
22+ from database .agent_version_db import query_current_version_no
2223from database .tool_db import search_tools_for_sub_agent
2324from database .model_management_db import get_model_records , get_model_by_model_id
2425from database .client import minio_client
@@ -75,26 +76,31 @@ async def create_agent_config(
7576 language : str = LANGUAGE ["ZH" ],
7677 last_user_query : str = None ,
7778 allow_memory_search : bool = True ,
79+ version_no : int = 0 ,
7880):
7981 agent_info = search_agent_info_by_agent_id (
80- agent_id = agent_id , tenant_id = tenant_id )
82+ agent_id = agent_id , tenant_id = tenant_id , version_no = version_no )
8183
8284 # create sub agent
8385 sub_agent_id_list = query_sub_agents_id_list (
84- main_agent_id = agent_id , tenant_id = tenant_id )
86+ main_agent_id = agent_id , tenant_id = tenant_id , version_no = version_no )
8587 managed_agents = []
8688 for sub_agent_id in sub_agent_id_list :
89+ # Get the current published version for this sub-agent (from draft version 0)
90+ sub_agent_version_no = query_current_version_no (
91+ agent_id = sub_agent_id , tenant_id = tenant_id ) or 0
8792 sub_agent_config = await create_agent_config (
8893 agent_id = sub_agent_id ,
8994 tenant_id = tenant_id ,
9095 user_id = user_id ,
9196 language = language ,
9297 last_user_query = last_user_query ,
9398 allow_memory_search = allow_memory_search ,
99+ version_no = sub_agent_version_no ,
94100 )
95101 managed_agents .append (sub_agent_config )
96102
97- tool_list = await create_tool_config_list (agent_id , tenant_id , user_id )
103+ tool_list = await create_tool_config_list (agent_id , tenant_id , user_id , version_no = version_no )
98104
99105 # Build system prompt: prioritize segmented fields, fallback to original prompt field if not available
100106 duty_prompt = agent_info .get ("duty_prompt" , "" )
@@ -202,13 +208,13 @@ async def create_agent_config(
202208 return agent_config
203209
204210
205- async def create_tool_config_list (agent_id , tenant_id , user_id ):
211+ async def create_tool_config_list (agent_id , tenant_id , user_id , version_no : int = 0 ):
206212 # create tool
207213 tool_config_list = []
208214 langchain_tools = await discover_langchain_tools ()
209215
210216 # now only admin can modify the agent, user_id is not used
211- tools_list = search_tools_for_sub_agent (agent_id , tenant_id )
217+ tools_list = search_tools_for_sub_agent (agent_id , tenant_id , version_no = version_no )
212218 for tool in tools_list :
213219 param_dict = {}
214220 for param in tool .get ("params" , []):
@@ -355,7 +361,21 @@ async def create_agent_run_info(
355361 user_id : str ,
356362 language : str = "zh" ,
357363 allow_memory_search : bool = True ,
364+ is_debug : bool = False ,
358365):
366+ # Determine which version_no to use based on is_debug flag
367+ # If is_debug=false, use the current published version (current_version_no)
368+ # If is_debug=true, use version 0 (draft/editing state)
369+ if is_debug :
370+ version_no = 0
371+ else :
372+ # Get current published version number
373+ version_no = query_current_version_no (agent_id = agent_id , tenant_id = tenant_id )
374+ # Fallback to 0 if no published version exists
375+ if version_no is None :
376+ version_no = 0
377+ logger .info (f"Agent { agent_id } has no published version, using draft version 0" )
378+
359379 final_query = await join_minio_file_description_to_query (minio_files = minio_files , query = query )
360380 model_list = await create_model_config_list (tenant_id )
361381 agent_config = await create_agent_config (
@@ -365,19 +385,45 @@ async def create_agent_run_info(
365385 language = language ,
366386 last_user_query = final_query ,
367387 allow_memory_search = allow_memory_search ,
388+ version_no = version_no ,
368389 )
369390
370- remote_mcp_list = await get_remote_mcp_server_list (tenant_id = tenant_id )
391+ remote_mcp_list = await get_remote_mcp_server_list (tenant_id = tenant_id , is_need_auth = True )
371392 default_mcp_url = urljoin (LOCAL_MCP_SERVER , "sse" )
372393 remote_mcp_list .append ({
373394 "remote_mcp_server_name" : "nexent" ,
374395 "remote_mcp_server" : default_mcp_url ,
375- "status" : True
396+ "status" : True ,
397+ "authorization_token" : None
376398 })
377399 remote_mcp_dict = {record ["remote_mcp_server_name" ]: record for record in remote_mcp_list if record ["status" ]}
378400
379- # Filter MCP servers and tools
380- mcp_host = filter_mcp_servers_and_tools (agent_config , remote_mcp_dict )
401+ # Filter MCP servers and tools, and build mcp_host with authorization
402+ used_mcp_urls = filter_mcp_servers_and_tools (agent_config , remote_mcp_dict )
403+
404+ # Build mcp_host list with authorization tokens
405+ mcp_host = []
406+ for url in used_mcp_urls :
407+ # Find the MCP record for this URL
408+ mcp_record = None
409+ for record in remote_mcp_list :
410+ if record .get ("remote_mcp_server" ) == url and record .get ("status" ):
411+ mcp_record = record
412+ break
413+
414+ if mcp_record :
415+ mcp_config = {
416+ "url" : url ,
417+ "transport" : "sse" if url .endswith ("/sse" ) else "streamable-http"
418+ }
419+ # Add authorization if present
420+ auth_token = mcp_record .get ("authorization_token" )
421+ if auth_token :
422+ mcp_config ["authorization" ] = auth_token
423+ mcp_host .append (mcp_config )
424+ else :
425+ # Fallback to string format if record not found
426+ mcp_host .append (url )
381427
382428 agent_run_info = AgentRunInfo (
383429 query = final_query ,
0 commit comments