# dnjfr/mcp-db-results-anonymizer architecture

> Architecture diagram of the GitHub repository dnjfr/mcp-db-results-anonymizer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dnjfr/mcp-db-results-anonymizer
- Repository: https://github.com/dnjfr/mcp-db-results-anonymizer

## Overview

1. **Overall shape**
   - This repo is a **Python MCP server** that sits between an AI coding agent and databases.
   - Its core architectural boundary is the **proxy layer**: the agent never connects to databases directly; it only talks to this MCP server, which executes queries, anonymizes results, and returns safe output.

2. **Entry point and protocol surface**
   - `src/server.py` is the server entry point, implemented with **FastMCP**.
   - The server exposes both **`/sse`** and **streamable HTTP `/mcp`** so multiple MCP-capable agents can connect to the same instance.
   - The server is meant to run as a containerized service; the repo’s Docker and compose files are part of the runtime boundary, not just packaging.

3. **Tool layer: what the agent can ask for**
   - `src/tools/` contains the MCP tool implementations:
     - `listTables`
     - `describeTable`
     - `querySql`
     - `queryNosql`
     - `queryNosqlAggregate`
     - `generateTestFixtures`
   - These tools define the main data-flow contract: metadata discovery, query execution, and fixture export all pass through the anonymization pipeline before reaching the agent.

4. **Database access layer**
   - `src/database/` splits connectors by backend:
     - `sql_connector.py` as the shared SQL abstraction
     - `mssql_connector.py`, `mysql_connector.py`, and likely PostgreSQL support through the shared SQL path
     - `nosql_connector.py` for MongoDB
   - This layer isolates database-specific connection and query execution details from the rest of the system.
   - The README shows the server can connect to databases over a **shared Docker network**, which is important because the MCP container is separate from the database containers.

5. **Schema and PII detection pipeline**
   - `src/detection/` is responsible for deciding what needs anonymization.
   - The pipeline combines several signals:
     - manual/registry-based classification
     - column-name matching
     - value regex scanning
     - SQL lineage analysis in `sql_lineage.py`
     - fallback value scanning in `value_scanner.py`
   - `column_matcher.py` and `registry.py` suggest a centralized ruleset for identifying sensitive fields.
   - This detection layer is critical because it determines which fields remain intact and which are pseudonymized.

6. **Anonymization engine**
   - `src/anonymizer/` contains the transformation logic:
     - `pseudonymizer.py` handles deterministic fake replacement
     - `pipeline.py` orchestrates row/document rewriting
   - The README describes the pseudonymization strategy as **Faker plus a seed derived from a hash of the value and session salt**, which gives stable fake values within a session while avoiding real-data leakage.
   - Numeric and date fields can be perturbed or shifted, so the system preserves shape and utility but not exact sensitive values.

7. **Persistent mapping store**
   - `src/storage/mapping_store.py` uses **local SQLite** to store hashed mappings.
   - This is a private, server-side state boundary: mappings stay on the MCP host, not in the agent, and not in the external databases.
   - The mapping store is what helps keep pseudonyms consistent enough for a session while still preventing exposure of real data.

8. **Security controls around execution**
   - `src/security/` contains three major controls:
     - `auth.py` for Bearer-token style access
     - `rate_limiter.py` for request throttling
     - `sql_validator.py` for read-only, fail-closed SQL validation
   - This layer prevents the MCP server from becoming a general database execution endpoint.
   - The architecture assumes untrusted agent input, so query validation is a first-class boundary before database access occurs.

9. **Configuration and deployment model**
   - `src/config.py` loads YAML configuration, with `config.example.yaml` as the template.
   - The README and docs indicate the server is designed for a **Docker-based deployment** with separate infrastructure created by the installer.
   - The database credentials remain server-side; the agent only sees the MCP interface, not raw connection details.

10. **Operational tooling and demo data**
   - `install.sh`, `Makefile`, `docker-compose.yml`, and `infra/` support setup, packaging, and local demo infrastructure.
   - `infra/` also contains scripts for generating fake/demo data and updating CSVs, which suggests the repo supports repeatable sample environments and onboarding workflows.
   - `src/tools/fixtures.py` and `infra/fake_data_mongo.py` align with this goal by producing anonymized fixture outputs and fake dataset support.

11. **Security hook ecosystem for agents**
   - `scripts/` contains agent-side hooks such as `security-hook.sh`, plus read/write variants and install/uninstall helpers.
   - These hooks are a secondary defense layer outside the MCP server itself, blocking dangerous tool calls in clients like Claude Code, Codex, OpenCode, and Cursor.
   - Architecturally, this is “defense in depth”: the server enforces security, and the agent runtime adds a policy checkpoint before tool execution.

12. **External boundaries and limitations**
   - The system integrates with **PostgreSQL, MySQL, SQL Server, and MongoDB**.
   - A notable boundary is that **queries are only protected once executed through the proxy**; sensitive literals embedded directly in SQL text can still travel in the query itself.
   - The project also notes **no native TLS**, so HTTPS is expected to be handled by a reverse proxy if deployed beyond local Docker.

13. **How the main request flow works**
   - Agent sends a query or schema request to the MCP server.
   - Tool layer routes the request to the correct database connector.
   - Detection logic identifies PII-relevant fields using schema and data heuristics.
   - Anonymizer rewrites sensitive values while preserving structure.
   - Mapping state is stored locally in SQLite.
   - The server returns anonymized results back to the agent.

14. **How to think about the architecture diagram**
   - Draw the server as the central trust boundary.
   - Put the agent on one side, databases on the other, and SQLite mapping storage inside the server boundary.
   - Show separate lanes for:
     - MCP transport and tool handling
     - database connectors
     - detection/anonymization pipeline
     - security checks
     - agent hook enforcement
   - The key architectural idea is not “an app that queries databases,” but **a security proxy that mediates and rewrites database results before the agent can see them**.

Main entry points: [`src/server.py`](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/server.py) (MCP server), [`src/tools/query_sql.py`](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/query_sql.py) (SQL query tool), [`src/anonymizer/pipeline.py`](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/anonymizer/pipeline.py) (Anonymizer), [`docker-compose.yml`](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/docker-compose.yml) (Compose stack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_client["Clients & runtime"]
  node_agent(("AI agent<br/>external client"))
  node_reverse_proxy["Reverse proxy<br/>deployment edge"]
end

subgraph group_grp_server["MCP server"]
  node_server["MCP server<br/>FastMCP entrypoint<br/>[server.py]"]
  node_config["Config<br/>YAML loader<br/>[config.py]"]
  node_metadata_tool["Metadata tools<br/>schema discovery<br/>[metadata.py]"]
  node_sql_tool["SQL query tool<br/>MCP tool<br/>[query_sql.py]"]
  node_nosql_tool["NoSQL tool<br/>MCP tool<br/>[query_nosql.py]"]
  node_fixtures_tool["Fixture export<br/>MCP tool<br/>[fixtures.py]"]
end

subgraph group_grp_protection["Protection pipeline"]
  node_auth["Auth<br/>bearer access<br/>[auth.py]"]
  node_rate_limiter["Rate limit<br/>throttling<br/>[rate_limiter.py]"]
  node_sql_validator["SQL validator<br/>read-only guard<br/>[sql_validator.py]"]
  node_detection{{"PII detection<br/>signal fusion"}}
  node_column_matcher["Column matcher<br/>rules engine<br/>[column_matcher.py]"]
  node_lineage["SQL lineage<br/>query analysis<br/>[sql_lineage.py]"]
  node_value_scanner["Value scanner<br/>regex scan<br/>[value_scanner.py]"]
  node_anonymizer{{"Anonymizer<br/>rewrite engine<br/>[pipeline.py]"}}
  node_pseudonymizer["Pseudonymizer<br/>stable faker<br/>[pseudonymizer.py]"]
  node_mapping_store[("Mapping store<br/>SQLite state<br/>[mapping_store.py]")]
end

subgraph group_grp_data["Databases"]
  node_sql_connector["SQL connector<br/>shared adapter<br/>[sql_connector.py]"]
  node_mssql_connector["SQL Server<br/>db connector<br/>[mssql_connector.py]"]
  node_mysql_connector["MySQL<br/>db connector<br/>[mysql_connector.py]"]
  node_nosql_connector["MongoDB<br/>db connector<br/>[nosql_connector.py]"]
end

subgraph group_grp_ops["Ops & hooks"]
  node_docker["Docker runtime<br/>containerization"]
  node_compose["Compose stack<br/>service wiring<br/>[docker-compose.yml]"]
  node_hooks["Security hooks<br/>agent guardrails<br/>[security-hook.sh]"]
end

node_agent -->|"MCP requests"| node_server
node_reverse_proxy -.->|"optional TLS"| node_server
node_docker -->|"runs"| node_server
node_compose -->|"wires"| node_server
node_server -->|"loads"| node_config
node_server -->|"checks"| node_auth
node_server -->|"throttles"| node_rate_limiter
node_server -->|"exposes"| node_metadata_tool
node_server -->|"exposes"| node_sql_tool
node_server -->|"exposes"| node_nosql_tool
node_server -->|"exposes"| node_fixtures_tool
node_sql_tool -->|"validates"| node_sql_validator
node_sql_tool -->|"analyzes"| node_detection
node_nosql_tool -->|"analyzes"| node_detection
node_fixtures_tool -->|"rewrites"| node_anonymizer
node_detection -->|"uses"| node_column_matcher
node_detection -->|"uses"| node_lineage
node_detection -->|"uses"| node_value_scanner
node_sql_tool -->|"sanitizes"| node_anonymizer
node_nosql_tool -->|"sanitizes"| node_anonymizer
node_anonymizer -->|"applies"| node_pseudonymizer
node_anonymizer -->|"stores"| node_mapping_store
node_sql_tool -->|"queries"| node_sql_connector
node_sql_connector -->|"supports"| node_mssql_connector
node_sql_connector -->|"supports"| node_mysql_connector
node_nosql_tool -->|"queries"| node_nosql_connector
node_sql_connector -.->|"connects"| node_mssql_connector
node_compose -.->|"connects"| node_mssql_connector
node_compose -.->|"connects"| node_mysql_connector
node_compose -.->|"connects"| node_nosql_connector
node_hooks -.->|"guards"| node_agent
node_hooks -.->|"defense in depth"| node_server

click node_reverse_proxy "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/docs/REVERSE_PROXY_HELP.md"
click node_server "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/server.py"
click node_config "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/config.py"
click node_metadata_tool "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/metadata.py"
click node_sql_tool "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/query_sql.py"
click node_nosql_tool "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/query_nosql.py"
click node_fixtures_tool "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/fixtures.py"
click node_auth "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/security/auth.py"
click node_rate_limiter "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/security/rate_limiter.py"
click node_sql_validator "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/security/sql_validator.py"
click node_column_matcher "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/detection/column_matcher.py"
click node_lineage "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/detection/sql_lineage.py"
click node_value_scanner "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/detection/value_scanner.py"
click node_anonymizer "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/anonymizer/pipeline.py"
click node_pseudonymizer "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/anonymizer/pseudonymizer.py"
click node_mapping_store "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/storage/mapping_store.py"
click node_sql_connector "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/sql_connector.py"
click node_mssql_connector "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/mssql_connector.py"
click node_mysql_connector "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/mysql_connector.py"
click node_nosql_connector "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/nosql_connector.py"
click node_docker "https://github.com/dnjfr/mcp-db-results-anonymizer/tree/main/Dockerfile"
click node_compose "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/docker-compose.yml"
click node_hooks "https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/scripts/security-hook.sh"

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_agent,node_reverse_proxy toneBlue
class node_server,node_config,node_metadata_tool,node_sql_tool,node_nosql_tool,node_fixtures_tool toneAmber
class node_auth,node_rate_limiter,node_sql_validator,node_detection,node_column_matcher,node_lineage,node_value_scanner,node_anonymizer,node_pseudonymizer,node_mapping_store toneMint
class node_sql_connector,node_mssql_connector,node_mysql_connector,node_nosql_connector toneRose
class node_docker,node_compose,node_hooks toneIndigo
```

## Components

### Clients & runtime

External agents and deployment surfaces that connect to the MCP server.

- **AI agent**: external client · MCP-capable coding agent that talks to the proxy server instead of databases directly.
- [Reverse proxy](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/docs/REVERSE_PROXY_HELP.md): deployment edge · Optional HTTPS termination boundary for production deployments. · `docs/REVERSE_PROXY_HELP.md`

### MCP server

Protocol entrypoint and tool surface exposed by the proxy service.

- [MCP server](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/server.py): FastMCP entrypoint · Main server process exposing SSE and streamable HTTP MCP endpoints. · `src/server.py`
- [Config](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/config.py): YAML loader · Loads server configuration and deployment settings. · `src/config.py`
- [Metadata tools](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/metadata.py): schema discovery · Tool surface for listing tables and describing schema. · `src/tools/metadata.py`
- [SQL query tool](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/query_sql.py): MCP tool · Runs SQL queries through validation, detection, and anonymization. · `src/tools/query_sql.py`
- [NoSQL tool](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/query_nosql.py): MCP tool · Runs MongoDB-style queries and aggregations through the protection pipeline. · `src/tools/query_nosql.py`
- [Fixture export](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/tools/fixtures.py): MCP tool · Generates sanitized fixtures from database results. · `src/tools/fixtures.py`

### Protection pipeline

Security checks, PII detection, anonymization, and session mapping state.

- [Auth](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/security/auth.py): bearer access · Bearer-token style request authentication. · `src/security/auth.py`
- [Rate limit](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/security/rate_limiter.py): throttling · Controls request volume to the server. · `src/security/rate_limiter.py`
- [SQL validator](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/security/sql_validator.py): read-only guard · Fail-closed validation for database queries before execution. · `src/security/sql_validator.py`
- **PII detection**: signal fusion · Combines schema, value, and lineage signals to identify sensitive fields.
- [Column matcher](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/detection/column_matcher.py): rules engine · Matches sensitive fields from column names and registry rules. · `src/detection/column_matcher.py`
- [SQL lineage](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/detection/sql_lineage.py): query analysis · Analyzes SQL structure to infer sensitive projections and flow. · `src/detection/sql_lineage.py`
- [Value scanner](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/detection/value_scanner.py): regex scan · Scans values for PII-like patterns and fallback signals. · `src/detection/value_scanner.py`
- [Anonymizer](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/anonymizer/pipeline.py): rewrite engine · Coordinates safe rewriting of rows and documents before results are returned. · `src/anonymizer/pipeline.py`
- [Pseudonymizer](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/anonymizer/pseudonymizer.py): stable faker · Deterministically replaces sensitive values with session-stable pseudonyms. · `src/anonymizer/pseudonymizer.py`
- [Mapping store](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/storage/mapping_store.py): SQLite state · Local persistent mapping of hashed values to pseudonyms. · `src/storage/mapping_store.py`

### Databases

Supported database backends accessed through connector abstractions.

- [SQL connector](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/sql_connector.py): shared adapter · Shared SQL backend abstraction used by relational connectors. · `src/database/sql_connector.py`
- [SQL Server](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/mssql_connector.py): db connector · SQL Server-specific connector and query execution path. · `src/database/mssql_connector.py`
- [MySQL](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/mysql_connector.py): db connector · MySQL-specific connector and query execution path. · `src/database/mysql_connector.py`
- [MongoDB](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/src/database/nosql_connector.py): db connector · MongoDB connector used for document queries and aggregations. · `src/database/nosql_connector.py`

### Ops & hooks

Containerization, configuration, and optional agent-side enforcement hooks.

- [Docker runtime](https://github.com/dnjfr/mcp-db-results-anonymizer/tree/main/Dockerfile): containerization · Container image and compose-based service runtime boundary. · `Dockerfile`
- [Compose stack](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/docker-compose.yml): service wiring · Local multi-container setup for server and databases. · `docker-compose.yml`
- [Security hooks](https://github.com/dnjfr/mcp-db-results-anonymizer/blob/main/scripts/security-hook.sh): agent guardrails · Client-side hook scripts that block risky tool calls before they reach the server. · `scripts/security-hook.sh`

## Connections

- AI agent → MCP server: MCP requests · Agent sends tool calls and schema requests to the server.
- Reverse proxy → MCP server: optional TLS · Production deployments may front the server with a reverse proxy.
- Docker runtime → MCP server: runs · Server is packaged as a containerized service.
- Compose stack → MCP server: wires · Compose defines the local service topology.
- MCP server → Config: loads · Startup configuration is loaded server-side.
- MCP server → Auth: checks · Incoming requests are authenticated before tool execution.
- MCP server → Rate limit: throttles · Requests are rate limited at the service boundary.
- MCP server → Metadata tools: exposes · Server exposes schema discovery tools to the agent.
- MCP server → SQL query tool: exposes · Server exposes relational query execution.
- MCP server → NoSQL tool: exposes · Server exposes document query execution.
- MCP server → Fixture export: exposes · Server exposes sanitized fixture generation.
- SQL query tool → SQL validator: validates · SQL queries are checked before execution.
- SQL query tool → PII detection: analyzes · Sensitive fields are identified from SQL results and lineage.
- NoSQL tool → PII detection: analyzes · Document results are scanned for sensitive values.
- Fixture export → Anonymizer: rewrites · Fixture export uses the same sanitization pipeline.
- PII detection → Column matcher: uses · Column names and registry rules seed detection.
- PII detection → SQL lineage: uses · SQL lineage informs field sensitivity decisions.
- PII detection → Value scanner: uses · Fallback scanning catches sensitive values.
- SQL query tool → Anonymizer: sanitizes · Relational results are rewritten before returning to the agent.
- NoSQL tool → Anonymizer: sanitizes · Document results are rewritten before returning to the agent.
- Anonymizer → Pseudonymizer: applies · Stable pseudonyms and shifts are generated here.
- Anonymizer → Mapping store: stores · Hashed mappings persist on the MCP host.
- SQL query tool → SQL connector: queries · Relational tools go through the shared SQL adapter.
- SQL connector → SQL Server: supports · SQL Server is one supported relational backend.
- SQL connector → MySQL: supports · MySQL is one supported relational backend.
- NoSQL tool → MongoDB: queries · MongoDB queries and aggregations use the NoSQL connector.
- SQL connector → SQL Server: connects · Relational access is delegated to backend-specific connectors.
- Compose stack → SQL Server: connects · Compose can wire the server to database containers on a shared network.
- Compose stack → MySQL: connects · Compose can wire the server to database containers on a shared network.
- Compose stack → MongoDB: connects · Compose can wire the server to database containers on a shared network.
- Security hooks → AI agent: guards · Agent-side hooks block dangerous tool usage before MCP calls.
- Security hooks → MCP server: defense in depth · Hooks complement server-side controls but do not replace them.
