A plugin is a program that serves one store as a Stash source. You write it in any language, and keep it in your own repository. Stash starts it, and sends it requests over the plugin protocol. The [configuration reference](/docs/user-guide/stash/reference/configuration/index.md#plugin) tells how a user adds a plugin to a mount.

The [Python SDK](https://github.com/strands-agents/stash/tree/833198a2b91f6490291149d06b4daac8de0727c8/integrations/plugin-sdk/python) speaks this protocol for you. Read on to write a plugin in another language.

## Transport

-   Stash runs the program `stash-source-<plugin>` from its `PATH`, or the `command` of the source.
-   Each message is one JSON-RPC 2.0 object on one line, in UTF-8. Stash writes requests to the program’s stdin, and reads responses from its stdout.
-   The program writes nothing else to stdout. A line that is not a response is skipped. Write logs to stderr: Stash writes each line to its daemon log.
-   Stash can send a request before the plugin answers the last one. Each response has the `id` of its request, so a plugin can answer in any order.
-   Stash times each request from when it sends it. A plugin that answers one request at a time must answer each one quickly, or the requests that wait behind it time out.
-   The plugin keeps nothing that Stash needs. Stash can stop it, and start it again, at any time. It starts a new one after the plugin exits, and after a request gets no answer in 30 seconds.
-   A new process must declare the same capabilities as the first one. If it does not, Stash refuses it until the mount starts again.
-   An answer can have at most 16 MiB.

The examples below wrap for readability. Send each object on one line.

## `initialize`

The first request to each new process.

Request:

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocol_version": 1,
    "settings": {"user_id": "example-user"}
  }
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocol_version": 1,
    "capabilities": ["list", "get", "search", "ingest"],
    "description": "Memories for the example user in mem0."
  }
}
```

| Field | Required | Description |
| --- | --- | --- |
| `params.protocol_version` | yes | `1`. Another version fails the start. |
| `params.settings` | yes | The source’s `settings` table. It is an empty object when the source has none. |
| `result.protocol_version` | yes | `1`. Another version fails the start. |
| `result.capabilities` | yes | The methods that the plugin serves: `list`, `get`, `search`, `ingest`, and `plan`. |
| `result.description` | no | Text for the source’s `_sources/NAME.md` page. |

Refuse settings that you cannot use with a `validation` error. Then the source fails to start, and its `_sources/NAME.md` page gives the reason, with your message, in at most 200 characters. The current daily daemon log has the whole reason.

## Records

`list`, `get`, and `search` give records. Stash makes one file of each record, at `<dir>/<name>`. You choose the folder, the name, and the text.

```json
{"id": "mem-42", "body": "The user is allergic to peanuts.\n",
 "dir": "health", "name": "peanut-allergy.md",
 "created_at": "2026-10-14T09:00:00Z", "updated_at": "2026-10-15T08:00:00Z"}
```

| Field | Required | Description |
| --- | --- | --- |
| `id` | yes | The same for the same record in each call. `get` receives it. |
| `body` | yes | The text of the file. |
| `dir` | no | The folder below the source, such as `team/runbooks`. With none, the source’s own folder. See [Paths](#paths). |
| `name` | no | The whole file name, such as `peanut-allergy.md` or `menu.csv`. With none, the file is `<date>__<words of the body>.md`. See [Paths](#paths). |
| `preview` | no | The line that a search shows for the record. With none, the body. |
| `created_at` | no | RFC 3339 time. With no `name`, its date starts the file name. |
| `updated_at` | no | RFC 3339 time. |

A file whose name ends in `.md` is the body, then a YAML block with `stash-fetched-at`, the time that Stash fetched the record. Any other file is the body alone, so a JSON or CSV file stays valid.

### Paths

A folder path is empty for the source root, or is a relative path of `/`\-separated segments. Each segment is 1–255 UTF-8 bytes, is not `.` or `..`, and has no NUL character. A file `name` is one segment.

For a record’s `dir` and `name`, Stash also refuses a segment that starts with `.` or has a control character. Stash composes these values to Unicode NFC. A record that breaks a path rule makes its answer `malformed`.

Treat `id` as opaque data. Do not copy an external ID into `name`. For example, `INC/4412` contains `/`. Use a separate safe name, or leave `name` out so Stash makes one from `created_at` and `body`.

When two records have one name, also with other case, the second one gets `__` and 16 hex characters of the sha256 of its id before its extension.

## Folders and filters

A `list` or a `search` can name a folder, in `scope`, and conditions on the records of your store, in `filters`. Stash leaves out each one that the request does not have.

```json
{
  "scope": {"subtree": "health"},
  "filters": [
    {
      "key": "createdAt",
      "op": "gte",
      "value": {"datetime": "2026-10-14T00:00:00Z"}
    }
  ]
}
```

| Field | Description |
| --- | --- |
| `scope` | `{"exact": "health"}`: the records whose `dir` is that folder. `{"subtree": "health"}`: that folder and each folder below it. `"all"` or left out: all records. |
| `filters[].key` | A key of the records in your store, such as `createdAt`. |
| `filters[].op` | `gt`, `gte`, `lt`, `lte`, or `eq`. |
| `filters[].value` | `{"datetime": "<RFC 3339>"}`, `{"number": 3}`, or `{"text": "ops"}`. |

Each record that you give must match all of them. If you cannot apply one, answer with an `unsupported` error. Never ignore it, or the agent gets records from outside its folder.

## `list`

One page of the records.

Request:

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "list",
  "params": {"cursor": null, "limit": 100}
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "records": [{"id": "mem-42", "body": "..."}],
    "next": "page-2"
  }
}
```

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `params.cursor` | string or `null` | yes | The cursor from the last response. `null` starts the listing. |
| `params.limit` | positive integer or `null` | yes | The most records in the page. `null` requests the plugin’s natural page. |
| `params.scope` | object or `"all"` | no | The folder selection from [Folders and filters](#folders-and-filters). |
| `params.filters` | array | no | The filters from [Folders and filters](#folders-and-filters). |
| `result.records` | array | yes | The records in this page. |
| `result.next` | string or `null` | no | The next cursor. `null` or an omitted field ends the listing. |

## `get`

One whole record.

Request:

```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "get",
  "params": {"id": "mem-42"}
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {"record": {"id": "mem-42", "body": "..."}}
}
```

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `params.id` | string | yes | The record ID that `list` or `search` returned. |
| `result.record` | object | yes | The whole record. |

Answer a missing record with a `not_found` error.

## `search`

The records that match a text, best first.

Request:

```json
{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "search",
  "params": {
    "query": "allergies",
    "limit": 10,
    "scope": {"subtree": "health"}
  }
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "results": [
      {"record": {"id": "mem-42", "body": "..."}, "score": 0.83}
    ]
  }
}
```

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `params.query` | string | yes | The search text. |
| `params.limit` | positive integer | yes | The most records to return. |
| `params.scope` | object or `"all"` | no | The folder selection from [Folders and filters](#folders-and-filters). |
| `params.filters` | array | no | The filters from [Folders and filters](#folders-and-filters). |
| `result.results` | array | yes | The matches, best first. |
| `result.results[].record` | object | yes | One matching record. |
| `result.results[].score` | number | no | The score that `stash search` shows. |

## `plan`

What the mount loads when it starts. Stash asks one time, after `initialize`, and only a plugin that declares `plan`. Without `plan`, the mount loads nothing when it starts, and files come only from `search` and `get`.

Request:

```json
{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "plan",
  "params": {}
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "id": 5,
  "result": {
    "directories": ["health", "team"],
    "steps": [
      {
        "list": {
          "limit": 25,
          "scope": {"exact": "health"},
          "filters": [
            {
              "key": "createdAt",
              "op": "gte",
              "value": {"datetime": "2026-10-14T00:00:00Z"}
            }
          ]
        }
      },
      {
        "search": {
          "query": "What does the user prefer?",
          "limit": 10
        }
      }
    ]
  }
}
```

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `result.directories` | array of strings | no | Folder paths that follow [Paths](#paths), to show when no record is in them. The default is `[]`. |
| `result.steps` | array | no | At most 10 `list` or `search` steps. The default is `[]`. |
| `result.steps[].list` | object | per step | The `list` parameters. `limit` is required and is at most 100. |
| `result.steps[].search` | object | per step | The `search` parameters. `query` and `limit` are required. `query` is not empty, and `limit` is at most 100. |

A plan that lists or searches when the plugin does not serve that method fails the start.

## `ingest`

The turns of an agent’s session, to write back. Stash sends `ingest` only when the source sets `write.trajectory = {}`.

Request:

```json
{
  "jsonrpc": "2.0",
  "id": 6,
  "method": "ingest",
  "params": {
    "token": "5f1c...",
    "session": "session-1",
    "turns": [
      {
        "id": "turn-1",
        "role": "user",
        "text": "Deploy it.",
        "at": "2026-10-14T00:00:00Z"
      }
    ]
  }
}
```

Response:

```json
{
  "jsonrpc": "2.0",
  "id": 6,
  "result": {"id": "agent-sessions/session-1/5f1c.md"}
}
```

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `params.token` | string | yes | The idempotency token for this batch. |
| `params.session` | string | yes | The agent session ID. |
| `params.turns` | array | yes | The turns in the batch. |
| `params.turns[].id` | string | yes | The turn ID. |
| `params.turns[].role` | string | yes | `user`, `assistant`, `tool`, or `other`. |
| `params.turns[].text` | string | yes | The turn text. |
| `params.turns[].at` | string | yes | The RFC 3339 time of the turn. |
| `result.id` | string | yes | The ID of what the plugin wrote. |

Stash sends a batch again after a failure. The same batch has the same `token`, so write it one time, for example under a key made from the token.

## Errors

Answer a failure with a JSON-RPC error. Put its kind in `data.kind`:

Error response:

```json
{"jsonrpc": "2.0", "id": 7, "error": {"code": -32000,
 "message": "mem0 refused the API key", "data": {"kind": "access_denied"}}}
```

| `data.kind` | Stash |
| --- | --- |
| `throttled`, `unavailable`, `timeout` | sends the request again, after a wait |
| `not_found` | shows that the record does not exist |
| `access_denied`, `validation`, `malformed`, `unsupported`, `rejected`, `other` | does not send the request again |

To get your secrets again, for example after the store refuses a token, exit. Stash starts a new plugin for the next request, and reads its `env` again.

An error with no `data.kind` gets its kind from its JSON-RPC code: `-32601` (method not found) is `unsupported`, `-32602` (invalid params) is `validation`, and each other code is `other`. Stash shows the message on one line, after the method and the code: `get: JSON-RPC error -32000: mem0 refused the API key`.