Skip to content

AI Incident Assistant

Production-style incident response playground: authenticated chat, MCP incident tools, template-driven CAN/RCA generation, Supabase sessions + quotas, and gateway-routed model orchestration.

Built with
Next.js · TypeScript · Supabase · LangChain · MCP · Vercel AI Gateway · Turnstile · LangSmith

The live app

Screenshots from the running build.

AI Incident Assistant, screenshot 1 of 3

What it is

AI Incident Assistant is a production-style incident response playground. Operators sign in, chat with an LLM, create and update Sev1 incidents, load templates (CAN, Sev1 canvas, RCA), and generate documents from structured incident data.

It shows gateway-routed models, MCP-backed tools, Supabase-backed auth and sessions, rolling per-user chat quotas, and two layers of observability: gateway-level metrics plus trace-level tooling.

The stack, layer by layer
LayerWhat runs there
InterfaceNext.js and TypeScript, deployed on Vercel
ModelsVercel AI Gateway routes each request to the configured model; LangChain trims the history
ToolsAn MCP server with incident, template and document tools, storing its data as local JSON
Auth and dataSupabase Auth, and Postgres for chat sessions and the per-user quota
Abuse protectionCloudflare Turnstile and a rolling per-user chat quota
ObservabilityGateway-level metrics plus LangSmith traces

What each area does

Capability matrix: what each area does
AreaHow it works
Chat orchestrationA route handler picks the model, binds the tools, trims the token history and keeps the session in sync.
ContextA thin system prompt, history trimmed with LangChain, and the saved session memory folded into the system string.
AuthenticationSupabase Auth with middleware guards at the edge, behind Cloudflare Turnstile.
Session memoryA Postgres JSONB envelope holds the raw messages, a structured memory summary and key facts. It is re-injected on every turn and written back to the same row the user owns under row level security.
Abuse controlsA rolling per-user window, counted through a Supabase RPC and enforced on the chat API.

MCP tools

MCP tools by group
GroupTools
Incident
  • create_incident
  • get_incident
  • update_incident
  • list_incidents
Templates
  • get_can_templates
  • get_sev1_canvas
  • get_rca_templates
  • load_template
Documents
  • generate_can_document
  • generate_rca_document
Meta
  • list_tools

Incident and template storage is local JSON in the MCP server's data directory, with validation, safer writes and bounded runtime.

How a request flows

Three views of one chat turn: how the prompt is built, how memory is kept between turns, and the order of calls from submit to response.

Request pipeline

The system prompt is thin, the history is trimmed with LangChain, and the saved session memory is folded into the system string. The request then branches into a model-only path or a tool-enabled one.

AI Incident Assistant request pipelineThe user client sends a message to the chat route, which checks the JWT and the chat quota in Supabase. The route builds a thin prompt with the prompt engine, trims history to a token budget, and injects persisted memory into the system prompt; the client lists and saves chat sessions, whose summary and key facts feed that memory. A decision gate sends conversational turns to the model only, and actionable turns to the tool-enabled path, which calls the MCP toolset. Both paths return the response payload.User clientChat routeJWT and quota in SupabasePrompt engineTrim historyInject memoryfrom saved chat sessionsDecision gateModel onlyTool enabledMCP toolsetResponseconversationalactionable
The chat route checks the session and quota first, then builds a small prompt with memory folded in. The decision gate only reaches for tools when the turn needs them.

Context retention

From the browser, the envelope is loaded and merged, optional CAN guardrails apply, the history is trimmed to a token budget and the model generates. Memory is written back to the same row the user already owns under RLS.

AI Incident Assistant context retentionThe chat UI sends recent history and the conversationId. The route loads the JSON envelope from the chat_sessions row, then updates the incident summary and structured key facts, persisting those memory fields back to the row. For a CAN-style report with missing facts it asks for the missing fields before calling the model. Otherwise it skips a duplicate last user turn, trims history to a token budget, builds the system prompt with the memory block, and calls the AI Gateway, whose reply returns to the chat UI.Chat UIHistoryrecent turns, conversationIdLoad envelopereads the chat_sessions rowUpdate memorysummary, key facts, persistedCAN report?Ask for factsback to the chatSkip duplicatelast user turnSystem promptplus memory blockTrim historytoken budgetAI GatewayReply in the chat UImissingok
Memory lives in the same chat_sessions row the user already owns under RLS: read at the start of a turn, updated before the model call.

End-to-end runtime

The request runs from the user's submit through API orchestration and the decision gates to optional MCP calls, then the response.

AI Incident Assistant end-to-end runtimeA sequence across the user, client, Supabase, API route, prompt engine, AI Gateway and MCP server: the prompt is posted to the chat route, the JWT is validated, prior memory is loaded when a conversation exists, the quota is checked, a thin system prompt is assembled and history trimmed, memory is updated, and then either the model answers directly or, for actionable turns, tools are registered and called in a loop before the final response. The client renders the reply and saves the session.1User → ClientSubmit prompt2Client → RoutePOST /api/chat3Route → SupabaseValidate JWT session4Supabase → RouteAuthenticated useroptconversationId present5Route → SupabaseSELECT chat_sessions.messages6Supabase → RoutePrior memory, stored messagesCAN-style requests may return early iffacts are missing7Route → Supabaseincrement_and_check_chat_quota RPC8Supabase → RouteAllowed, remainingIntent and entity detection9Route → PromptAssemble thin system prompt10RouteTrim history (LangChain)optconversationId present11Route → SupabaseUPDATE envelope.memoryAppend memory block to the system stringaltconversational12Route → GatewaygenerateText13Gateway → RouteResponseactionable14Route → MCPList and register tools15Route → GatewaygenerateText with toolsloopeach tool call16Gateway → MCPCall tool17MCP → GatewayTool result18Gateway → RouteFinal response19Route → ClientJSON and metadata20Client → SupabaseDebounced save of chat_sessions (RLS)21Client → UserRender response
Every turn in order, with replies dashed or muted. Cobalt marks the path from the user's prompt to the rendered answer.

Environment controls

Environment variables by area
AreaVariables
Models
  • AI_GATEWAY_MODEL
  • AI_GATEWAY_BASE_URL
Supabase
  • NEXT_PUBLIC_SUPABASE_URL
  • NEXT_PUBLIC_SUPABASE_ANON_KEY
Quota
  • CHAT_QUOTA_MAX
  • CHAT_QUOTA_WINDOW_HOURS
Security
  • TURNSTILE_SECRET_KEY
  • CAPTCHA_COOKIE_SECRET