Skip to content
reaatechREAATECH

@reaatech/tool-use-firewall-core

npm v0.1.0

Core types, error classes, structured logger, sensitive data redactor, and ReDoS-safe regex utilities for the tool-use-firewall ecosystem. Exports TypeScript types (`RequestContext`, `Middleware`, `InterceptorResponse`), a typed error hierarchy (`FirewallError`, `RateLimitError`, `ValidationError`, `PolicyViolationError`, `BudgetExceededError`, `ApprovalRequiredError`), a JSON logger that writes to stderr, a `redact()` function with built-in patterns for API keys and tokens, and `safeRegExp()`/`isSafeRegex()` for ReDoS-safe regex compilation.

@reaatech/tool-use-firewall-core

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Core types, error classes, structured logger, sensitive data redactor, and ReDoS-safe regex utilities for tool-use-firewall. This is the foundational package used by every other @reaatech/tool-use-firewall-* package.

Installation

terminal
npm install @reaatech/tool-use-firewall-core
# or
pnpm add @reaatech/tool-use-firewall-core

Feature Overview

  • Request context typesRequestContext, Middleware, MiddlewareResult, InterceptorResponse for building the interceptor pipeline
  • Typed error hierarchyFirewallError, RateLimitError, ValidationError, PolicyViolationError, BudgetExceededError, ApprovalRequiredError — all with error codes
  • Structured logger — JSON logging to stderr (never stdout) to avoid corrupting MCP JSON-RPC streams
  • Sensitive data redactor — Pattern-based redaction of API keys, bearer tokens, emails, and custom patterns
  • ReDoS-safe regexsafeRegExp() and isSafeRegex() with nested quantifier detection, depth limits, and length caps
  • Glob-to-regex conversionglobToRegex() for tool name pattern matching
  • Zero runtime dependencies — lightweight and tree-shakeable

Quick Start

typescript
import { FirewallError, Logger, redact, safeRegExp } from "@reaatech/tool-use-firewall-core";
 
// Throw typed errors
throw new FirewallError({ code: "POLICY_VIOLATION", message: "Blocked by policy" });
 
// Structured logging (writes to stderr)
const log = new Logger("MyService");
log.info("Request processed", { toolName: "db_query", latency: 42 });
 
// Redact sensitive data
const safe = redact({ api_key: "sk-1234", query: "SELECT 1" });
// → { api_key: "[REDACTED]", query: "SELECT 1" }
 
// Safe regex compilation
const re = safeRegExp("^SELECT\\s+.*$", "i");

Exports

Types

ExportDescription
RequestContextImmutable request metadata flowing through the pipeline: requestId, sessionId, method, toolName, arguments, metadata
createRequestContextFactory for constructing a RequestContext with receivedAt timestamp and empty metadata map
MiddlewareInterface: execute(context: RequestContext): Promise<MiddlewareResult>
MiddlewareActionUnion: CONTINUE | BLOCK | APPROVAL_REQUIRED
MiddlewareResult{ action, reason?, metadata? }
InterceptorResponse{ allowed, action, reason?, metadata? }

Error Classes

ClassCodeDescription
FirewallError(custom)Base error with code, message, requestId, details, toJSON()
PolicyViolationErrorPOLICY_VIOLATIONBlocked by a policy rule
RateLimitErrorRATE_LIMIT_EXCEEDEDRate limit hit, includes retryAfterMs
ValidationErrorVALIDATION_ERRORArgument validation failed
BudgetExceededErrorBUDGET_EXCEEDEDSession cost budget exceeded
ApprovalRequiredErrorAPPROVAL_REQUIREDHuman approval required, includes approvalId

Utilities

ExportDescription
LoggerJSON structured logger, writes to stderr (never stdout)
redactDeep-redact values using pattern matching
DEFAULT_REDACTION_PATTERNSBuilt-in patterns: API keys, bearer tokens, emails
safeRegExp(pattern, flags?)Compile a regex safely, throws UnsafeRegexError on ReDoS patterns
isSafeRegex(pattern)Boolean check for ReDoS safety
globToRegex(pattern)Convert *-wildcard glob to anchored regex
UnsafeRegexErrorThrown by safeRegExp when a pattern is rejected as unsafe

License

MIT