Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(guardrails): log pre_call hook rewrite application in mcp-host

Andy Paviabe10ed3dbb58
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index fbfcd57b1..448e7c438 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.260",
+ "version": "0.1.261",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.260",
+ "version": "0.1.261",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index e7929ffab..da2032a86 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.260",
+ "version": "0.1.261",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/core/adapters/hookedLlmPort.ts b/mcp-host/src/core/adapters/hookedLlmPort.ts
index 11eef70f9..fbf889a68 100644
--- a/mcp-host/src/core/adapters/hookedLlmPort.ts
+++ b/mcp-host/src/core/adapters/hookedLlmPort.ts
@@ -93,6 +93,20 @@ function refusalResponse(reason?: string): ToolCompletionResponse {
}
}
+/**
+ * Approximate request size for the pre_call diagnostic log: total chars across
+ * message string content only. Sizes, never content (§8.4). Mirrors the hook's
+ * own `messagesChars`, so mcp-host's `before→after` lines up with the hook's
+ * reported `beforeChars/afterChars` when a rewrite lands.
+ */
+function approxRequestChars(req: ToolCompletionRequest): number {
+ let n = 0
+ for (const m of req.messages ?? []) {
+ if (typeof m.content === 'string') n += m.content.length
+ }
+ return n
+}
+
export class HookedLlmPort implements LlmPort {
constructor(
private readonly inner: LlmPort,
@@ -149,11 +163,28 @@ export class HookedLlmPort implements LlmPort {
// and a reject blocks the call outright (no tokens spent).
const preContribs: Array<Contributor<ToolCompletionRequest, ToolCompletionResponse>> = []
for (const hook of this.hooks.preCall) {
+ const before = approxRequestChars(req)
const c = await hook.preCall(req)
if (c) {
preContribs.push(c)
if (c.rewrite) req = c.rewrite // already system-role-stripped + capability-gated (N4/F4)
}
+ // Diagnostic (content-free, sizes only per §8.4): make the dial AND whether
+ // the rewrite was actually applied observable, so a may_rewrite hook's
+ // compaction can be confirmed end-to-end against the hook's own logs — the
+ // `before→after` here should match the hook's reported before/after chars.
+ if (c?.rewrite) {
+ const after = approxRequestChars(req)
+ console.log(
+ `[HookedLlmPort] pre_call ${hook.id}: applied rewrite ${before}→${after} chars (Δ${before - after})`
+ )
+ } else if (c?.decision === 'deny') {
+ console.log(
+ `[HookedLlmPort] pre_call ${hook.id}: deny (reason=${logSafeReason(c.reasonCode)})`
+ )
+ } else {
+ console.log(`[HookedLlmPort] pre_call ${hook.id}: no rewrite (continue), ${before} chars`)
+ }
}
if (aggregateDecision(preContribs) === 'deny') return this.denyRefusal(preContribs, 'pre_call')
diff --git a/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts b/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts
index 31f07c882..d73071df5 100644
--- a/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts
+++ b/mcp-host/src/core/guardrails/hooks/remoteLlmHook.ts
@@ -87,6 +87,11 @@ export class RemoteLlmHook {
private readonly hardDeadlineMs: number = 15000
) {}
+ /** Stable hook id (the descriptor's id) — for diagnostics/logging only. */
+ get id(): string {
+ return this.descriptor.id
+ }
+
private has(cap: Capability): boolean {
return this.descriptor.capabilities.includes(cap)
}