How to Configure Mastra ClickHouse Guardrails

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up mastra clickhouse guardrails by giving your agent a locked-down database user. You also pair that database user with strict column grants and app input checks. Many teams start by giving an AI agent a broad database login to move fast. Broad roles let one bad prompt read private columns or run heavy table scans. Putting limits in place at both the app layer and the database stops this risk. How mastra clickhouse guardrails work in native setups ClickHouse manages users, roles, row

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up mastra clickhouse guardrails by giving your agent a locked-down database user. You also pair that database user with strict column grants and app input checks. Many teams start by giving an AI agent a broad database login to move fast. Broad roles let one bad prompt read private columns or run heavy table scans. Putting limits in place at both the app layer and the database stops this risk.

How mastra clickhouse guardrails work in native setups

ClickHouse manages users, roles, row rules, settings profiles, and quotas through SQL commands. To turn on SQL access control, an admin first sets up a storage directory. The admin also needs an account with user management rights turned on. A user can only hand out database rights that they already hold themselves. Creating a new user for your agent takes the create user right.

During user setup, the admin picks the login method and allowed client hosts. The admin also assigns default roles and per-user settings for that agent account. For tighter control, you can grant read rights on specific table columns only. When an agent holds grants for just two columns, it can query only those two. Any query asking for other columns will fail right away.

Even a wildcard query selecting all columns fails and returns no data at all. A wildcard query works only if the table holds just the allowed columns. Admins can also grant read rights across a group of tables using a name prefix. On the Mastra side, developers wrap database tools in input checks before running queries. These checks validate tool arguments and stop the agent from sending raw write commands.

What native database roles solve and what stays exposed

Native ClickHouse roles and column grants do a great job of blocking unauthorized table access. They make sure the agent account logs in from known hosts with fixed rights. If an agent tries to read a forbidden column, the database stops the query. Quotas and settings profiles also help limit how much memory or time a query uses.

Still, database grants only check who logs in and which columns a query names. They do not inspect what individual statements do in flight or what values come back. An allowed text or notes column might still hold emails, phone numbers, or card numbers. Once a column is granted, the database sends every matching row back in plain text. Native grants cannot rewrite sensitive strings inside a permitted column on the fly.

ClickHouse Access Management + AI Guardrails: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static credentials also create extra work for engineering teams. The Mastra app must store a database password or key to open sessions. If that key leaks in logs or code, anyone on an allowed host can use it. Database roles also treat every agent prompt the same way once the session starts. You cannot pause one risky query for human approval while letting safe queries run.

Why statement-level inspection matters for AI agents

AI agents write queries based on user input, which makes their traffic hard to predict. A user might ask a question that tricks the agent into pulling customer national IDs. If those IDs sit inside an allowed column, native ClickHouse grants will return them. Checking queries and results in flight closes this gap without rebuilding your tables.

Runtime checks can scan query results and mask emails or card numbers before the agent reads them. Data masking rewrites matched fields in results so raw values stay out of prompts. Teams can also tie each agent request to a real user identity for clean audit logs. This gives security teams clear proof of what the agent asked and what data returned.

Check ClickHouse queries and result columns on the wire

ClickHouse users and row policies decide who connects. They do not check each statement on the wire or redact sensitive string columns in query results. With hoop.dev, you run an open-source proxy, a sidecar for your ClickHouse. It reads ClickHouse's native TCP protocol before queries run while ClickHouse stays in charge of login and grants.

Multi-statement queries and table mutations are split and checked by hoop.dev guardrails. Banned actions are refused with a native ClickHouse error. On returned rows, hoop.dev data masking rewrites String and FixedString columns that match your rules. The audit trail logs each query, table, and verdict. See the MIT-licensed source on GitHub.

Frequently asked questions

What happens when an agent runs a select all query with column grants?

ClickHouse refuses a select all query when the user only has rights to specific columns. The query returns no data unless the table holds only the exact columns granted.

How do you turn on SQL access control in ClickHouse for agent roles?

An admin sets the access storage directory on the server and enables user management rights. After that, the admin can create users, roles, and row rules with SQL commands.

Can ClickHouse column grants hide emails inside a free-text column?

No, column grants are all-or-nothing for each column you allow the agent to read. To hide emails or phone numbers inside allowed columns, you need field masking in flight.

The query runs. The emails stay hidden.