ai-powered-conversational-intelligence

Tool / MCP Abstraction (agent/tools/)

What this is

A generic, governed way to describe and invoke this application’s real capabilities — SQL, document RAG, policy RAG, web search, media search, media generation — by name, with a uniform contract: name, description, category (READ/WRITE), required permission, timeout, retry policy, and structured audit logging on every call.

This is the first increment of a broader roadmap (see PLATFORM_TRANSFORMATION_ASSESSMENT.md) toward evolving this project into a multi-source “Enterprise AI Intelligence Platform.” It was scoped and built as its own, independently useful deliverable — not a partial feature — but it is intentionally the foundation for later steps (a research-planner upgrade, a future external MCP client), not a consumer-facing feature on its own.

Direction matters, and there are now two packages. This one is MCP-shaped so this app’s capabilities could be exposed to an external MCP client. mcp_client/ is the opposite direction – a client of remote MCP servers (Batch 8; see architecture/source-connector-contract.md Section 10). They share no code, and a remote tool is never registered into this package’s ToolRegistry – that is pinned by an AST check in tests/test_mcp_client.py.

What this is not

Not a rewrite of agent/orchestrator/nodes.py. That module’s five hardcoded LangGraph nodes (sql_subgraph_node, document_rag_node, policy_rag_node, web_search_node, generation_node, media_search_node) are completely unmodified. agent.orchestrator.graph.run_orchestrated still calls them directly in production, exactly as before this package existed. Every Tool in agent/tools/definitions.py wraps the same underlying function those nodes already call — there is no duplicated retrieval/generation/validation logic anywhere in this package.

Why the orchestrator wasn’t rewired to use it (yet)

agent/orchestrator/nodes.py is a ~1,150-line, heavily security-reviewed module: it already does its own permission filtering (router_node’s SOURCE_PERMISSIONS check), its own session-level expensive-source cost ceiling, its own human-approval gate for media generation, and its own carefully fail-open/fail-closed handling per source. Swapping those node bodies to call through ToolRegistry.execute would either duplicate that logic (permission-checked twice, in two different ways, with real drift risk) or require carefully threading the registry’s own checks through code that already has its own — a non-trivial, real-regression-risk change to a working, tested, audited code path, for no immediate behavioral benefit (nothing today needs to choose a tool dynamically; the orchestrator’s routing is a fixed graph edge per source). Per this project’s own engineering principle (“choose the smallest safe architectural change”), that integration is deferred to the next roadmap step, where it becomes load-bearing: a research planner that needs to select and invoke tools dynamically by name is the natural, first real consumer.

Package layout

The six tools

Tool Category Permission Wraps
sql_query READ EXECUTE_SQL agent.graph.run_agent
document_search READ DOCUMENTS_READ rag.graph.run_rag(collection="documents")
policy_search READ POLICY_RAG_QUERY rag.graph.run_rag(collection="policies")
web_search READ WEB_SEARCH search.web_search.web_search
media_search READ MEDIA_SEARCH media.search.search_media
media_generation WRITE MEDIA_GENERATE agent.orchestrator.nodes.execute_generation

media_generation is the only WRITE tool — it spends real, metered provider credit. The ToolCategory.WRITE tag itself grants no extra authorization; the actual human-approval gate (Settings.require_generation_approval) still lives entirely in generation_node/execute_generation, upstream of and unrelated to this package.

What ToolRegistry.execute enforces, uniformly

  1. Permission check (fail-closed) — agent.authz.has_role_permission, the exact same check router_node already performs for its own sources. Raises ToolPermissionError, logs a tool_permission_denied audit event.
  2. Hard per-attempt timeout — thread-based (daemon thread + join), mirroring db.execution._execute_with_timeout’s own pattern, the only portable way to bound an arbitrary Python call’s wall-clock time on Windows. A backstop, not the only layer — each wrapped function already has its own inner timeouts (DB query timeout, HTTP client timeout).
  3. Retry policy — max_attempts=1 by default for every registered tool; most of the wrapped functions already have their own internal retry/self-correction loop, so an outer retry would only add latency.
  4. Audit logging — one security.audit_log.log_security_event call per outcome: tool_permission_denied, tool_executed, or tool_execution_failed.
  5. Never raises a handler’s own exception — a failed tool call returns a ToolResult(success=False, error=...), never crashes the caller, matching this codebase’s “a source failure must not crash the whole run” posture.

Testing

27 tests, all passing; the full existing suite (1,406 tests before this change) passes unmodified alongside them.

Known limitations (disclosed, not hidden)