Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/read-verbs-get-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@tanstack/cms-core': minor
'@tanstack/cms-api': minor
'@tanstack/cms-client': minor
---

`get` and `list` are the canonical reads, with one bag each at every layer, rooted in the operations registry (`readVerbs`, `getInput`, `listInput` in `@tanstack/cms-core`).

- Server: `cmsAs(cms, ctx).get({ collection, id | locator, locale?, populate? })` and `.list({ collection?, locale?, where?, sort?, limit?, offset?, cursor?, populate? })`, and the argument forms `getAs(ctx, options)` and `listAs(ctx, options)`. A `live` context reads published documents; `preview` and `edit` read the working copy, with staged changes overlaid. `get` resolves to `{ id, collection, doc }` or `null`, and `list` to `{ items, total, nextCursor? }`. `list` pages 100 at a time by default (at most 500). `populate` defaults to `false`.
- HTTP and Start: the public `get` and `list` operations (and Start server functions), reading as the public site does: published documents for anyone, and the working copy for callers who may read drafts unless they ask for `live`.
- Client: `client.get(options)` and `client.list(options)`, typed from `Register`.
- Agents: `get_document` (new), and `list_documents`, both named `<verb>_<noun>` from the registry (`agentToolName`). `search_documents` stays its own verb.

**Breaking:**

- `list_documents` takes the `list` bag and returns `{ items: [{ id, collection, doc }], total, nextCursor? }` instead of an array of summaries. `limit` defaults to 100, and staged creates are no longer listed (`get_document` reads them).
- `get` and `list` are reserved operation names, so an app op named either one fails as a duplicate.

`read_document` is deprecated in favour of `get_document`. The older read verbs and operations keep their behaviour.
2 changes: 1 addition & 1 deletion docs/agents/content-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const reply = await chat({

### Tools and Permissions

The agent uses the twelve [agent tools](../mcp/operations-registry#the-agent-tool-registry-opschemas): `list_collections`, `list_documents`, `read_document`, `search_documents`, `list_revisions`, `list_translations`, and the write tools `create_document`, `update_document`, `delete_document`, `unpublish_document`, `restore_revision`, and `schedule_document`.
The agent uses the [agent tools](../mcp/operations-registry#the-agent-tool-registry-opschemas): `list_collections`, `list_documents`, `get_document`, `search_documents`, `list_revisions`, `list_translations`, and the write tools `create_document`, `update_document`, `delete_document`, `unpublish_document`, `restore_revision`, and `schedule_document`.

The tools run with the signed-in user's session, so the agent sees only the collections and fields that user may read, and preserves fields they may not write. Every write is **staged** in the [review queue](./review-queue) with `actor: 'content-agent'`, even when an admin is chatting. When the agent stages something, the page shows a **Review changes** link to `/review`.

Expand Down
2 changes: 1 addition & 1 deletion docs/agents/review-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ An agent often makes several edits to the same document before anyone reviews th

The pending change belongs to the door that opened it: an agent proposal (`agent`), an author's save (`review`) or a publisher's draft (`draft`). While it is open, a write through another door fails with `CONFLICT` and `details: { pendingId, source }` naming it. Approve, reject or discard it first.

Agents can read their own staged work. `read_document` and `update_document` fall back to the pending version when a document hasn't been committed yet, and `list_documents` includes pending creates. An agent can create a document, read it back, and refine it before anyone approves.
Agents can read their own staged work. `get_document` (in the agent's `edit` mode) and `update_document` read the pending version, including a document that hasn't been committed yet, so an agent can create a document, read it back, and refine it before anyone approves. `list_documents` lists committed documents; staged creates appear in the review queue.

## Reviewing Changes

Expand Down
27 changes: 23 additions & 4 deletions docs/api/cms-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,30 @@ const cms = createCmsClient({
getAuthHeader: () => (process.env.CMS_API_KEY ? `Bearer ${process.env.CMS_API_KEY}` : undefined),
})

const { items, total } = await cms.collection('post').published({ limit: 10 })
const { items, total } = await cms.list({ collection: 'post', limit: 10 })
console.log(`${items.length} of ${total} posts`)
```

`mode` asks every call for a read mode. The client sends it as the `x-cms-read-mode` header (`CMS_READ_MODE_HEADER`), and the server builds the caller's access context in that mode. `live` reads published documents only, even for an editor's key. `edit` reads the working copy, meaning the drafts the caller may read. Without `mode` the server reads in `edit`, so a CMS reader's `published()` includes drafts. A client with a `mode` appends it to its cache scope, so live and edit reads never share a query key.
`mode` asks every call for a read mode. The client sends it as the `x-cms-read-mode` header (`CMS_READ_MODE_HEADER`), and the server builds the caller's access context in that mode. `live` reads published documents only, even for an editor's key. `edit` reads the working copy, meaning the drafts the caller may read. Without `mode` the server reads in `edit`, so a CMS reader's `list()` includes drafts. A client with a `mode` appends it to its cache scope, so live and edit reads never share a query key.

### `get(options)` and `list(options?)`

The canonical reads, the `get` and `list` operations. They take the same bags as [`getAs` and `listAs`](./cms-core-server#getasctx-options-and-listasctx-options) on the server, and their document types come from `Register`.

```ts validate=type
import { createCmsClient } from '@tanstack/cms-client'

const cms = createCmsClient({ baseUrl: 'https://cms.example.com', mode: 'live' })

const about = await cms.get({ collection: 'page', locator: 'about' })
const first = await cms.list({ collection: 'post', sort: { field: 'title' }, limit: 20 })
const next = first.nextCursor
? await cms.list({ collection: 'post', sort: { field: 'title' }, limit: 20, cursor: first.nextCursor })
: undefined
console.log(about?.doc, first.items.length, next?.total)
```

`get` resolves to `{ id, collection, doc }`, or `null` when the document is missing or the caller can't see it. `list` resolves to `{ items: [{ id, collection, doc }], total, nextCursor? }`. Both are public: anonymous callers read published documents, and callers who may read drafts see the working copy unless the client's `mode` is `live`.

### `op(name, input)` and `call<T>(name, input?)`

Expand All @@ -82,7 +101,7 @@ console.log(found?.collection, redirect?.to, custom.count)

### `collection(name)`

Typed helpers for one collection, wrapping the document operations:
Typed helpers for one collection, wrapping the older document operations. Its `list` and `get` are the admin editor's reads; for content, use the client's `get` and `list`:

| Method | Operation | Access | Returns |
| --- | --- | --- | --- |
Expand All @@ -94,7 +113,7 @@ Typed helpers for one collection, wrapping the document operations:
| `delete(id, options?)` | `deleteDoc` | `content.delete` | `{ ok }`. Option: `force`. |
| `discardDraft(id)` | `discardDraft` | `content.write` + publish rights | `{ ok }` |

`get()` is an editing read and needs `content.read`. Anonymous readers use `published()`, or `op('getPublicDocument', { slugOrId })` for one document.
`collection(name).get()` is an editing read and needs `content.read`. Anonymous readers use the client's `get` and `list`.

Saving follows the [workflow rules](./cms-core-server#workflow-drafts-review-queue-revisions-scheduling): changes to a published document are a draft until saved with `publish: true`, and callers without publish rights have their changes staged for review (`staged: true`).

Expand Down
71 changes: 68 additions & 3 deletions docs/api/cms-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Contexts are frozen and come from the builders. A copied object is not a context
function cmsAs(cms: Cms | CmsConfig, ctx: AccessContext): CmsReads
```

The read verbs bound to `ctx`, scoped to `cms` (a `createCms` instance or the config `defineCms` returned): `queryDocuments`, `groupDocuments`, `queryDocumentSummaries`, `listDocuments`, `readDocument`, `readDocumentWithPending`, `readSingleton`, `readDocumentBySlug`, `findDocumentBySlug`, `populateRelations`, `findReferences`, `listTranslations` and `listRevisions`, plus `context`. They take the same arguments as the `…As` functions below, without the context. Documents the context can't read come back as missing, and fields it can't read are removed. `postRead` hooks receive the context as `access`.
The read verbs bound to `ctx`, scoped to `cms` (a `createCms` instance or the config `defineCms` returned). `get` and `list` are the canonical reads (see [`getAs` and `listAs`](#getasctx-options-and-listasctx-options)). The older vocabulary is bound too: `queryDocuments`, `groupDocuments`, `queryDocumentSummaries`, `listDocuments`, `readDocument`, `readDocumentWithPending`, `readSingleton`, `readDocumentBySlug`, `findDocumentBySlug`, `populateRelations`, `findReferences`, `listTranslations` and `listRevisions`, plus `context`. They take the same arguments as the `…As` functions below, without the context. Documents the context can't read come back as missing, and fields it can't read are removed. `postRead` hooks receive the context as `access`.

```ts validate=type
import { defineCms } from '@tanstack/cms-core'
Expand All @@ -91,7 +91,11 @@ import {
const cms = defineCms({ collections: [], blockTypes: [] })

export async function homePage() {
return cmsAs(cms, publicContext()).queryDocuments({ collection: 'post', limit: 10 })
return cmsAs(cms, publicContext()).list({ collection: 'post', limit: 10 })
}

export async function postPage(slug: string) {
return cmsAs(cms, publicContext()).get({ collection: 'post', locator: slug })
}

export async function editorDrafts(request: Request) {
Expand All @@ -106,7 +110,7 @@ export async function sitemapIds() {
}
```

Each verb also has an argument form taking the context first: `queryDocumentsAs(ctx, options)`, `readDocumentAs(ctx, collection, id)`, `readDocumentBySlugAs`, `findDocumentBySlugAs`, `readSingletonAs`, `readDocumentWithPendingAs`, `queryDocumentSummariesAs`, `listDocumentsAs`, `groupDocumentsAs`, `populateRelationsAs`, `findReferencesAs`, `listTranslationsAs` and `listRevisionsAs`. These read against the registered CMS.
Each verb also has an argument form taking the context first: `getAs(ctx, options)`, `listAs(ctx, options)`, `queryDocumentsAs(ctx, options)`, `readDocumentAs(ctx, collection, id)`, `readDocumentBySlugAs`, `findDocumentBySlugAs`, `readSingletonAs`, `readDocumentWithPendingAs`, `queryDocumentSummariesAs`, `listDocumentsAs`, `groupDocumentsAs`, `populateRelationsAs`, `findReferencesAs`, `listTranslationsAs` and `listRevisionsAs`. These read against the registered CMS.

### Checks

Expand All @@ -127,6 +131,67 @@ The plain read functions below (`queryDocuments`, `readDocument`, …) are depre

## Documents and queries

### `getAs(ctx, options)` and `listAs(ctx, options?)`

```ts
function getAs(ctx: AccessContext, options: GetOptions): Promise<ReadResult | null>
function listAs(ctx: AccessContext, options?: ListOptions): Promise<ListResult>

type GetOptions = {
collection: string
locale?: string // with `locator`: match the document in this locale
populate?: boolean // default false
} & ({ id: string } | { locator: string }) // exactly one

interface ListOptions {
collection?: string // omit to list every collection ctx may read
locale?: string // only documents in this locale
where?: WhereInput // object form, see Filters below
sort?: { field: string; dir?: 'asc' | 'desc' }
limit?: number // 1–500, default 100
offset?: number
cursor?: string // `nextCursor` from the previous page
populate?: boolean // default false
}

interface ReadResult<TDoc = Doc> {
id: string
collection: string
doc: TDoc
relations?: Record<string, Doc | Array<Doc> | null> // with `populate`
}

interface ListResult<TDoc = Doc> {
items: Array<ReadResult<TDoc>>
total: number
nextCursor?: string
}
```

The canonical reads. One bag each, the same at every layer: the store (`cmsAs(cms, ctx).get/list`), HTTP and Start (the `get` and `list` operations), `@tanstack/cms-client` (`client.get/list`), and the agent tools (`get_document`, `list_documents`). The bag carries no visibility. The context decides it:

- A `live` context reads published documents only.
- A `preview` or `edit` context reads the working copy, with staged changes overlaid.

A `locator` is the document's slug or its id, with leading slashes ignored. A document that is missing, or that `ctx` can't see, is `null` from `get` and absent from `list`. Documents are redacted for `ctx`, and filtering or sorting on a field it can't read is `FORBIDDEN`. Both functions validate the bag (`getInput`, `listInput` from `@tanstack/cms-core`) and throw `BAD_REQUEST` for an invalid one.

Full-text search stays its own verb, `queryDocumentsAs` with `query`, and `search_documents` for agents. The older read verbs below keep their behaviour.

```ts validate=type
import { listAs, publicContext } from '@tanstack/cms-core/server'

export async function allPosts() {
const ids: Array<string> = []
let cursor: string | undefined
do {
const page = await listAs(publicContext(), { collection: 'post', limit: 500, cursor })
ids.push(...page.items.map((i) => i.id))
cursor = page.nextCursor
} while (cursor)
return ids
}
```

### `queryDocumentsAs(ctx, options?)`

```ts
Expand Down
2 changes: 1 addition & 1 deletion docs/api/cms-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ interface OpSchema {
const opSchemas: Array<OpSchema>
```

The agent tool set, shared by the content agent, the HTTP MCP server, and WebMCP: `list_collections`, `list_documents`, `read_document`, `search_documents`, `list_revisions`, `list_translations` (read), and `create_document`, `update_document`, `delete_document`, `unpublish_document`, `restore_revision`, `schedule_document` (write, always staged for review). The server runs them with `runOp()` from `@tanstack/cms-core/server`. See [Shared Operation Registry](../mcp/operations-registry).
The agent tool set, shared by the content agent, the HTTP MCP server, and WebMCP: `list_collections`, `list_documents`, `get_document`, `read_document` (deprecated), `search_documents`, `list_revisions`, `list_translations` (read), and `create_document`, `update_document`, `delete_document`, `unpublish_document`, `restore_revision`, `schedule_document` (write, always staged for review). The server runs them with `runOp()` from `@tanstack/cms-core/server`. See [Shared Operation Registry](../mcp/operations-registry).

These agent tools are separate from the HTTP API operations (`saveDoc`, `getPublicDocument`, …) in [`@tanstack/cms-api`](./cms-api).

Expand Down
8 changes: 5 additions & 3 deletions docs/api/routes-and-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ curl -X POST https://example.com/api/cms/v1/ops/saveDoc \
Runs a `GET` operation (and only those). Pass the input as JSON in `?input=`, or as flat query parameters for string-only inputs.

```bash
curl 'https://example.com/api/cms/v1/ops/listPublishedDocuments?input={"collection":"post","limit":10}'
curl 'https://example.com/api/cms/v1/ops/getPublicDocument?slugOrId=about'
curl 'https://example.com/api/cms/v1/ops/list?input={"collection":"post","limit":10}'
curl 'https://example.com/api/cms/v1/ops/get?collection=page&locator=about'
```

### Operations
Expand All @@ -237,6 +237,8 @@ Every operation is also a TanStack Start server function in [`@tanstack/cms-api/
| Operation | Method | Access | Input | Result |
| --- | --- | --- | --- | --- |
| `getAuthSession` | GET | public | `{}` | The caller's `CmsSession` plus `ai: { text, image }` capabilities. |
| `get` | GET | public | `{ collection, id \| locator, locale?, populate? }` | `{ id, collection, doc, relations? }` or `null`. The canonical read ([`getAs`](./cms-core-server#getasctx-options-and-listasctx-options)): published only, except for callers who may read drafts of the collection and don't ask for `live`; drafts are read with staged changes overlaid. |
| `list` | GET | public | `{ collection?, locale?, where?, sort?: { field, dir? }, limit? (1–500, default 100), offset?, cursor?, populate? }` | `{ items: [{ id, collection, doc, relations? }], total, nextCursor? }`. The canonical list, with the same visibility as `get`. |
| `listDocumentSummaries` | GET | `content.read` | `{ collection, search?, status?: 'all' \| 'published' \| 'draft' \| 'scheduled', locale?, ids? (≤ 500), sort?: { field, dir? }, limit? (1–500, default 50), offset?, columns? (≤ 20) }` | `{ items: DocSummary[], total }` |
| `getDocument` | GET | `content.read` | `{ collection, id }` | `{ doc, isPending, pendingId?, pendingSource?, livePublished }`, including any staged change. |
| `resolveRedirect` | GET | public | `{ path }` | `{ to, status }` or `null` |
Expand Down Expand Up @@ -278,7 +280,7 @@ Operations that need `agent.use` also count against the `ai` rate limit and retu

### `/api/mcp`

A [Model Context Protocol](../mcp/http-mcp-server) server over Streamable HTTP (`GET`, `POST`, `DELETE`), exposing the agent tools (`list_documents`, `read_document`, `create_document`, …). Write tools stage their changes for review.
A [Model Context Protocol](../mcp/http-mcp-server) server over Streamable HTTP (`GET`, `POST`, `DELETE`), exposing the agent tools (`list_documents`, `get_document`, `create_document`, …). Write tools stage their changes for review.

Access is granted to:

Expand Down
29 changes: 29 additions & 0 deletions docs/deployment/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,35 @@ becomes `accessContext({ session, mode })`, `publishedOnly: true` becomes
warning. Replace that reason with a real one, or with the visitor's context
where the read serves the public.

### 26. One read vocabulary: `get` and `list`

`get` and `list` are the canonical reads, with one bag each at every layer:
`cmsAs(cms, ctx).get/list` (or `getAs`/`listAs`) on the server, the `get`
and `list` operations over HTTP and Start, `client.get/list`, and the agent
tools `get_document` and `list_documents`. See
[`getAs` and `listAs`](../api/cms-core-server#getasctx-options-and-listasctx-options).

```ts
const post = await cmsAs(cms, ctx).get({ collection: 'post', locator: 'hello' })
const { items, nextCursor } = await cmsAs(cms, ctx).list({ collection: 'post', limit: 20 })
```

The older verbs (`queryDocumentsAs`, `readDocumentBySlugAs`,
`listPublishedDocuments`, `getPublicDocument`, …) keep working as they did,
and a later alpha moves the starters and docs over.

- **`list_documents` is the canonical `list`.** It takes the `list` bag
(`where`, `sort`, `cursor` and `populate` are new; `limit` defaults to 100)
and returns `{ items: [{ id, collection, doc }], total, nextCursor? }`
instead of an array of summaries. Staged creates are no longer listed;
`get_document` still reads them.
- **`read_document` is deprecated** in favour of `get_document`, which takes
`id` or `locator` and returns `{ id, collection, doc }` or `null`.
`read_document` still returns the bare document and is removed in a later
alpha.
- **`get` and `list` are reserved operation names**: an app op with either
name now fails as a duplicate.

## Versioning policy

From 1.0, TanStack CMS follows [semantic versioning](https://semver.org). The
Expand Down
2 changes: 1 addition & 1 deletion docs/mcp/http-mcp-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const cmsMcp = createMCPServer({
- `GET /api/mcp` opens the server-to-client stream.
- `DELETE /api/mcp` ends a session.

The tools are the twelve agent tools listed in the [Operations Registry](./operations-registry#the-agent-tool-registry-opschemas): six read tools (`list_collections`, `list_documents`, `read_document`, `search_documents`, `list_revisions`, `list_translations`) and six write tools that stage changes (`create_document`, `update_document`, `delete_document`, `unpublish_document`, `restore_revision`, `schedule_document`).
The tools are the agent tools listed in the [Operations Registry](./operations-registry#the-agent-tool-registry-opschemas): the read tools (`list_collections`, `list_documents`, `get_document`, `read_document` (deprecated), `search_documents`, `list_revisions`, `list_translations`) and six write tools that stage changes (`create_document`, `update_document`, `delete_document`, `unpublish_document`, `restore_revision`, `schedule_document`).

## Who Can Connect

Expand Down
Loading
Loading