How to Configure Mastra SQL Server Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a mastra sql server sandbox by making a dedicated database user. Then you give that user a strict, read-only database role for your agent. Teams often start by reusing an existing app login for their AI agents. This shortcut gives the agent broad rights to change or delete table rows. If a prompt goes wrong, an over-privileged agent can drop tables or read secret rows. How a mastra sql server sandbox works with native roles A safe setup starts inside the database engine before the

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 a mastra sql server sandbox by making a dedicated database user. Then you give that user a strict, read-only database role for your agent. Teams often start by reusing an existing app login for their AI agents. This shortcut gives the agent broad rights to change or delete table rows. If a prompt goes wrong, an over-privileged agent can drop tables or read secret rows.

How a mastra sql server sandbox works with native roles

A safe setup starts inside the database engine before the agent ever runs. In SQL Server, a database user is the identity of a login inside a database. When an identity needs many databases on an instance, teams make a standard login. That login lives in the main system database and maps to each target database. If the agent only needs one database, you can use a contained database user. This user has a password and is not tied to the main system database. An admin must first turn on contained databases on the server and the database. Notice that SQL Server offers thirteen distinct types of database users for different needs.

Once the user exists, you must limit what the Mastra agent can do. SQL Server provides fixed database roles with built-in rights that you cannot change. Every role, including the default public role, holds the basic right to connect. To keep the sandbox safe, admins place the agent user in the reader role. Members of this reader role can read all data from all user tables. They can also read from all user views inside that database. Admins can also add the deny writer role to block any data changes. Members of the deny writer role cannot add, change, or delete any table data. You should never give an AI agent the database owner role. The owner role can run all maintenance tasks and even drop the whole database.

Why native roles are not enough for agent queries

Native roles do a good job of deciding who can log into the database. They also set a hard floor on whether a user can write data. Still, fixed roles apply to the whole database at once. A member of the reader role can read every single user table and view. You cannot change the rights of that fixed role to hide specific columns.

This leaves a big gap when a Mastra agent runs queries on its own. The database checks the role at login, not the safety of each query in flight. An agent with reader rights can still pull emails, phone numbers, or card numbers. It can also run a bad join that locks up the server for minutes. Native roles also do not tie an individual agent prompt to a specific query. To block bad queries or mask national IDs in results, you need query-level checks.

Kubernetes API Server Access + AI Sandbox Environments: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Check SQL Server batches and results on the wire

SQL Server logins and roles decide who connects. They do not check what each TDS batch does or mask sensitive values in returned rows. With hoop.dev, you run an open-source proxy, a sidecar for your SQL Server. It reads plain batches and parameterized queries on the TDS wire while passing Windows login through untouched.

Before a batch hits the server, hoop.dev guardrails check the statement against your rules. Disallowed actions get a native TDS error with your message. On the way back, hoop.dev data masking rewrites sensitive values in supported result columns that match your rules. Every query and decision is logged. Explore the MIT-licensed project on GitHub.

Frequently asked questions

Should I use a standard login or a contained user for Mastra?

Use a contained user if your Mastra agent only needs one or two databases. An admin must enable contained databases on the server and database first. Use a standard login in the master database if the agent reads many databases.

Can I edit the fixed reader role to hide one table from my agent?

No, you cannot change the permissions assigned to fixed database roles in SQL Server. The fixed reader role always grants read access to all user tables and views. To hide specific tables, you must build custom permissions or use query-level controls.

How do I stop a Mastra agent from changing data in the sandbox?

Do not put the agent user in the fixed writer or owner roles. You can also add the user to the fixed deny writer role. That role ensures the user cannot add, change, or delete any table data.

The query runs. The emails stay hidden.