How dnjfr/mcp-db-results-anonymizer works
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: server.py MCP server · query_sql.py SQL query tool · pipeline.py Anonymizer · docker-compose.yml Compose stack
Components 25
Clients & runtime
External agents and deployment surfaces that connect to the MCP server.
- AI agent · MCP-capable coding agent that talks to the proxy server instead of databases directly.
- Reverse proxy · 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 · Main server process exposing SSE and streamable HTTP MCP endpoints.
src/server.py - Config · Loads server configuration and deployment settings.
src/config.py - Metadata tools · Tool surface for listing tables and describing schema.
src/tools/metadata.py - SQL query tool · Runs SQL queries through validation, detection, and anonymization.
src/tools/query_sql.py - NoSQL tool · Runs MongoDB-style queries and aggregations through the protection pipeline.
src/tools/query_nosql.py - Fixture export · Generates sanitized fixtures from database results.
src/tools/fixtures.py
Protection pipeline
Security checks, PII detection, anonymization, and session mapping state.
- Auth · Bearer-token style request authentication.
src/security/auth.py - Rate limit · Controls request volume to the server.
src/security/rate_limiter.py - SQL validator · Fail-closed validation for database queries before execution.
src/security/sql_validator.py - PII detection · Combines schema, value, and lineage signals to identify sensitive fields.
- Column matcher · Matches sensitive fields from column names and registry rules.
src/detection/column_matcher.py - SQL lineage · Analyzes SQL structure to infer sensitive projections and flow.
src/detection/sql_lineage.py - Value scanner · Scans values for PII-like patterns and fallback signals.
src/detection/value_scanner.py - Anonymizer · Coordinates safe rewriting of rows and documents before results are returned.
src/anonymizer/pipeline.py - Pseudonymizer · Deterministically replaces sensitive values with session-stable pseudonyms.
src/anonymizer/pseudonymizer.py - Mapping store · Local persistent mapping of hashed values to pseudonyms.
src/storage/mapping_store.py
Databases
Supported database backends accessed through connector abstractions.
- SQL connector · Shared SQL backend abstraction used by relational connectors.
src/database/sql_connector.py - SQL Server · SQL Server-specific connector and query execution path.
src/database/mssql_connector.py - MySQL · MySQL-specific connector and query execution path.
src/database/mysql_connector.py - MongoDB · 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 · Container image and compose-based service runtime boundary.
Dockerfile - Compose stack · Local multi-container setup for server and databases.
docker-compose.yml - Security hooks · Client-side hook scripts that block risky tool calls before they reach the server.
scripts/security-hook.sh
Connections 32
- 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.