# OpenCode

> Add Alexein to OpenCode with one command and sign in, so it searches your memory and saves what it learns in every project.

OpenCode searches your Alexein memory before it answers, and saves what you tell it and what you build together.
This page is for OpenCode 2 or newer.

## 1. Add Alexein

**Your own memory**

1. Run this command:

   ```bash title="Terminal"
   opencode mcp add alexein --url https://api.alexein.ai/v1/mcp --global
   ```

   `--global` adds Alexein for you in every project. Leave it out to add it to this project only.
   `npx @alexein/cli connect --only opencode` runs the same command for you, and offers the sign-in after it.
2. Sign in. In a terminal, run:

   ```bash title="Terminal"
   opencode mcp auth alexein
   ```

   Your browser opens Alexein's sign-in. In OpenCode, you can also run `/mcps`, choose `alexein` and sign in.
3. Sign in, keep **Read and save** so OpenCode can save, and select **Allow**. There is no key to copy.

Or add the `alexein` entry to OpenCode's settings yourself, under `mcp.servers`, and sign in as above:

```json title="~/.config/opencode/opencode.json"
{
  "mcp": {
    "servers": {
      "alexein": {
        "type": "remote",
        "url": "https://api.alexein.ai/v1/mcp"
      }
    }
  }
}
```

Over SSH or in a container, the sign-in asks you to paste the address your browser ends on after **Allow**. To
disconnect, run `opencode mcp logout alexein`, or select **Disconnect** on it in
[My memory › AI assistants](https://memory.alexein.ai/memory/assistants).

### Or with a key

For scripts and CI, where nobody is there to sign in, use a personal key instead.

1. Open [My memory › Personal keys](https://memory.alexein.ai/memory/keys) and select **Create key**. Label it `OpenCode`,
   choose **Read and write** so OpenCode can save, and copy the key. It is shown once.
2. Put it in an environment variable, in the shell profile OpenCode starts from (`~/.zshrc`, `~/.bashrc`):

```bash title="~/.zshrc"
export ALEXEIN_API_KEY="ax_..."
```

3. Add the `alexein` entry with the key's header, and sign-in turned off:

```json title="~/.config/opencode/opencode.json"
{
  "mcp": {
    "servers": {
      "alexein": {
        "type": "remote",
        "url": "https://api.alexein.ai/v1/mcp",
        "oauth": false,
        "headers": {
          "Authorization": "Bearer {env:ALEXEIN_API_KEY}",
          "X-Alexein-User": "me"
        }
      }
    }
  }
}
```

OpenCode fills in `{env:ALEXEIN_API_KEY}` from the environment, so the key is not written into the file.
`X-Alexein-User: me` names your own memory.
**App user**

This tab is for the [Console](/developer-platform), at app.alexein.ai, not My memory. To give OpenCode the memory of one
user of an app you build on Alexein, use an API key from the Console's [API keys](https://app.alexein.ai/console/keys) page, and name the [app user](/concepts/app-users):

```json title="opencode.json"
{
  "mcp": {
    "servers": {
      "alexein": {
        "type": "remote",
        "url": "https://api.alexein.ai/v1/mcp",
        "oauth": false,
        "headers": {
          "Authorization": "Bearer {env:ALEXEIN_API_KEY}",
          "X-Alexein-User": "user_42"
        }
      }
    }
  }
}
```

Set `ALEXEIN_API_KEY` to that API key. To see what it saved, open the app user under
[App users](https://app.alexein.ai/console/users).

## 2. Teach it to use memory

Add these lines to `~/.config/opencode/AGENTS.md`, the instructions OpenCode reads in every project. Create the
file if it does not exist.

```text title="~/.config/opencode/AGENTS.md"
Use Alexein as my memory.
Before you answer anything about me, my work or my plans, search it with memory_search.
When I tell you something new, save it with memory_add, without asking.
Save small details too: preferences, people, places, dates and decisions.
Never save passwords, keys or other secrets.
```

Start a new session for OpenCode to read it.

## 3. Check it works

1. Run `opencode mcp list` in a terminal, or `/mcps` in OpenCode. `alexein` is listed.
2. Ask: "What do you remember about me?" OpenCode calls `memory_search`.
3. Tell it something new about you. Then open [My memory](https://memory.alexein.ai/memory): it is
   there within a few minutes.

## Troubleshooting

- **`alexein` needs sign-in, or answers `401`.** Run `opencode mcp auth alexein` again. With a key:
  `ALEXEIN_API_KEY` is not set in the shell OpenCode started from. Set it and start OpenCode again.
- **The entry is ignored.** On OpenCode 1, update OpenCode; the entry above is OpenCode 2's.
- **`memory_add` is missing.** You chose **Read only** when you signed in, or the key is **Read**. Run
  `opencode mcp logout alexein` and sign in again with **Read and save**, or create a **Read and write** key.

More in [Troubleshooting](/mcp/troubleshooting).

