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 <willem.jiang@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
geniusroad
2025-12-25 21:10:04 +08:00
committed by GitHub
parent fb319aaa44
commit 84a7f7815c

View File

@@ -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(