How to Configure Mastra SQL Server MCP Server
You set up a mastra sql server mcp server by linking your agent framework to a database user. Then you limit that user to a read-only database role so the agent cannot change data. Many engineering teams rush this step during early testing. They give the agent a shared admin account to make queries work fast. That broad role lets a single bad prompt alter rows or drop a production database. Starting with a tight database user keeps your data safe when agents run queries. How the mastra sql ser
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 mcp server by linking your agent framework to a database user. Then you limit that user to a read-only database role so the agent cannot change data. Many engineering teams rush this step during early testing. They give the agent a shared admin account to make queries work fast. That broad role lets a single bad prompt alter rows or drop a production database. Starting with a tight database user keeps your data safe when agents run queries.
How the mastra sql server mcp server architecture works
The setup links two main parts in your stack. The Mastra agent framework plans tasks and picks which tools to run. The SQL Server MCP server takes those tool calls and runs database queries. First, you must create the right account inside your database.
In SQL Server, a database user is the identity of a login for a database. When an app needs many databases on one server, teams make a login in master. Then they map a SQL user to that login. If the agent only needs one database, a contained database user is a good option. This user has its own password and does not tie to a login in master. An admin must turn on contained databases on the server and database first.
After you make the user, you must assign a role. SQL Server offers fixed database roles with preset rules. You cannot change the permissions assigned to fixed database roles. Every role, including the public role, gets the basic right to connect. For an AI agent, you should assign the db_datareader role. Members of db_datareader can read all data from all user tables and views. You can also add the db_denydatawriter role for extra safety. Members of db_denydatawriter cannot add, change, or delete any table data. Avoid the db_datawriter role unless the agent must write data. Members of db_datawriter can add, delete, or change data in all user tables. Never use the db_owner role for an agent. That role can run all maintenance tasks and can even drop the database.
Once the user and roles are ready, you pass the login details to the MCP server. You then register that MCP server inside your Mastra agent setup. When a user asks a question, Mastra sends a request to the MCP server. The MCP server logs in as the database user and runs the query.
Why native roles are not enough for agent queries
Setting up a read-only database user solves the first big risk. It stops your AI agent from deleting tables or changing rows. Still, native database roles only check who logs in at the start. They do not inspect what individual queries do while they run.
Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
The db_datareader role lets the agent read every user table and view. You cannot edit this fixed role to hide specific columns. If a table holds customer emails, phone numbers, or card numbers, the agent gets them. The database sends those raw rows back to the MCP server and the model. If the agent needs to write data, db_datawriter opens up every user table at once. One flawed prompt can still wipe out rows across tables you wanted to protect.
Static database users also make auditing hard for security teams. Every agent run shares the same database login. When a slow query locks a table, the database log only shows that shared user. You cannot see which person or prompt triggered the bad query.
Govern SQL Server queries and responses in flight
Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev is an open-source proxy that sits next to SQL Server as a sidecar for your SQL Server. It inspects TDS batches and parameterized queries in real time.
With hoop.dev guardrails and hoop.dev data masking, banned statements are refused with your message. Matched sensitive fields in supported result columns are masked in memory, and each decision is logged per listener. Check out the code on GitHub.
Frequently asked questions
What database role should I give a Mastra SQL Server agent?
Give the database user the db_datareader role and the db_denydatawriter role. This pair lets the agent read user tables while blocking all data changes.
Should I use a login in master or a contained database user?
Use a contained database user if the agent only needs one or a few databases. Use a login in master if the user needs many databases on the server.
Does the db_datareader role hide sensitive columns from AI models?
No, members of db_datareader can read all data from all user tables and views. It does not filter out emails, phone numbers, or card numbers from query results.
Related guides
In this post