Open History to see the last commit
1---
2title: Run a harness in the terminal
3id: harness-cli
4order: 3
5description: "Run your harness in a terminal with line mode or your own screen, a print mode for scripts and CI, NDJSON output, an ACP mode for editors, and an HTTP server."
6keywords:
7 - tanstack ai
8 - harness
9 - cli
10 - terminal
11 - ink
12---
13
14You built a harness and want to use it like Claude Code: type in a terminal, watch it work, approve tools. You also want to run it in CI. `runCli` gives one harness all of these modes.
15
16## Install
17
18<!-- ::start:tabs variant="package-manager" mode="install" -->
19
20react: @tanstack/ai-harness-cli
21vue: @tanstack/ai-harness-cli
22solid: @tanstack/ai-harness-cli
23svelte: @tanstack/ai-harness-cli
24preact: @tanstack/ai-harness-cli
25angular: @tanstack/ai-harness-cli
26vanilla: @tanstack/ai-harness-cli
27octane: @tanstack/ai-harness-cli
28
29<!-- ::end:tabs -->
30
31## 1. Write the entry file
32
33```ts group=harness-cli
34import { defineHarness } from '@tanstack/ai-harness'
35import { runCli } from '@tanstack/ai-harness-cli'
36import { openaiText } from '@tanstack/ai-openai'
37
38const assistant = defineHarness({
39 name: 'acme/assistant',
40 adapter: openaiText('gpt-6.1-sol'),
41})
42
43process.exitCode = await runCli(assistant)
44```
45
46## 2. Pick a mode
47
48To use the harness yourself:
49
50- No flags: line mode. Type a message and press Enter. With a `ui`, your own screen starts in its place (see [Run your own screen](#run-your-own-screen)).
51- `-p "prompt"`: run one prompt, print the answer, and exit.
52- `-p "prompt" --output ndjson`: print every AG-UI event as one JSON line.
53
54To use the harness from another program:
55
56- `--acp`: serve the harness as an ACP v2 agent over stdio, for editors. Needs `@tanstack/ai-acp` with harness support.
57- `--mcp`: serve the harness as an MCP server over stdio, for Claude Code, Cursor, and other MCP clients. Needs `@tanstack/ai-mcp` with harness support. Add `--yes` to approve every tool call.
58- `--serve`: serve the session protocol over HTTP on `127.0.0.1:8787`. Every request needs the bearer token. Pass `--token`, set `HARNESS_TOKEN`, or copy the token the CLI prints. With `@tanstack/ai-mcp` and its harness support, it also serves MCP at `/mcp`. One token, one user, every thread, so keep it on a network you trust (see [Serve over HTTP safely](#serve-over-http-safely)).
59
60`--mcp` and `--serve` run only the commands in `expose.commands` of the harness. `--serve` also sets only the config keys in `expose.config`. Add the names to `expose` in `defineHarness`, for example `expose: { commands: ['connect:github'] }`. Line mode and `-p` run every command. See [Choose what clients can change](./connect#choose-what-clients-can-change).
61
62Line mode reads one message or command per line. In a terminal, you can type while a turn runs:
63
64- `/cancel` cancels the running turn at once.
65- A message joins the running turn as a steer. The model reads it at its next call.
66- A message that you type when no turn runs starts a new turn.
67
68With piped input, each line waits for the turn that the line before it started. A script runs in order. In a terminal, line mode also opens sign-in links in the browser.
69
70## 3. Use it in CI
71
72`-p` exits with a code your script can check:
73
74| Code | Meaning |
75|---|---|
76| 0 | The turn finished. |
77| 1 | The turn failed. |
78| 3 | The turn waits for a person: an approval, an answer, or a sign-in. |
79| 130 | The turn was cancelled, or SIGINT (Ctrl+C) stopped the CLI. |
80| 143 | SIGTERM cancelled the turn or stopped the CLI. |
81
82`EXIT` from `@tanstack/ai-harness-cli` has these codes, for example `EXIT.needsAction` (3). `-p` exits 3 at the first [wait](./waits) of the turn, because nobody can answer in this mode. It writes what the turn waits for on stderr. On a durable host, the wait stays in the log, so a later run, or another server, can answer it.
83
84The codes 130 and 143 apply in every mode. See [Stop with Ctrl+C](#stop-with-ctrlc).
85
86## Stop with Ctrl+C
87
88Ctrl+C works the same in every mode:
89
90- The first Ctrl+C (SIGINT) or SIGTERM cancels the running turn.
91- A second one, or one while no turn runs, stops the CLI. `runCli` resolves to 130 for SIGINT, and to 143 for SIGTERM.
92- When the CLI made the host, it closes the host with `recoverable: true`. Another host then runs the cut work again.
93- When you gave the CLI your own `host`, the CLI releases only its session handle. Your host stays open.
94
95The CLI hears the signals on `process`. To send them yourself, for example in a test, pass a `signals` source. An `EventEmitter` works:
96
97```ts group=harness-cli-signals
98import { EventEmitter } from 'node:events'
99import { defineHarness } from '@tanstack/ai-harness'
100import { runCli } from '@tanstack/ai-harness-cli'
101import { openaiText } from '@tanstack/ai-openai'
102
103const assistant = defineHarness({
104 name: 'acme/assistant',
105 adapter: openaiText('gpt-6.1-sol'),
106})
107
108const signals = new EventEmitter()
109const exit = runCli(assistant, { argv: ['-p', 'Write a long story.'], signals })
110// No turn runs yet, so this stops the CLI.
111signals.emit('SIGTERM')
112console.log(await exit) // 143
113```
114
115## Serve over HTTP safely
116
117`--serve` is the quick way to put a harness on HTTP, so it makes the trust decisions for you: one bearer token, one user, every thread. Anyone with the token acts as that one user. They can open or drive any thread on the host, including the tools it runs (shell commands, file edits) on the machine the CLI runs on. Treat the token like a shell login.
118
119- It binds to `127.0.0.1:8787`, reachable only from the same machine. Change the bind with `--host` and `--port`. `--host 0.0.0.0` puts it on the network, so do that only behind TLS and a proxy you trust.
120- The CLI signs media links with its own key, never with the token. By default it makes a new random key at each start, so media links stop working after a restart. To keep them working, pass `--media-key` or set `HARNESS_MEDIA_KEY`, with a key of at least 32 bytes. Change the key to stop every link a turn already handed out.
121
122Need per-user access, your own sign-in check, or a rule for which threads each user can open? Use [`createHarnessHandler`](./connect) on a host with [`access`](./access) instead. The host reads the user of each request and checks each action, so no thread is open by accident.
123
124## Use your own host and user
125
126By default, the CLI builds its own host in memory and opens sessions with no user. Your server already has a durable host with its own stores, and each person has their own credentials. Give the CLI that host and the user:
127
128```ts group=harness-cli-host
129import { createHarnessHost, defineHarness } from '@tanstack/ai-harness'
130import { runCli } from '@tanstack/ai-harness-cli'
131import { memoryPersistence } from '@tanstack/ai-persistence'
132import { memoryCredentialStore } from '@tanstack/ai/byok/server'
133import { openaiText } from '@tanstack/ai-openai'
134
135const assistant = defineHarness({
136 name: 'acme/assistant',
137 adapter: openaiText('gpt-6.1-sol'),
138})
139
140const host = createHarnessHost({
141 persistence: memoryPersistence(),
142 credentials: memoryCredentialStore(),
143})
144
145process.exitCode = await runCli(assistant, { host, user: { id: 'ada' } })
146await host.close()
147```
148
149- You own the host, so the CLI does not close it. Close it yourself when `runCli` returns. Without `host`, the CLI builds a host and closes it.
150- Give `host`, or `persistence` and `credentials`, not both. With both, `runCli` writes `Give runCli a host, or persistence and credentials, not both.` and returns code 1.
151- Line mode, `-p`, your own `ui`, `--acp`, and `--mcp` open their sessions as `user`, so `/connect` saves keys for that user.
152- `--serve` checks the token first. On a host without `access`, each request then runs as `user`. On a host with [`access`](./access), `getUser` reads the user of each request.
153- On a host with `access`, give `user` too. `--mcp` does not start on such a host, because stdio has no login.
154
155## Commands in line mode
156
157For the session:
158
159- `/agents`: list the agents.
160- `/agent <name> {"json":"input"}`: run an agent in the background. When it is done, a new turn starts with its result.
161- `/config`: show the settings. `/config <key> <value>` changes one.
162- `/connect <id>` and `/disconnect <id>`: sign in to a connector, or out.
163
164For the running work:
165
166- `/cancel`: cancel the running turn.
167- `/status`: show what runs and what waits.
168- `/exit`: quit.
169
170Plugin commands (for example `/model` or `/todos`) show up in `/help`.
171
172When a turn waits for you, each next line answers one wait, in order. The turn continues after the last one:
173
174- An approval: `y` approves and `n` refuses. An approval of `permissions()` also takes `once`, `always`, or `reject`.
175- A question: type the answer. The CLI reads it with the schema of the question.
176- A sign-in: sign in, then type `y` to go on, or `n` to skip it.
177- A client tool: the terminal cannot run it. Any line skips it.
178
179## Send files and save media
180
181To send a file with a message, write `@` and the path of the file:
182
183```text
184what is wrong in @./bug.png
185compare @"old logo.png" with @./new-logo.png
186```
187
188- The path is relative to the working folder. Put quotes around a path with spaces.
189- `@path` works in line mode, with `-p`, with piped input, and in your own `ui`.
190- A `@word` that is not a file stays in the text, for example `@types/node`.
191- If the CLI does not know the type of the file, it does not send the message. To send the path as text, remove the `@`.
192
193The CLI saves the media that a turn makes in a folder. The default folder is `./<harness name>-media` in the working folder, for example `./acme-assistant-media`. To use another folder, add `--media-dir <dir>`:
194
195```bash
196npx tsx cli.ts -p "Draw a logo for acme" --media-dir ./out
197```
198
199- Line mode prints `[image saved: <path>]` for each file.
200- `-p` prints the same line on stderr, so stdout keeps only the answer.
201- `-p --output ndjson` adds the saved `path` to the value of the `harness.media` event.
202
203The CLI never replaces a file. If the name is taken, it adds `-1`, `-2`, and so on to the new name.
204
205## Run your own screen
206
207Line mode prints plain lines. For a full screen with your own layout, pass `ui` to `runCli`. It works with any TUI library, for example Ink, OpenTUI, or blessed.
208
209`ui` gets a ready [session view](./custom-ui) and resolves when the user quits. This entry file starts an Ink screen:
210
211```tsx ignore
212import { render } from 'ink'
213import { runCli } from '@tanstack/ai-harness-cli'
214import { assistant } from './harness'
215import { Screen } from './screen'
216
217process.exitCode = await runCli(assistant, {
218 ui: async (view) => {
219 await render(<Screen view={view} />).waitUntilExit()
220 },
221})
222```
223
224- `ui` runs only in an interactive terminal. Piped input uses line mode. `-p`, `--acp`, `--mcp`, and `--serve` do not use `ui`.
225- When `ui` resolves, the CLI disposes the view and `runCli` returns.
226- In a reopened thread, a saved child agent shows only its card. To show what it wrote, call `view.loadAgent({ id })`, for example when the screen draws the card. See [Show subagents](./custom-ui#show-subagents).
227- To write `Screen`, read [Build your own UI](./custom-ui).
228
229For an Ink screen with approvals, questions, sign-ins, and child agents, copy [`examples/harness-cli/src/tui.tsx`](https://github.com/TanStack/ai/blob/main/examples/harness-cli/src/tui.tsx).
230
231## What you have now
232
233- One entry file that runs your harness as a terminal app, a script step, an editor agent, an MCP server, or an HTTP server.
234- A CLI that uses your durable host and runs as your user.
235- A Ctrl+C that cancels the turn first, then stops with exit code 130.
236- Exit code 3 from `-p` when a turn waits for a person, and one line for each answer in line mode.
237- Your own terminal screen on the same session, with any TUI library.
238- Files that you send with `@path`, and a folder with the media that your agents make.
239
240Next: keep long turns alive through crashes with [durable sessions](./durable-sessions).