@agent-custody/receipts 0.6.14 → 0.6.15

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 CHANGED
@@ -5,7 +5,7 @@ Chain of custody for AI agents: a signed receipt for every tool call, checkable
5
5
  Two producers, one receipt format, one verifier.
6
6
 
7
7
  - **The gateway** is an MCP proxy between an agent and the systems it can affect. For every tool call, allowed or denied, it checks a delegation grant signed by the human principal, gathers the facts the policy needs by calling upstream itself, evaluates a Cedar policy that fails closed, forwards the call only on allow, and emits a signed receipt appended to a Merkle transparency log.
8
- - **The SDK** is an interceptor inside the agent's own process, hooked into the framework's tool-call callbacks: Claude Code, the Claude Agent SDK, the OpenAI Agents SDK, the Vercel AI SDK, LangChain, or any function you wrap. It reaches everything the gateway cannot see and issues the same receipts, labelled as self-reported.
8
+ - **The SDK** is an interceptor inside the agent's own process, hooked into the framework's tool-call callbacks: Claude Code, the Claude Agent SDK, the OpenAI Agents SDK, the Vercel AI SDK, OpenClaw, Hermes, LangChain, or any function you wrap. It reaches everything the gateway cannot see and issues the same receipts, labelled as self-reported.
9
9
 
10
10
  Anyone holding the public keys can verify a receipt offline. The agent is not trusted. The layer around it is, and the receipt says exactly how far that trust extends, starting with who issued it.
11
11
 
@@ -107,6 +107,8 @@ Every receipt names its issuer, and the verifier prints what that issuer kind is
107
107
  | OpenAI Agents SDK (JS) | SDK | `wrapTools` | `observeRunner` | a real `Runner` with a scripted model |
108
108
  | Vercel AI SDK | SDK | `wrapTools` | | a real `generateText` loop over the SDK's mock model |
109
109
  | LangChain / LangGraph (JS) | SDK | `tool(issuer.wrap(fn))` | `ReceiptCallbackHandler` | real `StructuredTool` invocations |
110
+ | OpenClaw | SDK | `registerOpenClaw` / `openclawHooks`: `before_tool_call` deny | `after_tool_call` | OpenClaw's documented hook contract, in the plugin's process |
111
+ | Hermes Agent | sidecar + [Python package](../python/README.md) | `agent_custody.hermes`: `pre_tool_call` block | `post_tool_call` | the documented hook contract, against a live sidecar |
110
112
  | anything else | SDK | `issuer.wrap(name, fn)` | `issuer.record` | plain functions |
111
113
  | Python: LangChain, OpenAI Agents SDK, CrewAI, Claude Agent SDK | sidecar + [Python package](../python/README.md) | `wrap_tools` (OpenAI Agents, CrewAI), `claude_hook` PreToolUse deny, `client.wrap` | `ReceiptCallbackHandler` | the real Python packages, receipts checked by this verifier |
112
114
  | Go, Java, Rust, any language with HTTP | sidecar | decide then record | record | [examples/languages](examples/languages), each run against a live sidecar |
@@ -0,0 +1,50 @@
1
+ import { type SdkIssuer } from "./index.ts";
2
+ /** What OpenClaw passes as the hook context (PluginHookToolContext), the fields this adapter reads. */
3
+ export interface OpenClawToolContext {
4
+ agentId?: string;
5
+ sessionKey?: string;
6
+ sessionId?: string;
7
+ runId?: string;
8
+ toolName?: string;
9
+ toolCallId?: string;
10
+ [k: string]: unknown;
11
+ }
12
+ /** PluginHookBeforeToolCallEvent */
13
+ export interface OpenClawBeforeToolCallEvent {
14
+ toolName: string;
15
+ params: Record<string, unknown>;
16
+ runId?: string;
17
+ toolCallId?: string;
18
+ [k: string]: unknown;
19
+ }
20
+ /** PluginHookAfterToolCallEvent */
21
+ export interface OpenClawAfterToolCallEvent {
22
+ toolName: string;
23
+ params: Record<string, unknown>;
24
+ runId?: string;
25
+ toolCallId?: string;
26
+ result?: unknown;
27
+ error?: string;
28
+ durationMs?: number;
29
+ [k: string]: unknown;
30
+ }
31
+ /** The part of PluginHookBeforeToolCallResult this adapter produces. */
32
+ export interface OpenClawBeforeToolCallResult {
33
+ block: true;
34
+ blockReason: string;
35
+ }
36
+ /**
37
+ * The two handlers. `before_tool_call` evaluates the policy: on deny it issues a denial receipt and blocks with the
38
+ * reason and the receipt id, which OpenClaw shows the model as the tool's result; on allow, or with no policy, it
39
+ * returns nothing, so OpenClaw's own approvals and other plugins still apply. It never auto-approves. If the receipt
40
+ * cannot be issued (the log refused it) the call is blocked too: nothing runs without evidence. `after_tool_call`
41
+ * issues the receipt for the completed or failed call; a refused leaf is thrown, which OpenClaw logs.
42
+ */
43
+ export declare function openclawHooks(issuer: SdkIssuer): {
44
+ before_tool_call: (event: OpenClawBeforeToolCallEvent, ctx?: OpenClawToolContext) => Promise<OpenClawBeforeToolCallResult | undefined>;
45
+ after_tool_call: (event: OpenClawAfterToolCallEvent, ctx?: OpenClawToolContext) => Promise<void>;
46
+ };
47
+ /** Registers both hooks on a plugin api (`api.on(name, handler)`), for a plugin's `register(api)`. */
48
+ export declare function registerOpenClaw(api: {
49
+ on: (name: string, handler: (event: any, ctx?: any) => unknown) => unknown;
50
+ }, issuer: SdkIssuer): void;
@@ -0,0 +1,52 @@
1
+ // Adapter for OpenClaw plugin hooks. OpenClaw runs plugins in the agent's own process and offers two tool hooks:
2
+ // `before_tool_call`, which may block a call with a reason the model sees, and `after_tool_call`, which carries the
3
+ // result or the error. Both are awaited. This file mirrors OpenClaw's typed hook contract (src/plugins/hook-types.ts)
4
+ // loosely, on purpose, so it does not depend on the openclaw package; a plugin entry wires it with `api.on`.
5
+ //
6
+ // import { definePluginEntry } from "openclaw/plugin-sdk/plugin-sdk";
7
+ // import { createSdkIssuer, loadSdkConfig } from "@agent-custody/receipts";
8
+ // import { registerOpenClaw } from "@agent-custody/receipts/sdk/openclaw";
9
+ // export default definePluginEntry({ id: "agent-custody", name: "agent-custody", description: "A receipt for every tool call",
10
+ // register(api) { registerOpenClaw(api, createSdkIssuer(loadSdkConfig(process.env.AGENT_CUSTODY_CONFIG!))); } });
11
+ import { receiptIdOf } from "./index.js";
12
+ function toEvent(event, ctx) {
13
+ const raw = event.params;
14
+ const args = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : { input: raw ?? null };
15
+ return { tool: event.toolName, args, session: { id: ctx?.sessionId ?? ctx?.sessionKey ?? null, toolUseId: event.toolCallId ?? ctx?.toolCallId ?? null } };
16
+ }
17
+ /**
18
+ * The two handlers. `before_tool_call` evaluates the policy: on deny it issues a denial receipt and blocks with the
19
+ * reason and the receipt id, which OpenClaw shows the model as the tool's result; on allow, or with no policy, it
20
+ * returns nothing, so OpenClaw's own approvals and other plugins still apply. It never auto-approves. If the receipt
21
+ * cannot be issued (the log refused it) the call is blocked too: nothing runs without evidence. `after_tool_call`
22
+ * issues the receipt for the completed or failed call; a refused leaf is thrown, which OpenClaw logs.
23
+ */
24
+ export function openclawHooks(issuer) {
25
+ return {
26
+ before_tool_call: async (event, ctx) => {
27
+ const ev = toEvent(event, ctx);
28
+ const policy = issuer.decide(ev);
29
+ if (!policy || policy.decision !== "deny")
30
+ return undefined;
31
+ const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
32
+ try {
33
+ const bundle = await issuer.record(ev, { status: "denied", reason }, policy);
34
+ return { block: true, blockReason: `agent-custody: ${reason} (receipt ${receiptIdOf(bundle)})` };
35
+ }
36
+ catch (e) {
37
+ return { block: true, blockReason: `agent-custody: ${reason}; the denial receipt could not be issued (${e instanceof Error ? e.message : String(e)})` };
38
+ }
39
+ },
40
+ after_tool_call: async (event, ctx) => {
41
+ const ev = toEvent(event, ctx);
42
+ const outcome = event.error !== undefined ? { status: "failed", result: event.error } : { status: "executed", result: event.result ?? null };
43
+ await issuer.record(ev, outcome, issuer.decide(ev));
44
+ },
45
+ };
46
+ }
47
+ /** Registers both hooks on a plugin api (`api.on(name, handler)`), for a plugin's `register(api)`. */
48
+ export function registerOpenClaw(api, issuer) {
49
+ const hooks = openclawHooks(issuer);
50
+ api.on("before_tool_call", hooks.before_tool_call);
51
+ api.on("after_tool_call", hooks.after_tool_call);
52
+ }
package/docs/sdk.md CHANGED
@@ -130,6 +130,35 @@ const refund = tool(issuer.wrap("stripe.refund", fn), { name: "stripe.refund", s
130
130
 
131
131
  LangChain callbacks cannot block a tool, so the handler evaluates no policy; it records what happened, including the `tool_call_id` when one is present, and unwraps `ToolMessage` outputs. For enforcement wrap the function at construction. Do not do both on one tool or it will be recorded twice.
132
132
 
133
+ ## OpenClaw
134
+
135
+ [src/sdk/openclaw.ts](../src/sdk/openclaw.ts). OpenClaw runs plugins in the agent's process and awaits two tool hooks: `before_tool_call`, which can block a call with a reason the model sees, and `after_tool_call`, which carries the result or the error. A plugin is three files: `package.json` with `"openclaw": { "extensions": ["./index.ts"] }`, `openclaw.plugin.json`, and the entry:
136
+
137
+ ```ts
138
+ import { definePluginEntry } from "openclaw/plugin-sdk/plugin-sdk";
139
+ import { createSdkIssuer, loadSdkConfig } from "@agent-custody/receipts";
140
+ import { registerOpenClaw } from "@agent-custody/receipts/sdk/openclaw";
141
+
142
+ export default definePluginEntry({
143
+ id: "agent-custody",
144
+ name: "agent-custody",
145
+ description: "A signed receipt for every tool call; a policy can deny one before it runs",
146
+ register(api) {
147
+ registerOpenClaw(api, createSdkIssuer(loadSdkConfig(process.env.AGENT_CUSTODY_CONFIG ?? "./sdk.json")));
148
+ },
149
+ });
150
+ ```
151
+
152
+ ```json
153
+ { "id": "agent-custody", "name": "agent-custody", "activation": { "onStartup": true }, "configSchema": { "type": "object", "additionalProperties": false } }
154
+ ```
155
+
156
+ Then `openclaw plugins install --link ./agent-custody-plugin && openclaw plugins enable agent-custody`. `before_tool_call` evaluates the policy: on deny it issues a denial receipt and returns `{ block: true, blockReason: "agent-custody: … (receipt <id>)" }`; on allow, or with no policy, it returns nothing, so OpenClaw's own approvals and other plugins still apply. It never auto-approves, and if the denial receipt cannot be issued the call is blocked all the same. `after_tool_call` issues the receipt for the executed or failed call, with OpenClaw's `sessionId` and `toolCallId` as the receipt's session. `openclawHooks(issuer)` returns the two handlers for a plugin that registers them itself. Typed loosely on purpose, mirroring OpenClaw's `hook-types.ts`, so the package does not depend on `openclaw`; exercised against that contract in [example 21](../examples/21-openclaw.ts) and the test.
157
+
158
+ ## Hermes Agent
159
+
160
+ [packages/python/agent_custody/hermes.py](../../python/agent_custody/hermes.py), through the sidecar like every Python adapter. Hermes runs Python plugins in the agent's process and fires `pre_tool_call(tool_name, args, task_id, **kwargs)`, which may return `{"action": "block", "message": …}`, and `post_tool_call(tool_name, args, result, task_id, duration_ms, **kwargs)`. The plugin is the directory [packages/python/hermes-plugin](../../python/hermes-plugin/): a `plugin.yaml` declaring both hooks and a `sidecar_url` setting, and an `__init__.py` that is one line, `from agent_custody.hermes import register`. Copy it to `~/.hermes/plugins/agent-custody/`, `pip install agent-custody`, start the sidecar, `hermes plugins enable agent-custody`. A denied call is blocked before it runs with the receipt id in the message; an allowed one gets no action, so Hermes's own guardrails and approvals still apply; every completed call is recorded with the Hermes `task_id` as the receipt's session. `hermes_hooks(client)` returns the two callables; `register_hermes(ctx, client)` registers them on a plugin context.
161
+
133
162
  ## Any other framework: wrap the function
134
163
 
135
164
  Every agent framework ends up calling a function. Wrap it.
package/docs/tutorials.md CHANGED
@@ -30,6 +30,7 @@ Suggested reading order is the numbering. Output lands in `examples-out/`, which
30
30
  | 18 | OpenTelemetry export | [18-opentelemetry.ts](../examples/18-opentelemetry.ts) | a stand-in OTLP collector, `otel` in the config, one span per receipt with the receipt id as trace id, the collector going away and the next receipt still issued | `src/otel.ts`, `src/issue.ts` |
31
31
  | 19 | Splunk export | [19-splunk.ts](../examples/19-splunk.ts) | a stand-in HTTP Event Collector, `splunk` in the config with the token from the environment, one event per receipt with the receipt id and log position as fields, the collector going away and the next receipt still issued | `src/splunk.ts`, `src/otel.ts` |
32
32
  | 20 | one gateway for many agents, over HTTP | [20-http-gateway.ts](../examples/20-http-gateway.ts) | a gateway host served over Streamable HTTP, two agents connecting with their own grants and seeing their own tools, receipts naming the right agent, a refusal by grant, and a stranger's grant getting no session | `src/gateway-http.ts`, `src/gateway.ts` |
33
+ | 21 | OpenClaw plugin hooks | [21-openclaw.ts](../examples/21-openclaw.ts) | the two hooks driven with OpenClaw's event shapes: an allowed call gets no result, a denied one is blocked with the receipt id, executed and failed calls are recorded, every receipt verifies | `src/sdk/openclaw.ts` |
33
34
 
34
35
  ## How policies are defined, in one paragraph
35
36
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-custody/receipts",
3
- "version": "0.6.14",
3
+ "version": "0.6.15",
4
4
  "mcpName": "io.github.svayatta/agent-custody",
5
5
  "description": "Chain of custody for AI agents: signed, independently verifiable receipts for tool calls. MCP gateway + Cedar policy + Merkle transparency log",
6
6
  "license": "Apache-2.0",
@@ -41,6 +41,10 @@
41
41
  "types": "./dist/sdk/langchain.d.ts",
42
42
  "default": "./dist/sdk/langchain.js"
43
43
  },
44
+ "./sdk/openclaw": {
45
+ "types": "./dist/sdk/openclaw.d.ts",
46
+ "default": "./dist/sdk/openclaw.js"
47
+ },
44
48
  "./cli": {
45
49
  "types": "./dist/cli.d.ts",
46
50
  "default": "./dist/cli.js"