AI · Guide

How to Connect Claude Code (or Any AI Agent) to Your Database Safely with MCP

AI agents are far more useful when they can see your schema and data. Here is how to give them that through MCP while making sure they can only read.

Updated November 29, 2026By the QueryOS team7 min read

Quick answer

Run a database MCP server that exposes only the connections you choose and only allows read-only queries, then add it to your agent’s MCP config. With QueryOS Premium: tick Allow AI tools to read this database on a connection and point your agent at /Applications/QueryOS.app/Contents/MacOS/QueryOS --mcp. Back it with a read-only database user.

Disclosure: QueryOS is our product and is shown here as one option. Every other step in this guide works with any database client.

Why connect an agent to your database?

An agent that can list tables, read column types and run a quick SELECT writes far better queries, migrations and bug fixes than one guessing from your code. The risk is obvious: the same access could run a DELETE. The safe pattern is three layers: expose only some connections, allow only reads, and connect as a user that cannot write anyway.

Option 1: QueryOS’s built-in MCP server

The MCP server is part of QueryOS Premium.

  1. In QueryOS, open the connection you want the agent to see, go to the Advanced tab and tick Allow AI tools to read this database. Leave it off for everything else, including production unless you really mean it.
  2. Add QueryOS to your MCP client. For Claude Desktop or any client that uses a JSON config:
{
  "mcpServers": {
    "queryos": {
      "command": "/Applications/QueryOS.app/Contents/MacOS/QueryOS",
      "args": ["--mcp"]
    }
  }
}

For Claude Code, from your project folder:

claude mcp add queryos -- /Applications/QueryOS.app/Contents/MacOS/QueryOS --mcp

The agent can then list the allowed connections and their tables, describe a table, and run read-only queries. Anything QueryOS classifies as a write, including writes hidden in a CTE, SELECT … INTO or EXPLAIN ANALYZE, is refused.

Layer 2: a read-only database user

Whatever tool you use, give the agent’s connection a user that cannot write. In PostgreSQL:

CREATE ROLE agent_ro LOGIN PASSWORD 'choose-a-strong-one';
GRANT CONNECT ON DATABASE shop TO agent_ro;
GRANT USAGE ON SCHEMA public TO agent_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO agent_ro;
ALTER ROLE agent_ro SET default_transaction_read_only = on;

In MySQL:

CREATE USER 'agent_ro'@'%' IDENTIFIED BY 'choose-a-strong-one';
GRANT SELECT ON shop.* TO 'agent_ro'@'%';

Layer 3: point it at a replica or a copy

If you can, connect the agent to a read replica or a recent copy rather than the primary production database. Heavy exploratory queries then can’t slow down your application.

Mind what the agent reads

Whatever the agent reads goes into its conversation with its AI provider. Keep tables with personal or secret data out of reach: use a schema the role can’t see, or views that leave sensitive columns out.

Checklist

Try QueryOS on your Mac23 databases and a review step before every change, free. Premium adds the AI assistant and unlimited tabs for $15 once.

Download free

Frequently asked questions

What is MCP?

The Model Context Protocol is an open standard that lets AI apps such as Claude Code, Claude Desktop and Cursor call tools you provide, for example listing tables or running a query.

Can an AI agent change my data through QueryOS’s MCP server?

No. It only sees connections where you ticked Allow AI tools to read this database, and it only runs statements QueryOS classifies as read-only. For defence in depth, also connect as a database user that has only SELECT rights.

Does the MCP server send my data anywhere?

QueryOS’s MCP server runs locally and answers your agent. The agent then sends what it reads to its AI provider as part of the conversation, so only allow connections you are comfortable sharing with that provider.