Connect to Codant over stdio
A custom TUI, desktop client, or local script can start codant bridge as a child process and send JSON-RPC requests to the running Codant daemon over standard input and output. codant serve --stdio uses the same relay. The data directory determines which daemon the bridge connects to. When using CODANT_DATA_DIR, give the daemon and the client subprocess the same value. See packages/api/src/bridge.ts and runtimePaths in packages/core/src/platform.ts. CLI entry point source
Send your first request
Run codant serve in one terminal. In a second terminal with Node.js available, save the example as codant-ping.mjs and run node codant-ping.mjs. The Windows MSI puts codant.cmd on PATH, so the example runs the fixed command codant bridge through cmd.exe on Windows.
To select an executable, run node codant-ping.mjs "<path-to-native-executable>" using an .exe on Windows. For a source build, run node codant-ping.mjs node "<Codant-checkout>/packages/cli/dist/index.js". Both forms launch the specified program directly and pass its arguments individually. Both relay commands use the same running daemon for daemon/initialize and daemon/ping, as covered by packages/cli/test/bridge-cli.test.ts. Windows MSI launcher
The example initializes protocol 0.1, checks that the response advertises daemon/ping, and sends a ping with empty parameters {}. It matches replies by ID, verifies the daemon's instanceId, and closes the bridge input after receiving the ping result. See the request and response fields in packages/api/src/protocol.ts, packages/api/src/daemon-lifecycle-rpc.ts, and the initialization reference.
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { createInterface } from 'node:readline';
const [program, ...prefix] = process.argv.slice(2);
const useWindowsShim = process.platform === 'win32' && program === undefined;
const command = useWindowsShim ? process.env.ComSpec || 'cmd.exe' : program ?? 'codant';
const args = useWindowsShim ? ['/d', '/s', '/c', 'codant bridge'] : [...prefix, 'bridge'];
const bridge = spawn(command, args, {
shell: false,
stdio: ['pipe', 'pipe', 'inherit'],
});
const started = once(bridge, 'spawn');
const exited = new Promise(resolve => bridge.once('close', resolve));
const lines = createInterface({ input: bridge.stdout, crlfDelay: Infinity });
const replies = lines[Symbol.asyncIterator]();
let writeError;
bridge.stdin.on('error', error => {
writeError = error;
lines.close();
});
const timeout = setTimeout(() => bridge.kill(), 10_000);
async function call(id, method, params) {
await new Promise((resolve, reject) => {
const line = JSON.stringify({ jsonrpc: '2.0', id, method, params });
bridge.stdin.write(`${line}\n`, error => error ? reject(error) : resolve());
});
while (true) {
const { value, done } = await replies.next();
if (done) throw writeError ?? new Error(`Bridge closed before reply ${id}`);
const frame = JSON.parse(value);
if (frame?.jsonrpc !== '2.0') throw new Error('Invalid JSON-RPC frame');
if (frame.id !== id) continue;
if (frame.error) {
throw new Error(`${method}: ${frame.error.code} ${frame.error.message}`);
}
return frame.result;
}
}
try {
await started;
const init = await call('init', 'daemon/initialize', { protocolVersion: '0.1' });
if (init?.protocolVersion !== '0.1' || typeof init.instanceId !== 'string' ||
!init.instanceId || !Array.isArray(init.capabilities) ||
!init.capabilities.includes('daemon/ping')) {
throw new Error('Expected protocol 0.1 with daemon/ping capability');
}
const ping = await call('ping', 'daemon/ping', {});
if (ping?.instanceId !== init.instanceId) throw new Error('Daemon identity mismatch');
bridge.stdin.end();
if (await exited !== 0) throw new Error('Bridge exited unsuccessfully');
console.log(JSON.stringify(ping));
} finally {
clearTimeout(timeout);
lines.close();
if (bridge.exitCode === null && bridge.signalCode === null) bridge.kill();
}Messages and exit behavior
- Input and output use UTF-8 JSONL, with one complete JSON-RPC object per line terminated by
LF. Daemon frames appear on stdout and relay diagnostics on stderr. The daemon reassembles messages across stream chunks and limits inbound line payloads to 65,536 bytes, excluding the trailingLF. Seepackages/api/src/framing.ts,packages/api/src/bridge.ts, andpackages/api/test/protocol.test.ts. - Wait for
daemon/initializeto succeed before sending other requests. Use a non-empty string ID for every request and handle notifications without IDs separately from replies. Enable features according to the capabilities returned on the current connection. Seepackages/api/src/connection.ts,packages/api/src/protocol.ts, andpackages/api/test/client-notification-interleave.test.ts. - Closing a relay ends that connection. If the relay cannot reach the daemon, it exits with code
1and prompts you on stderr to runcodant serve. See the cases inpackages/cli/test/bridge-cli.test.ts.
Use the same connection to read a snapshot, subscribe to bus events, and check recovery after disconnecting. Observe tasks with Node.js
Choose an integration
| Use case | Codant entry point |
|---|---|
| A custom TUI, desktop client, or local script | Use codant bridge or codant serve --stdio and send daemon/initialize. See packages/cli/src/index.ts and packages/api/src/bridge.ts. |
| The experimental official VS Code extension | codant serve --ide provides a local WebSocket. The extension reads the selected IDE bridge record and uses its Bearer credential within the extension host. |
| An MCP client consuming tools provided by Codant | codant mcp-server starts the MCP stdio entry point with its own tool catalog and protocol. See packages/cli/src/index.ts and packages/api/src/mcp-server-protocol.ts. |
Connect the VS Code panel
The VS Code integration guide covers building the VSIX, selecting an IDE bridge record, and opening a session. The standalone official GUI is pending release.
For a custom desktop app with a web frontend, the local host starts and holds the stdio bridge connection and passes view data to the frontend. Use the static docs page to download examples, then run them in a local terminal or through your own local host.
