@softr/sdk 1.13.2 → 1.15.0
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.
- package/CHANGELOG.md +16 -0
- package/dist/auth/methods/authenticator.js +8 -1
- package/dist/auth/methods/code.js +3 -1
- package/dist/auth/methods/emailCode2fa.js +3 -1
- package/dist/auth/methods/magicLink.js +2 -1
- package/dist/auth/methods/password.js +3 -1
- package/dist/auth/methods/passwordLink.js +3 -1
- package/dist/auth/methods/profile.js +3 -1
- package/dist/auth/methods/signup.js +3 -1
- package/dist/auth/methods/social.js +2 -1
- package/dist/auth/platform.js +14 -8
- package/dist/auth/session.js +18 -3
- package/dist/core/email.js +2 -1
- package/dist/core/file-upload.js +2 -1
- package/dist/core/integration.js +4 -2
- package/dist/core/workflow.js +2 -1
- package/dist/datasource/datasource.d.ts +2 -1
- package/dist/datasource/datasource.js +2 -1
- package/dist/datasource/record.d.ts +44 -1
- package/dist/datasource/record.js +74 -6
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/mcp.d.ts +9 -2
- package/dist/mcp.js +102 -22
- package/dist/package.js +1 -1
- package/dist/tool.d.ts +64 -0
- package/dist/tool.js +51 -0
- package/package.json +1 -1
- package/skills/softr-sdk/SKILL.md +1 -1
- package/skills/softr-sdk/ai/SKILL.md +1 -1
- package/skills/softr-sdk/auth/SKILL.md +1 -1
- package/skills/softr-sdk/charts-and-metrics/SKILL.md +3 -1
- package/skills/softr-sdk/current-user/SKILL.md +1 -1
- package/skills/softr-sdk/email/SKILL.md +1 -1
- package/skills/softr-sdk/file-uploads/SKILL.md +1 -1
- package/skills/softr-sdk/formatting/SKILL.md +1 -1
- package/skills/softr-sdk/integrations/SKILL.md +1 -1
- package/skills/softr-sdk/mcp/SKILL.md +75 -17
- package/skills/softr-sdk/push-notifications/SKILL.md +1 -1
- package/skills/softr-sdk/pwa/SKILL.md +1 -1
- package/skills/softr-sdk/record-mutations/SKILL.md +1 -1
- package/skills/softr-sdk/record-queries/SKILL.md +57 -13
- package/skills/softr-sdk/region-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/workflows/SKILL.md +1 -1
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: mcp
|
|
3
3
|
description: >-
|
|
4
|
-
Open the app to AI assistants over MCP: softrMcpMiddleware({ actions })
|
|
5
|
-
@softr/sdk, registered in src/start.ts ahead of softrAuthMiddleware,
|
|
6
|
-
actions you list into tools an assistant can
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
Open the app to AI assistants over MCP: softrMcpMiddleware({ actions, tools })
|
|
5
|
+
from @softr/sdk, registered in src/start.ts ahead of softrAuthMiddleware,
|
|
6
|
+
turns the actions and defineTool tools you list into tools an assistant can
|
|
7
|
+
call as the signed-in visitor. defineTool runs the app's own logic, labelled
|
|
8
|
+
read-only or not. The list is explicit — anything left out is invisible over
|
|
9
|
+
MCP. The app's own login must land on toReturnPath, or the approval step
|
|
10
|
+
dead-ends.
|
|
9
11
|
metadata:
|
|
10
12
|
type: sub-skill
|
|
11
13
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
14
|
+
library_version: 1.15.0
|
|
13
15
|
---
|
|
14
16
|
|
|
15
|
-
**Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
|
|
17
|
+
**Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP. The tools you hand it come from `defineTool`, also on `@softr/sdk`, and call the same server logic the pages do.
|
|
16
18
|
|
|
17
19
|
# MCP
|
|
18
20
|
|
|
19
|
-
An app can answer an AI assistant on its own origin. The assistant signs in as an ordinary app user, and the actions you list become the tools it may call. Not wired by default — a new app serves no MCP endpoint until you add the middleware.
|
|
21
|
+
An app can answer an AI assistant on its own origin. The assistant signs in as an ordinary app user, and the actions and tools you list become the tools it may call. Not wired by default — a new app serves no MCP endpoint until you add the middleware.
|
|
20
22
|
|
|
21
23
|
## The URL
|
|
22
24
|
|
|
@@ -34,12 +36,13 @@ Custom domain, default subdomain, a staging host — whatever origin the app is
|
|
|
34
36
|
// src/start.ts
|
|
35
37
|
import { createCsrfMiddleware, createStart } from '@tanstack/react-start'
|
|
36
38
|
import { softrAuthMiddleware, softrMcpMiddleware } from '@softr/sdk'
|
|
37
|
-
import { listCafes
|
|
39
|
+
import { listCafes } from './actions'
|
|
40
|
+
import { addCafe, findCafes } from './mcp-tools'
|
|
38
41
|
|
|
39
42
|
export const startInstance = createStart(() => ({
|
|
40
43
|
requestMiddleware: [
|
|
41
44
|
createCsrfMiddleware({ filter: (ctx) => ctx.handlerType === 'serverFn' }),
|
|
42
|
-
softrMcpMiddleware({ actions: [listCafes, addCafe] }),
|
|
45
|
+
softrMcpMiddleware({ actions: [listCafes], tools: [findCafes, addCafe] }),
|
|
43
46
|
softrAuthMiddleware(),
|
|
44
47
|
],
|
|
45
48
|
}))
|
|
@@ -47,16 +50,66 @@ export const startInstance = createStart(() => ({
|
|
|
47
50
|
|
|
48
51
|
Order is part of the contract. `softrMcpMiddleware` goes **before** `softrAuthMiddleware`, and that is the only order it supports.
|
|
49
52
|
|
|
50
|
-
##
|
|
53
|
+
## Tools from the app's own logic
|
|
54
|
+
|
|
55
|
+
An action is one list, create, update or delete on one datasource. When the app does more than that — a number from a counter, an activity log entry, an email, an author stamped on the record — a tool built from the action skips it. Use `defineTool` and call the logic the app's pages already run:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// src/mcp-tools.ts
|
|
59
|
+
import { defineTool } from '@softr/sdk'
|
|
60
|
+
import { ds } from '@softr/sdk/server'
|
|
61
|
+
import { z } from 'zod'
|
|
62
|
+
import { createIssueFor } from './issues'
|
|
63
|
+
import { projectIssues } from './actions'
|
|
64
|
+
|
|
65
|
+
export const createIssue = defineTool({
|
|
66
|
+
name: 'create_issue',
|
|
67
|
+
title: 'Create issue',
|
|
68
|
+
description: 'Create an issue in a project. Returns the new issue with its key.',
|
|
69
|
+
input: z.object({
|
|
70
|
+
projectId: z.string().describe('The project record id.'),
|
|
71
|
+
title: z.string().describe('A one-line summary.'),
|
|
72
|
+
}),
|
|
73
|
+
readOnly: false,
|
|
74
|
+
destructive: false,
|
|
75
|
+
handler: async (input, { user }) => createIssueFor(user, input),
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
export const findIssues = defineTool({
|
|
79
|
+
name: 'find_issues',
|
|
80
|
+
description: 'Issues in a project, newest first. Filter by status.',
|
|
81
|
+
input: z.object({
|
|
82
|
+
projectId: z.string().describe('The project record id.'),
|
|
83
|
+
status: z.enum(['open', 'done']).optional().describe('Only issues with this status.'),
|
|
84
|
+
}),
|
|
85
|
+
readOnly: true,
|
|
86
|
+
handler: async (input) => {
|
|
87
|
+
let list = ds.list(projectIssues).where((q) => q.project.isOneOf([input.projectId]))
|
|
88
|
+
if (input.status) list = list.where((q) => q.status.is(input.status))
|
|
89
|
+
const { records } = await list.orderBy((q) => q.createdAt.desc).limit(50)
|
|
90
|
+
return { issues: records.map((r) => ({ id: r.id, ...r.fields })) }
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- **`handler` runs as the signed-in user.** `ds` calls inside it act as that user, with their groups, as a click in the app would. `user` is the same `User` a server function reads from `context.user`. Move logic a server function and a tool share into a function that takes the user, and pass `context.user` from the server function and `user` from the tool.
|
|
96
|
+
- **`readOnly` is required.** `true` only when the tool changes nothing. A read-only tool that calls `ds.create`, `ds.update`, `ds.delete`, `email`, `workflow`, `upload` or an `integration` action whose method is not `GET` or `HEAD` from `@softr/sdk/server` fails with an error, and nothing is written or sent. An integration that reads with `POST`, such as a search or GraphQL API, fails too: make that tool `readOnly: false`. So does every `auth` call that is not a plain read: a sign-in, a sign-up, a password reset, a sign-out, a profile change or an authenticator change. Your own calls to other services are not stopped, so keep them out of a read-only tool too. An `ai` call is not stopped either: it changes no data, but it spends the workspace's AI credits for an assistant approved to read only, so use it there on purpose.
|
|
97
|
+
- **`destructive`** says whether a tool that is not read-only deletes or overwrites data: `true` when it does, `false` when it only adds. The assistant may ask the user before it runs a destructive one. Left out, the assistant is told neither and treats the tool as one that may delete or overwrite, so set it on every tool that is not read-only.
|
|
98
|
+
- **`input` is a zod object.** Describe each field with `.describe()`: the assistant reads it. Use types JSON can carry: a string for a date, not `z.date()`, which fails when the tool is defined. A top-level argument the schema does not name is refused with a message saying which, and the handler does not run. Inside a nested object an unknown key is dropped without a word, so declare a nested object with `z.strictObject` to have it refused too.
|
|
99
|
+
- **`description` says what the tool does and when to use it.** The assistant picks tools by it.
|
|
100
|
+
- **Return JSON.** An object is best: the assistant gets it as structured data.
|
|
101
|
+
- **What the handler throws is shown to the assistant.** Throw an `Error` whose message says what went wrong in words the assistant can act on. The message of any other throw reaches it too, so catch a failed call to another service and throw your own message, not one that may carry a URL or a key.
|
|
102
|
+
|
|
103
|
+
## Choosing the tools
|
|
51
104
|
|
|
52
105
|
The list is the whole tool surface. Write it out:
|
|
53
106
|
|
|
54
|
-
- An action you leave out is invisible to the assistant, even where the app's pages use it freely.
|
|
55
|
-
- An action you list still obeys its own `permissions
|
|
56
|
-
-
|
|
57
|
-
- Two actions with the same `id` throw at startup. So does anything that is not a `defineQuery` or `
|
|
107
|
+
- An action or tool you leave out is invisible to the assistant, even where the app's pages use it freely.
|
|
108
|
+
- An action you list still obeys its own `permissions`, and a tool's `ds` calls obey theirs. Listing it does not widen who may run it — the assistant runs as whoever signed in, with that user's groups.
|
|
109
|
+
- Anything that changes data reaches only an assistant the user allowed to make changes when they connected it. One allowed to read only sees the reads, the read-only tools and `whoami`.
|
|
110
|
+
- Two actions with the same `id` throw at startup, and so does a tool whose `name` is taken. So does anything that is not a `defineQuery`, `defineMutation` or `defineTool` value.
|
|
58
111
|
|
|
59
|
-
|
|
112
|
+
Name every tool back to the owner, saying which ones may change data.
|
|
60
113
|
|
|
61
114
|
## The login round trip
|
|
62
115
|
|
|
@@ -68,7 +121,11 @@ Connecting starts with an approval page the app serves. Most people hit it signe
|
|
|
68
121
|
|
|
69
122
|
## Failure modes
|
|
70
123
|
|
|
71
|
-
**Registering with no actions.** `softrMcpMiddleware()` is valid and serves an endpoint with nothing on it. The assistant connects, lists tools, finds only `whoami`. Pass
|
|
124
|
+
**Registering with no actions or tools.** `softrMcpMiddleware()` is valid and serves an endpoint with nothing on it. The assistant connects, lists tools, finds only `whoami`. Pass them.
|
|
125
|
+
|
|
126
|
+
**A write as an action, where the app has logic around it.** The record is written and everything the app does around it is skipped: no counter, no log entry, no email, no author. Write it as a `defineTool` that calls that logic.
|
|
127
|
+
|
|
128
|
+
**Marking a tool read-only that changes data.** Its writes fail when it runs. Set `readOnly: false`.
|
|
72
129
|
|
|
73
130
|
**Registering after `softrAuthMiddleware`.** Not a supported order. Put the MCP middleware first.
|
|
74
131
|
|
|
@@ -82,4 +139,5 @@ Connecting starts with an approval page the app serves. Most people hit it signe
|
|
|
82
139
|
|
|
83
140
|
- [record-queries/SKILL.md](../record-queries/SKILL.md): declaring the read actions you expose
|
|
84
141
|
- [record-mutations/SKILL.md](../record-mutations/SKILL.md): declaring the write actions you expose
|
|
142
|
+
- [current-user/SKILL.md](../current-user/SKILL.md): the `User` a tool's handler gets
|
|
85
143
|
- [auth/SKILL.md](../auth/SKILL.md): the sign-in and second-factor flows the round trip passes through
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.
|
|
13
|
+
library_version: 1.15.0
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: record-queries
|
|
3
3
|
description: >-
|
|
4
|
-
Fetch records with defineQuery, ds.list and ds.
|
|
5
|
-
names of your own choosing, pagination and its limits, and the
|
|
6
|
-
keeps each visitor to their own records, with one query per role
|
|
7
|
-
fields that role may see. Use to put records on a page
|
|
8
|
-
|
|
9
|
-
unsorted or missing a field. Writes
|
|
10
|
-
rows are charts-and-metrics.
|
|
4
|
+
Fetch records with defineQuery, ds.list, ds.get and ds.export: where and
|
|
5
|
+
orderBy, field names of your own choosing, pagination and its limits, and the
|
|
6
|
+
filter that keeps each visitor to their own records, with one query per role
|
|
7
|
+
for the fields that role may see. Use to put records on a page, fill a
|
|
8
|
+
dropdown or export them to a file, to write any server function that reads a
|
|
9
|
+
table, or when a read comes back empty, unsorted or missing a field. Writes
|
|
10
|
+
are record-mutations; totals and chart rows are charts-and-metrics.
|
|
11
11
|
metadata:
|
|
12
12
|
type: sub-skill
|
|
13
13
|
library: softr-sdk
|
|
14
|
-
library_version: 1.
|
|
14
|
+
library_version: 1.15.0
|
|
15
15
|
---
|
|
16
16
|
|
|
17
17
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A route `loader` also runs in the browser, so it calls a server function, never `ds`. A helper that calls `ds` outside a handler, in this file or in another one, goes in `createServerOnlyFn` from `@tanstack/react-start`, or inside the handler. An exported helper without it keeps the server-only import in the browser bundle, and every page stops loading until it moves. Do not make such a helper a server function when it takes an id: any visitor can call a server function with any id. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -127,19 +127,63 @@ if (first.nextOffset !== null) {
|
|
|
127
127
|
|
|
128
128
|
`nextOffset` is `null` when there are no more pages. `total` is the count of all matching records, not the count returned.
|
|
129
129
|
|
|
130
|
-
To read every
|
|
130
|
+
To read every matching record, use `ds.export` (see [Export records](#export-records)), not a loop over `ds.list` pages.
|
|
131
|
+
|
|
132
|
+
Pass `nextOffset` back unchanged, and type whatever carries it (a validator, a state, a search param) as `number | string | null`. Most datasources answer a number, the row position, and for those a page can also be reached directly, `.offset((page - 1) * PAGE_SIZE)`. Some datasources (BigQuery is one) answer a string, their own cursor: it reads only the page after the one that answered it, with the same `where` and `orderBy`, and a read with a computed number fails. For those, build "Load more" or next-page paging from `nextOffset`, never numbered pages.
|
|
133
|
+
|
|
134
|
+
### Keep a large table on the server
|
|
135
|
+
|
|
136
|
+
A read returns at most 100 records, and a cursor datasource reads a page only after the one before it. A loop over every page makes one request per 100 records, one after another, and the page shows nothing until the last one answers. On a table of thousands of records, that is a long wait each time the page opens.
|
|
137
|
+
|
|
138
|
+
So a list or table page reads one page at a time:
|
|
139
|
+
|
|
140
|
+
- Search, filters and sort go into `where` and `orderBy`, and the server function takes them as input. A change reads the first page again.
|
|
141
|
+
- "Load more" or a next-page button reads the next page from `nextOffset`.
|
|
142
|
+
- Counts, totals, averages, group tables and summary strips come from `ds.metric` and `ds.chart` with the same `where` (see [charts-and-metrics](../charts-and-metrics/SKILL.md)), never from math over loaded records.
|
|
143
|
+
- Every matching record is for a download the visitor starts, and comes from `ds.export` (see [Export records](#export-records)). Never read every record to show a page, unless the user asks for it again (see below).
|
|
131
144
|
|
|
132
145
|
```ts
|
|
133
|
-
|
|
146
|
+
// Wrong: every page before anything shows, then filters and figures in the browser.
|
|
134
147
|
let offset: number | string | null = null
|
|
135
148
|
do {
|
|
136
|
-
const page = await ds.list(
|
|
137
|
-
|
|
149
|
+
const page = await ds.list(customers).limit(100).offset(offset)
|
|
150
|
+
rows.push(...page.records)
|
|
138
151
|
offset = page.nextOffset
|
|
139
152
|
} while (offset !== null)
|
|
153
|
+
|
|
154
|
+
// Right: one page of the records that match the visitor's filters.
|
|
155
|
+
const listCustomers = createServerFn()
|
|
156
|
+
.validator(z.object({ search: z.string().optional(), offset: z.union([z.number(), z.string()]).nullable() }))
|
|
157
|
+
.handler(async ({ data }) => {
|
|
158
|
+
const { search, offset } = data
|
|
159
|
+
let list = ds.list(customers).orderBy((q) => q.signedUpAt.desc).limit(50).offset(offset)
|
|
160
|
+
if (search) list = list.where((q) => q.name.contains(search))
|
|
161
|
+
return await list
|
|
162
|
+
})
|
|
140
163
|
```
|
|
141
164
|
|
|
142
|
-
|
|
165
|
+
A request to load the whole table into the browser, so that filters feel instant, asks for a fast page. Build it as above, and tell the user in your reply why the reads stay on the server. When they ask for it again after that reply, build it as they ask: the page reads its records with `ds.export`. Say in your reply what it costs: the page reads every record each time it opens, so it opens more slowly as the table grows.
|
|
166
|
+
|
|
167
|
+
### Export records
|
|
168
|
+
|
|
169
|
+
`ds.export` reads every record that matches, for a file the visitor downloads or copies: a CSV, a JSON file, a spreadsheet. It takes the same `where` and `orderBy` as `ds.list`, and no `limit` or `offset`. Give it the list's filters, so the file holds what the visitor sees:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
const exportCustomers = createServerFn()
|
|
173
|
+
.validator(z.object({ search: z.string().optional() }))
|
|
174
|
+
.handler(async ({ data }) => {
|
|
175
|
+
const { search } = data
|
|
176
|
+
let rows = ds.export(customers).orderBy((q) => q.signedUpAt.desc)
|
|
177
|
+
if (search) rows = rows.where((q) => q.name.contains(search))
|
|
178
|
+
return await rows
|
|
179
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
- It returns `{ records }`, every matching record. Each record has the same shape as a `ds.list` record.
|
|
183
|
+
- A large table takes seconds. Call it from the button that starts the download, and show that the file is being prepared. Never call it from a loader or when a page opens, unless the user asks again for the whole table in the browser (see above).
|
|
184
|
+
- Each call to the datasource reads 1,000 records. When the datasource's API refuses pages that big, or the user names a page size, set it with `.pageSize(200)`, from 1 to 1,000.
|
|
185
|
+
- Build the file from `records`, in the format the user asked for.
|
|
186
|
+
- Every exported record reaches the visitor, so the query reads through the same `filter` as the list, and declares only the fields the visitor may download.
|
|
143
187
|
|
|
144
188
|
### Populate a control from a linked table
|
|
145
189
|
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.15.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.15.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.
|