Skip to content

Commit 3ae3b9c

Browse files
authored
🐛Fixed bug: The mcp server will only be connected when the corresponding mcp tool is used.
🐛Fixed bug: The mcp server will only be connected when the corresponding mcp tool is used.
2 parents e39ca0d + 74a6fd6 commit 3ae3b9c

File tree

4 files changed

+407
-86
lines changed

4 files changed

+407
-86
lines changed

‎backend/agents/create_agent_info.py‎

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

302279
async 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

‎backend/database/agent_db.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def add_tool_field(tool_info):
302302
tool_info["source"] = tool.source
303303
tool_info["class_name"] = tool.class_name
304304
tool_info["is_available"] = tool.is_available
305+
tool_info["usage"] = tool.usage
305306
tool_info["inputs"] = tool.inputs
306307
tool_info["output_type"] = tool.output_type
307308
return tool_info

‎sdk/nexent/core/agents/agent_model.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ToolConfig(BaseModel):
2525
output_type: Optional[str] = Field(description="Tool output type")
2626
params: Dict[str, Any] = Field(description="Initialization parameters")
2727
source: str = Field(description="Tool source, can be local or mcp")
28+
usage: Optional[str] = Field(description="MCP server name", default=None)
2829
metadata: Optional[Dict[str, Any]] = Field(description="Metadata", default=None)
2930

3031
class AgentConfig(BaseModel):

0 commit comments

Comments
 (0)