@protobase/cli 0.58.2 → 0.58.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.58.2";
2
+ var version = "0.58.3";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.58.2",
3
+ "version": "0.58.3",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -42,12 +42,12 @@
42
42
  "dependencies": {
43
43
  "@better-auth/core": "^1.7.12",
44
44
  "@hono/node-server": "^2.1.3",
45
- "@protobase/config": "0.58.2",
46
- "@protobase/layout": "0.58.2",
47
- "@protobase/query": "0.58.2",
48
- "@protobase/schema": "0.58.2",
49
- "@protobase/server": "0.58.2",
50
- "@protobase/ui": "0.58.2",
45
+ "@protobase/config": "0.58.3",
46
+ "@protobase/layout": "0.58.3",
47
+ "@protobase/query": "0.58.3",
48
+ "@protobase/schema": "0.58.3",
49
+ "@protobase/server": "0.58.3",
50
+ "@protobase/ui": "0.58.3",
51
51
  "@react-email/render": "^2.1.0",
52
52
  "@scalar/hono-api-reference": "^0.12.10",
53
53
  "@tailwindcss/vite": "^4.3.3",
@@ -19,5 +19,6 @@ Fields and actions filled by a model, the assistant beside the admin, and MCP.
19
19
  - [MCP comments](../reference/mcp.md#comments): AI clients read a record's comments, and with MCP writes on add one after the person approves it.
20
20
  - [MCP batch writes](../reference/mcp.md#batch-writes): When MCP writes are on, AI clients make many creates, updates and deletes across resources in one transaction, after the person approves the batch.
21
21
  - [Connected apps](../reference/auth.md#oauth-for-mcp-clients): People allow an AI client to sign in, and disconnect it later.
22
- - [MCP tools of your own](../reference/mcp.md#the-apps-own-tools) (beta): `mcp/tools.ts` adds your own tools to the MCP endpoint, which AI clients call as the signed-in person, each change after the person approves it; for now a tool keeps its results small and catches its own refusals.
22
+ - [MCP tools of your own](../reference/mcp.md#the-apps-own-tools) (beta): `mcp/tools.ts` adds your own tools to the MCP endpoint, which AI clients call as the signed-in person, each change after the person approves it; a tool refuses as the built-in tools do, by throwing the API's problem.
23
+ - [MCP server for hosts](../reference/mcp.md#building-on-it): A server with its own sign-in serves tools of its own over MCP on Protobase's server, without app records, each with its title, `_meta`, refusals and approvals.
23
24
  - [MCP address](../reference/mcp.md#connecting-a-client): Signed-in people find the app's MCP address on their account page, copy it or add it to Claude in one click.
@@ -68,7 +68,7 @@ Making the query role needs `CREATEROLE` once, for the role that applies the mig
68
68
 
69
69
  `assistant/tools.ts` adds tools of the app's own, which the built-in backend offers after its two: its default export is an array of tools, found by that path when the app starts. A tool is `defineTool({ name, description, parameters, run })` from `@protobase/server`: `parameters` is the JSON Schema of its arguments, and `run(args, context)` returns the text the model reads. A name must be 1 to 64 letters, digits, `_` or `-`, and a name taken twice stops the server at startup. A backend elsewhere has its own tools, so the file does nothing there, nor with `assistant: false`. A tool's `annotations` are MCP's tool hints (`readOnlyHint`, `destructiveHint`, ...), which the dock ignores. The file serves the dock only, not the app's MCP endpoint: tools for AI clients over MCP go in `mcp/tools.ts`, which can export these too (see [The app's own tools](mcp.md#the-apps-own-tools) on MCP).
70
70
 
71
- `context` holds the user's `session`, `locale` (the language they read, `'en'` or `'nl'`, for the texts a tool shows them such as its approvals; set over MCP, absent in the dock), `show(part)` to add a part to the answer, `approve(request)` for an approval card, `conversation` (the dock's, absent over MCP), `resources()` (the resources the user may read, restricted to their fields, with their views), and `records`: `list(resource, params)` with the REST API's list parameters, `aggregate(resource, { group_by, filter?, limit?, label? })` with its counts per value as `:aggregate` gives them, `get(resource, key)`, `create(resource, data)`, `update(resource, key, data, { etag? })` (without an ETag it updates any version), `delete(resource, key, { etag? })` (soft on a resource with `.softDelete()`) and `softDeleteField(resource)` (the field a delete sets, or `undefined` when a delete removes the record) on the app's resources **as the user**, through their access rules, tenant scope, validation and write hooks, as in the REST API. `get`, `create`, `update` and `delete` each answer `{ record, etag }`, and `list` a page of records. `batch(operations)` runs the operations of a REST [batch write](api.md#batch-writes) in one transaction and answers its `results`; a refusal or a missing record rejects with an `HttpProblem`, whose `detail` a tool can return to the model as text.
71
+ `context` holds the user's `session`, `locale` (the language they read, `'en'` or `'nl'`, for the texts a tool shows them such as its approvals; set over MCP, absent in the dock), `show(part)` to add a part to the answer, `approve(request)` for an approval card, `conversation` (the dock's, absent over MCP), `resources()` (the resources the user may read, restricted to their fields, with their views), and `records`: `list(resource, params)` with the REST API's list parameters, `aggregate(resource, { group_by, filter?, limit?, label? })` with its counts per value as `:aggregate` gives them, `get(resource, key)`, `create(resource, data)`, `update(resource, key, data, { etag? })` (without an ETag it updates any version), `delete(resource, key, { etag? })` (soft on a resource with `.softDelete()`) and `softDeleteField(resource)` (the field a delete sets, or `undefined` when a delete removes the record) on the app's resources **as the user**, through their access rules, tenant scope, validation and write hooks, as in the REST API. `get`, `create`, `update` and `delete` each answer `{ record, etag }`, and `list` a page of records. `batch(operations)` runs the operations of a REST [batch write](api.md#batch-writes) in one transaction and answers its `results`; a refusal or a missing record rejects with an `HttpProblem`, whose `detail` a tool can return to the model as text, such as with `.catch(refused)`: `refused` from `@protobase/server` turns a refusal into `{ "error": <slug>, "message": <detail> }` and throws anything else again.
72
72
 
73
73
  A tool with a [widget](#widget-parts) creates or finds what it is about, shows a widget with its id, and returns the id and the state at that moment:
74
74
 
@@ -1,4 +1,4 @@
1
- <!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/mcp.md: run `bun run docs:skill`, do not edit. -->
1
+ <!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/mcp.mdx: run `bun run docs:skill`, do not edit. -->
2
2
 
3
3
  # MCP
4
4
 
@@ -162,15 +162,19 @@ A batch is checked before the person is asked: an operation other than a create,
162
162
 
163
163
  ## The app's own tools
164
164
 
165
- `mcp/tools.ts` adds tools of the app's own, offered after the built-in ones to everyone signed in, whatever `mcpEndpoint.writes` says. Its default export is an array of `defineTool(...)` from `@protobase/server`, the same shape as the [assistant's own tools](assistant.md#the-apps-own-tools): a `name` of 1 to 64 letters, digits, `_` or `-`, a `description` the client's model reads, `parameters`, the JSON Schema of its arguments, which the endpoint checks each call against, `annotations`, MCP's tool hints such as `readOnlyHint`, and `run(args, context)`, which returns the text the client reads. The file is found when the app starts, under `protobase dev` and `protobase serve` alike, and only with `auth.mcp` on; with `mcpEndpoint: false` it offers nothing.
165
+ `mcp/tools.ts` adds tools of the app's own, offered after the built-in ones to everyone signed in, whatever `mcpEndpoint.writes` says. Its default export is an array of `defineTool(...)` from `@protobase/server`, the same shape as the [assistant's own tools](assistant.md#the-apps-own-tools): a `name` of 1 to 64 letters, digits, `_` or `-`, a `description` the client's model reads, `parameters`, the JSON Schema of its arguments, which the endpoint checks each call against, `annotations`, MCP's tool hints such as `readOnlyHint`, optionally a `title` and `_meta`, which `tools/list` gives clients as they are, and `run(args, context)`, which returns the text the client reads. The file is found when the app starts, under `protobase dev` and `protobase serve` alike, and only with `auth.mcp` on; with `mcpEndpoint: false` it offers nothing.
166
166
 
167
167
  `context` holds the caller's `session`, the `locale` they read, `approve(request)`, `resources()`, the resources they may read, and `records`, the app's records **as that person**: their role's access rules, tenant, hidden fields, `validate` and write hooks apply, as in the REST API and the built-in tools. Its methods are the assistant's, listed [there](assistant.md#the-apps-own-tools); a refusal rejects with an `HttpProblem`. `show` does nothing over MCP, and there is no `conversation`.
168
168
 
169
+ A tool refuses by throwing an `HttpProblem` with a status below 500, such as `notFound(detail)` or `forbidden(slug, detail)`, or by letting through the one `records` rejects with: the client gets `{ "error": <slug>, "message": <detail> }` as an error result, as from the built-in tools, and nothing is reported. Anything else a tool throws is reported to the app's error handler and reaches the client as "The server failed to answer.", or, for a query the database stopped at its statement timeout, as a message naming the tool. `refusalOf(error)` from `@protobase/server` gives that `{ error, message }` text for a refusal and `undefined` otherwise, and `refused(error)` gives it or throws the error again, for a tool that returns a refusal as its result rather than as an error.
170
+
171
+ Nothing cuts a tool's result, as nothing cuts the built-in tools': a tool that can return a lot takes a limit or the fields to return, as `list_records` takes `page_size` and `fields`. <gap>A tool result larger than an AI client takes cannot be refused with a message naming what to leave out because a size guard is not implemented.</gap>
172
+
169
173
  A tool that reads:
170
174
 
171
175
  ```ts
172
176
  // mcp/tools.ts
173
- import { defineTool, HttpProblem } from '@protobase/server'
177
+ import { defineTool } from '@protobase/server'
174
178
 
175
179
  const openTasks = defineTool({
176
180
  name: 'open_tasks',
@@ -178,10 +182,7 @@ const openTasks = defineTool({
178
182
  parameters: { type: 'object', properties: { limit: { type: 'integer', minimum: 1, maximum: 50 } } },
179
183
  annotations: { readOnlyHint: true },
180
184
  run: async (args, { records }) => {
181
- const page = await records.list('tasks', { filter: 'status = "open"', order_by: 'created_at desc', page_size: Number(args.limit ?? 10), fields: ['id', 'title', 'due_on'] }).catch((error: unknown) => {
182
- if (error instanceof HttpProblem) return { error: error.slug, message: error.message }
183
- throw error
184
- })
185
+ const page = await records.list('tasks', { filter: 'status = "open"', order_by: 'created_at desc', page_size: Number(args.limit ?? 10), fields: ['id', 'title', 'due_on'] })
185
186
  return JSON.stringify(page)
186
187
  },
187
188
  })
@@ -222,8 +223,6 @@ export default [...assistantTools]
222
223
 
223
224
  A host calling `createAdmin` itself passes the tools as `options.mcp.tools`; the JSON config's `mcpEndpoint` takes none.
224
225
 
225
- <gap>An app's own MCP tool cannot have its result kept within a size budget, nor its refusals turned into `{ error, message }`, because neither is implemented for tools outside the built-in ones.</gap> Until then a tool keeps its own results small and catches its own refusals, as `open_tasks` above does; anything else it throws reaches the client as "The server failed to answer.".
226
-
227
226
  ## Browsers and hosts
228
227
 
229
228
  A client in a browser may call the endpoint from any origin: it sends its token, never cookies. A request addressed to another host than that of the app's [`url`](config/app.md#url) is refused, against DNS rebinding.
@@ -235,7 +234,7 @@ The endpoint serves each request on its own, without an MCP session: a `GET` or
235
234
  `@protobase/server` exports the pieces the endpoint is made of, for an MCP server of your own with tools of your own:
236
235
 
237
236
  - `mcpHandler({ authenticate, resource, scope, server })`: the endpoint as a fetch handler. It checks the host, admits a valid token (`mcpGate`) and serves the request with the server that `server(session, request)` builds for its caller; `request` is the HTTP request, such as for the language the person reads.
238
- - `mcpServer({ info, instructions, tools, context, report, rolesWithoutApproval, approvals })`: an MCP server named by `info`, its `name`, `version` and `icons`, such as `mcpIcons(origin, icons)`, the app's icons on `origin`, and offering [assistant tools](assistant.md#the-apps-own-tools), each with its JSON Schema and its `annotations`, MCP's tool hints. `context` is the tools' context without `approve`, with the `locale` its approvals are asked in (English when absent): each call gets its own, which asks the client's user, or approves at once when the session's user holds one of `rolesWithoutApproval`. `approvals` is `mcpApprovals()`, built once and passed to every server the endpoint builds: it keeps the approvals asked for until they are answered or expire. `instructions` is what it tells clients when they connect, by default `mcpInstructions`, the advice on reading records above.
237
+ - `mcpServer({ info, instructions, tools, context, report, rolesWithoutApproval, approvals })`: an MCP server named by `info`, its `name`, `version` and `icons`, such as `mcpIcons(origin, icons)`, the app's icons on `origin`, and offering [assistant tools](assistant.md#the-apps-own-tools), each with its JSON Schema, its `annotations`, MCP's tool hints, and its `title` and `_meta`, and each refusing as [the app's own tools](#the-apps-own-tools) do. `context` is the tools' context without `approve`, with the `locale` its approvals are asked in (English when absent): each call gets its own, which asks the client's user, or approves at once when the session's user holds one of `rolesWithoutApproval`. It is a `ToolContext`, with the app's `records`, `resources()` and `show`, unless every tool is declared with a smaller one, such as `defineTool<BaseToolContext>(...)`, whose context is only `session`, `locale` and `approve`: a host without app records then passes `{ session }`. `report` gets what a tool throws other than a refusal. `approvals` is `mcpApprovals()`, built once and passed to every server the endpoint builds: it keeps the approvals asked for until they are answered or expire. `instructions` is what it tells clients when they connect, by default `mcpInstructions`, the advice on reading records above.
239
238
  - `mcpTools({ db, sql, writes, commented, own })`: the endpoint's tools for a session; `writes` is `true`, `false` or `{ resources, rolesWithoutApproval }`, `commented` names the resources with comments, and `own` are [the app's own tools](#the-apps-own-tools), offered after the rest.
240
239
  - `readRecordTools()`, or `listResourcesTool()`, `describeResourceTool()`, `listRecordsTool()` and `getRecordTool()`: the read tools alone; `readOnlyQueryTool(db)` is `run_read_only_query`, and `readWriteQueryTool(db)` proposes one write statement that the user approves before it runs.
241
240
  - `commentTools({ commented }, writes)`, or `listCommentsTool({ commented })` and `addCommentTool({ commented, resources })`: the comment tools on the resources named in `commented`, which call the context's `comments`, and `add_comment` only when `writes` is not `false`.
@@ -245,36 +244,53 @@ The endpoint serves each request on its own, without an MCP session: a `GET` or
245
244
 
246
245
  A host that builds its own server around `createAdmin`, rather than running the project with `protobase dev` or `protobase serve`, can serve its own endpoint at `auth.mcp.path` instead: `mcpEndpoint: false` (`createAdmin`'s `options.mcp`) turns Protobase's off, and the host mounts its own in front of `createAdmin`'s app. The OAuth provider and its discovery stay, and `createAdmin` answers 404 at the path and under it. `protobase dev` and `protobase serve` have no place to mount another endpoint, so in a project served by them `mcpEndpoint: false` only turns the endpoint off.
247
246
 
248
- A tool of your own goes on the server `server` returns, with the context its tools get. `authenticate` is the host's own `Authenticator`: it verifies the client's access token for the endpoint's address, its audience, and returns a session with the `client` the token was issued to:
247
+ A tool of your own goes on the server `server` returns, with the context its tools get. `authenticate` is the host's own `Authenticator`, such as [`jwtAuthenticator(...)`](auth.md): it verifies the client's access token for the endpoint's address, its audience, and returns a session with the `client` the token was issued to. A host with no app records, such as a console that reads its own database, declares its tools with `BaseToolContext` and passes `{ session }`; here a tool with a `title` and `_meta`, which refuses another person's project and asks before it archives one:
249
248
 
250
- ```ts
251
- import { defineTool, mcpApprovals, mcpHandler, mcpServer, type Session, type ToolContext } from '@protobase/server'
249
+ ```ts title="mcp-endpoint.ts"
250
+ import { defineTool, mcpApprovals, mcpHandler, mcpServer, notFound, type Authenticator, type BaseToolContext } from '@protobase/server'
252
251
 
253
- const ping = defineTool({
254
- name: 'ping',
255
- description: 'Answers pong.',
256
- parameters: { type: 'object', properties: {} },
257
- annotations: { readOnlyHint: true },
258
- run: async () => 'pong',
259
- })
252
+ // The host's own data: here a map, in a real host its own database or API.
253
+ const projects = new Map([[1, { id: 1, name: 'Website', owner: 'ann', archived: false }]])
260
254
 
261
- const context = (session: Session): Omit<ToolContext, 'approve'> => ({
262
- session,
263
- records: myRecords(session),
264
- resources: async () => ({ models: [], views: [] }),
265
- show: () => {},
255
+ // Shown to clients that read `securitySchemes`: each tool needs the person's sign-in.
256
+ const signedIn = { securitySchemes: [{ type: 'oauth2', scopes: ['mcp'] }] }
257
+
258
+ const ownProject = (id: unknown, userId: string | number) => {
259
+ const project = projects.get(Number(id))
260
+ if (!project || project.owner !== userId) throw notFound(`No project ${String(id)}`)
261
+ return project
262
+ }
263
+
264
+ export const archiveProject = defineTool<BaseToolContext>({
265
+ name: 'archive_project',
266
+ title: 'Archive a project',
267
+ description: 'Archives one of the person\'s projects.',
268
+ parameters: { type: 'object', properties: { id: { type: 'integer' } }, required: ['id'] },
269
+ annotations: { readOnlyHint: false, destructiveHint: false },
270
+ _meta: signedIn,
271
+ run: async (args, { session, approve }) => {
272
+ const project = ownProject(args.id, session.user.id)
273
+ const answer = await approve({ title: `Archive project "${project.name}"?`, approveLabel: 'Archive' })
274
+ if (answer !== 'approved') return JSON.stringify({ archived: false })
275
+ project.archived = true
276
+ return JSON.stringify({ archived: true })
277
+ },
266
278
  })
267
279
 
268
280
  const approvals = mcpApprovals()
269
281
 
270
- const handler = mcpHandler({
271
- authenticate: verifyMcpToken,
272
- resource: 'https://admin.example.com/api/mcp',
273
- scope: 'mcp',
274
- server: (session) => mcpServer({ info: { name: 'My app', version: '1.0.0' }, tools: [ping], context: context(session), report: console.error, approvals }),
275
- })
282
+ /** The host's MCP endpoint, admitting the tokens `authenticate`, the host's own `Authenticator`, verifies. */
283
+ export const projectsEndpoint = (authenticate: Authenticator) =>
284
+ mcpHandler({
285
+ authenticate,
286
+ resource: 'https://console.example.com/api/mcp',
287
+ scope: 'mcp',
288
+ server: (session) => mcpServer({ info: { name: 'Console', version: '1.0.0' }, tools: [archiveProject], context: { session }, report: console.error, approvals }),
289
+ })
276
290
  ```
277
291
 
292
+ A host that wants the person's records in the app instead serves the app's endpoint and passes its tools as `options.mcp.tools` on `createAdmin`, as in [the app's own tools](#the-apps-own-tools).
293
+
278
294
  A tool that changes something calls `context.approve(...)` before it changes anything, and only once: over MCP the client asks the person, then calls the tool again from the start with their answer, and `approve` then resolves with it.
279
295
 
280
296
  `mcpServer` returns the SDK's `McpServer`, so `registerTool` on it adds a tool written against the SDK directly.