How to Add AI Agent Guardrails to an MCP Agent

Getting an AI agent to work is the easy part. Before it touches real customer data, someone will ask who pays for its model calls, what happens when the provider goes down, why it can delete rows, and what the model gets to see. In this tutorial, you'll answer each one for a Model Context Protocol (MCP) agent with a gateway setting and no middleware to write or maintain.

The gateway here is CData Connect AI, which sits between the agent, its model, and its data, so AI agent guardrails become settings you change without touching the agent's code. This is the demo I gave live at CData Foundations 2026, written out so you can follow along.

The 10-minute session:

At a glance

AI agent guardrails are controls on what an agent can spend, call, see, and change. Here they're MCP guardrails and gateway settings in Connect AI, outside the agent's code. Tested November 2026 with the open-source agent quickstart. The LLM Gateway is in early access as of November 2026. Here's where you set each one:

Guardrail

Where you set it

Logging, budgets, and model failover

A project in Connect AI, plus one key in .env

Read-only MCP tools

One line in .env

Masking for sensitive data

Data Security in Connect AI

Access limited to one user

The connection's Permissions tab

An agent calls a model through Connect AI's LLM Gateway and reaches data through Connect AI's MCP server, with guardrails on both.

What you need

  • A Connect AI Registering a model provider requires the Administrator role. Creating routing rules, projects, and virtual keys requires Administrator or Workspace Administrator. Data Security, used in Step 4, is part of the Business plan.

  • Access to the LLM Gateway, Connect AI's gateway for large language model (LLM) calls, which is in early access as of November 2026. Request it before you start.

  • An API key from a model provider the gateway supports, such as Anthropic or OpenAI. You enter it once in Connect AI, and your app never holds it.

  • A Connect AI personal access token (PAT), from Settings > Access Tokens.

  • Docker with Compose. On a Mac without Docker Desktop, brew install colima docker docker-compose && colima start works. If docker compose then says unknown command, run docker-compose up instead.

The same settings work on your own agent, but only for the model calls and data that go through Connect AI. If it also calls another MCP server directly, such as one for GitHub, that server isn't covered. (Tool servers in the MCP Gateway can put third-party MCP servers behind one agent URL. See Where to go next.)

Step 1: Set up the agent and sample data

The agent quickstart is a small Python agent built on LangGraph, with a chat page so you can watch it work. Its sample table of 50 made-up customers includes fake US Social Security numbers (SSNs) and test card numbers, so you can see right away whether a guardrail worked. Any connection in Connect AI works the same way.

Add the sample data in Connect AI:

  1. Open Sources and add a CSV connection named DemoCustomers.

  2. Set URI to https://raw.githubusercontent.com/CDataSoftware/connect-ai-agent-quickstart/main/sample-data/customers.csv.

  3. Change Connection Type to HTTPS. It defaults to Amazon S3.

  4. Click Save & Test.

Then clone the repo:

git clone https://github.com/CDataSoftware/connect-ai-agent-quickstart.git
cd connect-ai-agent-quickstart
cp .env.example .env

In .env, set CDATA_USERNAME to your Connect AI login email and CDATA_PAT to your PAT. Leave the model settings for Step 2.

Here's the agent, trimmed from app/agent.py. It loads its tools from an MCP server, picks a model by name, and passes both to LangGraph:

mcp = MultiServerMCPClient({
    "cdata": {
        "transport": "streamable_http",
        "url": settings.cdata_mcp_url,
        "headers": {"Authorization": settings.mcp_auth_header},
    }
})
tools = await mcp.get_tools()
llm = init_chat_model(settings.llm_model)
agent = create_agent(llm, tools, system_prompt=SYSTEM_PROMPT)

This code doesn't change for the rest of the tutorial.

Step 2: Route model calls through an LLM gateway

The Connect AI LLM Gateway sits between the agent and its model provider. The provider key stays in Connect AI, and every call gets logged and counted against the project's budget.

Set up the gateway in Connect AI:

  1. Register a provider. On LLM Providers, click + Add Provider, choose Anthropic, enter your Anthropic API key, and add a model such as Claude Sonnet. Add a second model, such as Claude Haiku, if you want failover.

  2. Pick a routing rule. Click Routing in the menu to open the LLM Gateway On the Routing Rules tab, add a Single provider rule for one model, or a Priority Failover rule that tries one model first and the other if it's unavailable.

  3. Create a project. On the Projects tab, add a project, assign the rule, and set a monthly budget.

  4. Issue a virtual key. On the Virtual Keys tab, click + Issue Virtual Key for the project. A virtual key is the key your app uses in place of the provider's. It starts with cgw_ and is shown once, so paste it into .env immediately:

ANTHROPIC_API_KEY=cgw_your_virtual_key

.env.example already sets ANTHROPIC_BASE_URL= https://cloud.cdata.com/gateway/v1, so you only change the key line. The agent still uses the Anthropic software development kit (SDK) and doesn't know anything changed, and the gateway docs show the OpenAI SDK working the same way (what an LLM gateway does has the background). My session started from the direct setup; to compare, comment out ANTHROPIC_BASE_URL and put your own Anthropic key in ANTHROPIC_API_KEY.

Start the agent:

docker compose up

Then open . If the log says Startup failed: or that Connect AI rejected the credentials, the quickstart's troubleshooting table has the fix.

Ask it something that touches the sensitive columns:

Select the customer ID, full name, SSN, and credit card for customer 1004 in DemoCustomers, and show me exactly what the query returns.

The agent writes the query and returns customer 1004's full SSN and card number, to you and to the model. My session opened on exactly this, with a full SSN on the projector. Steps 4 and 5 change that answer.

The agent's answer showing a customer's full SSN and card number, with 13 read and write tools in the chat page header, before the gateway

From my Foundations 2026 session, which used a Postgres table with different customer names and started without the gateway (the header shows a direct provider key).

That call is in Logs > LLM Logs in Connect AI, with its tokens, its estimated cost, the model the app asked for, and the model that answered.

An LLM Logs entry showing the model requested, the model served, and input and output tokens

The gateway also gives you:

  • My project's routing rule fails over from Claude Sonnet to Claude Haiku. In my testing, the rule picked the model no matter what name the app sent, so you can swap models without redeploying.

  • My project's monthly budget is a $25 hard limit, which blocks new requests once estimated spend reaches it. Every response I got also carried headers with the budget used and remaining, so the app can tell when it's getting close.

  • If the virtual key leaks, you revoke it on Virtual Keys and nothing gets redeployed.

Plenty of AI gateways do routing, logs, and budgets. For me, the part that matters more is what data the agent can touch, and that's the next three steps.

Step 3: Limit the agent to read-only MCP tools

The chat page header says the agent has 13 tools. They come from Connect AI's MCP server, and three of them can change data: execute_insert, execute_update, and executeProcedure.

The MCP server takes an ops parameter that narrows the tools it offers. Uncomment this line in .env:

CDATA_MCP_URL=https://mcp.cloud.cdata.com/mcp?ops=get_catalogs,get_schemas,get_tables,get_columns,get_instructions,queryData

The quickstart reloads .env without a restart, within a second or two, and the header drops to six tools, all read-only. (The MCP docs' example URL also keeps search_context, a read-only tool that helps the agent find tables.) If you ask the agent to delete customer 1004, it tells you it has no tool for that.

The chat page header showing six read-only tools, listed by name

Screenshot from my Foundations 2026 session, which used a Postgres table, so your table and customer names will differ.

Step 4: Mask PII in MCP query results

Read-only tools still return customer 1004's SSN and card number. PII masking for AI agents (masking personally identifiable information) keeps the model from receiving those values.

Open Data Security in Connect AI. Turn on the US Social Security Number and Credit Card Number rules, set each outbound rule to Redact, and save.

Ask the question from Step 2 again, and both values come back as XXXX.

The same query now returning XXXX for the SSN and the card number, through the LLM Gateway with six read-only tools

Screenshot from my Foundations 2026 session, which used a Postgres table, so your table and customer names will differ.

The database still has the real values. Connect AI masks them in the query results before the agent gets them, so the model never sees them either. In my workspace, the rules applied to every agent and every model. The Data Security screen also takes custom rules written as a regular expression (say, your internal account ID format), and each rule can Block or Warn instead of Redact. For field-level options, see MCP field-level security.

If you test with your own made-up rows and an SSN comes back unmasked, check its first three digits. Some of my early test rows started with 000 or 666, which aren't valid SSNs, so the masking rules correctly skipped them. On a projector, they look exactly like a leak.

While I built the demo, Claude declined to query personal data when I asked for it casually. That's the model being polite, and that behavior can change with the next model or the next prompt. Masking runs whether the model cooperates or not. The Step 2 question asks for the exact query result so the model doesn't refuse and you can see what masking changes.

Step 5: Scope the agent to the user it runs as

The agent signs in to Connect AI with your PAT, so it has exactly your access to each connection. (For the governance picture, see AI agent data governance.)

On the DemoCustomers connection's Permissions tab, uncheck Select on your user's row and save. Then ask about customer 1004 again. (Permissions add up, so this works only if no role or policy also gives you Select.)

With the same tools and the same PAT as a minute ago, the agent can't find the connection. The connection is still there, but for the agent DemoCustomer doesn't exist anymore, because to you it doesn't. If a teammate runs the same agent with their own PAT, it sees only what that teammate can see. Check Select again when you're done.

The agent replying that it couldn't find the data after I unchecked my Select permission

Screenshot from my Foundations 2026 session

Where to go next

The demo used one setting from each layer. Here's when you'd use the others.

On the LLM Gateway:

  • More providers behind one endpoint. You can switch between Anthropic, OpenAI, Google Gemini, Azure OpenAI, Cohere, or any OpenAI-compatible endpoint, and the app keeps the same base URL and virtual key.

  • A model allowlist, when teams should pick their own model from an approved list. The gateway refuses any model not on it.

  • A soft budget that sends a notification and lets requests through, for projects you'd rather watch than stop.

  • Prompt enrichment, when every request should carry your business context from the Context Editor (terms, table definitions, column mappings; in early access).

On the MCP Gateway:

  • Tool servers, when one agent needs the CData sources you pick and the third-party MCP servers you add behind a single URL.

  • Custom MCP tools, when you'd rather the agent call a reviewed operation than write its own queries.

Across Connect AI: a service account when the agent serves a team and shouldn't run as one person, and audit logs you can forward to your security information and event management (SIEM) system.

What these AI agent guardrails don't cover

  • Data Security doesn't check what a user types into the chat. When I typed a fake SSN there, it went through unmasked.

  • Agents that skip the gateway. An agent with its own provider key or database password bypasses all of these guardrails. Give the app only a virtual key and a PAT.

  • .env edits. The ops line is in the app's config, so anyone who can edit the file can remove it. The Permissions tab is enforced in Connect AI, so use both.

  • Prompt injection. Fewer tools, masked data, and narrow permissions limit what an injected prompt can do, but they don't stop it. (More on MCP gateway threats.)

Add AI agent guardrails to your own agent

Start with the Connect AI Quick Start Guide, then point the agent quickstart at a connection you care about and repeat Steps 2 to 5.

Frequently asked questions

Do I need to change my agent's code to add these guardrails?

No. The LLM Gateway is a base URL and a key, read-only tools are an MCP server URL, and masking and permissions are settings in Connect AI.

How do I make an MCP server read-only?

Limit the tools it offers. With Connect AI's MCP server, add an ops parameter to the server URL that lists only read tools, such as get_tables and queryDat. The agent then has no tool that can insert, update, or run stored procedures.

Does PII masking cover what a user types into the chat?

No. Masking covers the queries and results that pass through Connect AI. Chat text reaches the model unmasked.

What's the difference between an LLM gateway and an MCP gateway?

An LLM gateway controls model calls: logging, routing, and budgets. An MCP gateway controls tool calls, and its guardrails set which tools an agent gets and what data comes back.

Should a production agent use a personal access token or a service account?

A PAT fits an agent that acts as one person, like a developer's own tool. Production agents often run as a service account, an identity for software. Service accounts can't use connections set to per-user authentication, so they can't limit access per end user.

Get started with CData Connect AI

Connect to any data source in minutes and start building with your data.

Read the docs