hmcp-gateway 0.1.0 → 0.2.1

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.
@@ -0,0 +1,265 @@
1
+ import { ApprovalStore, AuditLog, type Effect, type Policy } from "hmcp-core";
2
+ /**
3
+ * The Claude Code guard: the same policy the gateway enforces, applied to
4
+ * every tool Claude Code itself runs.
5
+ *
6
+ * The gateway only sees calls to MCP servers it fronts. Claude Code also has
7
+ * a terminal, file edits, web fetches, plugins and claude.ai connectors, none
8
+ * of which go through it. Claude Code runs a `PreToolUse` hook before every
9
+ * one of those, and this is what that hook asks: one call, one decision, from
10
+ * the same `decide()`, the same approvals database and the same audit log.
11
+ *
12
+ * What it deliberately does *not* do is say "allow". A hook that allows skips
13
+ * Claude Code's own permission prompts, and a guard that switched those off
14
+ * would be loosening what the person already had. So a call policy permits
15
+ * gets no opinion at all, and Claude Code's own rules still apply on top.
16
+ */
17
+ /** The audit `component`, and half of the approvals scope, for everything the guard writes. */
18
+ export declare const GUARD_COMPONENT = "guard:claude-code";
19
+ /**
20
+ * What a Claude Code tool is called in policy: `claude-code__Bash`,
21
+ * `claude-code__mcp__github__create_issue`. A rule matches it like any other
22
+ * tool (`match: "claude-code__Bash"`, `match: "claude-code__*"`).
23
+ *
24
+ * The hyphen is on purpose. An upstream name is lowercase letters, digits and
25
+ * underscores only, so no tool the gateway serves can ever be called this.
26
+ */
27
+ export declare const GUARD_TOOL_PREFIX = "claude-code__";
28
+ /** What the toolbox is called in Claude Code's config, unless `adopt --entry-name` said otherwise. */
29
+ export declare const GUARD_DEFAULT_ENTRY = "hmcp";
30
+ /**
31
+ * How long a held call waits for a person before Claude Code is told to try
32
+ * again. Longer than the gateway's, because a hook may take up to ten minutes
33
+ * where an MCP client gives up after one.
34
+ */
35
+ export declare const GUARD_DEFAULT_WAIT_SECONDS = 120;
36
+ /** Rule ids the guard writes that are not policy rules. */
37
+ export declare const GUARD_RELAY_RULE = "guard.own-toolbox";
38
+ export declare const GUARD_OUTCOME_RULE = "guard.outcome";
39
+ export declare const GUARD_ERROR_RULE = "guard.error";
40
+ /** A tool named like the toolbox's that is not one of its tools: checked, not exempt. */
41
+ export declare const GUARD_LOOKALIKE_RULE = "guard.lookalike-toolbox";
42
+ /** A Claude Code call that would change what the guard is, held for a person whatever policy says. */
43
+ export declare const GUARD_PROTECT_RULE = "guard.protect-self";
44
+ /** The fields of a Claude Code hook event the guard reads. Typed structurally: Claude Code is not a dependency. */
45
+ export interface GuardHookEvent {
46
+ readonly hook_event_name?: string;
47
+ readonly session_id?: string;
48
+ readonly tool_name?: unknown;
49
+ readonly tool_input?: unknown;
50
+ readonly tool_response?: unknown;
51
+ /** Set on `PostToolUseFailure`: what went wrong. */
52
+ readonly error?: unknown;
53
+ readonly tool_use_id?: string;
54
+ readonly agent_id?: string;
55
+ readonly agent_type?: string;
56
+ readonly cwd?: string;
57
+ }
58
+ /** Nothing to say (Claude Code decides as it would have), or a refusal with the words the model is shown. */
59
+ export type GuardVerdict = {
60
+ readonly kind: "pass";
61
+ } | {
62
+ readonly kind: "deny";
63
+ readonly reason: string;
64
+ };
65
+ export interface GuardContext {
66
+ readonly policy: Policy;
67
+ readonly audit: AuditLog;
68
+ /** The approvals database. The guard scopes it itself, to `GUARD_COMPONENT` and the tenant. */
69
+ readonly store: ApprovalStore;
70
+ /** What the toolbox is called in Claude Code, so its own tools are not checked twice. */
71
+ readonly entryNames?: readonly string[];
72
+ /** How long a held call waits for a person, in seconds. */
73
+ readonly waitSeconds?: number;
74
+ readonly tenantValue?: string | undefined;
75
+ readonly now?: () => number;
76
+ /**
77
+ * The toolbox whose own tools are exempt, when the guard was given a
78
+ * gateway.yaml. Absent for a bare policy.yaml: then nothing is exempt,
79
+ * because there is no toolbox to check an entry against.
80
+ */
81
+ readonly toolbox?: GuardToolbox | undefined;
82
+ /** Where Claude Code's config is read from, to see what `mcp__hmcp__…` really is. */
83
+ readonly home?: string | undefined;
84
+ /**
85
+ * The MCP servers an Agent SDK program passed to `query()`. When set, these
86
+ * are what `mcp__<entry>__…` names refer to, instead of Claude Code's files.
87
+ */
88
+ readonly mcpServers?: Readonly<Record<string, unknown>> | undefined;
89
+ /** Said in every refusal for a guard that cannot work, so nobody is locked out. */
90
+ readonly wayOut?: string | undefined;
91
+ /** The hook process's own folder. Default: `process.cwd()`; a test names one. */
92
+ readonly hookCwd?: string | undefined;
93
+ /** The project Claude Code was started for. Default: `CLAUDE_PROJECT_DIR`; a test names one. */
94
+ readonly claudeProjectDir?: string | undefined;
95
+ /** This build's `cli.js`, the one launch of the toolbox by node that is taken as ours. Default: beside this file. */
96
+ readonly ownCliPath?: string | undefined;
97
+ /**
98
+ * The guard's own files -- its gateway.yaml and policy, its approvals
99
+ * database and its audit log -- which a Claude Code tool may not change
100
+ * without a person saying so.
101
+ */
102
+ readonly ownFiles?: readonly string[] | undefined;
103
+ }
104
+ /** What the guard knows about the toolbox it was pointed at. */
105
+ export interface GuardToolbox {
106
+ /** Its gateway.yaml, absolute. */
107
+ readonly configPath: string;
108
+ /** Its upstream names, which are the first half of each tool name when it namespaces. */
109
+ readonly upstreams: readonly string[];
110
+ readonly namespace: boolean;
111
+ }
112
+ /** A Claude Code tool, as policy sees it. */
113
+ export interface GuardTool {
114
+ readonly tool: string;
115
+ readonly effect: Effect | undefined;
116
+ }
117
+ /** The name and effect policy judges a Claude Code tool by. */
118
+ export declare function guardTool(toolName: string): GuardTool;
119
+ /**
120
+ * What the model is told when the guard itself could not decide.
121
+ *
122
+ * A guard that cannot work refuses every call, including the terminal and the
123
+ * file edits someone would use to fix it from inside Claude Code. So the
124
+ * refusal always carries the way out, for the person to do by hand.
125
+ */
126
+ export declare function guardFailureReason(err: unknown, wayOut?: string): string;
127
+ /**
128
+ * How to switch the guard off from outside Claude Code, in words for a person.
129
+ *
130
+ * The command is the one that runs this guard (a published version, or this
131
+ * build on disk), so it works whichever the hook was installed with.
132
+ */
133
+ export declare function guardWayOut(where: {
134
+ scope?: string | undefined;
135
+ projectDir?: string | undefined;
136
+ home?: string | undefined;
137
+ }): string;
138
+ /**
139
+ * Decides one `PreToolUse` event.
140
+ *
141
+ * Mirrors `enforceCall` in core, step for step, with one difference: the call
142
+ * is not run here. A permitted call is recorded as `relayed` and handed back
143
+ * to Claude Code, and the `PostToolUse` hook records what became of it.
144
+ *
145
+ * Fails closed: anything that throws is a refusal, never a pass.
146
+ */
147
+ export declare function guardDecision(event: GuardHookEvent, ctx: GuardContext,
148
+ /** Aborted when whoever runs the hook gives up on it (the Agent SDK's timeout): the wait stops, and it is a refusal. */
149
+ signal?: AbortSignal): Promise<GuardVerdict>;
150
+ /**
151
+ * Records what became of a call Claude Code ran (`PostToolUse`).
152
+ *
153
+ * The result itself is not kept: it is whatever a file or a command printed.
154
+ * It is scanned, the way the gateway scans an upstream's results, and only
155
+ * what was found is written down. Never throws: the call has already happened,
156
+ * and there is nothing left to refuse.
157
+ */
158
+ export declare function guardOutcome(event: GuardHookEvent, ctx: Pick<GuardContext, "policy" | "audit" | "tenantValue">): void;
159
+ /** A hook's answer, in the shape Claude Code and the Agent SDK both read. */
160
+ export interface GuardHookOutput {
161
+ readonly hookSpecificOutput?: {
162
+ readonly hookEventName: "PreToolUse";
163
+ readonly permissionDecision: "deny";
164
+ readonly permissionDecisionReason: string;
165
+ };
166
+ }
167
+ /** The hook's answer, in Claude Code's shape. `{}` is no opinion. */
168
+ export declare function hookOutput(verdict: GuardVerdict): GuardHookOutput;
169
+ export interface OpenedGuard {
170
+ readonly policy: Policy;
171
+ readonly base: string;
172
+ readonly context: (options?: {
173
+ waitSeconds?: number;
174
+ entryNames?: readonly string[];
175
+ session?: string;
176
+ home?: string;
177
+ mcpServers?: Readonly<Record<string, unknown>>;
178
+ wayOut?: string;
179
+ ownCliPath?: string;
180
+ hookCwd?: string;
181
+ claudeProjectDir?: string;
182
+ }) => GuardContext;
183
+ readonly close: () => void;
184
+ }
185
+ /**
186
+ * Opens the policy, the approvals database and the audit log a guard uses.
187
+ *
188
+ * `configPath` is a toolbox's gateway.yaml or a policy.yaml on its own. Every
189
+ * relative path inside it means "beside this file", as it does for the
190
+ * gateway (see `openGateway`), so the guard and the toolbox share one log
191
+ * and one database and the console shows both.
192
+ */
193
+ export declare function openGuard(configPath: string): OpenedGuard;
194
+ /** What `policy.approvals.wait_seconds` says, or the guard's own default when it says nothing. */
195
+ export declare function guardWaitSeconds(policy: Policy): number;
196
+ export interface GuardHookRun {
197
+ /** What Claude Code is to read on stdout: a decision as JSON, or nothing for no opinion. */
198
+ readonly stdout: string;
199
+ readonly stderr: string;
200
+ readonly exitCode: number;
201
+ }
202
+ /**
203
+ * One run of `hmcp-gateway guard`: the hook event in, Claude Code's answer out.
204
+ *
205
+ * Before a call (`pre`), every way this can go wrong -- no config, a config
206
+ * that does not load, input that is not a hook event -- is answered with a
207
+ * refusal, exit 0, so Claude Code reads it as one. After a call (`post`)
208
+ * nothing is left to refuse, so a failure is only reported on stderr.
209
+ */
210
+ export declare function runGuardHook(options: {
211
+ readonly config?: string | undefined;
212
+ readonly event?: string | undefined;
213
+ readonly waitSeconds?: string | number | undefined;
214
+ readonly entryNames?: readonly string[] | undefined;
215
+ /** Which settings file installed this hook (`--scope`), so a refusal can say how to take it out. */
216
+ readonly scope?: string | undefined;
217
+ readonly projectDir?: string | undefined;
218
+ /** Where Claude Code's config lives. The CLI passes the real home folder; a test passes its own. */
219
+ readonly home?: string | undefined;
220
+ readonly input: string;
221
+ }): Promise<GuardHookRun>;
222
+ /** A hook callback in the Claude Agent SDK's shape, typed structurally. */
223
+ export type GuardHookCallback = (input: GuardHookEvent, toolUseId: string | undefined, options: {
224
+ signal?: AbortSignal;
225
+ }) => Promise<GuardHookOutput>;
226
+ /** One SDK matcher. `timeout` is in seconds; `onFailure: "block"` makes a hook that throws stop the call. */
227
+ export interface GuardHookMatcher {
228
+ readonly hooks: GuardHookCallback[];
229
+ readonly timeout: number;
230
+ readonly onFailure: "block";
231
+ }
232
+ export interface GuardHooks {
233
+ readonly PreToolUse: GuardHookMatcher[];
234
+ readonly PostToolUse: GuardHookMatcher[];
235
+ readonly PostToolUseFailure: GuardHookMatcher[];
236
+ }
237
+ /** How much longer than the wait a hook is given, so the guard answers before the SDK gives up on it. */
238
+ export declare const GUARD_TIMEOUT_MARGIN_SECONDS = 30;
239
+ /**
240
+ * The guard as Agent SDK hooks, for an agent a company runs itself:
241
+ *
242
+ * query({ prompt, options: { hooks: hmcpGuardHooks({ config: "gateway.yaml" }) } })
243
+ *
244
+ * The same `guardDecision` the Claude Code hook runs, in-process. The SDK runs
245
+ * hooks even under `bypassPermissions`, which is what makes this the one place
246
+ * every tool call passes through.
247
+ *
248
+ * Each matcher carries its own `timeout`, always longer than the wait, so the
249
+ * guard is the one that answers a held call -- and `onFailure: "block"`, so a
250
+ * callback that throws stops the call rather than letting it through. If the
251
+ * SDK gives up anyway and aborts the signal, the wait stops at once and the
252
+ * answer is a refusal; an approval given after that is left for the retry.
253
+ *
254
+ * `mcpServers` is what the program passes to `query()`: the guard reads it to
255
+ * tell the toolbox's own tools from a server that only shares its name. Without
256
+ * it, Claude Code's config files are read instead.
257
+ */
258
+ export declare function hmcpGuardHooks(options: {
259
+ readonly config: string;
260
+ readonly waitSeconds?: number;
261
+ readonly entryNames?: readonly string[];
262
+ readonly mcpServers?: Readonly<Record<string, unknown>>;
263
+ /** Where Claude Code's config is read from, when `mcpServers` is not given. Default: the home folder. */
264
+ readonly home?: string;
265
+ }): GuardHooks;