> 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
