All posts

Data masking for MCP servers on Postgres

Many assume that data masking for a PostgreSQL server requires custom application logic or a separate proxy. In reality, you can enforce data masking at the database wire‑protocol level without touching the client code. When a large language model (LLM) drives code generation, it often spins up a temporary “MCP server” that queries a production PostgreSQL instance for reference data. Those queries can inadvertently expose personally identifiable information (PII) or other sensitive fields. If t

Free White Paper

Data Masking (Static) + Single Sign-On (SSO): The Complete Guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free. No spam. Unsubscribe anytime.

Many assume that data masking for a PostgreSQL server requires custom application logic or a separate proxy. In reality, you can enforce data masking at the database wire‑protocol level without touching the client code.

When a large language model (LLM) drives code generation, it often spins up a temporary “MCP server” that queries a production PostgreSQL instance for reference data. Those queries can inadvertently expose personally identifiable information (PII) or other sensitive fields. If the server forwards raw rows to the model, the downstream output may leak data that should never leave the database.

Organizations typically protect access with an identity provider (IdP) that issues OIDC or SAML tokens. The token proves who the requestor is, and the token’s claims drive just‑in‑time authorisation. This setup solves the "who can connect" problem but leaves the actual data path untouched. The request still travels straight from the MCP server to the PostgreSQL endpoint, meaning every column is delivered in clear text, no audit of which fields were read, and no opportunity to block or redact sensitive values.

Why data masking matters for MCP servers

Without an inline guard, a single stray query can exfiltrate credit‑card numbers, health identifiers, or internal identifiers. The risk is amplified when an LLM can incorporate that data into generated code, documentation, or responses that are later shared with a broader audience. Masking at the database layer ensures that the protection is applied once, regardless of how many downstream services consume the result.

Beyond compliance, inline masking reduces the blast radius of a compromised MCP instance. Even if an attacker gains a foothold, the data they can see is already sanitized, limiting the value of the breach.

Implementing data masking with hoop.dev on PostgreSQL

hoop.dev acts as a Layer 7 gateway that sits directly in the PostgreSQL wire‑protocol flow. The gateway runs a lightweight agent inside the same network segment as the database, holds the database credentials, and presents a single endpoint to the MCP server. When the MCP server initiates a connection, it authenticates to hoop.dev using the same OIDC token that the organisation already trusts.

Continue reading? Get the full guide.

Data Masking (Static) + Single Sign-On (SSO): Architecture Patterns & Best Practices

Free. No spam. Unsubscribe anytime.

Because hoop.dev is the only component that sees the raw query and response, it can apply policy‑driven masking before any row leaves the gateway. The policy is defined in terms of column names or data patterns; when a query returns a column marked as sensitive, hoop.dev replaces the value with a placeholder or a hash. The masking happens in real time, so the MCP server receives the redacted result without any code change.

At the same time, hoop.dev records each statement at the command level. The session log includes the authenticated identity, the exact SQL issued, and a timestamp. This audit trail satisfies internal governance and external audit requirements without relying on database‑side logging, which often lacks fine‑grained user context.

Because the gateway holds the database credential, the MCP server does not obtain the password or IAM token. This separation means that even a compromised server cannot reuse the credential to connect directly to PostgreSQL, reinforcing the principle of least privilege.

All of these enforcement outcomes, inline masking, command‑level audit, credential off‑load, and just‑in‑time scope, exist only because hoop.dev occupies the data path. The identity provider decides who may start a request, but hoop.dev is the sole place where the request is inspected and transformed.

Getting started

To try this approach, follow the getting‑started guide. The guide walks you through deploying the gateway, registering a PostgreSQL target, and defining a masking policy for columns such as ssn or email. All configuration is expressed in declarative form, and the gateway picks up changes without restarting the database.

For deeper details on how masking rules are expressed and how they interact with other guardrails, see the learn section of the documentation.

Frequently asked questions

  • Will masking add noticeable latency? The gateway processes rows in memory and only rewrites fields marked for redaction. In most workloads the overhead is measured in milliseconds, which is negligible compared with network round‑trip time.
  • Can I mask only specific columns? Yes. Policies are defined per‑table and per‑column, allowing you to target exactly the data you consider sensitive while leaving other columns untouched.
  • Is the original data ever stored by hoop.dev? No. The gateway forwards the masked result to the client and discards the raw values after the session is recorded. The audit log keeps the statement text but not the unredacted row data.

By placing the masking logic in the data path, you gain consistent protection across every client, including AI‑driven MCP servers, without altering application code.

Explore the source code, contribute improvements, or clone the repository to get your own instance running: hoop.dev on GitHub.

Open source

Save the open-source gateway for agent data access

Hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.

Star and save the repo →More posts