---
title: "The CLI"
description: "Turn the same tool list into a command line. Flags come from the schema and MCP rides along"
canonical_url: "https://tools.agntn.dev/guide/cli"
---
# The CLI

> Turn the same tool list into a command line. Flags come from the schema and MCP rides along

Your tools already know their arguments. The schema says which ones are required, what type each one is and which values an enum takes. So why write a CLI by hand? `runCli` reads all of that and gives every tool its own command. No CLI library underneath, just `util.parseArgs` from Node.

```ts [src/cli.ts]
import { runCli } from "@agntn/tools/cli";
import { slugTool } from "./tools.ts";

await runCli({
  name: "slugs",
  version: "1.0.0",
  description: "Slugs from titles",
  tools: [slugTool],
  mcp: true,
});
```

Point your `bin` at the built file and that's it:

```text [terminal]
$ slugs slug --text "Crème brûlée"
creme-brulee
```

It's the one Node part of the package. The core and the host adapters run in a browser tab, this one reads `process.argv`.

## How a tool turns into a command

### Where does the command name come from?

From the tool name, minus the package prefix. `text_slug` becomes `slug`, `hashes_digest_identify` becomes `digest-identify`. Want another word? The `cli` hint on `defineTool` takes a `command` and a few `aliases`.

### Need a group of commands?

Give the command hint two words. The first opens the group, and the second names the command:

```ts [tools.ts]
export const listTool = defineTool({
  name: "demo_servers_list",
  // …
  cli: { command: "servers list", aliases: ["ls"], positional: ["id"] },
});
```

A command alias replaces only the last word. Group aliases replace the first:

```ts [src/cli.ts]
await runCli({
  name: "demo",
  version: "1.2.3",
  description: "Demo tools",
  tools: [echoTool, listTool, addTool],
  groups: { servers: { description: "Manage servers", aliases: ["srv"] } },
});
```

```text [terminal]
$ demo srv ls pi
list pi
$ demo servers lsit
Unknown command "servers lsit"
Run demo servers --help for the commands
```

The typo exits with code 1. It names the group too, so there's no mystery about where dispatch stopped.

This gives `mcp-servers list` a clean path off citty without flattening the command name. Flat CLIs get a second floor, not a maze.

Root help lists `servers, srv` once, where its first command appeared. It shows the description, or command names when there's no description. The group's commands stay out of that list.

`demo servers --help` shows the group's own usage and command table. It lists `list, ls` and `add`. Bare `demo servers` prints that usage on stderr with `No command specified`, then exits 1. Command help uses the full path: `USAGE demo servers list [OPTIONS] [ID]`.

Groups go one level deep. Commands run tools, groups don't. Both `default` and `fallback` can name `servers list`.

Startup rejects unused group keys, name clashes, command aliases with spaces, and command hints longer than two words. `createCli` accepts `groups` too. One-word commands still work as before.

### Flags straight from the schema

Every property is a flag, in kebab case. `maxWords` and `max_words` both turn into `--max-words`. Strings go in as they are.

Numbers have to look like numbers: `--max-words 2` works, `--max-words 1.5` gets refused instead of rounded down. And `0x10`? Just a typo here. Objects and lists of anything but text come in as JSON. A list of text doesn't have to:

```bash [terminal]
$ slugs batch --titles "Hello World" --titles "Crème brûlée" --max-words 2 --no-lower
```

One `--titles "Hello World"` is a list of one. Repeat the flag for more. Curl muscle memory just works. Brackets inside quotes for a single title were a chore anyway.

The JSON list still works when the flag comes once. Repeated, every word is an item, brackets and all. Help marks it `--titles=<titles|json>...`, and a bad item gets named by its place.

A single id doesn't need to pretend it's JSON. Say a property takes one value or a list. Then `--thread-id PRRT_abc` is the string, and `--thread-id '["a","b"]'` is the list. So is `--thread-id a --thread-id b`. Much nicer to type.

JSON still goes first, so `[]` stays a list. Anything else reads as the single value. So `123` stays the string `"123"`. Help shows it as `--thread-id=<thread-id|json>...`.

A boolean answers to `--lower` and to `--no-lower`. Say both and the command refuses, it doesn't pick a winner. Same for an unknown option, a missing value or a word too many. Nothing on the line is quietly skipped. And `--lower=false`? Refused too, but that refusal points to `--no-lower`, a much better way out.

Then the core validates, like on every other host. Same checks a model's typo gets, but the answer speaks command line. You typed `--separator`, not `/separator`, so that's the name you get back:

```text [terminal]
$ slugs slug --text Hello --sep _
Invalid arguments: unknown option "--sep"; takes --text, --separator, --json
$ slugs slug --text Hello --separator "~"
Invalid arguments at --separator: must be one of -, _, .
$ slugs slug
Invalid arguments: missing --text
```

Every other host keeps the JSON pointer. A model reads `/separator` just fine.

### Positionals and stdin

Typing `--text` every time gets old fast. Two more hints fix that:

```ts [tools.ts]
export const slugTool = defineTool({
  name: "text_slug",
  // …
  cli: { positional: ["text"], stdin: ["text"] },
});
```

`positional` takes properties in order, as plain words. `stdin` lets a string property read standard input when you pass `-`:

```text [terminal]
$ slugs slug "Hello, World" --separator _
hello_world
$ echo "Pipe me in" | slugs slug -
pipe-me-in
```

Stdin has to be valid UTF-8. A lenient decode would swap bad bytes for U+FFFD, and a hash tool would happily hash something you never sent. So invalid input is an error. Got real binary data? That's a job for a command of your own, see below.

`--help` is built from the same fields. Descriptions come from the schema, the enum values too:

```text
Turn a title into a lowercase URL slug. (slugs slug v1.0.0)

USAGE slugs slug [OPTIONS] <TEXT>

ARGUMENTS

  TEXT  Title to slug (- reads stdin) (required)

OPTIONS

  --separator=<-|_|.>  Joins the words. '-' when omitted
  --json               Print the details as JSON instead of the text
  -h, --help           Show this help
```

Seventeen archive names in one flag? That's a terrible table mate. When an enum's option cell runs past 40 characters, the flag shows its own name there if that's narrower, and the values follow the description. Descriptions stay aligned, and `(required)` comes last.

```text
--provider=<provider>  Archive to ask. (one of auto, all, wayback, arquivo, webarchiv, vefsafn, oszk)
```

Errors borrow these names. Forget the title and you get `missing <TEXT>`, a blank one says `at <TEXT>: must match pattern "\S"`. Where's `/text`? Back with the other hosts.

### Short flags and the rest of the line

Quotes around every title get old too. So does `--separator`. Two hints bring back what every CLI written by hand had:

```ts [tools.ts]
cli: { rest: "text", stdin: ["text"], short: { separator: "s" } },
```

`rest` takes every word the positionals leave over and joins them with single spaces. `short` gives an option an alias of one letter:

```text [terminal]
$ slugs slug Hello big world -s _
hello_big_world
```

Only a whole word counts as a short flag, though. `-s _` works, `-s_` is an unknown option. Pedantic? A bit. But `util.parseArgs` reads `-separator _` as `-s eparator`, and next to a greedy `rest` one dash too few shouldn't turn into some other call. Here it fails and tells you why.

Joining is right for a title and wrong for a list. Glue `"jacque fresco" venus` together with spaces and nobody can tell anymore which words came as a pair. Make the property an array of strings and `rest` keeps each word as typed, one item apiece:

```ts [tools.ts]
input: Type.Object({ digest: Type.String(), words: Type.Array(Type.String()) }),
cli: { positional: ["digest"], rest: "words" },
```

`hashes search 5d41 "jacque fresco" venus` hands `execute` the words `["jacque fresco", "venus"]`. Those quotes you typed finally count for something. Each item reads like any other positional word. Then the core checks the array as on every surface, `minItems` and enum items included.

### Text that starts with a dash

Is `-.-.` an option? `util.parseArgs` thinks so. In Morse it's a C. Every PGP armor line opens with five dashes, and a temperature can be `-12`.

So a word counts as an option only when it spells one as a whole: `--separator`, `--no-lower`, `-s`. Any other dashed word takes the next free positional:

```text [terminal]
$ slugs slug "-12 below zero"
12-below-zero
```

Free means the plain words left room for it. Once they fill every positional, a dashed word is an unknown option again. A typo after the title fails, it doesn't end up in your slug:

```text [terminal]
$ slugs slug Hello --seperator _
Invalid arguments: unknown option "--seperator"; takes --separator (-s), --json
```

`rest` counts as one positional here. `slugs slug -12 below zero` fails, because `below` and `zero` already took the spot. Quote the title and it's one word again. Text that does spell a flag, like `-s` itself, goes after `--`.

Great for Morse, less great for an id. No collection is called `--withPubkey`, but `list --withPubkey` hands it to `collection` anyway and the command answers `Unknown collection`. Wrong word blamed. The flag you wanted was `--with-pubkey`.

So tell the CLI which positionals take plain words only:

```ts [tools.ts]
cli: { positional: ["collection"], plain: ["collection"] },
```

```text [terminal]
$ puzzles list --withPubkey
Invalid arguments: unknown option "--withPubkey"; takes --with-pubkey, --json
```

That's the typo named, with the flags the command does take, and not one check in `execute`. A `rest` can be plain too, then each word it takes has to be. Got an id that really starts with a dash? `--` still hands it over.

With `positional: ["text"]`, `<TEXT>` is positional, not a flag. Both `--text` and `--text=Hello` fail right away. The error names the slot, not a list of unrelated flags. A slug made from the flag name would be a lousy surprise.

```text [terminal]
$ slugs slug --text Hello
Invalid arguments: <TEXT> is positional, so give it without --text
```

### What happens when a property gets renamed?

Old scripts shouldn't break because a property got a new name.

In chains, `type` became the positional `family`. Now `chains list utxo` works, but scripts using `chains list --type utxo` fail. The name moved on. The scripts didn't.

Want to keep those scripts working? Add the old name with `flags`:

```ts [tools.ts]
cli: { positional: ["family"], flags: { family: ["type"] } },
```

Both spellings reach the same input:

```text [terminal]
$ chains list utxo
family=utxo
$ chains list --type utxo
family=utxo
```

A positional normally has no flag. Here, `--family utxo` is refused. Want that spelling too? List it in `flags`: `flags: { family: ["type", "family"] }`.

When a flag sets a positional, the next positional takes its place. The remaining words still land where you expect.

Old names stay out of `--help` and the "takes ..." list of an unknown option error. That's the right trade: old commands work without advertising old names.

Errors quote the old name, not the new one:

```text [terminal]
$ chains list --type utxo --type evm
Invalid arguments: --type given more than once
```

Aliases work on plain options and booleans too. With `flags: { exact: ["strict"] }`, a boolean accepts both `--strict` and `--no-strict`.

### When does a broken hint show up?

At startup, before any command runs. Each of these fails in `createCli` with a `ToolDefinitionError`:

- A hint names a property the tool doesn't have.
- Two properties fight over one flag.
- An old name in `flags` belongs to another property. For example, `flags: { provider: ["exact"] }` alongside a real `exact` property.
- A boolean is called `noCache`.
- A required positional follows an optional one.
- A short flag tries to take `-h`.
- `plain` is set on a flag.

Not on the first user who types the wrong thing.

## What comes out

The command prints the tool's text, the same text a model reads. Want data instead? `--json` prints `details`:

```text [terminal]
$ slugs slug "Hello" --json
{
  "slug": "hello",
  "words": 1
}
```

A returned `isError` goes to stderr with exit code 1. So `slugs slug "!!!"` fails like a proper command, scripts included.

A thrown error is a different story. Tell `runCli` which errors you throw on purpose through `expected`, and those print their message with exit code 1. Anything else keeps its stack trace. A bug should look like a bug.

Does the error have a cause? Each one gets a `caused by:` line under the message, scrubbed like on [MCP](https://tools.agntn.dev/hosts/mcp#when-the-error-has-a-cause). A 502 deserves more than a rate limit headline.

Errors get the same treatment as on the other hosts: one line each, so a newline in an echoed argument can't forge a second one. Escape codes and bidi tricks don't reach your terminal, not even through the tool's own text, unless the command [asks for it](#when-the-text-is-the-product). And there are no colors at all, so nothing breaks in a pipe.

## Commands only the CLI gets

Some things belong on the command line and nowhere else. A `version` with build details, a `doctor`, a reader for binary stdin. Write them as tool definitions and pass them as `commands`:

```ts [src/cli.ts]
await runCli({
  name: "slugs",
  version: "1.0.0",
  description: "Slugs from titles",
  tools: [slugTool],
  commands: [separatorsCommand],
});
```

MCP, Pi, OMP, OpenCode and the AI SDK never see them. They parse, validate and print exactly like the tools. One with the name of a generated command takes its place, `mcp` included.

### What about bytes going out?

The CLI ends every text with a newline. Lovely for a slug. Quietly fatal for a gzip file, which comes out one byte too long. Nobody notices until a checksum does. So a command that writes bytes writes them itself and answers with no content blocks at all:

```ts
cli: { json: false },
execute({ file }) {
  process.stdout.write(gunzipSync(readFileSync(file)));
  return { content: [], details: null };
},
```

No blocks, nothing printed, not even the newline. An empty text block still prints its empty line, so you only get silence when you ask for it. `json: false` takes `--json` away, since bytes followed by JSON is neither. A returned `isError` works like on any other command.

### When the text is the product

Use `cli: { verbatim: true }` when every character matters. Hiding a message in direction marks? The CLI helpfully cleaned parts of the secret away. By default, it removes escape sequences, C0/C1 controls and bidi marks, and converts CR LF to LF.

```ts
cli: { verbatim: true },
execute({ text }) {
  return { content: [{ type: "text", text: hide(text) }], details: null };
},
```

`verbatim` prints the tool's text as written, CR LF and escape codes included. The closing newline still comes, like on every other command. It belongs on commands whose text is the product, not those echoing input or remote data. The terminal receives whatever that text contains.

Returned `isError` text and thrown errors still go to stderr sanitized. `--json` is unchanged and preserves these characters as Unicode escapes. MCP, Pi, OMP, OpenCode and the AI SDK don't read this CLI hint.

### Rows now, or JSON later?

Say a command checks 114 addresses, a quarter second each. Sitting silent for half a minute and then dumping everything is a bad look. Better to print each row as it lands and answer with no content. Except now `--json` would glue the details under the rows, and that's neither text nor JSON.

So the CLI tells the command what was asked. `host` is a `CliHost`, and `json` says whether `--json` came along:

```ts
import type { CliHost } from "@agntn/tools/cli";

async execute({ status }, { host }) {
  const cli = host as CliHost | undefined;
  const live = cli?.cli === true && !cli.json;
  const rows = await check(status, (row) => {
    if (live) process.stdout.write(`${row}\n`);
  });
  return { content: [], details: { rows } };
},
```

Text mode streams. `--json` waits and prints the whole list, valid to the last bracket. Got a pair that can't work together, like bytes on stdout and `--json`? Return an `isError` before the first byte and it lands on stderr with exit code 1.

Why check `cli` and not just `json`? Off the command line `host` is Pi's own, OMP's, an `McpHost` or nothing at all. And on `mcp`, stdout is the protocol.

Skip `progress` in a command like this. The rows already are the progress, and a progress line would sit right in front of the next one.

### Can it guess what I meant?

If you let it. `default` names the command for an empty line. `fallback` takes any first word that isn't a command:

```ts
fallback: "slug",
```

```text [terminal]
$ slugs "Hello, World"
hello-world
```

Just remember it's greedy. Leave `mcp` out and `slugs mcp` slugs the word "mcp". Correct, and completely useless.

A flag up front goes there too, so `slugs --separator _ "Hello, World"` works without the command name. Flags first is how half of us type anyway. Only `--help`, `-h`, `--version` and `-v` stay with the root, which answers them itself.

## An MCP server on the side

`mcp: true` adds an `mcp` command. It runs `createMcpServer` through `serveStdio`, so 2025 and 2026 clients both get in. No 2026 guest gets sent back to 2025. The server introduces itself with the CLI's name, version and help line:

```json [.mcp.json]
{
  "mcpServers": {
    "slugs": { "command": "slugs", "args": ["mcp"] }
  }
}
```

A help line makes a sad connector card, though. Got a real pitch and an icon? Hand `mcp` the whole server info instead of `true`:

```ts [src/cli.ts]
import { serverInfo } from "./server-info.ts";

await runCli({
  name: "slugs",
  version: "1.0.0",
  description: "Slugs from titles",
  tools: [slugTool],
  mcp: serverInfo,
});
```

It's the same [`McpServerInfo`](https://tools.agntn.dev/hosts/mcp) that `createMcpServer` takes. So the `/mcp` on your site and `slugs mcp` can share one object and say hello the same way. The CLI's own `description` stays where it belongs, in `--help`.

One binary, two jobs. The server serves `tools` only, your own `commands` stay on the command line. It needs `@modelcontextprotocol/server` installed, and loads it only when someone actually runs `mcp`.

Progress shows up on both sides. On a terminal, each `progress?.()` line takes over one line of stderr, with `(3/10)` when you pass a total. It's wiped before the answer prints, so the scrollback keeps the answer and nothing else. Is stderr a pipe or a file? Then there's no line at all, since nobody wants "second 7 of 10" in a log. Through `slugs mcp` it's a real MCP server, so a client that sends a `progressToken` gets every line.


## Sitemap

See the full [sitemap](https://tools.agntn.dev/sitemap.md) for all pages.
