Agentic Sessions and Protocols ⏱ 9 min read 📁 examples/14_coding_agent.mjs

Lesson 09: Shell Tool Security and Sandbox Gates

Enforce a rigorous 3-layer security gate (safe, blocked, and HITL-approved) for shell execution, complete with sensitive environment redaction and synthesis fallback.

1. Why This Feature Exists

Allowing LLMs to run arbitrary shell commands is an extreme security liability. Merci SDK provides a declarative 3-layer security boundary that intercepts commands before process spawning.

2. Key Symbols and Exports

createShellTool(options)
Creates a shell execution tool with strict command whitelisting and blacklisting.
Three-Layer Security Gate
Safe commands run immediately; dangerous commands (rm, curl | sh) are blocked; modifying commands require HITL confirmation.
printenv & Secret Redaction
Guards against environmental secret extraction in LLM tool context.
Synthesis Fallback
Provides simulated responses if command execution is denied by policy or user.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getToolModel
} from 'merci-sdk';
import { createShellTool } from 'merci-sdk/tools';

const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });

const shellTool = createShellTool({
    cwd: process.cwd(),
    allowedCommands: ['ls', 'git status', 'cat', 'grep', 'node -v'],
    blockedCommands: ['rm', 'mkfs', 'dd', 'curl', 'wget', 'printenv'],
    requireApproval: ['git commit', 'npm install', 'pnpm add']
});

const session = client.session(getToolModel({ provider: PROVIDER }))
    .withTools([shellTool] });

const result = await session.run('List the files in the current directory and check git status.');
console.log(result);

4. Wire and Telemetry Mechanics

When the model attempts a shell call, the command is tokenized and matched against the security policy. Blocked commands reject immediately with a security violation error; approved commands execute via child_process.spawn.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/14_coding_agent.mjs

Read the full example: examples/14_coding_agent.mjs →

Expected Output: Demonstrates execution of safe commands (ls, git status), intercepts blocked commands, and prompts for confirmation on gated operations.