# Api

Source: https://aunysillyme.dev/projects/shared-agent-memory/docs/api/

The server defaults to `http://127.0.0.1:32123`. Requests use JSON and an `Authorization: Bearer` header. Set the header from your environment. Never put a key in a URL. Keep remote HTTP inside an encrypted private network.

## Write requests

`POST /collect` uses the write key. The body has `device`, `inventory` and `files`.

- `device` is a stable machine name.
- `inventory` maps each selected AI name to its full list of file paths.
- `files` contains changed files, each with `ai`, `path`, `fingerprint` and `chunks`.
- `fingerprint` is a lowercase SHA-256 digest of original bytes.
- `chunks` is an ordered list of normalized text.

The server validates limits from `src/lib/constants.js`. A file must fit in one batch. Every batch carries the same full inventory. A successful response contains `acknowledged`, a list of `ai` and `path` pairs. Only committed files are acknowledged. Retries replace file links without duplicating content.

An empty inventory is refused. A deletion of more than half and more than 20 current file rows is refused. Guard counts apply to this device and selected AIs. Other devices stay unchanged.

`GET /status?device=<name>` uses the write key and returns per-AI file counts and whether this device is known. A completed collector scan checks this route before diffing. Missing source counts cause remembered files to be resent.

## Search requests

`POST /search` uses the read key. Fields are `query`, `scope`, `k`, `device` and `ai`. Defaults and caps are defined in `src/search/search.js` and `src/lib/constants.js`. Scope defaults to `own`. A named AI scope selects that AI's provenance. `all` selects every allowed source. Device narrows the result to a machine.

All privacy tags must permit the querying AI. The server filters before a sqlite-vec cosine KNN query. Each returned hit has `frame`, `data`, `source`, `tags` and `distance`. The frame identifies stored data as untrusted. It never promotes source text into instructions.

The read key authorizes choosing an AI identity. Use separate server instances for mutually untrusted clients. The AI name and privacy rules are routing controls within one trusted read-key group.

`GET /health` uses the read key and reports scanner availability, embedder availability and device freshness.

## Rebuild requests

The CLI previews a full scan with `rebuild --dry`. `rebuild --confirm` starts a staging session at `POST /rebuild/start`, uploads capped batches to `/rebuild/<id>/batch`, then calls `/rebuild/<id>/commit`. A failed session is cancelled at `/rebuild/<id>/cancel`.

Each session preserves the old store until every inventory file is staged, scanned and embedded. Commit replaces only that device in one transaction. Other devices keep their rows. Sessions expire according to the stale interval in `src/lib/constants.js`.

## MCP transport

`shared-agent-memory mcp --ai <tool>` reads newline-delimited JSON-RPC from stdin and writes responses to stdout. Content-Length framing is also accepted for older hosts. Diagnostic text never goes to stdout.

The only tool is `memory_search`. It accepts `query`, `scope`, `k` and `device`. The host selects the AI name when launching the process. MCP initialize, initialized notifications, ping, tools/list and tools/call are supported.
