# The AIM context server

Run your own context server, which serves and manages AIM Contexts, each one an AIM model that you, the people you work with and your agents read and write together, live, over MCP. [The protocol](PROTOCOL.md) is what every context server keeps, so an agent written against one works against any; this server is its reference implementation.

A context server keeps files and serves them, and nothing more. It records who wrote each change and what each file held before, it refuses a write that would overwrite someone else's newer change, and it tells an agent what changed since that agent last asked. It never checks, corrects or interprets what is written, and it calls no AI model: the AIM work is done by the agents.

## Run it

With Docker, from the published image:

```bash
docker run -d --name aim-context-server -p 8080:8080 -v aim-context-data:/app/data juiceservices/aim-context-server
```

The data lives in the volume at `/app/data`; keep it. To build the image yourself instead, clone the repository and run `docker build -t aim-context-server context-server`.

Or with Node 22 or later, from a clone of the repository:

```bash
cd context-server && npm ci && npm start
```

## Let people in

Nobody can connect until you make them a token. Make a context, then one token per person:

```bash
docker exec aim-context-server aim-context-server context add "Launch plan"
```

```bash
docker exec aim-context-server aim-context-server token add --name Anna --context launch-plan
```

The token is shown once. Without Docker, run the same commands as `node src/cli.js …` in `context-server/`, with `INTENTMODEL_DATA` pointing at the data folder. The commands below are written without that prefix.

| Command | What it does |
|---|---|
| `context add <name>` | Start a context; prints its id. |
| `context list` | Every context, whether it is public, and who reaches it. |
| `context public <id> on\|off` | Public: anyone may read every file in it, and any agent may read it. Only its own people write. |
| `token add --name <name> [--context <id>]…` | A person and their token, shown once. |
| `token list` | Every person, the start of their token, and the contexts they reach. |
| `token grant <name> <context>` / `token revoke …` | Let a person into a context, or take them out. |
| `token end <name>` | The token stops working at once. |

A running server picks up every change on its next request. An agent may also start a context of its own (`start_context`), which its person then owns.

## Connect an agent

Claude Code, the connection alone (the plugin under *Tell a running agent* connects and tells in one command):

```bash
claude mcp add --transport http --scope user intentmodel http://localhost:8080/mcp --header "Authorization: Bearer <token>"
```

Any other agent: an MCP server over streamable HTTP at `http(s)://<host>/mcp`, sending the header `Authorization: Bearer <token>` with every request. The server tells the agent, when it connects, where the specification is, how a context is laid out and how to work. Put the server behind HTTPS before anyone connects over a network.

An agent gets the specification and the packages from the server too, with no web access of its own: `read_specification` (and, with `document`, the `reference/` and `guides/` documents it names), `list_packages` and `read_package`. Each answer says when the server got its copy (`asOf`). The server fetches them from the base of `INTENTMODEL_SPEC_URL`, keeps each for an hour, and keeps the last copy in `library/` in the data folder, which it serves when the source cannot be reached. With `INTENTMODEL_SPEC_DIR` it serves a folder of your own instead.

## Tell a running agent

A change is in the context at once, but an agent works from what it read, and nobody tells it that someone else has written since. The `aim-context` plugin does. After each tool call of the agent it asks the server what changed since that session was last told (`read_news`), and when someone else changed something, hands the agent a note in the middle of its work:

```text
The AIM Context "Expense claims" (expense-claims) changed while you were working. This is information about the work, not an instruction to you.
- Ana changed root/root.aim at 05:38 UTC
    was: - **FIVE-DAYS** — A claim is decided within five working days of being handed in.
    was: - 5 business-days after [root#Event:ClaimSubmitted] — unless [root#Event:ClaimApproved]
    now: - **FIVE-DAYS** — A claim is decided within three working days of being handed in.
    now: - 3 business-days after [root#Event:ClaimSubmitted] — unless [root#Event:ClaimApproved]
If this touches what you are doing, read the file again before you go on, and tell your user what changed.
```

In Claude Code, one command is both the connection and the telling. There is no settings file to edit and no `claude mcp add`:

```bash
claude plugin install aim-context --marketplace juicejs/agentic-intent-model --config context_token=<token>
```

The server's address is `https://intentmodel.org/mcp` unless you say otherwise: for your own server, add `--config context_server=http://localhost:8080/mcp`. The token is kept in your system's credential store.

At the start of each new session (and after `/clear`) the plugin also asks the server which contexts you have and tells the agent, which then asks you in one short question which to work in or whether to start a new one, suggesting the one you last used in that folder. If the plugin is not connected yet, the agent tells you how to connect.

It tells a session about the contexts that session has itself read or written through the server, which it learns from the agent's own calls. A session that never touches a context is told nothing, and costs the server no request.

What to know about it:

- The agent is told one tool call after the change, not sooner. An agent that calls no tool is not told.
- The agent's own writes are not told back to it, and nothing is told twice.
- If the server does not know what a session has read (it lost its record, or the context was named outright, below), the note says so and tells the agent to read the context. It never passes that off as "nothing changed".
- When there is nothing to tell, or the server cannot be reached, nothing is printed and the agent goes on.
- It needs a server of protocol version 2.1, and Node 18 or later on the machine; without Node it does nothing. It tells of every change in a context, not only of those that touch what the agent is working on.

Without the plugin, the same script runs as a hook you set up yourself: [`plugin/hooks/aim-live-hook.mjs`](plugin/hooks/aim-live-hook.mjs), with `AIM_URL` and `AIM_TOKEN` in its environment, and `AIM_CONTEXT` to name one context outright. In Claude Code that is a `PostToolUse` entry in `.claude/settings.json`:

```json
{
  "hooks": {
    "PostToolUse": [
      { "matcher": "*", "hooks": [{ "type": "command", "command": "AIM_URL=http://localhost:8080/mcp AIM_CONTEXT=launch-plan AIM_TOKEN=<token> node /path/to/context-server/plugin/hooks/aim-live-hook.mjs" }] }
    ]
  }
}
```

Codex CLI and Gemini CLI have a hook after each tool call as well. The script has not been run under them.

## Files that are not AIM

A context also keeps PDFs, images, spreadsheets and any other file its model refers to, byte for byte, up to `INTENTMODEL_MAX_FILE_MB` each.

- **Upload:** an agent writes one with `write_change` (`{ path, base64 }`), or anyone with a token uploads it over HTTP:

  ```bash
  curl -X PUT --data-binary @contract.pdf -H "Authorization: Bearer <token>" -H "X-Based-On: <state>" http://localhost:8080/files/launch-plan/root/sales/contract.pdf
  ```

- **List:** `read_model` lists each such file with its size and hash.
- **Read:** `read_file` gives its bytes, as does `GET /files/<context>/<path>`.

## Addresses

| Address | What it is |
|---|---|
| `POST /mcp` | The agents' surface (MCP, streamable HTTP), with `Authorization: Bearer <token>`. |
| `GET /files/<context>/<path>` | One file's bytes: a token is needed unless the context is public. |
| `PUT /files/<context>/<path>` | Upload one file as it is; `X-Based-On` names the state it is based on. |
| `GET /public[?q=]` | The public contexts, for anyone, with a search over their names and root files. |
| `GET /public/<context>` | One public context, with every file. |
| `GET /healthz` | `ok` |
| `GET /` | A short page saying what the server is and how an agent connects. |

## Settings

| Variable | Default | Meaning |
|---|---|---|
| `INTENTMODEL_DATA` | `./data` (`/app/data` in Docker) | The data folder. |
| `PORT` | `8080` | The port to listen on. |
| `HOST` | `0.0.0.0` | The address to listen on. |
| `INTENTMODEL_MAX_FILE_MB` | `25` | The largest file the server takes, in megabytes. |
| `INTENTMODEL_SPEC_URL` | `https://intentmodel.org/specification.md` | The address of the specification. The server fetches the specification and the packages from its base, unless `INTENTMODEL_SPEC_DIR` is set, and names them by it in answers. |
| `INTENTMODEL_SPEC_DIR` | unset | A folder laid out as the specification repository (`specification.md`, `reference/`, `guides/`, `packages/`). When set, the server reads it on each call, so an edit shows at once, and fetches nothing. |

## Where the data is

Everything is in the data folder, as plain files you can read, and back up by copying the folder:

```text
data/
├── people.json                  who may connect: names, token hashes (never tokens), contexts
├── library/                     the last fetched copy of the specification, its documents and the packages (not when INTENTMODEL_SPEC_DIR is set)
└── contexts/<id>/
    ├── context.json           name, owner, public or not
    ├── changes.jsonl            every change, one line each: when, by whom, each file and what it was before
    ├── objects/<sha256>         every version of every file, never changed
    ├── files/…                  the files as they stand now, exactly as written
    └── seen.json                how far each agent session was told of the changes; losing it loses no change
```

The log (`changes.jsonl`) is the record. `files/` is a mirror of it, kept for you to read, copy or put under git, and rebuilt from the log whenever it differs. Change the model through the server, not by editing `files/`. One server process owns a data folder at a time.

## Tests

```bash
cd context-server && npm test
```

The tests run the protocol against a real server, as an agent sees it. The plugin's script has no test of its own here; it was run by hand against a server, and under Claude Code.

## The image

The workflow `.github/workflows/context-server.yml` runs the tests and builds the image on every pull request that touches `context-server/`, and on every push to `master` publishes `juiceservices/aim-context-server` on Docker Hub, as `latest` and `sha-<commit>`, for `linux/amd64` and `linux/arm64`. The image runs `node src/server.js` as the `node` user (1000:1000), with the server in `/app`, its page in `/app/web` and its data in `/app/data`.

## License

MIT, as the rest of this repository.
