Skip to content

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

TypeScript

feat(mcp-host): render guardrail deny as graceful refusal

Andy Pavia62e3391c5a8e
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index e569f9498..557b9c78e 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.248",
+ "version": "0.1.249",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.248",
+ "version": "0.1.249",
"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 c5c1672fb..82817857a 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.248",
+ "version": "0.1.249",
"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/__tests__/hookedLlmPort.test.ts b/mcp-host/src/core/adapters/__tests__/hookedLlmPort.test.ts
index 08eb176d8..410a15d34 100644
--- a/mcp-host/src/core/adapters/__tests__/hookedLlmPort.test.ts
+++ b/mcp-host/src/core/adapters/__tests__/hookedLlmPort.test.ts
@@ -58,11 +58,11 @@ describe('installed hooks (end-to-end via HookedLlmPort)', () => {
...over,
})
- it('a moderate deny returns a filtered response and aborts the in-flight call (§7.1)', async () => {
+ it('a moderate deny returns a graceful refusal and aborts the in-flight call (§7.1)', async () => {
const inner = fakePort()
const moderateDeny: FetchLike = async url => ({
status: url.endsWith('/v1/moderate') ? 422 : 200,
- text: async () => '{"code":"blocked"}',
+ text: async () => '{"code":"moderation_blocked"}',
})
const hooks = buildLlmLaneHooks(
[dscr({ lifecyclePoints: ['moderate'], capabilities: ['may_deny'] })],
@@ -73,12 +73,35 @@ describe('installed hooks (end-to-end via HookedLlmPort)', () => {
)
const hooked = new HookedLlmPort(inner, r => r, hooks)
const res = await hooked.completeWithTools({ messages: [], tools: [] })
- expect(res.finish_reason).toBe('content_filter')
+ // Graceful: a normal Stop completion with a first-party refusal message, so
+ // the turn completes instead of failing as LLM_CONTENT_FILTERED.
+ expect(res.finish_reason).toBe('stop')
+ expect(res.content).toBe(
+ "I can't help with that — the request was flagged by a content policy."
+ )
+ expect(res.tool_calls).toBeNull()
// Concurrent: the call is launched, then the moderation deny aborts it.
expect(vi.mocked(inner.completeWithTools)).toHaveBeenCalledTimes(1)
expect(inner.lastToolReq?.signal?.aborted).toBe(true)
})
+ it('a pre_call reject returns a graceful refusal without calling the model', async () => {
+ const inner = fakePort()
+ const preReject: FetchLike = async () => ({
+ status: 200,
+ text: async () => '{"action":"reject","code":"jailbreak_blocked"}',
+ })
+ const hooks = buildLlmLaneHooks(
+ [dscr({ lifecyclePoints: ['pre_call'], capabilities: ['may_deny'] })],
+ { getAuthToken: () => '', fetchImpl: preReject }
+ )
+ const hooked = new HookedLlmPort(inner, r => r, hooks)
+ const res = await hooked.completeWithTools({ messages: [], tools: [] })
+ expect(res.finish_reason).toBe('stop')
+ expect(res.content).toBe("I can't comply with that request.")
+ expect(vi.mocked(inner.completeWithTools)).not.toHaveBeenCalled()
+ })
+
it('moderation runs concurrently with the call; a pass proceeds to the result', async () => {
const inner = fakePort()
const moderatePass: FetchLike = async () => ({ status: 200, text: async () => '{}' })
diff --git a/mcp-host/src/core/adapters/hookedLlmPort.ts b/mcp-host/src/core/adapters/hookedLlmPort.ts
index 3b8ab6d5f..68c14d40a 100644
--- a/mcp-host/src/core/adapters/hookedLlmPort.ts
+++ b/mcp-host/src/core/adapters/hookedLlmPort.ts
@@ -7,11 +7,12 @@
* 1. apply the first-party built-in request-shaping chain (`prompt-shaping`,
* `token-trim`);
* 2. run installed `pre_call` hooks (each may rewrite the request or reject) —
- * a `deny` returns a content-filtered response WITHOUT calling the model;
+ * a `deny` returns a graceful refusal (finish_reason Stop) WITHOUT calling
+ * the model, so the turn completes with a message instead of erroring;
* 3. dispatch the (possibly rewritten) request. When `moderate` hooks exist they
* run CONCURRENTLY with the model call (spec §7.1) — a moderation `deny`
* aborts the in-flight call via a linked `AbortSignal` (no tokens wasted) and
- * returns a filtered response; the happy path proceeds to `post_call`;
+ * returns a graceful refusal; the happy path proceeds to `post_call`;
* 4. if the model call throws, run installed `on_error` hooks — a gated hook may
* `substitute` a safe text-only result (else the error surfaces);
* 5. run installed `post_call` hooks over the result — each may redact content or
@@ -22,7 +23,7 @@
* Response-capability enforcement (F4), system-prompt immutability (N4), and
* subtractive actions (N5) are enforced inside `RemoteLlmHook`.
*/
-import { aggregateDecision } from '../guardrails'
+import { aggregateDecision, pickPresentationReason } from '../guardrails'
import type { Contributor, GuardrailsConfig } from '../guardrails'
import { type RequestShaper, buildLlmBuiltinChain } from '../guardrails/llm/builtinChain'
import {
@@ -42,13 +43,34 @@ import {
const EMPTY_HOOKS: LlmLaneHooks = { preCall: [], moderate: [], postCall: [], onError: [] }
-/** A safe content-filtered response returned in place of a denied model call (spec §7.1). */
-function filteredResponse(): ToolCompletionResponse {
+/**
+ * First-party refusal text keyed by the winning deny `reasonCode`. The hook's
+ * own message (`audit.modelReason`) is UNTRUSTED — it comes from an out-of-process
+ * container — so it is never surfaced to the user verbatim (§8.4); we map the
+ * controlled reason code to a canned message instead.
+ */
+const REFUSAL_BY_REASON: Record<string, string> = {
+ moderation_blocked: "I can't help with that — the request was flagged by a content policy.",
+ jailbreak_blocked: "I can't comply with that request.",
+ content_filtered: "I can't help with that request.",
+ hook_unavailable:
+ "I can't complete that request right now — a required safety check is unavailable. Please try again shortly.",
+}
+const DEFAULT_REFUSAL = "I can't help with that request."
+
+/**
+ * A denied model call renders as a GRACEFUL assistant refusal — `finish_reason:
+ * Stop` + refusal text — not a content-filter error. The turn then flows through
+ * the normal text→response delivery path (the user gets a message, the task
+ * completes) instead of failing as `LLM_CONTENT_FILTERED`. The refusal text is
+ * first-party, selected by the winning deny `reasonCode` (spec §7.1/§8.4).
+ */
+function refusalResponse(reason?: string): ToolCompletionResponse {
return {
- content: 'This request was blocked by a content guardrail policy.',
+ content: (reason && REFUSAL_BY_REASON[reason]) || DEFAULT_REFUSAL,
tool_calls: null,
usage: { input_tokens: 0, output_tokens: 0, total_tokens: 0 },
- finish_reason: FinishReason.ContentFilter,
+ finish_reason: FinishReason.Stop,
}
}
@@ -90,7 +112,11 @@ export class HookedLlmPort implements LlmPort {
if (c.rewrite) req = c.rewrite // already system-role-stripped + capability-gated (N4/F4)
}
}
- if (aggregateDecision(preContribs) === 'deny') return filteredResponse()
+ if (aggregateDecision(preContribs) === 'deny') {
+ const reason = pickPresentationReason(preContribs, 'deny')
+ console.warn(`[HookedLlmPort] pre_call deny — refusing turn (reason=${reason ?? 'unknown'})`)
+ return refusalResponse(reason)
+ }
return this.hooks.moderate.length === 0
? this.dispatch(req)
@@ -128,13 +154,20 @@ export class HookedLlmPort implements LlmPort {
response => ({ ok: true as const, response }),
error => ({ ok: false as const, error })
)
- const moderationVerdict = Promise.all(this.hooks.moderate.map(hook => hook.moderate(req))).then(
- cs => aggregateDecision(cs.filter((c): c is NonNullable<typeof c> => c != null))
- )
-
- if ((await moderationVerdict) === 'deny') {
+ // Keep the contributions (not just the verdict) so a deny can carry its
+ // reason code into the graceful refusal message.
+ const moderationContribs = Promise.all(
+ this.hooks.moderate.map(hook => hook.moderate(req))
+ ).then(cs => cs.filter((c): c is NonNullable<typeof c> => c != null))
+
+ const contribs = await moderationContribs
+ if (aggregateDecision(contribs) === 'deny') {
controller.abort() // cancel the in-flight call (§7.1)
- return filteredResponse()
+ const reason = pickPresentationReason(contribs, 'deny')
+ console.warn(
+ `[HookedLlmPort] moderation deny — refusing turn (reason=${reason ?? 'unknown'})`
+ )
+ return refusalResponse(reason)
}
const outcome = await modelOutcome