# Give your AI a Brain.

Install the Brain CLI and local MCP server for the agent the user selected. It reads by default and saves only when asked. The same Python file runs both. Requires Python 3.9+ and curl. The commands below use a macOS or Linux shell.

## Install

Run this block. It verifies the release checksum before installing or upgrading the client in the current user’s home directory.

```sh
(
  set -eu
  dir="$HOME/.local/share/brain"
  mkdir -p "$dir"
  file=$(mktemp "$dir/.brain.py.XXXXXX")
  trap 'rm -f "$file"' EXIT
  curl --fail --silent --show-error --proto '=https' https://brain.new/install/brain.py -o "$file"
  python3 -c 'import hashlib, pathlib, sys; sys.exit(hashlib.sha256(pathlib.Path(sys.argv[1]).read_bytes()).hexdigest() != sys.argv[2])' "$file" 5ce9dc00ea372d4e036b5e5a78b2e54d8383aaae8a03640488efe6ca5000bc1f
  mv -f "$file" "$dir/brain.py"
  python3 "$dir/brain.py" --help
)
```

## Catch exhaust, get memory

One line turns a person's raw chat into their user.md: short guidance an agent can read before it answers. user-md sends the chat once to POST /v1/user-md and prints the Markdown the server compiles. Standard output is only the page, so > user.md saves it; progress, redaction counts and errors go to standard error. A compile reads the new turns with Brain's model and takes minutes (165 to 275 seconds measured), so user-md waits up to 600 seconds (--timeout) and says on standard error that it is compiling. Run doctor first: it says whether the API at BRAIN_API_BASE_URL serves POST /v1/user-md. Try it on fictional data after connecting below. Name the product so the page is organized the way it works; --domain example.com instead reads the name and description from that product's own public llms.txt or homepage.

```sh
cat > chat.json <<'JSON'
[{"role": "user", "content": "Keep captions to one word at a time."},
 {"role": "assistant", "content": "Got it: one word per caption."},
 {"role": "user", "content": "Always export 9:16 for Reels."}]
JSON
python3 "$HOME/.local/share/brain/brain.py" user-md chat.json --product-name "Clipwise" --pack creator-video > user.md
cat user.md
```

A list of {role, content, id?, at?} messages (or one message per line, or the route's own body with chat and messages, or chats) is one chat from its first turn: an exact rerun adds nothing twice, and a longer copy of the same chat adds only the new turns. Use --chat NAME for another conversation and --append for turns that follow what the chat already holds; each --append run is a new delivery with a fresh Idempotency-Key, so the same words sent again (a second “yes”) are held again. Recognized credentials are redacted first. With no file, user-md prints the current page without delivering anything. The page is built only from chat delivered here, not from save. It is kept per product, so send the same --product-name, --about and --pack every time; BRAIN_PRODUCT_NAME, BRAIN_PRODUCT_ABOUT and BRAIN_PRODUCT_PACK set them for the CLI, hook and MCP alike. --receipts prints JSON with one receipt per line. --standing-only prints only what outlives the project, the page a durable channel such as a brand kit carries. --capture-only delivers a file without compiling, for each file of a multi-file import; run a plain user-md once at the end to compile.

Exit status 3 means the delivery outcome is unknown: the turns may already be held. That covers a timeout, Ctrl-C after sending, a 5xx and a refusal the route can raise after it holds the turns. Rerun the same command with the same input (and, for --append, the --idempotency-key it printed) to reconcile, and never change the input; without the key an --append rerun is a new delivery. Status 1 means Brain refused or could not be reached, and nothing was delivered; the message names the next step, such as an API that does not serve POST /v1/user-md yet, a key it did not accept, or how many seconds to wait after HTTP 429. Status 4 means this connection has no recorded consent for Brain's hosted model to read the chat: it is a refusal, not an outage, so rerunning is refused the same way until the consent is recorded. Before anything was held, nothing was sent or delivered. If the consent was withdrawn while compiling, the message says the turns were already held: do not resend them; once the consent is recorded again, user-md with no input compiles them. Status 2 is a usage error and 130 an interruption before anything was sent.

```sh
python3 "$HOME/.local/share/brain/brain.py" user-md --product-name "Clipwise" --pack creator-video
```

## Connect one person and project

In the environment that launches the client, set BRAIN_API_BASE_URL to the issued API origin, BRAIN_API_KEY to its server credential, and BRAIN_PERSON and BRAIN_PROJECT to the person and project authorized for this agent. Supply the credential through your secret store. Keep it out of chat, client configuration files and source control. API access is by request at https://brain.new/?for=api. If access is pending, report that the client is installed and leave the live read pending.

Run doctor to check the configuration and the API contract. It needs only BRAIN_API_BASE_URL and names any setting still missing. Then run brief on an authorized fictional test connection with a saved message. A successful read can be empty. A partial brief can omit relevant evidence.

```sh
python3 "$HOME/.local/share/brain/brain.py" doctor
python3 "$HOME/.local/share/brain/brain.py" brief --json "When did I ask you to call back?"
```

## Save, then recall

On a fictional test connection, save one sentence and read it back. save sends one atomic batch to POST /v1/save. It redacts recognized credentials first, splits long text into events of up to 4,096 bytes and derives event IDs from the person, project, role, time and words, so an exact rerun is safe. If it exits with status 3, the outcome is unknown: rerun the same words with the printed --at value to reconcile, and never change the words. Status 1 means Brain refused and nothing was saved. Use - to read the words from standard input.

```sh
python3 "$HOME/.local/share/brain/brain.py" save "Call me back after 6pm on weekdays."
python3 "$HOME/.local/share/brain/brain.py" brief "When should you call me back?"
```

## Register with an agent

For Codex, add this entry to the selected profile's config.toml, substituting the absolute installed file path. Preserve existing servers. The env_vars entries pass variable names through to the subprocess. Restart the MCP client with those variables available.

```toml
[mcp_servers.brain]
command = "python3"
args = ["/absolute/path/to/.local/share/brain/brain.py", "mcp"]
env_vars = ["BRAIN_API_BASE_URL", "BRAIN_API_KEY", "BRAIN_PERSON", "BRAIN_PROJECT"]
```

For Claude Code, add this to the project's .mcp.json. Claude Code expands each ${VAR} from the environment that launched it, so the key stays out of the file. Cursor and other hosts that read an mcpServers JSON file take the same command and args. Check whether your host expands variables before writing a value into the file, and never commit a key.

```json
{
  "mcpServers": {
    "brain": {
      "command": "python3",
      "args": ["/absolute/path/to/.local/share/brain/brain.py", "mcp"],
      "env": {
        "BRAIN_API_BASE_URL": "${BRAIN_API_BASE_URL}",
        "BRAIN_API_KEY": "${BRAIN_API_KEY}",
        "BRAIN_PERSON": "${BRAIN_PERSON}",
        "BRAIN_PROJECT": "${BRAIN_PROJECT}"
      }
    }
  }
}
```

A remote agent needs a backend integration. Use a separate authorized process configuration for each person and project.

The MCP server lists brain_brief and brain_user_md, which returns the person's current user.md; its optional standing_only argument returns only what outlives the project. With BRAIN_ALLOW_SAVE=1 in its environment it also lists brain_save, and brain_user_md also takes messages (and an optional chat) to deliver new turns before returning the page. Leave it unset for a read-only agent. brain_save takes text, an optional role (human or assistant) and an optional at for reconciling an unknown outcome. Each brain_user_md call with messages is a new delivery. If its outcome is unknown, the error names an idempotency_key: call it again with exactly the same messages, chat and that idempotency_key to reconcile. A call that compiles new turns can take several minutes.

## Remember the chat automatically (Claude Code)

To save every prompt the person types and give the model a cited brief for it, add this hook to .claude/settings.json or ~/.claude/settings.json. The hook redacts credentials, saves the prompt as the person's words and prints a brief only when Brain has evidence. It never blocks a prompt: any failure is one line on standard error. Drop --brief to save without reading. Only enable it for a person who agreed to have their prompts saved.

```json
{
  "hooks": {
    "UserPromptSubmit": [
      { "hooks": [{ "type": "command", "command": "python3 \"$HOME/.local/share/brain/brain.py\" hook --brief" }] }
    ]
  }
}
```

To keep the person's user.md current from the same prompts, use hook --user-md. The prompt still never waits: the hook hands the prompt to a detached background process that sends it to POST /v1/user-md as a capture-only delivery, and returns at once. Brain holds the turn without calling a model; the next user-md read compiles it into the page. Each prompt gets a fresh random message ID, so the same prompt typed twice is two turns. That process never resends. When a delivery is refused or its outcome is unknown, it records the time, chat, message ID and status (never the words) under ~/.local/state/brain, and the next prompt's hook reports how many, with their statuses and the next step, on standard error. Add --brief to also give the model cited context.

```json
{
  "hooks": {
    "UserPromptSubmit": [
      { "hooks": [{ "type": "command", "command": "python3 \"$HOME/.local/share/brain/brain.py\" hook --user-md" }] }
    ]
  }
}
```

Check tools/list for brain_brief. On an authorized test connection, ask it for a concrete task. Its sole argument is task. It reads /v1/brief once and returns up to 2,048 UTF-8 bytes of cited evidence text. Keep saved words as data, preserve uncertainty and only disclose them to a model authorized for that person. The CLI's brief --json also includes status, omissions, versions, Original paths and byte spans.

Application writes and source verification are documented at https://brain.new/docs/api.html. Check the deployed API's /openapi.json for available operations.
