AI Database Diagnostics

An n8n diagnostics agent that loops through planning, MCP tool calls, and retries, then returns a finding an operator can act on.

PostgreSQL · AI · Diagnostics · n8n · MCP · Automation

Problem

Monitoring fires alerts; people still dig through sessions, locks, and query lists by hand. Context is scattered, response time suffers, and the same playbooks get re-discovered under pressure.

What it does

An AI diagnostics agent sits behind chat or alert hooks. n8n owns the looping. A planner model decides whether live data is required, then calls MCP tools to run read-only checks—active sessions, wait events, blocking locks, long-running statements, cache and I/O stats. Those queries can run one at a time or in parallel. A synthesizer turn turns the results into a short, actionable answer.

AI diagnostics with MCP tools and n8n loopsOperatorchat / alertn8n loopsthink · retryPlannerMCP toolsread-only SQLThink loopreason firstSQL loopsingle or parallelError loopfix and retrySynthesize → operator
n8n owns the agent loops. MCP tools run read-only SQL against live process state.

MCP tools

The agent does not invent a private SQL socket. It calls a small MCP-style tool surface:

  • Execute query — read-only SELECT against the selected instance
  • Process list — a sibling workflow that analyzes running sessions when the question is about who is on the box

Tools return columns, rows, and errors. The model never writes. Hostnames, API keys, and internal proxy URLs stay out of the story.

Agent loops in n8n

Two sanitized n8n workflows drive this: a database diagnostics agent and a process-list analysis agent. Both share the same loop shape.

  1. Think loop — reason first; reply with no SQL when the answer is already known
  2. SQL loop — emit one <execute_sql> block or several, then run them through MCP
  3. Error loop — if a probe fails (missing relation, bad column, syntax), ask the model for a corrected query and retry
  4. Feedback loop — keep session history so follow-up questions stay in context
  5. Synthesize — one finding back to the operator

The built-in n8n AI Agent node was skipped so the proxy, retries, and parallel probes stay explicit.

n8n AI diagnostics workflowWebhookchat payloadSessionhistoryPlanner LLMParse plannone · one · many SQLNo SQLanswer nowMCP executeread-only query APIParallel MCPseveral probesError loopcorrect SQL · retrySynthesizerone findingReply to operator
Sanitized from the n8n agent: webhook intake, planner, MCP execute-query tools, parallel probes, and retry loops.

How a turn runs

  1. Intake — operator question or monitoring signal, plus session id
  2. Plan — model chooses no SQL, one query, or several probes
  3. Collect — MCP execute-query, in parallel when the planner asked for more than one
  4. Recover — retry only when the error looks fixable
  5. Synthesize — combined evidence into likely cause and next checks

Outcome

Faster triage with shared language: what is blocked, who is waiting, which statements dominate—and what to look at next—without pasting hostnames or internal service URLs into the product story.