Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(host): run post-result hooks on tool error results
Alfredo Lopeza24955589052
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex c06fc7ee3..1d5ffdcd0 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.508",+ "version": "0.1.509","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.508",+ "version": "0.1.509","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex bbaf43977..705096994 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.508",+ "version": "0.1.509","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/orchestration/__tests__/guardrailGate.test.ts b/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.tsindex 71b280601..e8559bf0d 100644--- a/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.ts+++ b/mcp-host/src/core/orchestration/__tests__/guardrailGate.test.ts@@ -219,6 +219,62 @@ describe('guardrail gate in executeToolCalls', () => {expect(toolResults[0].content).toBe('[fenced:[redacted:ok]]')})+ // Post-result hooks redact error text too (a thrown upstream message can+ // carry a secret) and observe failed calls, so every error result the+ // execution boundary produces must pass through transformResult.+ it.each([+ {+ path: 'a thrown tool error',+ tools: (): Record<string, Tool> => {+ const throwing = new StubTool('do_thing')+ throwing.execute = async params => {+ throwing.calls.push(params)+ throw new Error('upstream said SECRET-TOKEN-123')+ }+ return { do_thing: throwing }+ },+ raw: 'Tool execution failed: upstream said SECRET-TOKEN-123',+ },+ {+ // Admission accepts the call; the execution boundary re-validates the+ // effective parameters and refuses them.+ path: 'an execution-boundary validation failure',+ tools: (): Record<string, Tool> => {+ const strict = new StubTool('do_thing')+ let validations = 0+ Object.assign(strict, {+ validateParams: () =>+ ++validations === 1+ ? { is_valid: true, errors: [] }+ : { is_valid: false, errors: ['bad SECRET-TOKEN-123'] },+ })+ return { do_thing: strict }+ },+ raw: 'Parameter validation failed: bad SECRET-TOKEN-123',+ },+ {+ path: 'a missing tool',+ tools: (): Record<string, Tool> => ({}),+ raw: 'Tool not found: do_thing',+ },+ ])('PostToolUse transformResult also sees $path', async ({ tools, raw }) => {+ const seen: Array<{ content: string; isError: boolean }> = []+ const guardrail: ToolLaneGuardrail = {+ async decide(_id, input) {+ return { decision: 'allow', reasonCode: 'r', effectiveInput: input, source: 'host_rule' }+ },+ async transformResult(_id, _input, result) {+ seen.push(result)+ return { content: '[redacted]', isError: result.isError }+ },+ }+ const config = makeConfig(new StubTool('unused'), guardrail)+ config.toolRegistry = registry(tools())+ const { toolResults } = await executeToolCalls([call], config, 0)+ expect(seen).toEqual([{ content: raw, isError: true }])+ expect(toolResults[0]).toMatchObject({ content: '[redacted]', is_error: true })+ })+it('ask + matching one-shot approval → proceeds and clears pending', async () => {const tool = new StubTool('do_thing')const conversation: Partial<Conversation> = {diff --git a/mcp-host/src/core/orchestration/toolUseLoopSingleTool.ts b/mcp-host/src/core/orchestration/toolUseLoopSingleTool.tsindex 103af937d..b559f7764 100644--- a/mcp-host/src/core/orchestration/toolUseLoopSingleTool.ts+++ b/mcp-host/src/core/orchestration/toolUseLoopSingleTool.ts@@ -35,6 +35,13 @@ export async function executeSingleTool(): Promise<ToolResult> {const { toolRegistry, toolOutputProcessor, events, toolTimeout } = config+ // Post-result hooks see error results too: they redact error text (a thrown+ // upstream message can carry a secret) and observe failed calls.+ const errorResult = async (content: string): Promise<ToolResult> => {+ const result: ToolResult = { tool_call_id: call.id, name: call.name, content, is_error: true }+ return transformResult ? transformResult(result) : result+ }+const tool = toolRegistry.get(call.name)let validation = toolOutputProcessor.beforeExecution(call.name, call.arguments)if (validation.is_valid) {@@ -52,22 +59,10 @@ export async function executeSingleTool(},timestamp: new Date(),})- return {- tool_call_id: call.id,- name: call.name,- content: `Parameter validation failed: ${validation.errors.join(', ')}`,- is_error: true,- }+ return errorResult(`Parameter validation failed: ${validation.errors.join(', ')}`)}- if (!tool) {- return {- tool_call_id: call.id,- name: call.name,- content: `Tool not found: ${call.name}`,- is_error: true,- }- }+ if (!tool) return errorResult(`Tool not found: ${call.name}`)const renderContent = (content: string): string => {if (!tool.requiresSanitization()) return content@@ -279,12 +274,7 @@ export async function executeSingleTool(logger.error({ toolName: call.name, err }, 'Tool execution failed')- return {- tool_call_id: call.id,- name: call.name,- content: errorMessage,- is_error: true,- }+ return await errorResult(errorMessage)} finally {if (watcherId) {clearInterval(watcherId)