Advanced ⏱ 8 min read 📁 examples/27_acp_agent.mjs

Lesson 38: merci behind an editor protocol

the ACP v1 agent: session lifecycle, updates, the fail-closed permission model

1. Why This Feature Exists

An editor speaks the Agent Client Protocol to this process. The ACP client is the official SDK client on the other end of a real NDJSON transport (two byte pipes, in memory); the ACP agent is createAcpAgent wrapping a live merci session scoped to a workspace.

2. What the Example Demonstrates

A — the editor connects
initialize negotiates ACP v1 and reports ACP_AGENT_VERSION; the client announces its capabilities.
B — session/new
the agent mints a session id and the merci session factory receives the editor's cwd; session/list shows it.
C — a coding question
a real model reads config.json in the workspace and answers with the retry limit; every thought, message chunk and tool call arrives as a session/update notification over the transport, and the turn ends with ACP_STOP_REASONS.END_TURN.
D — permissions
permissionAllowed grants only an option the agent offered — a reject, an unknown option id, a cancelled outcome and an empty list all deny. Then a live shell command is offered to the editor, the editor rejects it, and the command never runs.
E — cancellation
session/cancel is a notification, sent while the turn is streaming; the prompt resolves with the CANCELLED stop reason. What this shows: - the ACP v1 agent and session lifecycle, driven by a real client - fail-closed permissions wired from a shell gate to the editor - stop reasons as the protocol's answer for every turn Run: node examples/27_acp_agent.mjs Needs: PROVIDER + provider credentials in secret.env. @version 0.27.0
imported from 'merci-sdk'
ACP_AGENT_VERSION, ACP_STOP_REASONS, EVENTS, createAcpAgent, createContextTools, createShellTool, defaultDisplay, initializeClient, loadCredentials, log, permissionAllowed

3. How the Core Call Works

The entry point, condensed. The full runnable file is linked below.

import { EventEmitter } from 'node:events';
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
    EVENTS,
    defaultDisplay, initializeClient, loadCredentials, log,
} from 'merci-sdk';
import { ACP_AGENT_VERSION, ACP_STOP_REASONS, createAcpAgent, permissionAllowed } from 'merci-sdk/acp';
import { createContextTools, createShellTool } from 'merci-sdk/tools';
import { client, methods, ndJsonStream, PROTOCOL_VERSION } from '@agentclientprotocol/sdk/unstable';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';

const WORKSPACE = join(SCRATCH_DIR, '27-acp-workspace');
const CONFIG_FILE = join(WORKSPACE, 'config.json');
const CONFIG = { service: 'billing', retryLimit: 3, timeoutSeconds: 30 };
const EDITOR_FILE = join(WORKSPACE, 'editor-only.txt');
const CODING_QUESTION = 'Read the file config.json in this workspace and report the retry limit. Answer in one sentence and name the file you read.';
const SHELL_QUESTION = `Run this exact shell command with the shell tool: node -e "require('node:fs').writeFileSync('editor-only.txt','ran')" . Then report the outcome in one sentence.`;
const LONG_QUESTION = 'Write a 300-word description of a deployment pipeline, with headings.';

const ALLOW = { optionId: 'opt-allow', name: 'Allow once', kind: 'allow_once' };
const REJECT = { optionId: 'opt-reject', name: 'Reject', kind: 'reject_once' };
const CASES = [
    { name: 'allow_once', options: [ALLOW], answer: { outcome: { outcome: 'selected', optionId: 'opt-allow' } }, expected: true },
    { name: 'reject_once', options: [REJECT], answer: { outcome: { outcome: 'selected', optionId: 'opt-reject' } }, expected: false },
    { name: 'an option id that was never offered', options: [ALLOW], answer: { outcome: { outcome: 'selected', optionId: 'never-offered' } }, expected: false },
    { name: 'a cancelled outcome', options: [ALLOW], answer: { outcome: { outcome: 'cancelled' } }, expected: false },
    { name: 'an empty option list', options: [], answer: { outcome: { outcome: 'selected', optionId: 'opt-allow' } }, expected: false },
];

/**
 * Two byte pipes wired into a pair of NDJSON streams: the agent writes one way,
 * the client writes the other. Real protocol bytes, no child process.
 */
function ndjsonDuplex() {
    const clientToAgent = new TransformStream();
    const agentToClient = new TransformStream();
    return {
        agentStream: ndJsonStream(agentToClient.writable, clientToAgent.readable),
        clientStream: ndJsonStream(clientToAgent.writable, agentToClient.readable),
    };
}

Read the full example: examples/27_acp_agent.mjs →

4. Behavior the Code Fixes

An editor speaks the Agent Client Protocol to this process. The ACP client is the official SDK client on the other end of a real NDJSON transport (two byte pipes, in memory); the ACP agent is createAcpAgent wrapping a live merci session scoped to a workspace.

5. Run the Example

Execute the runnable example from the repository root:

node examples/27_acp_agent.mjs
What it does: the ACP v1 agent: session lifecycle, updates, the fail-closed permission model