> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/main-components/middleware/guardrail.md).

# GuardrailMiddleware

Block or reject tool invocations by tool name and argument regex policies

Class: `bxModules.bxai.models.middleware.core.GuardrailMiddleware`

Guards tool execution only. It can block specific tools or reject calls whose arguments match forbidden regex patterns.

## Features

* Denylist tool names with `blockedTools`
* Validate arguments per tool with `argPatterns`
* Works with both normalized `toolArgs` and provider-native tool call shapes
* Returns `reject()` with reason when a rule matches

## Constructor

```javascript
middleware = new bxModules.bxai.models.middleware.core.GuardrailMiddleware(
    blockedTools: [ "deleteRecord", "dropTable" ],
    argPatterns : {
        runSQL: [ { query: "^SELECT" } ]
    }
)
```

## Configuration

| Option         | Type   | Default | Description                                                                   |
| -------------- | ------ | ------- | ----------------------------------------------------------------------------- |
| `blockedTools` | array  | `[]`    | Tool names that are always rejected                                           |
| `argPatterns`  | struct | `{}`    | Per-tool regex rules in the form `{ toolName: [ { paramName: "pattern" } ] }` |

## Hooks Used

* `beforeToolCall`

## Example

```javascript
agent = aiAgent(
    tools      : [ runSQLTool, deleteUserTool ],
    middleware : [
        new bxModules.bxai.models.middleware.core.GuardrailMiddleware(
            blockedTools: [ "deleteUser" ],
            argPatterns : {
                runSQL: [
                    { query: "(?i)^\\s*select\\b" },
                    { query: "(?i)drop|truncate|delete" }
                ]
            }
        )
    ]
)
```

## Notes

* Tool-name matching for `blockedTools` is case-insensitive.
* Pattern rules run only for tools present in `argPatterns`.
* This middleware does not sanitize prompt text or model output.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai.ortusbooks.com/main-components/middleware/guardrail.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
