Files
deer-flow/backend/src/tools/tools.py

85 lines
3.1 KiB
Python
Raw Normal View History

import logging
2026-01-14 07:19:43 +08:00
from langchain.tools import BaseTool
from src.config import get_app_config
from src.reflection import resolve_variable
2026-02-06 16:03:35 +08:00
from src.tools.builtins import ask_clarification_tool, present_file_tool, task_tool, view_image_tool
2026-01-16 21:48:00 +08:00
logger = logging.getLogger(__name__)
2026-01-16 21:48:00 +08:00
BUILTIN_TOOLS = [
present_file_tool,
2026-01-18 19:55:36 +08:00
ask_clarification_tool,
]
SUBAGENT_TOOLS = [
2026-02-05 19:59:25 +08:00
task_tool,
2026-02-06 16:03:35 +08:00
# task_status_tool is no longer exposed to LLM (backend handles polling internally)
2026-01-16 21:48:00 +08:00
]
2026-01-14 07:19:43 +08:00
2026-02-06 15:42:53 +08:00
def get_available_tools(
groups: list[str] | None = None,
include_mcp: bool = True,
model_name: str | None = None,
subagent_enabled: bool = False,
) -> list[BaseTool]:
"""Get all available tools from config.
Note: MCP tools should be initialized at application startup using
`initialize_mcp_tools()` from src.mcp module.
Args:
groups: Optional list of tool groups to filter by.
include_mcp: Whether to include tools from MCP servers (default: True).
model_name: Optional model name to determine if vision tools should be included.
2026-02-06 15:42:53 +08:00
subagent_enabled: Whether to include subagent tools (task, task_status).
Returns:
List of available tools.
"""
2026-01-14 07:19:43 +08:00
config = get_app_config()
2026-01-16 21:48:00 +08:00
loaded_tools = [resolve_variable(tool.use, BaseTool) for tool in config.tools if groups is None or tool.group in groups]
# Get cached MCP tools if enabled
# NOTE: We use ExtensionsConfig.from_file() instead of config.extensions
# to always read the latest configuration from disk. This ensures that changes
# made through the Gateway API (which runs in a separate process) are immediately
# reflected when loading MCP tools.
mcp_tools = []
if include_mcp:
try:
from src.config.extensions_config import ExtensionsConfig
from src.mcp.cache import get_cached_mcp_tools
extensions_config = ExtensionsConfig.from_file()
if extensions_config.get_enabled_mcp_servers():
mcp_tools = get_cached_mcp_tools()
if mcp_tools:
logger.info(f"Using {len(mcp_tools)} cached MCP tool(s)")
except ImportError:
logger.warning("MCP module not available. Install 'langchain-mcp-adapters' package to enable MCP tools.")
except Exception as e:
logger.error(f"Failed to get cached MCP tools: {e}")
# Conditionally add tools based on config
builtin_tools = BUILTIN_TOOLS.copy()
2026-02-06 15:42:53 +08:00
# Add subagent tools only if enabled via runtime parameter
if subagent_enabled:
builtin_tools.extend(SUBAGENT_TOOLS)
2026-02-06 16:03:35 +08:00
logger.info("Including subagent tools (task)")
# If no model_name specified, use the first model (default)
if model_name is None and config.models:
model_name = config.models[0].name
# Add view_image_tool only if the model supports vision
model_config = config.get_model_config(model_name) if model_name else None
if model_config is not None and model_config.supports_vision:
builtin_tools.append(view_image_tool)
logger.info(f"Including view_image_tool for model '{model_name}' (supports_vision=True)")
return loaded_tools + builtin_tools + mcp_tools