mcp-warden 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -39
- package/dist/builder/policy-builder.d.ts +61 -0
- package/dist/builder/policy-builder.d.ts.map +1 -0
- package/dist/builder/policy-builder.js +88 -0
- package/dist/builder/policy-builder.js.map +1 -0
- package/dist/cli/index.js +311 -62
- package/dist/cli/index.js.map +1 -1
- package/dist/core/interceptor.d.ts +36 -3
- package/dist/core/interceptor.d.ts.map +1 -1
- package/dist/core/interceptor.js +91 -44
- package/dist/core/interceptor.js.map +1 -1
- package/dist/events/typed-emitter.d.ts +25 -0
- package/dist/events/typed-emitter.d.ts.map +1 -0
- package/dist/events/typed-emitter.js +48 -0
- package/dist/events/typed-emitter.js.map +1 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/dist/security/arg-validator.d.ts +17 -0
- package/dist/security/arg-validator.d.ts.map +1 -0
- package/dist/security/arg-validator.js +88 -0
- package/dist/security/arg-validator.js.map +1 -0
- package/dist/types/policy.d.ts +62 -70
- package/dist/types/policy.d.ts.map +1 -1
- package/dist/types/policy.js +121 -40
- package/dist/types/policy.js.map +1 -1
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -8,12 +8,16 @@ High-performance security guardrails for MCP-compatible AI agents and tool execu
|
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
-
- Policy-based tool authorization
|
|
12
|
-
- Filesystem path enforcement
|
|
13
|
-
- Human-in-the-loop gating with `REQUIRES_APPROVAL` status
|
|
14
|
-
- Prompt-injection scanning
|
|
15
|
-
- Output redaction for
|
|
16
|
-
-
|
|
11
|
+
- Policy-based tool authorization (exact strings and regex patterns).
|
|
12
|
+
- Filesystem path enforcement — `blocked` denies all access; `read-only` allows reads and denies write-intent tool calls.
|
|
13
|
+
- Human-in-the-loop gating with `REQUIRES_APPROVAL` status.
|
|
14
|
+
- Prompt-injection scanning with word-boundary-aware regex matching.
|
|
15
|
+
- Output redaction for emails, API keys, IPv4/IPv6 addresses, and phone numbers — in a single regex pass.
|
|
16
|
+
- Global and per-tool rate limiting with O(1) circular-buffer implementation.
|
|
17
|
+
- Per-tool circuit-breaker protection with automatic TTL cleanup.
|
|
18
|
+
- Input size limits (max nesting depth and max byte size) to guard against oversized payloads.
|
|
19
|
+
- Metrics hook for observability (timing, allow/block decisions, violation codes).
|
|
20
|
+
- Extensible middleware chain — register custom policy layers with `.use()`.
|
|
17
21
|
- CLI audit workflow for identifying over-permissioned MCP servers.
|
|
18
22
|
|
|
19
23
|
## Table of Contents
|
|
@@ -21,6 +25,11 @@ High-performance security guardrails for MCP-compatible AI agents and tool execu
|
|
|
21
25
|
- [Why Security Matters for AI Agents](#why-security-matters-for-ai-agents)
|
|
22
26
|
- [Quick Start](#quick-start)
|
|
23
27
|
- [Policy Configuration](#policy-configuration)
|
|
28
|
+
- [Advanced Options](#advanced-options)
|
|
29
|
+
- [Per-Tool Rate Limits](#per-tool-rate-limits)
|
|
30
|
+
- [Metrics Hook](#metrics-hook)
|
|
31
|
+
- [Input Size Limits](#input-size-limits)
|
|
32
|
+
- [Custom Middleware](#custom-middleware)
|
|
24
33
|
- [CLI Commands](#cli-commands)
|
|
25
34
|
- [Development](#development)
|
|
26
35
|
- [License](#license)
|
|
@@ -30,14 +39,16 @@ High-performance security guardrails for MCP-compatible AI agents and tool execu
|
|
|
30
39
|
|
|
31
40
|
AI agents can execute tools with real-world side effects: reading files, modifying systems, calling external APIs, and handling sensitive data. Without guardrails, a single prompt injection or over-permissioned server can lead to data leakage, privilege escalation, or runaway tool loops.
|
|
32
41
|
|
|
33
|
-
mcp-warden
|
|
42
|
+
mcp-warden enforces a security boundary before and after every tool execution:
|
|
43
|
+
|
|
34
44
|
- Blocks unauthorized tools using explicit policy rules.
|
|
35
|
-
- Denies tool calls
|
|
45
|
+
- Denies tool calls targeting blocked filesystem paths; blocks write operations on read-only paths.
|
|
36
46
|
- Returns `REQUIRES_APPROVAL` before execution when `approvalRequired` is enabled.
|
|
37
|
-
- Detects prompt-injection signatures in tool arguments.
|
|
38
|
-
- Enforces rate limits to
|
|
39
|
-
- Applies circuit-breaker protection for repeated
|
|
47
|
+
- Detects prompt-injection signatures in tool arguments using word-boundary regex patterns.
|
|
48
|
+
- Enforces global and per-tool rate limits to prevent abuse and runaway call storms.
|
|
49
|
+
- Applies per-tool circuit-breaker protection for repeated failures.
|
|
40
50
|
- Redacts sensitive output data before it reaches downstream systems.
|
|
51
|
+
- Rejects oversized or deeply nested payloads before any other check runs.
|
|
41
52
|
|
|
42
53
|
## Quick Start
|
|
43
54
|
|
|
@@ -53,13 +64,11 @@ npm install mcp-warden
|
|
|
53
64
|
{
|
|
54
65
|
"allowedTools": ["list_dir", "read_file", "grep_search"],
|
|
55
66
|
"restrictedPaths": [
|
|
56
|
-
{
|
|
57
|
-
|
|
58
|
-
"mode": "blocked"
|
|
59
|
-
}
|
|
67
|
+
{ "path": "/etc", "mode": "blocked" },
|
|
68
|
+
{ "path": "/home", "mode": "read-only" }
|
|
60
69
|
],
|
|
61
70
|
"maxCallsPerMinute": 60,
|
|
62
|
-
"approvalRequired":
|
|
71
|
+
"approvalRequired": false
|
|
63
72
|
}
|
|
64
73
|
```
|
|
65
74
|
|
|
@@ -70,12 +79,15 @@ import { McpGuardian, type GuardianPolicy } from "mcp-warden";
|
|
|
70
79
|
|
|
71
80
|
const policy: GuardianPolicy = {
|
|
72
81
|
allowedTools: ["read_file", /^search_/],
|
|
73
|
-
restrictedPaths: [
|
|
82
|
+
restrictedPaths: [
|
|
83
|
+
{ path: "/etc", mode: "blocked" },
|
|
84
|
+
{ path: "/home", mode: "read-only" }
|
|
85
|
+
],
|
|
74
86
|
maxCallsPerMinute: 60,
|
|
75
|
-
approvalRequired:
|
|
87
|
+
approvalRequired: false
|
|
76
88
|
};
|
|
77
89
|
|
|
78
|
-
const guardian = new McpGuardian(policy
|
|
90
|
+
const guardian = new McpGuardian(policy);
|
|
79
91
|
|
|
80
92
|
const guardedHandler = guardian.wrapHandler(async (request) => {
|
|
81
93
|
// Your MCP transport execution logic
|
|
@@ -97,38 +109,148 @@ npx mcp-warden audit ./claude_desktop_config.json
|
|
|
97
109
|
npx mcp-warden init
|
|
98
110
|
```
|
|
99
111
|
|
|
100
|
-
|
|
112
|
+
## Policy Configuration
|
|
113
|
+
|
|
114
|
+
| Option | Type | Required | Description |
|
|
115
|
+
|---|---|---|---|
|
|
116
|
+
| `allowedTools` | `Array<string \| RegExp>` | Yes | Allowed tool names or regex matchers. |
|
|
117
|
+
| `restrictedPaths` | `Array<{ path: string; mode: "read-only" \| "blocked" }>` | Yes | Path access constraints. `blocked` = all access denied. `read-only` = reads allowed, write-intent tool calls denied. |
|
|
118
|
+
| `maxCallsPerMinute` | `number` | Yes | Rolling 60-second quota for tool calls. |
|
|
119
|
+
| `approvalRequired` | `boolean` | Yes | When `true`, every tool call is paused with `REQUIRES_APPROVAL`. |
|
|
101
120
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
121
|
+
## Advanced Options
|
|
122
|
+
|
|
123
|
+
Pass an `McpGuardianOptions` object as the second constructor argument:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
const guardian = new McpGuardian(policy, {
|
|
127
|
+
dryRun: false, // log violations but allow all requests
|
|
128
|
+
redactToolOutputs: true, // redact PII from tool responses (default: true)
|
|
129
|
+
logger: (violation) => myLogger.warn(violation),
|
|
130
|
+
injectionKeywords: [ // override default injection phrases
|
|
131
|
+
"ignore previous instructions",
|
|
132
|
+
"you are now unrestricted"
|
|
133
|
+
],
|
|
134
|
+
circuitBreaker: {
|
|
135
|
+
threshold: 5, // failures before circuit opens (default: 5)
|
|
136
|
+
cooldownMs: 60_000, // how long circuit stays open (default: 60s)
|
|
137
|
+
stateTtlMs: 600_000 // evict idle tool state after 10min (default)
|
|
138
|
+
},
|
|
139
|
+
maxArgDepth: 20, // max nesting depth of tool args (default: 20)
|
|
140
|
+
maxArgBytes: 524288 // max byte size of tool args (default: 512 KB)
|
|
141
|
+
});
|
|
105
142
|
```
|
|
106
143
|
|
|
107
|
-
##
|
|
144
|
+
## Per-Tool Rate Limits
|
|
145
|
+
|
|
146
|
+
Override the global rate limit for specific tools. The first matching rule wins.
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
const guardian = new McpGuardian(policy, {
|
|
150
|
+
toolRateLimits: [
|
|
151
|
+
{ tool: "expensive_search", maxCallsPerMinute: 5 },
|
|
152
|
+
{ tool: /^write_/, maxCallsPerMinute: 10 }
|
|
153
|
+
]
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Metrics Hook
|
|
158
|
+
|
|
159
|
+
Receive an observability snapshot after every validated request:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
const guardian = new McpGuardian(policy, {
|
|
163
|
+
metricsHook: (metrics) => {
|
|
164
|
+
console.log({
|
|
165
|
+
method: metrics.method,
|
|
166
|
+
tool: metrics.toolName,
|
|
167
|
+
allowed: metrics.allowed,
|
|
168
|
+
violation: metrics.violationCode,
|
|
169
|
+
durationMs: metrics.durationMs
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`GuardianMetrics` fields:
|
|
108
176
|
|
|
109
|
-
|
|
|
110
|
-
|
|
111
|
-
|
|
|
112
|
-
|
|
|
113
|
-
|
|
|
114
|
-
|
|
|
177
|
+
| Field | Type | Description |
|
|
178
|
+
|---|---|---|
|
|
179
|
+
| `timestamp` | `number` | Unix ms when the request was processed. |
|
|
180
|
+
| `method` | `string` | JSON-RPC method name. |
|
|
181
|
+
| `toolName` | `string \| undefined` | Tool name for `tools/call` requests. |
|
|
182
|
+
| `allowed` | `boolean` | Whether the request was allowed. |
|
|
183
|
+
| `violationCode` | `"PERMISSION_DENIED" \| "REQUIRES_APPROVAL" \| undefined` | Set when blocked. |
|
|
184
|
+
| `durationMs` | `number` | Processing time in milliseconds. |
|
|
185
|
+
|
|
186
|
+
## Input Size Limits
|
|
187
|
+
|
|
188
|
+
Requests with oversized or deeply nested arguments are rejected before any other middleware runs:
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
const guardian = new McpGuardian(policy, {
|
|
192
|
+
maxArgDepth: 10, // reject args nested deeper than 10 levels
|
|
193
|
+
maxArgBytes: 65536 // reject args larger than 64 KB
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Custom Middleware
|
|
198
|
+
|
|
199
|
+
Register additional policy layers with `.use()`. Middleware runs after all built-in checks:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
guardian.use(async (context, next) => {
|
|
203
|
+
if (context.toolName === "restricted_tool" && !context.isDryRun) {
|
|
204
|
+
return { allowed: false, reason: "This tool requires explicit opt-in." };
|
|
205
|
+
}
|
|
206
|
+
return next();
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
`GuardianContext` properties:
|
|
211
|
+
|
|
212
|
+
| Property | Type | Description |
|
|
213
|
+
|---|---|---|
|
|
214
|
+
| `request` | `JsonRpcRequest` | The raw JSON-RPC 2.0 request. |
|
|
215
|
+
| `policy` | `GuardianPolicy` | Active policy configuration. |
|
|
216
|
+
| `isDryRun` | `boolean` | Whether dry-run mode is active. |
|
|
217
|
+
| `toolName` | `string \| undefined` | Extracted tool name. |
|
|
218
|
+
| `toolArgs` | `unknown` | Extracted tool arguments. |
|
|
115
219
|
|
|
116
220
|
## CLI Commands
|
|
117
221
|
|
|
118
|
-
|
|
119
|
-
|
|
222
|
+
**Audit a config file:**
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
npx mcp-warden audit <config-path>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Scans `claude_desktop_config.json`, `cursor-settings.json`, or any MCP JSON config for over-permissioned servers. Exits with code `1` if critical findings are detected.
|
|
229
|
+
|
|
230
|
+
Critical findings include:
|
|
231
|
+
- Permissions granting full disk access (`*`, `all`, `filesystem:*`)
|
|
232
|
+
- Broad filesystem paths (`/`, `~`, `/Users/...`)
|
|
233
|
+
- Dangerous CLI flags (`--allow-all`, `--dangerously-skip-permissions`, etc.)
|
|
234
|
+
- Environment variables enabling unrestricted access
|
|
235
|
+
|
|
236
|
+
**Generate a default policy:**
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
npx mcp-warden init [--output <path>] [--force]
|
|
240
|
+
```
|
|
120
241
|
|
|
121
|
-
|
|
122
|
-
- Green: SAFE
|
|
123
|
-
- Red: CRITICAL
|
|
242
|
+
Creates a secure `mcp-policy.json` with conservative defaults. Use `--force` to overwrite an existing file.
|
|
124
243
|
|
|
125
244
|
## Development
|
|
126
245
|
|
|
127
246
|
```bash
|
|
128
247
|
npm install
|
|
129
|
-
npm run typecheck
|
|
130
|
-
npm test
|
|
131
|
-
npm run
|
|
248
|
+
npm run typecheck # TypeScript type checking
|
|
249
|
+
npm test # Run test suite (26 tests)
|
|
250
|
+
npm run coverage # Run tests with v8 coverage report
|
|
251
|
+
npm run lint # ESLint
|
|
252
|
+
npm run format # Prettier
|
|
253
|
+
npm run build # Compile to dist/
|
|
132
254
|
```
|
|
133
255
|
|
|
134
256
|
## License
|
|
@@ -137,4 +259,4 @@ npm run build
|
|
|
137
259
|
|
|
138
260
|
## Security Disclaimer
|
|
139
261
|
|
|
140
|
-
mcp-warden is a runtime governance layer designed to mitigate risks. It is not a replacement for OS-level permissions, network-level firewalls, or the principle of least privilege. Always run AI agents in isolated environments
|
|
262
|
+
mcp-warden is a runtime governance layer designed to mitigate risks in AI agent tool execution. It is not a replacement for OS-level permissions, network-level firewalls, or the principle of least privilege. Always run AI agents in isolated environments with minimal permissions where possible.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ArgSchema, GuardianPolicy } from "../types/policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fluent builder for constructing a `GuardianPolicy` without writing object literals.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const policy = new PolicyBuilder()
|
|
8
|
+
* .allow("read_file")
|
|
9
|
+
* .allow(/^search_/)
|
|
10
|
+
* .block("/etc")
|
|
11
|
+
* .readOnly("/home")
|
|
12
|
+
* .rateLimit(30)
|
|
13
|
+
* .argSchema("read_file", {
|
|
14
|
+
* type: "object",
|
|
15
|
+
* required: ["path"],
|
|
16
|
+
* properties: { path: { type: "string" } }
|
|
17
|
+
* })
|
|
18
|
+
* .build();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class PolicyBuilder {
|
|
22
|
+
private readonly _tools;
|
|
23
|
+
private readonly _paths;
|
|
24
|
+
private _limit;
|
|
25
|
+
private _approval;
|
|
26
|
+
private readonly _schemas;
|
|
27
|
+
/**
|
|
28
|
+
* Allow a tool by exact name or regex pattern.
|
|
29
|
+
* Call multiple times to allow multiple tools.
|
|
30
|
+
*/
|
|
31
|
+
allow(tool: string | RegExp): this;
|
|
32
|
+
/**
|
|
33
|
+
* Block all access (reads and writes) to a filesystem path.
|
|
34
|
+
*/
|
|
35
|
+
block(path: string): this;
|
|
36
|
+
/**
|
|
37
|
+
* Allow reads but deny write-intent tool calls on a filesystem path.
|
|
38
|
+
*/
|
|
39
|
+
readOnly(path: string): this;
|
|
40
|
+
/**
|
|
41
|
+
* Set the global tool-call rate limit (calls per rolling minute).
|
|
42
|
+
* Default: 60.
|
|
43
|
+
*/
|
|
44
|
+
rateLimit(maxPerMinute: number): this;
|
|
45
|
+
/**
|
|
46
|
+
* Require human approval before any tool call executes.
|
|
47
|
+
* Pass `false` to disable (useful when composing builders).
|
|
48
|
+
*/
|
|
49
|
+
requireApproval(required?: boolean): this;
|
|
50
|
+
/**
|
|
51
|
+
* Attach an argument shape schema to a specific tool.
|
|
52
|
+
* Requests with non-conforming arguments will be rejected.
|
|
53
|
+
*/
|
|
54
|
+
argSchema(tool: string, schema: ArgSchema): this;
|
|
55
|
+
/**
|
|
56
|
+
* Validate and return the constructed `GuardianPolicy`.
|
|
57
|
+
* Throws if the accumulated configuration is invalid.
|
|
58
|
+
*/
|
|
59
|
+
build(): GuardianPolicy;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=policy-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-builder.d.ts","sourceRoot":"","sources":["../../src/builder/policy-builder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAA6B,MAAM,oBAAoB,CAAC;AAE/F;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IAEzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyB;IAEhD,OAAO,CAAC,MAAM,CAAM;IAEpB,OAAO,CAAC,SAAS,CAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiC;IAE1D;;;OAGG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAKlC;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKzB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAK5B;;;OAGG;IACH,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;IAKrC;;;OAGG;IACH,eAAe,CAAC,QAAQ,UAAO,GAAG,IAAI;IAKtC;;;OAGG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,GAAG,IAAI;IAKhD;;;OAGG;IACH,KAAK,IAAI,cAAc;CAUxB"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { GuardianPolicySchema } from "../types/policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fluent builder for constructing a `GuardianPolicy` without writing object literals.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const policy = new PolicyBuilder()
|
|
8
|
+
* .allow("read_file")
|
|
9
|
+
* .allow(/^search_/)
|
|
10
|
+
* .block("/etc")
|
|
11
|
+
* .readOnly("/home")
|
|
12
|
+
* .rateLimit(30)
|
|
13
|
+
* .argSchema("read_file", {
|
|
14
|
+
* type: "object",
|
|
15
|
+
* required: ["path"],
|
|
16
|
+
* properties: { path: { type: "string" } }
|
|
17
|
+
* })
|
|
18
|
+
* .build();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class PolicyBuilder {
|
|
22
|
+
_tools = [];
|
|
23
|
+
_paths = [];
|
|
24
|
+
_limit = 60;
|
|
25
|
+
_approval = false;
|
|
26
|
+
_schemas = {};
|
|
27
|
+
/**
|
|
28
|
+
* Allow a tool by exact name or regex pattern.
|
|
29
|
+
* Call multiple times to allow multiple tools.
|
|
30
|
+
*/
|
|
31
|
+
allow(tool) {
|
|
32
|
+
this._tools.push(tool);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Block all access (reads and writes) to a filesystem path.
|
|
37
|
+
*/
|
|
38
|
+
block(path) {
|
|
39
|
+
this._paths.push({ path, mode: "blocked" });
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Allow reads but deny write-intent tool calls on a filesystem path.
|
|
44
|
+
*/
|
|
45
|
+
readOnly(path) {
|
|
46
|
+
this._paths.push({ path, mode: "read-only" });
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Set the global tool-call rate limit (calls per rolling minute).
|
|
51
|
+
* Default: 60.
|
|
52
|
+
*/
|
|
53
|
+
rateLimit(maxPerMinute) {
|
|
54
|
+
this._limit = maxPerMinute;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Require human approval before any tool call executes.
|
|
59
|
+
* Pass `false` to disable (useful when composing builders).
|
|
60
|
+
*/
|
|
61
|
+
requireApproval(required = true) {
|
|
62
|
+
this._approval = required;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Attach an argument shape schema to a specific tool.
|
|
67
|
+
* Requests with non-conforming arguments will be rejected.
|
|
68
|
+
*/
|
|
69
|
+
argSchema(tool, schema) {
|
|
70
|
+
this._schemas[tool] = schema;
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Validate and return the constructed `GuardianPolicy`.
|
|
75
|
+
* Throws if the accumulated configuration is invalid.
|
|
76
|
+
*/
|
|
77
|
+
build() {
|
|
78
|
+
const hasSchemas = Object.keys(this._schemas).length > 0;
|
|
79
|
+
return GuardianPolicySchema.parse({
|
|
80
|
+
allowedTools: [...this._tools],
|
|
81
|
+
restrictedPaths: [...this._paths],
|
|
82
|
+
maxCallsPerMinute: this._limit,
|
|
83
|
+
approvalRequired: this._approval,
|
|
84
|
+
...(hasSchemas ? { toolArgSchemas: { ...this._schemas } } : {})
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=policy-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-builder.js","sourceRoot":"","sources":["../../src/builder/policy-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,aAAa;IACP,MAAM,GAAe,EAAE,CAAC;IAExB,MAAM,GAAsB,EAAE,CAAC;IAExC,MAAM,GAAG,EAAE,CAAC;IAEZ,SAAS,GAAG,KAAK,CAAC;IAET,QAAQ,GAA8B,EAAE,CAAC;IAE1D;;;OAGG;IACH,KAAK,CAAC,IAAqB;QACzB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAY;QAChB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,IAAY;QACnB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,SAAS,CAAC,YAAoB;QAC5B,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC;QAC3B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,eAAe,CAAC,QAAQ,GAAG,IAAI;QAC7B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,SAAS,CAAC,IAAY,EAAE,MAAiB;QACvC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,KAAK;QACH,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;QACzD,OAAO,oBAAoB,CAAC,KAAK,CAAC;YAChC,YAAY,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;YAC9B,eAAe,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;YACjC,iBAAiB,EAAE,IAAI,CAAC,MAAM;YAC9B,gBAAgB,EAAE,IAAI,CAAC,SAAS;YAChC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,EAAE,GAAG,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAChE,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { GuardianPolicySchema } from \"../types/policy.js\";\nimport type { ArgSchema, GuardianPolicy, PathRestriction, ToolRule } from \"../types/policy.js\";\n\n/**\n * Fluent builder for constructing a `GuardianPolicy` without writing object literals.\n *\n * @example\n * ```ts\n * const policy = new PolicyBuilder()\n * .allow(\"read_file\")\n * .allow(/^search_/)\n * .block(\"/etc\")\n * .readOnly(\"/home\")\n * .rateLimit(30)\n * .argSchema(\"read_file\", {\n * type: \"object\",\n * required: [\"path\"],\n * properties: { path: { type: \"string\" } }\n * })\n * .build();\n * ```\n */\nexport class PolicyBuilder {\n private readonly _tools: ToolRule[] = [];\n\n private readonly _paths: PathRestriction[] = [];\n\n private _limit = 60;\n\n private _approval = false;\n\n private readonly _schemas: Record<string, ArgSchema> = {};\n\n /**\n * Allow a tool by exact name or regex pattern.\n * Call multiple times to allow multiple tools.\n */\n allow(tool: string | RegExp): this {\n this._tools.push(tool);\n return this;\n }\n\n /**\n * Block all access (reads and writes) to a filesystem path.\n */\n block(path: string): this {\n this._paths.push({ path, mode: \"blocked\" });\n return this;\n }\n\n /**\n * Allow reads but deny write-intent tool calls on a filesystem path.\n */\n readOnly(path: string): this {\n this._paths.push({ path, mode: \"read-only\" });\n return this;\n }\n\n /**\n * Set the global tool-call rate limit (calls per rolling minute).\n * Default: 60.\n */\n rateLimit(maxPerMinute: number): this {\n this._limit = maxPerMinute;\n return this;\n }\n\n /**\n * Require human approval before any tool call executes.\n * Pass `false` to disable (useful when composing builders).\n */\n requireApproval(required = true): this {\n this._approval = required;\n return this;\n }\n\n /**\n * Attach an argument shape schema to a specific tool.\n * Requests with non-conforming arguments will be rejected.\n */\n argSchema(tool: string, schema: ArgSchema): this {\n this._schemas[tool] = schema;\n return this;\n }\n\n /**\n * Validate and return the constructed `GuardianPolicy`.\n * Throws if the accumulated configuration is invalid.\n */\n build(): GuardianPolicy {\n const hasSchemas = Object.keys(this._schemas).length > 0;\n return GuardianPolicySchema.parse({\n allowedTools: [...this._tools],\n restrictedPaths: [...this._paths],\n maxCallsPerMinute: this._limit,\n approvalRequired: this._approval,\n ...(hasSchemas ? { toolArgSchemas: { ...this._schemas } } : {})\n });\n }\n}\n"]}
|