# CLI

The @mojimoto/cli commands - set up coding agents, download TypeScript types and scaffold a Next.js preview route.

`@mojimoto/cli` is a small command-line tool for a site that reads from Mojimoto. It has three commands:

| Command | What it does |
| --- | --- |
| `init` | Sets up Claude Code, Cursor and Codex to work with your project: a skill, your content model and the MCP server. |
| `types` | Downloads generated TypeScript types for your content model. |
| `add-preview` | Scaffolds the Next.js route Mojimoto uses to preview one section at a time. |

It talks to Mojimoto over the same HTTP API as everything else, so it needs a project id and a token, never a login.

## Run it

Nothing to install. Run it with `npx` from the root of your site's repository:

```bash
npx @mojimoto/cli types \
  --endpoint https://mojimoto.com/api/v1 \
  --project acme \
  --token mjmt_your_read_token
```

```text
✓ Wrote mojimoto.generated.ts for project "acme" (83 lines).
```

It needs Node.js 18 or later.

To call it from your package scripts, add it as a dev dependency. The binary is called `mojimoto`:

```bash
npm install --save-dev @mojimoto/cli
```

```json package.json
{
  "scripts": {
    "cms:types": "mojimoto types -o types/mojimoto.ts"
  }
}
```

## Options and environment variables

The CLI has no config file and does not read a `.env` file. Each connection option is a flag or an environment variable, and the flag wins when both are set.

| Flag | Environment variable | Used by | Description |
| --- | --- | --- | --- |
| `--endpoint <url>` | `MOJIMOTO_ENDPOINT` | `types`, `init` | The API base, `https://mojimoto.com/api/v1`. Required for `types`. `init` uses that address when you leave it out. |
| `--project <id>` | `MOJIMOTO_PROJECT` | `types`, `init` | Your project's id. Required. |
| `--token <token>` | `MOJIMOTO_TOKEN` | `types`, `init` | A token that can read. The public read token is enough. Required for `types`, optional for `init`. |

With the three variables exported, a command needs no connection flags:

```bash
export MOJIMOTO_ENDPOINT=https://mojimoto.com/api/v1
export MOJIMOTO_PROJECT=acme
export MOJIMOTO_TOKEN=mjmt_your_read_token

npx @mojimoto/cli types -o types/mojimoto.ts
```

Two flags work everywhere:

| Flag | Description |
| --- | --- |
| `-h`, `--help` | Prints the usage text. |
| `-v`, `--version` | Prints the CLI version. |

Every command exits with `0` when it worked and `1` when it did not. Running the CLI with no command prints the usage text and exits with `1`.

## init

Sets up coding agents in the current directory. Run it in your front end repository.

```bash
npx @mojimoto/cli init --project acme --token mjmt_your_read_token
```

```text
✓ Wrote .claude/skills/mojimoto/SKILL.md
✓ Wrote .claude/skills/mojimoto/reference/content-model.md
✓ Updated CLAUDE.md
✓ Added the mojimoto server to .mcp.json
✓ Wrote .cursor/rules/mojimoto.mdc
✓ Added the mojimoto server to .cursor/mcp.json
✓ Updated AGENTS.md

Next steps:
  1. In Mojimoto, open the project's API keys page and create a token with write access.
  2. Export it before starting your agent: export MOJIMOTO_WRITE_TOKEN=<token>
     The MCP config reads it from the environment, so it never sits in your repo.
  3. MCP server: https://mojimoto.com/mcp/acme
```

| Flag | When left out | Description |
| --- | --- | --- |
| `--project <id>` | `MOJIMOTO_PROJECT` | Required. The project to set up. |
| `--token <token>` | `MOJIMOTO_TOKEN` | A read token, used once to download your content model. Without one, the content model file is a placeholder. |
| `--agent <names>` | `all` | Which agents to set up: `claude`, `cursor`, `codex`, a comma-separated list such as `claude,cursor`, or `all`. |
| `--endpoint <url>` | `MOJIMOTO_ENDPOINT`, then `https://mojimoto.com/api/v1` | The API base. |
| `--force` | off | Overwrite a skill or rule file that already exists. |

### What it writes

| File | Written for | Contents |
| --- | --- | --- |
| `.claude/skills/mojimoto/SKILL.md` | `claude` | A skill that tells the agent how to read, write and model content in this project. |
| `.claude/skills/mojimoto/reference/content-model.md` | `claude` | Your project's content model, as the generated TypeScript types. |
| `CLAUDE.md` | `claude` | A short note pointing the agent at the skill and the MCP server. |
| `.mcp.json` | `claude` | The project's MCP server. |
| `.cursor/rules/mojimoto.mdc` | `cursor` | The same guidance as the skill, as a Cursor rule. |
| `.cursor/mcp.json` | `cursor` | The project's MCP server. |
| `AGENTS.md` | `codex` | The same short note as `CLAUDE.md`. |

No token is written to any of these files. The read token is only used to download the content model. The MCP configs name an environment variable, `MOJIMOTO_WRITE_TOKEN`, that you export before you start the agent:

```json .mcp.json
{
  "mcpServers": {
    "mojimoto": {
      "type": "http",
      "url": "https://mojimoto.com/mcp/acme",
      "headers": {
        "Authorization": "Bearer ${MOJIMOTO_WRITE_TOKEN}"
      }
    }
  }
}
```

```json .cursor/mcp.json
{
  "mcpServers": {
    "mojimoto": {
      "url": "https://mojimoto.com/mcp/acme",
      "headers": {
        "Authorization": "Bearer ${env:MOJIMOTO_WRITE_TOKEN}"
      }
    }
  }
}
```

### Running it again

`init` is safe to run more than once:

- An existing `SKILL.md` or Cursor rule is left alone, so your edits survive. The CLI prints `• Skipped .claude/skills/mojimoto/SKILL.md (already exists; pass --force to overwrite).` Pass `--force` to replace it.
- `content-model.md` is rewritten every time a content model is downloaded, so run `init` with `--token` again after you change your model.
- The note in `CLAUDE.md` and `AGENTS.md` sits between `<!-- mojimoto:start -->` and `<!-- mojimoto:end -->`. A second run replaces what is between the markers and leaves the rest of the file as it was.
- The `mojimoto` server is added to `.mcp.json` and `.cursor/mcp.json` next to any servers already there. If one of those files is not valid JSON, the CLI leaves it untouched and tells you to add the server by hand.

### When something is missing

| Situation | What happens |
| --- | --- |
| No `--project` and no `MOJIMOTO_PROJECT` | Prints `Missing --project (or MOJIMOTO_PROJECT): the project slug from its URL in Mojimoto.` and exits with `1`. |
| An agent name it does not know | Prints `Unknown agent(s): emacs. Choose from claude, cursor, codex or all.` and exits with `1`. |
| The content model cannot be downloaded | Prints `• Could not download the content model (401).` with the status it got, then carries on. The content model file becomes a placeholder that sends the agent to the MCP `mojimoto://content-model` resource instead. |

The [Claude Code](/docs/ai/claude-code) page walks through the whole setup, and the [MCP server](/docs/ai/mcp) page lists what the agent can then do.

## types

Downloads the generated TypeScript types for your content model and writes them to a file. `generate-types` is an alias for the same command.

```bash
npx @mojimoto/cli types \
  --endpoint https://mojimoto.com/api/v1 \
  --project acme \
  --token mjmt_your_read_token \
  -o types/mojimoto.ts
```

```text
✓ Wrote types/mojimoto.ts for project "acme" (83 lines).
```

| Flag | When left out | Description |
| --- | --- | --- |
| `--endpoint <url>` | `MOJIMOTO_ENDPOINT` | Required. `types` has no built-in address. |
| `--project <id>` | `MOJIMOTO_PROJECT` | Required. |
| `--token <token>` | `MOJIMOTO_TOKEN` | Required. Any token that can read. |
| `-o`, `--output <file>` | `mojimoto.generated.ts` | Where to write the file, relative to the current directory. Missing folders are created. |

The command calls `GET {endpoint}/{project}/types` with your token and saves the response. What the file contains, and how to use it, is on the [Generated types](/docs/tools/generated-types) page.

When an option is missing, the CLI lists the ones it needs:

```text
Missing required option(s):
  --endpoint (or MOJIMOTO_ENDPOINT)
  --token (or MOJIMOTO_TOKEN)
```

When the API refuses the request, it prints the status and the response body:

```text
Request failed (401 Unauthorized) for https://mojimoto.com/api/v1/acme/types
{"message":"Invalid API token."}
```

## add-preview

Scaffolds a Next.js App Router route that renders one entry on its own, so Mojimoto can take a picture of it for the editor. Run it in a Next.js project that already has `@mojimoto/next` and `@mojimoto/react` installed.

```bash
npx @mojimoto/cli add-preview
```

```text
✓ Wrote app/preview/[type]/[uid]/page.tsx
✓ Wrote cms/sections.tsx

Next steps:
  1. Fill in @/cms/sections with your section components.
  2. Ensure @/lib/cms exports a `cms` delivery client.
  3. Set each model's preview path to "/preview/{type}/{uid}" and the project's preview base URL in Mojimoto.
```

| Flag | Default | Description |
| --- | --- | --- |
| `--app-dir <path>` | `app` | Your Next.js app directory, for example `src/app`. |
| `--client-import <module>` | `@/lib/cms` | The module the page imports your `cms` client from. |
| `--components-import <module>` | `@/cms/sections` | The module the page imports your `components` map from. |
| `--force` | off | Overwrite the two files if they already exist. Without it they are skipped. |

The page it writes:

```tsx app/preview/[type]/[uid]/page.tsx
import { createSlicePreviewPage } from '@mojimoto/next/preview';
import { cms } from '@/lib/cms';
import { components } from '@/cms/sections';

// Renders a single Mojimoto section in isolation so the CMS can screenshot it.
// Point each model's preview path at "/preview/{type}/{uid}".
export default createSlicePreviewPage({ client: cms, components });
```

`cms/sections.tsx` is a starting point for the `components` map: an empty object, typed as `SliceComponents` from `@mojimoto/react`, where each key is a content type's api id and each value is the React component that renders it. It is always written to `cms/sections.tsx` in the current directory, whatever `--components-import` says.

Afterwards, set **Preview path** on each content type to `/preview/{type}/{uid}`, and **Preview base URL** under **Settings** to the address of the site that serves the route. See [Preview](/docs/concepts/preview) and [Next.js](/docs/frameworks/nextjs).

## Unknown commands

Anything else prints an error and exits with `1`:

```text
Unknown command: deploy
Run `mojimoto --help` for usage.
```
