meshguard 0.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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -0
  3. package/dist/cjs/client.d.ts +92 -0
  4. package/dist/cjs/client.d.ts.map +1 -0
  5. package/dist/cjs/client.js +314 -0
  6. package/dist/cjs/client.js.map +1 -0
  7. package/dist/cjs/exceptions.d.ts +33 -0
  8. package/dist/cjs/exceptions.d.ts.map +1 -0
  9. package/dist/cjs/exceptions.js +60 -0
  10. package/dist/cjs/exceptions.js.map +1 -0
  11. package/dist/cjs/index.d.ts +23 -0
  12. package/dist/cjs/index.d.ts.map +1 -0
  13. package/dist/cjs/index.js +32 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/langchain.d.ts +106 -0
  16. package/dist/cjs/langchain.d.ts.map +1 -0
  17. package/dist/cjs/langchain.js +157 -0
  18. package/dist/cjs/langchain.js.map +1 -0
  19. package/dist/cjs/package.json +1 -0
  20. package/dist/cjs/types.d.ts +91 -0
  21. package/dist/cjs/types.d.ts.map +1 -0
  22. package/dist/cjs/types.js +6 -0
  23. package/dist/cjs/types.js.map +1 -0
  24. package/dist/esm/client.d.ts +92 -0
  25. package/dist/esm/client.d.ts.map +1 -0
  26. package/dist/esm/client.js +310 -0
  27. package/dist/esm/client.js.map +1 -0
  28. package/dist/esm/exceptions.d.ts +33 -0
  29. package/dist/esm/exceptions.d.ts.map +1 -0
  30. package/dist/esm/exceptions.js +53 -0
  31. package/dist/esm/exceptions.js.map +1 -0
  32. package/dist/esm/index.d.ts +23 -0
  33. package/dist/esm/index.d.ts.map +1 -0
  34. package/dist/esm/index.js +24 -0
  35. package/dist/esm/index.js.map +1 -0
  36. package/dist/esm/langchain.d.ts +106 -0
  37. package/dist/esm/langchain.d.ts.map +1 -0
  38. package/dist/esm/langchain.js +151 -0
  39. package/dist/esm/langchain.js.map +1 -0
  40. package/dist/esm/types.d.ts +91 -0
  41. package/dist/esm/types.d.ts.map +1 -0
  42. package/dist/esm/types.js +5 -0
  43. package/dist/esm/types.js.map +1 -0
  44. package/package.json +76 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MeshGuard
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,293 @@
1
+ # MeshGuard SDK for TypeScript / JavaScript
2
+
3
+ > AI agent governance — policy enforcement, audit logging, and trust management.
4
+
5
+ [![npm](https://img.shields.io/npm/v/meshguard)](https://www.npmjs.com/package/meshguard)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.4+-blue)](https://www.typescriptlang.org/)
7
+ [![Node.js](https://img.shields.io/badge/Node.js-18+-green)](https://nodejs.org/)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
+
10
+ MeshGuard provides governance guardrails for AI agents. This SDK lets your TypeScript/JavaScript agents enforce policies, generate audit trails, and manage trust — with zero runtime dependencies.
11
+
12
+ ## Features
13
+
14
+ - 🛡️ **Policy enforcement** — check, enforce, or govern any action
15
+ - 📋 **Audit logging** — full trail of every decision
16
+ - 🤖 **Agent management** — create, list, and revoke agents
17
+ - 🔗 **LangChain.js integration** — govern tools and toolkits
18
+ - 📦 **Zero runtime deps** — uses native `fetch` (Node 18+)
19
+ - 🎯 **Full TypeScript** — complete type definitions
20
+ - 🔄 **Dual output** — ESM + CommonJS
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ npm install meshguard
26
+ ```
27
+
28
+ ## Quick Start
29
+
30
+ ```ts
31
+ import { MeshGuardClient } from "meshguard";
32
+
33
+ const client = new MeshGuardClient({
34
+ gatewayUrl: "https://dashboard.meshguard.app",
35
+ agentToken: "your-agent-token",
36
+ });
37
+
38
+ // Check if an action is allowed
39
+ const decision = await client.check("read:contacts");
40
+ if (decision.allowed) {
41
+ console.log("Access granted!");
42
+ }
43
+ ```
44
+
45
+ ## Configuration
46
+
47
+ The client reads configuration from constructor options or environment variables:
48
+
49
+ | Option | Env Variable | Default |
50
+ | ------------ | ------------------------ | ------------------------ |
51
+ | `gatewayUrl` | `MESHGUARD_GATEWAY_URL` | `http://localhost:3100` |
52
+ | `agentToken` | `MESHGUARD_AGENT_TOKEN` | — |
53
+ | `adminToken` | `MESHGUARD_ADMIN_TOKEN` | — |
54
+ | `timeout` | — | `30000` (ms) |
55
+ | `traceId` | — | Auto-generated UUID |
56
+
57
+ ```ts
58
+ // Using environment variables (zero-config)
59
+ const client = new MeshGuardClient();
60
+
61
+ // Explicit options override env vars
62
+ const client = new MeshGuardClient({
63
+ gatewayUrl: "https://dashboard.meshguard.app",
64
+ agentToken: process.env.MY_TOKEN,
65
+ });
66
+ ```
67
+
68
+ ## Core Governance
69
+
70
+ ### check() — Non-throwing policy check
71
+
72
+ Returns a `PolicyDecision` — never throws on deny.
73
+
74
+ ```ts
75
+ const decision = await client.check("read:contacts");
76
+
77
+ if (decision.allowed) {
78
+ const contacts = await fetchContacts();
79
+ } else {
80
+ console.log(`Denied: ${decision.reason}`);
81
+ console.log(`Policy: ${decision.policy}, Rule: ${decision.rule}`);
82
+ }
83
+ ```
84
+
85
+ ### enforce() — Throwing policy check
86
+
87
+ Throws `PolicyDeniedError` if the action is denied.
88
+
89
+ ```ts
90
+ import { PolicyDeniedError } from "meshguard";
91
+
92
+ try {
93
+ await client.enforce("write:email");
94
+ await sendEmail(to, subject, body);
95
+ } catch (err) {
96
+ if (err instanceof PolicyDeniedError) {
97
+ console.error(`Blocked by policy: ${err.policy}`);
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### govern() — Governed function execution
103
+
104
+ Combines enforcement with execution — the function only runs if allowed.
105
+
106
+ ```ts
107
+ // Sync or async functions work
108
+ const contacts = await client.govern("read:contacts", async () => {
109
+ return db.contacts.findAll();
110
+ });
111
+
112
+ // With resource context
113
+ const file = await client.govern(
114
+ "read:file",
115
+ () => fs.readFileSync("/etc/config"),
116
+ "/etc/config",
117
+ );
118
+ ```
119
+
120
+ ## Proxy Requests
121
+
122
+ Route HTTP requests through the MeshGuard governance proxy:
123
+
124
+ ```ts
125
+ // GET through proxy
126
+ const response = await client.get("/api/users", "read:users");
127
+
128
+ // POST through proxy
129
+ const response = await client.post("/api/users", "write:users", {
130
+ body: JSON.stringify({ name: "Alice" }),
131
+ headers: { "Content-Type": "application/json" },
132
+ });
133
+
134
+ // Generic method
135
+ const response = await client.request("PATCH", "/api/users/1", "write:users", {
136
+ body: JSON.stringify({ name: "Bob" }),
137
+ });
138
+ ```
139
+
140
+ ## Admin Operations
141
+
142
+ These require an `adminToken`:
143
+
144
+ ### Agent Management
145
+
146
+ ```ts
147
+ const admin = new MeshGuardClient({
148
+ adminToken: "your-admin-token",
149
+ });
150
+
151
+ // List all agents
152
+ const agents = await admin.listAgents();
153
+ for (const agent of agents) {
154
+ console.log(`${agent.name} (${agent.trustTier})`);
155
+ }
156
+
157
+ // Create a new agent
158
+ const result = await admin.createAgent({
159
+ name: "data-bot",
160
+ trustTier: "verified",
161
+ tags: ["production", "data-team"],
162
+ });
163
+
164
+ // Revoke an agent
165
+ await admin.revokeAgent("agent-id-123");
166
+ ```
167
+
168
+ ### Audit Log
169
+
170
+ ```ts
171
+ // Get recent entries
172
+ const entries = await admin.getAuditLog({ limit: 100 });
173
+
174
+ // Filter by decision
175
+ const denials = await admin.getAuditLog({
176
+ limit: 50,
177
+ decision: "deny",
178
+ });
179
+ ```
180
+
181
+ ### Policies
182
+
183
+ ```ts
184
+ const policies = await admin.listPolicies();
185
+ ```
186
+
187
+ ## Health Check
188
+
189
+ ```ts
190
+ // Detailed health info
191
+ const status = await client.health();
192
+
193
+ // Quick boolean check
194
+ if (await client.isHealthy()) {
195
+ console.log("Gateway is up");
196
+ }
197
+ ```
198
+
199
+ ## LangChain.js Integration
200
+
201
+ Govern LangChain tools with MeshGuard policies:
202
+
203
+ ```ts
204
+ import { MeshGuardClient } from "meshguard";
205
+ import {
206
+ GovernedTool,
207
+ GovernedToolkit,
208
+ governedTool,
209
+ } from "meshguard/langchain";
210
+ ```
211
+
212
+ ### Wrap a single tool
213
+
214
+ ```ts
215
+ import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo";
216
+
217
+ const client = new MeshGuardClient();
218
+ const search = new DuckDuckGoSearch();
219
+
220
+ // Functional wrapper
221
+ const governed = governedTool("read:web_search", client, search);
222
+ const result = await governed.invoke("TypeScript SDK patterns");
223
+
224
+ // Class wrapper
225
+ const governedSearch = new GovernedTool({
226
+ tool: search,
227
+ action: "read:web_search",
228
+ client,
229
+ onDeny: (err) => `Search blocked: ${err.reason}`,
230
+ });
231
+ ```
232
+
233
+ ### Govern a toolkit
234
+
235
+ ```ts
236
+ const toolkit = new GovernedToolkit({
237
+ tools: [searchTool, calcTool, emailTool],
238
+ client,
239
+ actionMap: {
240
+ search: "read:web_search",
241
+ calculator: "execute:math",
242
+ email: "write:email",
243
+ },
244
+ defaultAction: "execute:tool",
245
+ });
246
+
247
+ const governedTools = toolkit.getTools();
248
+ // Pass governedTools to your LangChain agent
249
+ ```
250
+
251
+ ## Error Handling
252
+
253
+ All errors extend `MeshGuardError`:
254
+
255
+ ```ts
256
+ import {
257
+ MeshGuardError,
258
+ PolicyDeniedError,
259
+ AuthenticationError,
260
+ RateLimitError,
261
+ } from "meshguard";
262
+
263
+ try {
264
+ await client.enforce("dangerous:action");
265
+ } catch (err) {
266
+ if (err instanceof PolicyDeniedError) {
267
+ // Action was denied by policy
268
+ console.log(err.action); // "dangerous:action"
269
+ console.log(err.policy); // "safety-policy"
270
+ console.log(err.rule); // "block-dangerous"
271
+ console.log(err.reason); // "Action not permitted"
272
+ } else if (err instanceof AuthenticationError) {
273
+ // Token is invalid or expired
274
+ } else if (err instanceof RateLimitError) {
275
+ // Too many requests
276
+ } else if (err instanceof MeshGuardError) {
277
+ // Other gateway error
278
+ }
279
+ }
280
+ ```
281
+
282
+ ## Python SDK
283
+
284
+ Looking for the Python SDK? See [meshguard-python](https://github.com/dbhurley/meshguard-python).
285
+
286
+ ## Requirements
287
+
288
+ - **Node.js 18+** (uses native `fetch` and `crypto.randomUUID`)
289
+ - **TypeScript 5.0+** (optional — works with plain JavaScript too)
290
+
291
+ ## License
292
+
293
+ MIT — see [LICENSE](./LICENSE).
@@ -0,0 +1,92 @@
1
+ /**
2
+ * MeshGuard Client
3
+ *
4
+ * Core client for interacting with the MeshGuard gateway.
5
+ */
6
+ import type { MeshGuardOptions, PolicyDecision, Agent, CreateAgentOptions, AuditEntry, AuditLogOptions, HealthStatus, Policy } from "./types.js";
7
+ /**
8
+ * Client for the MeshGuard governance gateway.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const client = new MeshGuardClient({
13
+ * gatewayUrl: "https://dashboard.meshguard.app",
14
+ * agentToken: "your-agent-token",
15
+ * });
16
+ *
17
+ * // Check if an action is allowed
18
+ * const decision = await client.check("read:contacts");
19
+ * if (decision.allowed) {
20
+ * // proceed
21
+ * }
22
+ *
23
+ * // Or enforce (throws on deny)
24
+ * await client.enforce("read:contacts");
25
+ *
26
+ * // Or govern a function
27
+ * const result = await client.govern("read:contacts", async () => {
28
+ * return fetchContacts();
29
+ * });
30
+ * ```
31
+ */
32
+ export declare class MeshGuardClient {
33
+ readonly gatewayUrl: string;
34
+ readonly agentToken?: string;
35
+ readonly adminToken?: string;
36
+ readonly timeout: number;
37
+ readonly traceId: string;
38
+ constructor(options?: MeshGuardOptions);
39
+ private headers;
40
+ private adminHeaders;
41
+ private handleResponse;
42
+ private safeJson;
43
+ private fetch;
44
+ /**
45
+ * Check if an action is allowed by policy.
46
+ *
47
+ * Returns a {@link PolicyDecision} — never throws on deny.
48
+ */
49
+ check(action: string, resource?: string): Promise<PolicyDecision>;
50
+ /**
51
+ * Enforce policy — throws {@link PolicyDeniedError} if the action is denied.
52
+ */
53
+ enforce(action: string, resource?: string): Promise<PolicyDecision>;
54
+ /**
55
+ * Execute a function only if the action is allowed by policy.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * const contacts = await client.govern("read:contacts", async () => {
60
+ * return db.contacts.findAll();
61
+ * });
62
+ * ```
63
+ */
64
+ govern<T>(action: string, fn: () => T | Promise<T>, resource?: string): Promise<T>;
65
+ /**
66
+ * Make a governed request through the MeshGuard proxy.
67
+ */
68
+ request(method: string, path: string, action: string, init?: RequestInit): Promise<Response>;
69
+ /** GET through the governance proxy. */
70
+ get(path: string, action: string, init?: RequestInit): Promise<Response>;
71
+ /** POST through the governance proxy. */
72
+ post(path: string, action: string, init?: RequestInit): Promise<Response>;
73
+ /** PUT through the governance proxy. */
74
+ put(path: string, action: string, init?: RequestInit): Promise<Response>;
75
+ /** DELETE through the governance proxy. */
76
+ delete(path: string, action: string, init?: RequestInit): Promise<Response>;
77
+ /** Check gateway health. */
78
+ health(): Promise<HealthStatus>;
79
+ /** Quick boolean health check. */
80
+ isHealthy(): Promise<boolean>;
81
+ /** List all agents (requires admin token). */
82
+ listAgents(): Promise<Agent[]>;
83
+ /** Create a new agent (requires admin token). */
84
+ createAgent(options: CreateAgentOptions): Promise<Record<string, unknown>>;
85
+ /** Revoke an agent (requires admin token). */
86
+ revokeAgent(agentId: string): Promise<void>;
87
+ /** List all policies (requires admin token). */
88
+ listPolicies(): Promise<Policy[]>;
89
+ /** Get audit log entries (requires admin token). */
90
+ getAuditLog(options?: AuditLogOptions): Promise<AuditEntry[]>;
91
+ }
92
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,gBAAgB,EAChB,cAAc,EACd,KAAK,EACL,kBAAkB,EAClB,UAAU,EACV,eAAe,EACf,YAAY,EACZ,MAAM,EACP,MAAM,YAAY,CAAC;AASpB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,eAAe;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAEb,OAAO,GAAE,gBAAqB;IAmB1C,OAAO,CAAC,OAAO;IAUf,OAAO,CAAC,YAAY;YAUN,cAAc;YAuBd,QAAQ;YAUR,KAAK;IAiBnB;;;;OAIG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAgDvE;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAazE;;;;;;;;;OASG;IACG,MAAM,CAAC,CAAC,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