# fuatu/safeai architecture

> Architecture diagram of the GitHub repository fuatu/safeai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fuatu/safeai
- Repository: https://github.com/fuatu/safeai

## Overview

SafeAI is a local AI-agent security gateway with a dashboard for reviewing risky actions and session history. Agents send tool calls through the gateway; it identifies the client, evaluates risk, explains the action, masks secrets, and either forwards it or pauses for human approval. The dashboard also manages client connections, policies, and settings, while audit records support session and action history. README capabilities include chat-history ingestion and an OpenAI-compatible proxy; their detailed implementations were not excerpted, so only documented entry points are shown. Edges reflect excerpted calls or README claims.

Main entry points: [`frontend/src/App.tsx`](https://github.com/fuatu/safeai/blob/main/frontend/src/App.tsx) (Dashboard App), [`backend/gateway/proxy.py`](https://github.com/fuatu/safeai/blob/main/backend/gateway/proxy.py) (MCP Gateway), [`backend/routes/api.py`](https://github.com/fuatu/safeai/blob/main/backend/routes/api.py) (Dashboard API), [`backend/gateway/openai_proxy.py`](https://github.com/fuatu/safeai/blob/main/backend/gateway/openai_proxy.py) (OpenAI Proxy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Agent Gateway"]
  node_mcp["MCP Gateway<br/>[proxy.py]"]
  node_openai["OpenAI Proxy<br/>[openai_proxy.py]"]
  node_identity["Client Resolver<br/>[client_resolver.py]"]
  node_ingest["MCP Router<br/>[mcp_server.py]"]
  node_chat_history["Chat History Ingestion"]
end

subgraph group_protection["Security &amp; Decisions"]
  node_security["Risk Scoring<br/>[engine.py]"]
  node_dlp["Secret Masking<br/>[dlp.py]"]
  node_explainer["Action Explainer<br/>[engine.py]"]
  node_broker["Approval Broker<br/>[broker.py]"]
end

subgraph group_history["Audit &amp; Configuration"]
  node_api["Dashboard API<br/>[api.py]"]
  node_ws["WebSocket Updates<br/>[ws.py]"]
  node_audit[("Audit Store<br/>[audit_store.py]")]
  node_schemas["Session &amp; Policy Models<br/>[schemas.py]"]
  node_configs["Client Config Catalog<br/>[client_configs.py]"]
end

subgraph group_dashboard["Dashboard"]
  node_app["Dashboard App<br/>[App.tsx]"]
  node_approval["Approval Dialog<br/>[ApprovalModal.tsx]"]
  node_sessions["Session Views<br/>[SessionsPage.tsx]"]
  node_timeline["Session Timeline"]
  node_live["Live Activity"]
  node_settings["Settings Panel<br/>[SettingsPanel.tsx]"]
  node_clientpanel["Client Setup Panel"]
end

node_agent(("AI Agent"))
node_human(("User"))

node_agent -->|"sends tool calls"| node_ingest
node_ingest -->|"routes MCP"| node_mcp
node_mcp -->|"resolves client"| node_identity
node_mcp -->|"evaluates payload"| node_security
node_mcp -->|"masks secrets"| node_dlp
node_mcp -->|"explains action"| node_explainer
node_mcp -->|"requests decision"| node_broker
node_broker -->|"requests approval"| node_human
node_human -->|"submits decision"| node_api
node_api -->|"submits decision"| node_broker
node_mcp -->|"records actions"| node_audit
node_api -->|"reads sessions"| node_audit
node_audit -->|"stores records"| node_schemas
node_api -->|"evaluates intercept"| node_security
node_api -->|"explains intercept"| node_explainer
node_api -->|"serves client configs"| node_configs
node_openai -->|"evaluates requests"| node_security
node_openai -->|"requests decision"| node_broker
node_openai -->|"masks secrets"| node_dlp
node_openai -->|"explains action"| node_explainer
node_openai -->|"records actions"| node_audit
node_app -.->|"loads dashboard data"| node_api
node_app -->|"renders approval"| node_approval
node_app -->|"renders sessions"| node_sessions
node_sessions -->|"renders timeline"| node_timeline
node_app -->|"renders activity"| node_live
node_app -->|"renders settings"| node_settings
node_app -->|"renders client setup"| node_clientpanel
node_app -.->|"receives updates"| node_ws
node_ws -->|"reports approvals"| node_broker
node_chat_history -.->|"records conversations"| node_audit

click node_mcp "https://github.com/fuatu/safeai/blob/main/backend/gateway/proxy.py"
click node_openai "https://github.com/fuatu/safeai/blob/main/backend/gateway/openai_proxy.py"
click node_identity "https://github.com/fuatu/safeai/blob/main/backend/gateway/client_resolver.py"
click node_ingest "https://github.com/fuatu/safeai/blob/main/backend/gateway/mcp_server.py"
click node_security "https://github.com/fuatu/safeai/blob/main/backend/security/engine.py"
click node_dlp "https://github.com/fuatu/safeai/blob/main/backend/security/dlp.py"
click node_explainer "https://github.com/fuatu/safeai/blob/main/backend/explainer/engine.py"
click node_broker "https://github.com/fuatu/safeai/blob/main/backend/hitl/broker.py"
click node_api "https://github.com/fuatu/safeai/blob/main/backend/routes/api.py"
click node_ws "https://github.com/fuatu/safeai/blob/main/backend/routes/ws.py"
click node_audit "https://github.com/fuatu/safeai/blob/main/backend/storage/audit_store.py"
click node_schemas "https://github.com/fuatu/safeai/blob/main/backend/models/schemas.py"
click node_configs "https://github.com/fuatu/safeai/blob/main/backend/gateway/client_configs.py"
click node_app "https://github.com/fuatu/safeai/blob/main/frontend/src/App.tsx"
click node_approval "https://github.com/fuatu/safeai/blob/main/frontend/src/components/ApprovalModal.tsx"
click node_sessions "https://github.com/fuatu/safeai/blob/main/frontend/src/components/SessionsPage.tsx"
click node_timeline "https://github.com/fuatu/safeai/blob/main/frontend/src/components/SessionTimeline.tsx"
click node_live "https://github.com/fuatu/safeai/blob/main/frontend/src/components/LiveActivityView.tsx"
click node_settings "https://github.com/fuatu/safeai/blob/main/frontend/src/components/SettingsPanel.tsx"
click node_clientpanel "https://github.com/fuatu/safeai/blob/main/frontend/src/components/ClientConfigPanel.tsx"
click node_chat_history "https://github.com/fuatu/safeai/tree/main/backend/gateway"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mcp,node_openai,node_identity,node_ingest,node_chat_history,node_human toneBlue
class node_security,node_dlp,node_explainer,node_broker toneAmber
class node_api,node_ws,node_audit,node_schemas,node_configs toneMint
class node_app,node_approval,node_sessions,node_timeline,node_live,node_settings,node_clientpanel toneRose
class node_agent toneIndigo
```

## Components

### Agent Gateway

- [MCP Gateway](https://github.com/fuatu/safeai/blob/main/backend/gateway/proxy.py): `backend/gateway/proxy.py`
- [OpenAI Proxy](https://github.com/fuatu/safeai/blob/main/backend/gateway/openai_proxy.py): `backend/gateway/openai_proxy.py`
- [Client Resolver](https://github.com/fuatu/safeai/blob/main/backend/gateway/client_resolver.py): `backend/gateway/client_resolver.py`
- [MCP Router](https://github.com/fuatu/safeai/blob/main/backend/gateway/mcp_server.py): `backend/gateway/mcp_server.py`
- [Chat History Ingestion](https://github.com/fuatu/safeai/tree/main/backend/gateway): `backend/gateway`

### Security & Decisions

- [Risk Scoring](https://github.com/fuatu/safeai/blob/main/backend/security/engine.py): `backend/security/engine.py`
- [Secret Masking](https://github.com/fuatu/safeai/blob/main/backend/security/dlp.py): `backend/security/dlp.py`
- [Action Explainer](https://github.com/fuatu/safeai/blob/main/backend/explainer/engine.py): `backend/explainer/engine.py`
- [Approval Broker](https://github.com/fuatu/safeai/blob/main/backend/hitl/broker.py): `backend/hitl/broker.py`

### Audit & Configuration

- [Dashboard API](https://github.com/fuatu/safeai/blob/main/backend/routes/api.py): `backend/routes/api.py`
- [WebSocket Updates](https://github.com/fuatu/safeai/blob/main/backend/routes/ws.py): `backend/routes/ws.py`
- [Audit Store](https://github.com/fuatu/safeai/blob/main/backend/storage/audit_store.py): `backend/storage/audit_store.py`
- [Session & Policy Models](https://github.com/fuatu/safeai/blob/main/backend/models/schemas.py): `backend/models/schemas.py`
- [Client Config Catalog](https://github.com/fuatu/safeai/blob/main/backend/gateway/client_configs.py): `backend/gateway/client_configs.py`

### Dashboard

- [Dashboard App](https://github.com/fuatu/safeai/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Approval Dialog](https://github.com/fuatu/safeai/blob/main/frontend/src/components/ApprovalModal.tsx): `frontend/src/components/ApprovalModal.tsx`
- [Session Views](https://github.com/fuatu/safeai/blob/main/frontend/src/components/SessionsPage.tsx): `frontend/src/components/SessionsPage.tsx`
- [Session Timeline](https://github.com/fuatu/safeai/blob/main/frontend/src/components/SessionTimeline.tsx): `frontend/src/components/SessionTimeline.tsx`
- [Live Activity](https://github.com/fuatu/safeai/blob/main/frontend/src/components/LiveActivityView.tsx): `frontend/src/components/LiveActivityView.tsx`
- [Settings Panel](https://github.com/fuatu/safeai/blob/main/frontend/src/components/SettingsPanel.tsx): `frontend/src/components/SettingsPanel.tsx`
- [Client Setup Panel](https://github.com/fuatu/safeai/blob/main/frontend/src/components/ClientConfigPanel.tsx): `frontend/src/components/ClientConfigPanel.tsx`

### Other components

- **AI Agent**
- **User**

## Connections

- AI Agent → MCP Router: sends tool calls (evidence: `README.md`)
- MCP Router → MCP Gateway: routes MCP (evidence: `backend/gateway/mcp_server.py`)
- MCP Gateway → Client Resolver: resolves client (evidence: `backend/gateway/proxy.py`)
- MCP Gateway → Risk Scoring: evaluates payload (evidence: `backend/gateway/proxy.py`)
- MCP Gateway → Secret Masking: masks secrets (evidence: `backend/gateway/proxy.py`)
- MCP Gateway → Action Explainer: explains action (evidence: `backend/gateway/proxy.py`)
- MCP Gateway → Approval Broker: requests decision (evidence: `backend/gateway/proxy.py`)
- Approval Broker → User: requests approval (evidence: `README.md`)
- User → Dashboard API: submits decision (evidence: `backend/routes/api.py`)
- Dashboard API → Approval Broker: submits decision (evidence: `backend/routes/api.py`)
- MCP Gateway → Audit Store: records actions (evidence: `backend/gateway/proxy.py`)
- Dashboard API → Audit Store: reads sessions (evidence: `backend/routes/api.py`)
- Audit Store → Session & Policy Models: stores records (evidence: `backend/storage/audit_store.py`)
- Dashboard API → Risk Scoring: evaluates intercept (evidence: `backend/routes/api.py`)
- Dashboard API → Action Explainer: explains intercept (evidence: `backend/routes/api.py`)
- Dashboard API → Client Config Catalog: serves client configs (evidence: `backend/routes/api.py`)
- OpenAI Proxy → Risk Scoring: evaluates requests (evidence: `backend/gateway/openai_proxy.py`)
- OpenAI Proxy → Approval Broker: requests decision (evidence: `backend/gateway/openai_proxy.py`)
- OpenAI Proxy → Secret Masking: masks secrets (evidence: `backend/gateway/openai_proxy.py`)
- OpenAI Proxy → Action Explainer: explains action (evidence: `backend/gateway/openai_proxy.py`)
- OpenAI Proxy → Audit Store: records actions (evidence: `backend/gateway/openai_proxy.py`)
- Dashboard App → Dashboard API: loads dashboard data
- Dashboard App → Approval Dialog: renders approval (evidence: `frontend/src/App.tsx`)
- Dashboard App → Session Views: renders sessions (evidence: `frontend/src/App.tsx`)
- Session Views → Session Timeline: renders timeline
- Dashboard App → Live Activity: renders activity (evidence: `frontend/src/App.tsx`)
- Dashboard App → Settings Panel: renders settings (evidence: `frontend/src/App.tsx`)
- Dashboard App → Client Setup Panel: renders client setup (evidence: `frontend/src/App.tsx`)
- Dashboard App → WebSocket Updates: receives updates
- WebSocket Updates → Approval Broker: reports approvals (evidence: `backend/routes/ws.py`)
- Chat History Ingestion → Audit Store: records conversations (evidence: `README.md`)
