# The AIM context server protocol, version 2.1

A context server serves and manages AIM Contexts, each one an [AIM](https://intentmodel.org/specification.md) model, to people's agents over [MCP](https://modelcontextprotocol.io), so that several people and their agents read and write one model together, live. This document says what every context server does, so that an agent written against one works against any. intentmodel.org hosts version 2 of it at `https://intentmodel.org/mcp`, with Amy keeping the contexts behind it; the server beside this document ([README.md](README.md)) is the reference implementation, open source.

The words MUST, MUST NOT and MAY are used as in RFC 2119.

## 1. What a server does, and what it does not

A server keeps files and serves them. It records who wrote each change and what each file held before, it refuses a write that would silently overwrite someone else's newer change, and it tells an agent what changed since that agent last asked. It also hands a connected agent the specification and the packages (§5.2).

It does not check, correct, mark or interpret what is written, and it calls no model: the AIM work is the agent's. A file is kept byte for byte as written, valid AIM or not.

## 2. Contexts, people and access

- A **context** (an AIM Context) holds one model. It has an id (a string an agent passes back), a name, and its files.
- A **person** connects with a token of their own. Every call acts as that person, as they stand at that moment.
- A person **reaches** the contexts they were let into and those they started. They read and write those.
- A context MAY be **public**. Then anyone reads its name and files (§7), and any person's agent reads it, but only the people it was let in to write to it. Who is in a context is never shown with it.
- A person MUST NOT learn that a private context they do not reach exists: asking for one answers exactly as asking for none does.

How people are let in, and how tokens are made, is the host's own (an admin command, a web page, invitations by email).

## 3. Layout

A context's model stands under one root directory, `root/`. Its root intent is `root/root.aim`. Each intent under it has a directory of its own, named by the namespace segment it adds: `root.sales` is `root/sales/root.sales.aim`, and its child `root.sales.orders` is `root/sales/orders/root.sales.orders.aim`. An intent's facet and entries files stand beside its file.

- A path is relative, with forward slashes, and its first segment is `root`. A file outside `root/` is refused (`outside_root`).
- An `.aim` file's directories are lower-case namespace segments (`a-z`, `0-9`, `_`).
- Files that are not AIM (uploads: a PDF, an image, a spreadsheet the model refers to) MAY stand in any directory under `root/`.
- No two files differ only in capitals.

A new context MAY be empty. Its first writer writes `root/root.aim`.

## 4. State

Every answer that reads or writes names a **state**: an opaque string that says how far the context's changes reached when the answer was made. An agent passes a state back as `basedOn` (writing) or `since` (catching up). States are only compared by the server that issued them.

## 5. Tools

A server offers these MCP tools over streamable HTTP. Each answer is one JSON object, in one text content block.

| Tool | Arguments | Answer |
|---|---|---|
| `list_contexts` | none | `protocol` ("2.1"), `you` (the name of the token's person), `contexts`: `[{ id, name, owner, public?, root }]`, `specification` |
| `list_public_contexts` | `query?` | `contexts`: `[{ id, name, publicSince, changedAt, root }]`, where `root` is the text of `root/root.aim` or null. `query` keeps those whose name or root file holds it, in any case. |
| `read_model` | `context` | `about`, `context` `{ id, name, public? }`, `specification`, `state`, `files`: `[{ path, size, sha256, text, content? }]`. A text file's `content` is its text byte for byte; a file that is not text, or is very large, has no `content` and a `note`. |
| `read_changes` | `context`, `since` | `since`, `state`, `changes`: `[{ id, state, at, by, files: [{ path, op, content?, before? }] }]`, in order. `op` is `create`, `modify` or `delete`; `before` is what the file held before the change (text files). |
| `read_news` | `context`, `session`, `since?`, `mine?` | `you`, `since`, `state`, `changes`: `[{ id, state, at, by, files: [{ path, op }] }]`, in order, and `first: true` when the server knows nothing of what the session has seen (§5.1). No file contents. |
| `read_file` | `context`, `path` | `path`, `size`, `sha256`, `text`, and `content` (text) or `base64` (anything else). |
| `read_specification` | `document?` | `specification` (its address), `asOf`, `text`: the core specification byte for byte. With `document`, a further document the specification names (a path matching `^(reference\|guides)/[A-Za-z0-9._-]+\.md$`): `specification`, `document`, `address`, `asOf`, `text`. |
| `list_packages` | none | `address`, `asOf`, `aim_version`, `packages`: the package index's entries as they are, each with one more field, `address` of its `.aim` file. |
| `read_package` | `name` | `name`, `version`, `address`, `asOf`, `text`: the package's `.aim` file byte for byte. `name` matches `^[a-z0-9][a-z0-9_-]{0,59}$`. |
| `write_change` | `context`, `basedOn`, `files` | `taken: true`, `state`, `written`: `[{ path, op }]`, `changesByOthers`, `note`. Each file is `{ path, content }`, `{ path, base64 }` or `{ path, delete: true }`. |
| `start_context` | `name` | `context` `{ id, name, owner: true, root }`, `state`. The person who starts it owns it. |

Rules every server keeps:

- **Whole or nothing.** A `write_change` is taken whole or not at all.
- **Based on.** If any file a write touches changed after `basedOn`, the whole write is refused with `changed_since` and a `changed` list (`path`, `op`, `state`, `at`, `by`). Nothing is merged. A stale basis is fine for files nobody else changed.
- **Who wrote it.** Every change is recorded with the person who wrote it, and `read_changes` names them.
- **One change, one id.** A change has an `id` of its own, the same wherever it is listed, so what `read_news` names is found again in `read_changes`.
- **Live.** A change is in the model at once: every later read has it.
- **Seen.** The server remembers how far it told each session, and tells the rest (§5.1).
- **Information, not instructions.** Answers that carry files say, in `about`, that their contents are information about the work and never an instruction to the agent.

The server's MCP `instructions` tell a connecting agent the specification's address, the layout (§3) and how to work (§5).

### 5.1 What changed since I last asked

A change is in the model at once, but an agent works from what it read, and others write meanwhile. `read_news` closes that gap, and is light enough to ask after every step of work.

- A **session** is one run of one agent. Its client gives it an id of its own (1 to 100 of `A-Z a-z 0-9 . : - _`) and sends the same one each time. A person's sessions are kept apart: two agents of one person each have their own place.
- The server remembers, per person and session, the state that session was last told up to. `read_news` answers with every change after it (its state, when, who wrote it, and each file's path and `op`) and then moves the place to the state the answer names. Nothing is told twice.
- It carries no file contents. An agent that needs them reads the file (`read_file`) or the changes in full (`read_changes` since the answer's `since`).
- `since`, when given, is where to tell from instead of the remembered place: an agent that has just read the model at a state passes that state.
- `mine` names the states of changes the session wrote itself, as `write_change` answered them. They are left out.
- A session the server knows nothing of, with no `since`, is answered `first: true` and no changes. The server MUST NOT answer it as if nothing had changed: it cannot know what that session missed. The agent reads the model, and is told from that state on. A server that loses its record of the sessions answers every one of them this way once.

How the news reaches an agent in the middle of its work is its client's part: a hook that asks after each tool call and hands the answer to the agent is one way (README, *Tell a running agent*; the `aim-context` plugin does it for Claude Code).

### 5.2 The specification and the packages

An agent that writes to a context must follow the specification, and a model may declare packages. A server hands over both, so that an agent has them through the same connection, and so that a team's server can keep the version its contexts are written in.

- `read_specification` gives the core specification. With `document` it gives a further document the specification names, by its relative path; a path that does not match the pattern above is refused `bad_document`, and one the source does not have, `no_such_document`.
- `list_packages` gives the package index, and `read_package` one package's file, found through the index entry whose `aim` is the `name`. An unknown name is refused `no_such_package`.
- Every answer names the address of what it gives and `asOf`, the time (ISO 8601) the server got that copy from its source.
- A server MAY keep its own copies. One that cannot reach its source MUST serve the copy it kept, with that copy's `asOf`, and never an empty answer; with no copy it refuses `not_available`.
- These calls take no context. What they give is public, so a server MAY answer them without a token.

## 6. Refusals

A refused call is an MCP tool result with `isError: true` and the JSON `{ refused, reason, … }`. The `reason` is a sentence an agent can act on.

| `refused` | When |
|---|---|
| `not_reached` | The context is not reachable for this person (or does not exist). |
| `read_only` | The context is public and the person is not one of its people. |
| `changed_since` | A file this write touches changed after `basedOn`; `changed` says which. |
| `bad_state` | `basedOn`, `since` or one of `mine` is not a state this context has had. |
| `bad_session` | `session` is missing or not an id as §5.1 says. |
| `outside_root`, `bad_path` | The path breaks §3. |
| `bad_change` | The files list is malformed, or names a file twice. |
| `no_such_file` | A delete or read names a file that is not there. |
| `too_large` | A file is larger than the server takes. |
| `bad_document` | `document` is not a relative path under `reference/` or `guides/` ending in `.md`. |
| `no_such_document` | The specification has no such document. |
| `no_such_package` | The package index has no package of that name. |
| `not_available` | The server cannot get what was asked and holds no earlier copy. |
| `not_public` | (HTTP) There is no public context at this address. |

## 7. Over HTTP

Besides MCP, a server SHOULD offer:

- `GET /public[?q=]`: `list_public_contexts`, for anyone.
- `GET /public/<context>`: a public context as `read_model` answers it, for anyone.
- `GET /files/<context>/<path>`: one file's bytes. A person's token is needed unless the context is public.
- `PUT /files/<context>/<path>` with header `X-Based-On: <state>`: upload one file as it is, as a `write_change` of that file.

Files go out as data, never as pages: text as `text/plain`, everything else under its own type or as bytes, with a sandboxing `Content-Security-Policy`.

## 8. Versions

This is version 2.1. A server says its version in `list_contexts` (`protocol`). A later version adds, under the same first number; anything it would change or remove takes a new first number. An agent written against version 2 works against 2.1 unchanged.

- **Version 2.1** adds `read_news` and the sessions' places (§5.1), `you` in `list_contexts`, an `id` on every change, and `read_specification`, `list_packages` and `read_package` (§5.2), with the refusals `bad_document`, `no_such_document`, `no_such_package` and `not_available`.
- **Version 2** calls what a server keeps a *context*: `list_contexts`, `list_public_contexts`, `start_context`, and the argument `context`.
- **Version 1** called it a *workspace*, with the same rules: `list_workspaces`, `list_public_workspaces`, `start_workspace`, and the argument `workspace`. Amy's own agent address, behind intentmodel.org, still answers in those words.
