Skip to content

tool

Creates a builder for tool argument validation rules. Apply custom validation to specific tool arguments.

Signature

ts
const builder = tool(pattern);

Parameters

ParameterTypeDescription
patternstring | RegExpTarget tool name pattern (required)

Methods

MethodReturnsDescription
.check(fn)ToolBuilderSet validation function. Return true for violation (required)
.block(message?)RuleBlock on violation (severity: "error")
.warn(message?)RuleWarn on violation (severity: "warn")
.log(message?)RuleLog violation (severity: "info")

WARNING

Calling a terminal method without first calling .check() will throw an error.

check function

ts
(args: Record<string, unknown>) => boolean

Receives the tool's arguments object. Return true to indicate a violation.

Returns

ToolBuilder — after calling .check(), terminal methods return a Rule.

Examples

Restrict email recipients

ts
import { tool } from "open-mcp-guardrails";

tool("send_email")
  .check(args => !(args.to as string)?.endsWith("@company.com"))
  .block("Only internal addresses allowed");

Block system file writes

ts
tool(/write_file|delete_file/)
  .check(args => (args.path as string)?.startsWith("/etc"))
  .block("Cannot modify system files");

Prevent destructive SQL

ts
tool("execute_sql")
  .check(args => /\b(DROP|DELETE|TRUNCATE)\b/i.test(args.query as string))
  .block("Destructive SQL is not allowed");

JSON Config

In JSON config, tool validation uses declarative conditions instead of a check function:

json
{
  "type": "tool",
  "action": "block",
  "tool": "send_email",
  "conditions": [
    { "field": "to", "operator": "not_ends_with", "value": "@company.com" }
  ],
  "message": "Only internal addresses allowed"
}
json
{
  "type": "tool",
  "action": "block",
  "tool": "/write_file|delete_file/",
  "conditions": [
    { "field": "path", "operator": "starts_with", "value": "/etc" }
  ],
  "message": "Cannot modify system files"
}
json
{
  "type": "tool",
  "action": "block",
  "tool": "execute_sql",
  "conditions": [
    { "field": "query", "operator": "matches", "value": "/\\b(DROP|DELETE|TRUNCATE)\\b/i" }
  ],
  "message": "Destructive SQL is not allowed"
}

Available operators

OperatorDescription
equalsExact match
not_equalsNot equal
starts_withString prefix match
not_starts_withString prefix does not match
ends_withString suffix match
not_ends_withString suffix does not match
containsSubstring match
not_containsSubstring does not match
matchesRegex match (use /pattern/flags syntax)
not_matchesRegex does not match
existsField is present and non-null
not_existsField is missing or null