# Guardrails

An AISIX gateway can inspect both sides of MCP tool execution. An input guardrail runs before the upstream call, while an output guardrail runs before the tool result returns to the client. An input block prevents the call from reaching the upstream MCP server. An output block occurs after the upstream call but withholds its result from the client. Both return a failed tool result.

MCP tool calls use the same guardrail chain that governs model traffic. Create guardrails in the shared traffic-control section, then attach them at a scope that can apply to MCP traffic. For shared scope and enforcement-mode behavior, see [Guardrail Policy Behavior](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md).

## How Guardrails Apply to MCP[​](#how-guardrails-apply-to-mcp "Direct link to How Guardrails Apply to MCP")

The gateway runs guardrails only on `tools/call` requests. The MCP handshake and `tools/list` carry no tool content and are not scanned.

For each tool call, AISIX resolves the guardrail chain once and runs both directions through it:

* Input: the tool-call arguments are scanned before the call. If a guardrail blocks, AISIX rejects the call and never contacts the upstream server.
* Output: the tool result is scanned before it returns to the client. If a guardrail blocks, AISIX withholds the result and returns a failed tool result instead.

An MCP tool call has no model, so model-scoped guardrails do not apply to MCP. Environment, MCP server, caller API key, and team scopes can match MCP calls. In AISIX Enterprise, AISIX Cloud, and AISIX OSS, attachments determine a guardrail's scope. An unattached guardrail inspects no traffic, including MCP calls.

When no guardrail matches the caller, the tool call runs with no added guardrail latency.

## What Gets Scanned[​](#what-gets-scanned "Direct link to What Gets Scanned")

* Input: the arguments object of the `tools/call` request. AISIX feeds the arguments to the same input hook the model path uses.
* Output: the decoded text exposed by standard content blocks. This includes each block's `text`, a resource link's `title` and `description`, and an embedded resource's `resource.text`. Resource names and URIs are identifiers rather than content, and base64 `blob` values are not decoded.
* Output: every string value in the result's `structuredContent`. A tool can return this machine-readable field alongside `content` without mirroring it into a text block.

How a guardrail reads these fields depends on its kind:

* The built-in `keyword` and `pii` guardrails judge each piece separately: each string value, and in the arguments and `structuredContent` also each object key and each number. A rule never matches across two pieces, and a `pii` mask hit on a key or a number blocks the call or the result, because neither can be rewritten. See [How Keyword and PII Guardrails Read Structured Content](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/content-coverage.md#how-keyword-and-pii-guardrails-read-structured-content).
* Guardrails that moderate individual fields — Alibaba Cloud AI Guardrails, Bedrock, Lakera, Presidio, and custom scripts — inspect the string values.
* The remaining guardrails evaluate one combined text: the string values joined with newlines, on both input and output. Parameter names and JSON punctuation are not part of it.

When the arguments contain no string value, or a result uses a nonstandard shape in which none of these fields yields text, the guardrails that evaluate one combined text scan the serialized arguments or result instead. So do `keyword` and `pii` when they find nothing else to judge. Guardrails that moderate individual fields inspect only the fields listed above.

A protocol-level error result with no result payload has no tool output to scan and is allowed through.

The gateway inspects MCP tool arguments and results in flight. It does not store them; content capture is a separate surface from guardrail inspection.

## Block Response[​](#block-response "Direct link to Block Response")

When a guardrail blocks a tool call or tool result, AISIX returns HTTP `200` with a tool result marked `isError`, not the model path's HTTP `422`:

```
{

  "jsonrpc": "2.0",

  "id": 1,

  "result": {

    "content": [{ "type": "text", "text": "tool call blocked by content policy (guardrail 'block-secrets')" }],

    "isError": true

  }

}
```

MCP separates a request that was not valid, which is a JSON-RPC protocol error, from a call that did not succeed, which is `isError` on the result. A policy rejection is the second kind: the request was well formed, so the rejection reaches the calling agent as tool output it can read and act on rather than as a transport failure.

The message names the firing guardrail and whether the input arguments or output result was blocked. It never repeats the content that matched. The gateway records the blocked call as a [usage event](https://docs.api7.ai/ai-gateway/mcp-gateway/observability.md) with its guardrail-blocked flag set.

If AISIX cannot parse a tool result as the expected JSON response, the output failure policy decides whether it can be returned. One output-reading guardrail that fails closed is enough to withhold the result. AISIX returns it only when nothing in the chain reads output or every output reader is fail-open; the latter case records `guardrail_bypassed_reason: unscannable_body`.

For the full MCP error format and other MCP status behavior, see [Headers and Error Codes](https://docs.api7.ai/ai-gateway/reference/headers-and-error-codes.md#mcp-errors).

## Scope a Guardrail to One MCP Server[​](#scope-a-guardrail-to-one-mcp-server "Direct link to Scope a Guardrail to One MCP Server")

Attach the guardrail with the `mcp_server` scope to inspect only tool calls routed to one registered server. In AISIX Enterprise and AISIX Cloud, set `scope_id` to the server ID. In `resources.yaml`, use the server name. AISIX scans both the arguments sent to that server and the results it returns.

`mcp_server` is the only scope that selects traffic by destination server. To narrow coverage by caller instead, use an `api_key` attachment for one caller or a `team` attachment for callers on one team. These caller-based scopes also apply to model traffic from the same API key or team. A model scope never applies to MCP because a tool call resolves no model.

The server must be available to the environment where the guardrail applies. The AISIX Cloud Admin API rejects an attachment to a server that is not exposed there, while a resources file does not check that the attachment's `scope_id` names a server in the file, so an attachment to an undefined server guards nothing. Guarding several servers takes one attachment each; the guardrail still runs once per request.

## Verify Guardrail Blocking[​](#verify-guardrail-blocking "Direct link to Verify Guardrail Blocking")

Create a keyword guardrail with a term you can trigger, such as `secret`. Attach it to the caller API key you will use for the test. In AISIX Enterprise and AISIX Cloud, use the caller API key ID as the attachment's `scope_id`. In `resources.yaml`, use the key's `display_name`.

See [Configure Keyword Guardrails](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/keyword.md) for the configuration workflow.

Then connect an MCP client with a caller API key that allows a tool, and call that tool with an argument containing the blocked term.

The tool call should return HTTP `200` with a result whose `isError` is `true`, and the upstream MCP server is never contacted. A call whose arguments and result are both clean returns normally. Switching the guardrail to monitor mode lets the same call through while still recording the match; see [Use Monitor Mode](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/keyword.md#use-monitor-mode).

## AISIX Enterprise or AISIX Cloud[​](#aisix-enterprise-or-aisix-cloud "Direct link to AISIX Enterprise or AISIX Cloud")

In AISIX Enterprise and AISIX Cloud, create and attach guardrails through the control plane instead of declaring them in a resources file. To inspect MCP tool calls, use a scope that can apply to non-model traffic: the whole environment, a specific MCP server, the caller API key, or the team.

Do not use a model-specific scope when the guardrail should inspect MCP traffic. An MCP tool call has no model, so a model-scoped guardrail never runs on it.

## Next Steps[​](#next-steps "Direct link to Next Steps")

You now understand how guardrails inspect MCP tool arguments and results. Use these guides to create guardrails or observe the blocked calls:

* [Configure Keyword Guardrails](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/keyword.md): create a guardrail and choose its enforcement mode.
* [Guardrail Policy Behavior](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md): review scope matching and enforcement modes.
* [Observability](https://docs.api7.ai/ai-gateway/mcp-gateway/observability.md): see blocked tool calls in usage events and metrics.
