# jmmana/sqlguardllm architecture

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

- Interactive diagram: https://gitdiagram.com/jmmana/sqlguardllm
- Repository: https://github.com/jmmana/sqlguardllm

## Overview

sqlguardllm is a SQL firewall for LLM applications and agents. The principal workflow is: an application supplies model-generated SQL, a policy and trusted user context; the guard parses and validates the query, then rewrites permitted queries with row filters and a row cap, returning either safe SQL or violations. The Python package is the central runtime, exposed through HTTP, stdio/MCP and language clients. The README documents additional write, procedure, masking, auditing and integration capabilities; several are represented at their documented subsystem level because their implementation was not sampled.

Main entry points: [`src/sqlguardllm/guard.py`](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/guard.py) (Guard), [`clients/dotnet/src/SqlGuardLlm/SqlGuardLlm.cs`](https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm/SqlGuardLlm.cs) (.NET client), [`clients/node/src/index.ts`](https://github.com/jmmana/sqlguardllm/blob/main/clients/node/src/index.ts) (Node client), [`src/sqlguardllm/engine_stdio.py`](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/engine_stdio.py) (Stdio engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Policy and guard"]
  node_policy["Policy<br/>[policy.py]"]
  node_policyset["Policy sets<br/>[policy_set.py]"]
  node_guard["Guard<br/>[guard.py]"]
  node_results["Results<br/>[result.py]"]
  node_messages["Violation messages<br/>[messages.py]"]
end

subgraph group_rules["Validation and rewrite"]
  node_rules["SQL rules"]
  node_rowfilters["Row filters<br/>[row_filters.py]"]
  node_procedures["Procedure rules<br/>[procedures.py]"]
end

subgraph group_interfaces["Runtime interfaces"]
  node_http["HTTP server<br/>[server.py]"]
  node_stdio["Stdio engine<br/>[engine_stdio.py]"]
  node_mcp["MCP server<br/>[mcp_server.py]"]
  node_proxy["Database proxy<br/>[proxy.py]"]
  node_cli["CLI<br/>[cli.py]"]
end

subgraph group_clients["Language integrations"]
  node_node["Node client<br/>[index.ts]"]
  node_nodeengine["Embedded engine locator<br/>[engine.ts]"]
  node_dotnet[".NET client<br/>[SqlGuardLlm.cs]"]
  node_dotnetadapters[".NET adapters"]
  node_ef["EF Core interceptor"]
  node_semantic["Semantic Kernel filter"]
  node_langchain["LangChain tools<br/>[tools.py]"]
end

subgraph group_ops["Deployment features"]
  node_audit["Audit log<br/>[audit.py]"]
  node_explain["Policy explanation<br/>[explain.py]"]
end

node_app(("LLM application"))
node_database[("SQL database")]

node_app -->|"submits SQL"| node_http
node_app -->|"uses client"| node_node
node_app -->|"uses client"| node_dotnet
node_policy -->|"configures"| node_guard
node_guard -->|"validates and rewrites"| node_rules
node_guard -->|"applies filters"| node_rowfilters
node_guard -->|"checks procedures"| node_procedures
node_guard -->|"returns decision"| node_results
node_guard -->|"formats violations"| node_messages
node_guard -->|"records decision"| node_audit
node_guard -->|"explains policy"| node_explain
node_policyset -->|"contains policies"| node_policy
node_policyset -->|"selects guard"| node_guard
node_http -->|"checks requests"| node_guard
node_stdio -->|"dispatches checks"| node_guard
node_stdio -->|"loads policy"| node_policy
node_mcp -.->|"serves guard tools"| node_guard
node_proxy -.->|"guards queries"| node_guard
node_cli -.->|"runs guard"| node_guard
node_node -->|"locates engine"| node_nodeengine
node_node -.->|"uses engine protocol"| node_stdio
node_dotnet -->|"sends requests"| node_http
node_dotnetadapters -->|"registers client"| node_dotnet
node_ef -.->|"intercepts commands"| node_dotnet
node_semantic -.->|"filters functions"| node_dotnet
node_langchain -.->|"wraps guard"| node_guard
node_app -->|"executes returned SQL"| node_database

click node_policy "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/policy.py"
click node_policyset "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/policy_set.py"
click node_guard "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/guard.py"
click node_rules "https://github.com/jmmana/sqlguardllm/tree/main/src/sqlguardllm/rules"
click node_rowfilters "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/rules/row_filters.py"
click node_procedures "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/rules/procedures.py"
click node_results "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/result.py"
click node_messages "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/messages.py"
click node_audit "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/audit.py"
click node_explain "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/explain.py"
click node_http "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/server.py"
click node_stdio "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/engine_stdio.py"
click node_mcp "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/mcp_server.py"
click node_proxy "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/proxy.py"
click node_cli "https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/cli.py"
click node_node "https://github.com/jmmana/sqlguardllm/blob/main/clients/node/src/index.ts"
click node_nodeengine "https://github.com/jmmana/sqlguardllm/blob/main/clients/node/src/engine.ts"
click node_dotnet "https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm/SqlGuardLlm.cs"
click node_dotnetadapters "https://github.com/jmmana/sqlguardllm/tree/main/clients/dotnet/src/SqlGuardLlm.Extensions"
click node_ef "https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm.EntityFrameworkCore/SqlGuardCommandInterceptor.cs"
click node_semantic "https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm.SemanticKernel/SqlGuardFunctionFilter.cs"
click node_langchain "https://github.com/jmmana/sqlguardllm/blob/main/clients/langchain/src/langchain_sqlguardllm/tools.py"

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_policy,node_policyset,node_guard,node_results,node_messages toneBlue
class node_rules,node_rowfilters,node_procedures,node_database toneAmber
class node_http,node_stdio,node_mcp,node_proxy,node_cli toneMint
class node_node,node_nodeengine,node_dotnet,node_dotnetadapters,node_ef,node_semantic,node_langchain toneRose
class node_audit,node_explain,node_app toneIndigo
```

## Components

### Policy and guard

- [Policy](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/policy.py): `src/sqlguardllm/policy.py`
- [Policy sets](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/policy_set.py): `src/sqlguardllm/policy_set.py`
- [Guard](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/guard.py): `src/sqlguardllm/guard.py`
- [Results](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/result.py): `src/sqlguardllm/result.py`
- [Violation messages](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/messages.py): `src/sqlguardllm/messages.py`

### Validation and rewrite

- [SQL rules](https://github.com/jmmana/sqlguardllm/tree/main/src/sqlguardllm/rules): `src/sqlguardllm/rules`
- [Row filters](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/rules/row_filters.py): `src/sqlguardllm/rules/row_filters.py`
- [Procedure rules](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/rules/procedures.py): `src/sqlguardllm/rules/procedures.py`

### Runtime interfaces

- [HTTP server](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/server.py): `src/sqlguardllm/server.py`
- [Stdio engine](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/engine_stdio.py): `src/sqlguardllm/engine_stdio.py`
- [MCP server](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/mcp_server.py): `src/sqlguardllm/mcp_server.py`
- [Database proxy](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/proxy.py): `src/sqlguardllm/proxy.py`
- [CLI](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/cli.py): `src/sqlguardllm/cli.py`

### Language integrations

- [Node client](https://github.com/jmmana/sqlguardllm/blob/main/clients/node/src/index.ts): `clients/node/src/index.ts`
- [Embedded engine locator](https://github.com/jmmana/sqlguardllm/blob/main/clients/node/src/engine.ts): `clients/node/src/engine.ts`
- [.NET client](https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm/SqlGuardLlm.cs): `clients/dotnet/src/SqlGuardLlm/SqlGuardLlm.cs`
- [.NET adapters](https://github.com/jmmana/sqlguardllm/tree/main/clients/dotnet/src/SqlGuardLlm.Extensions): `clients/dotnet/src/SqlGuardLlm.Extensions`
- [EF Core interceptor](https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm.EntityFrameworkCore/SqlGuardCommandInterceptor.cs): `clients/dotnet/src/SqlGuardLlm.EntityFrameworkCore/SqlGuardCommandInterceptor.cs`
- [Semantic Kernel filter](https://github.com/jmmana/sqlguardllm/blob/main/clients/dotnet/src/SqlGuardLlm.SemanticKernel/SqlGuardFunctionFilter.cs): `clients/dotnet/src/SqlGuardLlm.SemanticKernel/SqlGuardFunctionFilter.cs`
- [LangChain tools](https://github.com/jmmana/sqlguardllm/blob/main/clients/langchain/src/langchain_sqlguardllm/tools.py): `clients/langchain/src/langchain_sqlguardllm/tools.py`

### Deployment features

- [Audit log](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/audit.py): `src/sqlguardllm/audit.py`
- [Policy explanation](https://github.com/jmmana/sqlguardllm/blob/main/src/sqlguardllm/explain.py): `src/sqlguardllm/explain.py`

### Other components

- **LLM application**
- **SQL database**

## Connections

- LLM application → HTTP server: submits SQL (evidence: `README.md`)
- LLM application → Node client: uses client (evidence: `README.md`)
- LLM application → .NET client: uses client (evidence: `README.md`)
- Policy → Guard: configures (evidence: `src/sqlguardllm/guard.py`)
- Guard → SQL rules: validates and rewrites (evidence: `src/sqlguardllm/guard.py`)
- Guard → Row filters: applies filters (evidence: `src/sqlguardllm/guard.py`)
- Guard → Procedure rules: checks procedures (evidence: `src/sqlguardllm/guard.py`)
- Guard → Results: returns decision (evidence: `src/sqlguardllm/guard.py`)
- Guard → Violation messages: formats violations (evidence: `src/sqlguardllm/guard.py`)
- Guard → Audit log: records decision (evidence: `src/sqlguardllm/guard.py`)
- Guard → Policy explanation: explains policy (evidence: `src/sqlguardllm/guard.py`)
- Policy sets → Policy: contains policies (evidence: `src/sqlguardllm/policy_set.py`)
- Policy sets → Guard: selects guard (evidence: `src/sqlguardllm/policy_set.py`)
- HTTP server → Guard: checks requests (evidence: `src/sqlguardllm/server.py`)
- Stdio engine → Guard: dispatches checks (evidence: `src/sqlguardllm/engine_stdio.py`)
- Stdio engine → Policy: loads policy (evidence: `src/sqlguardllm/engine_stdio.py`)
- MCP server → Guard: serves guard tools
- Database proxy → Guard: guards queries
- CLI → Guard: runs guard
- Node client → Embedded engine locator: locates engine (evidence: `clients/node/src/index.ts`)
- Node client → Stdio engine: uses engine protocol
- .NET client → HTTP server: sends requests (evidence: `clients/dotnet/src/SqlGuardLlm/SqlGuardLlm.cs`)
- .NET adapters → .NET client: registers client (evidence: `clients/dotnet/src/SqlGuardLlm.Extensions/ServiceCollectionExtensions.cs`)
- EF Core interceptor → .NET client: intercepts commands
- Semantic Kernel filter → .NET client: filters functions
- LangChain tools → Guard: wraps guard
- LLM application → SQL database: executes returned SQL (evidence: `README.md`)
