Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): reject workflow tool calls outside the step allowlist
Jose Ramirez03428f1288e2
diff --git a/mcp-host/src/workflow/__tests__/stepRouter.test.ts b/mcp-host/src/workflow/__tests__/stepRouter.test.tsindex 864779df6..be22175ef 100644--- a/mcp-host/src/workflow/__tests__/stepRouter.test.ts+++ b/mcp-host/src/workflow/__tests__/stepRouter.test.ts@@ -6,7 +6,7 @@ import {StepRouterConnectionError,ToolDispatchError,} from '../stepRouter'-import type { StepMcpServerRef } from '../types'+import type { AllowedToolsConfig, StepMcpServerRef } from '../types'describe('internal tool execution context', () => {it('forwards cancellation and deadline while projecting both recorded destinations', async () => {@@ -255,6 +255,127 @@ describe('StepMcpRouter.callTool', () => {})})+describe('StepMcpRouter call-time allowlist (spec §3.1.2)', () => {+ const notAllowed = (toolName: string) =>+ `Tool '${toolName}' is not allowed in this step and was not run. Use only the tools provided for this step.`++ async function routerWithAllowlist(allowedTools?: AllowedToolsConfig) {+ const client = mockClient([{ name: 'read' }, { name: 'write' }])+ const execute = vi.fn(async () => ({ success: true, content: 'internal ran' }))+ const router = new StepMcpRouter(mockFactory(new Map([['srv', client]])))+ router.registerInternalTools(+ [+ {+ name: 'clerum__read_workflow',+ description: 'read',+ parameters: { type: 'object' },+ execute,+ },+ {+ name: 'clerum__list_workflows',+ description: 'list',+ parameters: { type: 'object' },+ execute,+ },+ ],+ '/output'+ )+ await router.connect([{ name: 'srv', url: 'http://srv:3000' }])+ router.setAllowedTools(allowedTools)+ return { client, execute, router }+ }++ it('returns a recoverable tool error for an MCP tool outside include and never calls the server', async () => {+ const { client, router } = await routerWithAllowlist({ include: ['srv__read'] })++ const { result, record } = await router.callTool('srv__write', { path: 'README.md' })++ const errorResult = { success: false, error: notAllowed('srv__write') }+ expect(result).toEqual({ content: errorResult, isError: true })+ expect(record).toEqual({+ serverName: 'srv',+ toolName: 'write',+ args: { path: 'README.md' },+ result: errorResult,+ durationMs: 0,+ })+ expect(client.callTool).not.toHaveBeenCalled()+ })++ it('returns a recoverable tool error for a registered internal tool outside include and never executes it', async () => {+ const { execute, router } = await routerWithAllowlist({ include: ['srv__read'] })++ const { result, record } = await router.callTool('clerum__read_workflow', {+ namespace: 'sandbox-recipes',+ name: 'other-recipe',+ })++ expect(result).toEqual({+ content: { success: false, error: notAllowed('clerum__read_workflow') },+ isError: true,+ })+ expect(record.serverName).toBe('clerum')+ expect(record.toolName).toBe('read_workflow')+ expect(execute).not.toHaveBeenCalled()+ })++ it('answers a listed name that no server registered with a tool error instead of throwing', async () => {+ const { client, router } = await routerWithAllowlist({+ include: ['srv__read', 'srv__delete', 'shell_exec'],+ })++ await expect(router.callTool('srv__delete', {})).resolves.toMatchObject({+ result: { content: { success: false, error: notAllowed('srv__delete') }, isError: true },+ record: { serverName: 'srv', toolName: 'delete' },+ })+ await expect(router.callTool('shell_exec', {})).resolves.toMatchObject({+ result: { isError: true },+ record: { serverName: 'unknown', toolName: 'shell_exec' },+ })+ expect(client.callTool).not.toHaveBeenCalled()+ })++ it('dispatches listed MCP and internal tools normally', async () => {+ const { client, execute, router } = await routerWithAllowlist({+ include: ['srv__read', 'clerum__list_workflows'],+ })++ const mcp = await router.callTool('srv__read', { q: 1 })+ const internal = await router.callTool('clerum__list_workflows', {})++ expect(client.callTool).toHaveBeenCalledWith('read', { q: 1 }, {})+ expect(mcp.result).toEqual({ content: 'result', isError: false })+ expect(execute).toHaveBeenCalledTimes(1)+ expect(internal.result).toEqual({+ content: { success: true, content: 'internal ran' },+ isError: false,+ })+ })++ it.each<[string, AllowedToolsConfig | undefined]>([+ ['absent', undefined],+ ['empty', { include: [] }],+ ])('keeps unrestricted dispatch when include is %s', async (_label, allowedTools) => {+ const { client, execute, router } = await routerWithAllowlist(allowedTools)++ await router.callTool('srv__write', { path: 'notes.md' })+ await router.callTool('clerum__read_workflow', {})++ expect(client.callTool).toHaveBeenCalledWith('write', { path: 'notes.md' }, {})+ expect(execute).toHaveBeenCalledTimes(1)+ await expect(router.callTool('unknown__tool', {})).rejects.toThrow(ToolDispatchError)+ })++ it('still throws for an already cancelled caller before the allowlist check', async () => {+ const { client, router } = await routerWithAllowlist({ include: ['srv__read'] })++ await expect(+ router.callTool('srv__write', {}, { signal: AbortSignal.abort('step-timeout') })+ ).rejects.toThrow('step-timeout')+ expect(client.callTool).not.toHaveBeenCalled()+ })+})+describe('StepMcpRouter.disconnect', () => {it('disconnects all connected servers', async () => {const clientA = mockClient([{ name: 't1' }])diff --git a/mcp-host/src/workflow/stepRouter.ts b/mcp-host/src/workflow/stepRouter.tsindex 13bfecd3f..a6a91a9c8 100644--- a/mcp-host/src/workflow/stepRouter.ts+++ b/mcp-host/src/workflow/stepRouter.ts@@ -81,6 +81,15 @@ export interface McpClientConnection {export type McpClientFactory = (server: StepMcpServerRef) => McpClientConnection+/**+ * Error text the model receives when it calls a tool outside the step's+ * allowedTools. Recoverable: the step continues and the model may retry with+ * an offered tool (spec §3.1.2).+ */+function toolNotAllowedError(toolName: string): string {+ return `Tool '${toolName}' is not allowed in this step and was not run. Use only the tools provided for this step.`+}+// ─── StepMcpRouter ──────────────────────────────────────────────────────export class StepMcpRouter {@@ -93,6 +102,12 @@ export class StepMcpRouter {private readonly clientFactory: McpClientFactoryprivate readonly ajv = new Ajv({ allErrors: true, strict: false, useDefaults: true })private outputDir = '/output'+ // Names callTool() may dispatch when the step declares a non-empty+ // allowedTools.include. null = no allowlist in effect (every registered tool+ // is callable — the pre-§3.1.2 behaviour, kept for absent/empty include).+ // Deliberately NOT cleared by disconnect(): a restriction must never lapse+ // into "everything callable" on reuse; the caller re-applies it per step.+ private callableToolNames: Set<string> | null = nullconstructor(clientFactory: McpClientFactory) {this.clientFactory = clientFactory@@ -204,9 +219,51 @@ export class StepMcpRouter {return this.allTools.filter(t => includeSet.has(t.name))}+ /**+ * Enforce the step's allowlist at dispatch time (spec §3.1.2).+ *+ * - include absent or empty: no restriction. Every registered tool stays+ * callable, matching getFilteredTools(), which offers every tool then.+ * - include non-empty: callTool() dispatches only names that are both listed+ * and registered, i.e. exactly what getFilteredTools() offers the model.+ * Any other name gets a recoverable tool-error result and never runs.+ *+ * Membership is checked against the live registry on each call, so this may+ * be applied before or after connect().+ */+ setAllowedTools(allowedTools?: AllowedToolsConfig): void {+ const include = allowedTools?.include+ this.callableToolNames = include && include.length > 0 ? new Set(include) : null+ }++ private rejectNotAllowed(+ toolName: string,+ args: Record<string, unknown>+ ): { result: ToolResult; record: ToolCallRecord } {+ const separator = toolName.indexOf('__')+ const errorResult = { success: false, error: toolNotAllowedError(toolName) }+ // Arguments are model output (and so attacker-shapeable); log the name only.+ logger.warn(+ { toolName: toolName.slice(0, 200) },+ 'Workflow step tool call rejected: tool is not in allowedTools'+ )+ return {+ result: { content: errorResult, isError: true },+ record: {+ serverName: separator > 0 ? toolName.slice(0, separator) : 'unknown',+ toolName: separator > 0 ? toolName.slice(separator + 2) : toolName,+ args,+ result: errorResult,+ durationMs: 0,+ },+ }+ }+/*** Dispatch a tool call to the correct server by tool name prefix.* Internal tools (clerum__*) are dispatched locally without MCP connection.+ * With a non-empty allowlist (setAllowedTools), names outside the offered set+ * return a recoverable tool-error result instead of dispatching.*/async callTool(toolName: string,@@ -221,6 +278,16 @@ export class StepMcpRouter {throw new Error(reason)}+ // Spec §3.1.2: with an allowlist in effect, anything outside the offered set+ // (unlisted, or listed but never registered) is answered with a recoverable+ // tool error and is never dispatched.+ if (+ this.callableToolNames &&+ !(this.callableToolNames.has(toolName) && this.toolMap.has(toolName))+ ) {+ return this.rejectNotAllowed(toolName, args)+ }+// Check internal tools firstconst internalTool = this.internalToolMap.get(toolName)if (internalTool) {