Tools Runtime
Hermes tools are self-registering functions grouped into toolsets and executed through a central registry/dispatch system.
Primary files:
tools/registry.pymodel_tools.pytoolsets.pytools/terminal_tool.pytools/environments/*
Tool registration modelβ
Each tool module calls registry.register(...) at import time.
model_tools.py is responsible for importing/discovering tool modules and building the schema list used by the model.
How registry.register() worksβ
Every tool file in tools/ calls registry.register() at module level to declare itself. The function signature is:
registry.register(
name="terminal", # Unique tool name (used in API schemas)
toolset="terminal", # Toolset this tool belongs to
schema={...}, # OpenAI function-calling schema (description, parameters)
handler=handle_terminal, # The function that executes when the tool is called
check_fn=check_terminal, # Optional: returns True/False for availability
requires_env=["SOME_VAR"], # Optional: env vars needed (for UI display)
is_async=False, # Whether the handler is an async coroutine
description="Run commands", # Human-readable description
emoji="π»", # Emoji for spinner/progress display
)
Each call creates a ToolEntry stored in the singleton ToolRegistry._tools dict keyed by tool name. If a name collision occurs across toolsets, a warning is logged and the later registration wins.
Discovery: discover_builtin_tools()β
When model_tools.py is imported, it calls discover_builtin_tools() from tools/registry.py. This function scans every tools/*.py file using AST parsing to find modules that contain top-level registry.register() calls, then imports them:
# tools/registry.py (simplified)
def discover_builtin_tools(tools_dir=None):
tools_path = Path(tools_dir) if tools_dir else Path(__file__).parent
for path in sorted(tools_path.glob("*.py")):
if path.name in {"__init__.py", "registry.py", "mcp_tool.py"}:
continue
if _module_registers_tools(path): # AST check for top-level registry.register()
importlib.import_module(f"tools.{path.stem}")
This auto-discovery means new tool files are picked up automatically β no manual list to maintain. The AST check only matches top-level registry.register() calls (not calls inside functions), so helper modules in tools/ are not imported.
Each import triggers the module's registry.register() calls. Errors in optional tools (e.g., missing fal_client for image generation) are caught and logged β they don't prevent other tools from loading.
After core tool discovery, MCP tools and plugin tools are also discovered:
- MCP tools β
tools.mcp_tool.discover_mcp_tools()reads MCP server config and registers tools from external servers. - Plugin tools β
hermes_cli.plugins.discover_plugins()loads user/project/pip plugins that may register additional tools.
Tool availability checking (check_fn)β
Each tool can optionally provide a check_fn β a callable that returns True when the tool is available and False otherwise. Typical checks include:
- API key present β e.g.,
lambda: bool(os.environ.get("SERP_API_KEY"))for web search - Service running β e.g., checking if the Honcho server is configured
- Binary installed β e.g., verifying
playwrightis available for browser tools
When registry.get_definitions() builds the schema list for the model, it runs each tool's check_fn():
# Simplified from registry.py
if entry.check_fn:
try:
available = bool(entry.check_fn())
except Exception:
available = False # Exceptions = unavailable
if not available:
continue # Skip this tool entirely
Key behaviors:
- Check results are cached per-call β if multiple tools share the same
check_fn, it only runs once. - Exceptions in
check_fn()are treated as "unavailable" (fail-safe). - The
is_toolset_available()method checks whether a toolset'scheck_fnpasses, used for UI display and toolset resolution.
Toolset resolutionβ
Toolsets are named bundles of tools. Hermes resolves them through:
- explicit enabled/disabled toolset lists
- platform presets (
hermes-cli,hermes-telegram, etc.) - dynamic MCP toolsets
- curated special-purpose sets like
hermes-acp
How get_tool_definitions() filters toolsβ
The main entry point is model_tools.get_tool_definitions(enabled_toolsets, disabled_toolsets, quiet_mode):
-
If
enabled_toolsetsis provided β only tools from those toolsets are included. Each toolset name is resolved viaresolve_toolset()which expands composite toolsets into individual tool names. -
If
disabled_toolsetsis provided β start with ALL toolsets, then subtract the disabled ones. -
If neither β include all known toolsets.
-
Registry filtering β the resolved tool name set is passed to
registry.get_definitions(), which appliescheck_fnfiltering and returns OpenAI-format schemas. -
Dynamic schema patching β after filtering,
execute_codeandbrowser_navigateschemas are dynamically adjusted to only reference tools that actually passed filtering (prevents model hallucination of unavailable tools).
Legacy toolset namesβ
Old toolset names with _tools suffixes (e.g., web_tools, terminal_tools) are mapped to their modern tool names via _LEGACY_TOOLSET_MAP for backward compatibility.
Dispatchβ
At runtime, tools are dispatched through the central registry, with agent-loop exceptions for some agent-level tools such as memory/todo/session-search handling.
Dispatch flow: model tool_call β handler executionβ
When the model returns a tool_call, the flow is:
Model response with tool_call
β
run_agent.py agent loop
β
model_tools.handle_function_call(name, args, task_id, user_task)
β
[Agent-loop tools?] β handled directly by agent loop (todo, memory, session_search, delegate_task)
β
[Plugin pre-hook] β invoke_hook("pre_tool_call", ...)
β
registry.dispatch(name, args, **kwargs)
β
Look up ToolEntry by name
β
[Async handler?] β bridge via _run_async()
[Sync handler?] β call directly
β
Return result string (or JSON error)
β
[Plugin post-hook] β invoke_hook("post_tool_call", ...)
Error wrappingβ
All tool execution is wrapped in error handling at two levels:
-
registry.dispatch()β catches any exception from the handler and returns{"error": "Tool execution failed: ExceptionType: message"}as JSON. -
handle_function_call()β wraps the entire dispatch in a secondary try/except that returns{"error": "Error executing tool_name: message"}.
This ensures the model always receives a well-formed JSON string, never an unhandled exception.
Agent-loop toolsβ
Four tools are intercepted before registry dispatch because they need agent-level state (TodoStore, MemoryStore, etc.):
todoβ planning/task trackingmemoryβ persistent memory writessession_searchβ cross-session recalldelegate_taskβ spawns subagent sessions
These tools' schemas are still registered in the registry (for get_tool_definitions), but their handlers return a stub error if dispatch somehow reaches them directly.
Async bridgingβ
When a tool handler is async, _run_async() bridges it to the sync dispatch path:
- CLI path (no running loop) β uses a persistent event loop to keep cached async clients alive
- Gateway path (running loop) β spins up a disposable thread with
asyncio.run() - Worker threads (parallel tools) β uses per-thread persistent loops stored in thread-local storage
The DANGEROUS_PATTERNS approval flowβ
The terminal tool integrates a dangerous-command approval system defined in tools/approval.py:
-
Pattern detection β
DANGEROUS_PATTERNSis a list of(regex, description)tuples covering destructive operations:- Recursive deletes (
rm -rf) - Filesystem formatting (
mkfs,dd) - SQL destructive operations (
DROP TABLE,DELETE FROMwithoutWHERE) - System config overwrites (
> /etc/) - Service manipulation (
systemctl stop) - Remote code execution (
curl | sh) - Fork bombs, process kills, etc.
- Recursive deletes (
-
Detection β before executing any terminal command,
detect_dangerous_command(command)checks against all patterns. -
Approval prompt β if a match is found:
- CLI mode β an interactive prompt asks the user to approve, deny, or allow permanently
- Gateway mode β an async approval callback sends the request to the messaging platform
- Smart approval β optionally, an auxiliary LLM can auto-approve low-risk commands that match patterns (e.g.,
rm -rf node_modules/is safe but matches "recursive delete")
-
Session state β approvals are tracked per-session. Once you approve "recursive delete" for a session, subsequent
rm -rfcommands don't re-prompt. -
Permanent allowlist β the "allow permanently" option writes the pattern to
config.yaml'scommand_allowlist, persisting across sessions.
Terminal/runtime environmentsβ
The terminal system supports multiple backends:
- local
- docker
- ssh
- singularity
- modal
- daytona
It also supports:
- per-task cwd overrides
- background process management
- PTY mode
- approval callbacks for dangerous commands
Concurrencyβ
Tool calls may execute sequentially or concurrently depending on the tool mix and interaction requirements.