From 84a7f7815c13fe8181f8cb26ba6d2e1372c5847a Mon Sep 17 00:00:00 2001 From: geniusroad <69345949+geniusroad@users.noreply.github.com> Date: Thu, 25 Dec 2025 21:10:04 +0800 Subject: [PATCH] refactor(graph): Refactor tool loading logic within nodes (#782) * refactor(graph): Optimize tool loading logic within nodes - Pre-copy the default tool list during initialization - Merge MCP server configuration with default tool handling - Simplify conditional branches and unify agent creation logic - Remove duplicated agent creation code blocks * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Willem Jiang Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/graph/nodes.py | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/graph/nodes.py b/src/graph/nodes.py index 829ab24..3f2e47c 100644 --- a/src/graph/nodes.py +++ b/src/graph/nodes.py @@ -1149,6 +1149,7 @@ async def _setup_and_execute_agent_step( configurable = Configuration.from_runnable_config(config) mcp_servers = {} enabled_tools = {} + loaded_tools = default_tools[:] # Get locale from workflow state to pass to agent creation # This fixes issue #743 where locale was not correctly retrieved in agent prompt @@ -1171,8 +1172,8 @@ async def _setup_and_execute_agent_step( # Create and execute agent with MCP tools if available if mcp_servers: + # Add MCP tools to loaded tools if MCP servers are configured client = MultiServerMCPClient(mcp_servers) - loaded_tools = default_tools[:] all_tools = await client.get_tools() for tool in all_tools: if tool.name in enabled_tools: @@ -1181,32 +1182,18 @@ async def _setup_and_execute_agent_step( ) loaded_tools.append(tool) - llm_token_limit = get_llm_token_limit_by_type(AGENT_LLM_MAP[agent_type]) - pre_model_hook = partial(ContextManager(llm_token_limit, 3).compress_messages) - agent = create_agent( - agent_type, - agent_type, - loaded_tools, - agent_type, - pre_model_hook, - interrupt_before_tools=configurable.interrupt_before_tools, - locale=locale, - ) - return await _execute_agent_step(state, agent, agent_type, config) - else: - # Use default tools if no MCP servers are configured - llm_token_limit = get_llm_token_limit_by_type(AGENT_LLM_MAP[agent_type]) - pre_model_hook = partial(ContextManager(llm_token_limit, 3).compress_messages) - agent = create_agent( - agent_type, - agent_type, - default_tools, - agent_type, - pre_model_hook, - interrupt_before_tools=configurable.interrupt_before_tools, - locale=locale, - ) - return await _execute_agent_step(state, agent, agent_type, config) + llm_token_limit = get_llm_token_limit_by_type(AGENT_LLM_MAP[agent_type]) + pre_model_hook = partial(ContextManager(llm_token_limit, 3).compress_messages) + agent = create_agent( + agent_type, + agent_type, + loaded_tools, + agent_type, + pre_model_hook, + interrupt_before_tools=configurable.interrupt_before_tools, + locale=locale, + ) + return await _execute_agent_step(state, agent, agent_type, config) async def researcher_node(