picked 0.0.111-next.3 → 0.0.111

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.
Files changed (75) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/docs/explanation/app-authentication.md +178 -0
  3. package/docs/explanation/apps.md +169 -0
  4. package/docs/explanation/conditions-and-the-logic-engine.md +12 -2
  5. package/docs/explanation/feature-flag-allocation.md +5 -3
  6. package/docs/how-to/apps/add-a-page-to-the-dashboard.md +257 -0
  7. package/docs/how-to/apps/add-a-workflow-tool.md +199 -0
  8. package/docs/how-to/apps/add-an-mcp-server-to-an-app.md +210 -0
  9. package/docs/how-to/apps/connect-a-third-party-service-with-oauth.md +307 -0
  10. package/docs/how-to/apps/create-an-app-in-the-dashboard.md +162 -0
  11. package/docs/how-to/apps/deploy-an-app.md +201 -0
  12. package/docs/how-to/apps/develop-an-app-locally.md +152 -0
  13. package/docs/how-to/apps/host-an-app-on-vercel.md +226 -0
  14. package/docs/how-to/apps/install-an-app.md +116 -0
  15. package/docs/how-to/apps/link-to-an-installation-from-your-site.md +134 -0
  16. package/docs/how-to/apps/read-project-data-from-an-app.md +225 -0
  17. package/docs/how-to/apps/rotate-an-app-key.md +130 -0
  18. package/docs/how-to/apps/run-code-when-a-workspace-publishes.md +148 -0
  19. package/docs/how-to/apps/store-data-for-an-installation.md +230 -0
  20. package/docs/how-to/apps/test-an-app.md +359 -0
  21. package/docs/how-to/apps/upgrade-or-uninstall-an-app.md +97 -0
  22. package/docs/how-to/apps/use-an-app-tool-in-a-workflow.md +112 -0
  23. package/docs/index.md +6 -3
  24. package/docs/reference/apps/app-kit.md +811 -0
  25. package/docs/reference/apps/environment.md +116 -0
  26. package/docs/reference/apps/manifest.md +311 -0
  27. package/docs/reference/apps/protocol.md +497 -0
  28. package/docs/reference/dashboard/apps.md +504 -0
  29. package/docs/reference/dashboard/external-apis.md +5 -0
  30. package/docs/reference/dashboard/features.mdx +5 -5
  31. package/docs/reference/dashboard/labels.md +25 -8
  32. package/docs/reference/dashboard/lead-search.md +6 -6
  33. package/docs/reference/dashboard/reports.mdx +13 -5
  34. package/docs/reference/dashboard/workflow-tools.md +14 -0
  35. package/docs/reference/glossary.md +153 -140
  36. package/docs/reference/management-api/authentication.md +36 -27
  37. package/docs/reference/management-api/authorization-and-scopes.md +73 -21
  38. package/docs/reference/management-api/schema/api-key.md +10 -10
  39. package/docs/reference/management-api/schema/app.md +726 -0
  40. package/docs/reference/management-api/schema/auth.md +7 -7
  41. package/docs/reference/management-api/schema/common.md +54 -35
  42. package/docs/reference/management-api/schema/feature.md +1 -1
  43. package/docs/reference/management-api/schema/feedback.md +1 -1
  44. package/docs/reference/management-api/schema/index.md +35 -20
  45. package/docs/reference/management-api/schema/label.md +31 -8
  46. package/docs/reference/management-api/schema/organization.md +66 -66
  47. package/docs/reference/management-api/schema/project.md +5 -5
  48. package/docs/reference/management-api/schema/report.md +1 -1
  49. package/docs/reference/management-api/schema/shopify.md +19 -19
  50. package/docs/reference/management-api/schema/workflow.md +6 -1
  51. package/docs/reference/management-api/schema/workspace.md +2 -2
  52. package/docs/reference/sdk/analytics-events.md +42 -36
  53. package/docs/reference/sdk/conditions.md +3 -3
  54. package/docs/reference/sdk/fields.md +5 -4
  55. package/docs/reference/sdk/index.md +4 -4
  56. package/docs/reference/sdk/plugins/create-experience.md +11 -9
  57. package/docs/reference/sdk/plugins/hotjar.md +2 -2
  58. package/docs/reference/sdk/plugins/sentry.md +2 -2
  59. package/docs/reference/sdk/plugins/shopify.md +2 -2
  60. package/docs/reference/sdk/predicates.md +5 -5
  61. package/docs/reference/sdk/type-generation.md +24 -19
  62. package/docs/reference/tools/admin-api-client.md +15 -0
  63. package/docs/reference/tools/cli.md +192 -41
  64. package/docs/reference/tools/mcp.md +30 -9
  65. package/docs/tutorials/first-app.md +308 -0
  66. package/package.json +4 -4
  67. package/docs/explanation/how-pickedql-works.md +0 -76
  68. package/docs/how-to/analytics/index.md +0 -324
  69. package/docs/reference/management-api/schema/analytics.md +0 -75
  70. package/docs/reference/pickedql/data-sources.md +0 -174
  71. package/docs/reference/pickedql/dimensions.md +0 -103
  72. package/docs/reference/pickedql/filtering.md +0 -64
  73. package/docs/reference/pickedql/index.md +0 -18
  74. package/docs/reference/pickedql/schema.md +0 -172
  75. package/docs/reference/pickedql/virtual-functions.md +0 -248
@@ -0,0 +1,199 @@
1
+ ---
2
+ type: how-to
3
+ layout: docs
4
+ ---
5
+
6
+ # How to add a workflow tool to an app
7
+
8
+ This guide shows you how to add a tool to your app that workflows can call. When a workflow
9
+ node runs the tool, Picked sends the call to your server. Your server does the work and
10
+ answers with the output.
11
+
12
+ You need an app with a server that uses `@picked/app-kit`, and a public `endpoint`.
13
+ [Build and install your first app](/tutorials/first-app) makes these.
14
+
15
+ ## 1. Declare the tool
16
+
17
+ Add the tool to `workflowTools` in the manifest. The manifest must have an `endpoint`,
18
+ because Picked sends each call to `<endpoint>/tools/<name>`.
19
+
20
+ ```ts [picked.config.ts]
21
+ import { defineConfig } from '@picked/config/define'
22
+
23
+ export default defineConfig({
24
+ app: {
25
+ handle: 'my-app',
26
+ name: 'My App',
27
+ endpoint: 'https://my-app.example.com',
28
+ workflowTools: [
29
+ {
30
+ name: 'create_ticket',
31
+ label: 'Create ticket',
32
+ description: 'Opens a support ticket for a lead.',
33
+ effect: 'write',
34
+ arguments: {
35
+ email: { label: 'Email', type: 'string', required: true },
36
+ subject: { label: 'Subject', type: 'string', required: true },
37
+ priority: { label: 'Priority', type: 'string', options: ['low', 'normal', 'high'] },
38
+ },
39
+ output: {
40
+ ticketId: { label: 'Ticket ID', type: 'string' },
41
+ url: { label: 'Ticket URL', type: 'string', optional: true },
42
+ },
43
+ },
44
+ ],
45
+ },
46
+ })
47
+ ```
48
+
49
+ Set these fields correctly:
50
+
51
+ - **`name`.** A lower-case letter, then lower-case letters, digits and `_`. Your server
52
+ finds the tool by this name.
53
+ - **`effect`.** Set `write` when a call changes data outside the run. A test run calls a
54
+ `write` tool only when **Execute writes** is on. Set `read` for a tool that only reads.
55
+ - **`arguments`.** The fields that a node sets. Each has a `type`: `string`, `number`,
56
+ `boolean` or `json`.
57
+ - **`output`.** The values that your answer must contain. Set `optional: true` on a value
58
+ that an answer can omit.
59
+
60
+ [Workflow tools](/reference/apps/manifest#workflow-tools) gives each field.
61
+
62
+ ## 2. Write the tool
63
+
64
+ Give the tool to `createAppHandler`, in `tools`, with the `name` from the manifest. The
65
+ function gets the call and returns the output object.
66
+
67
+ ```ts [src/server.ts]
68
+ import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
69
+ import { serve } from '@picked/app-kit/node'
70
+
71
+ const port = process.env.APP_PORT ?? '4103'
72
+ const { pickedKeys } = readAppEnvironment(process.env, port)
73
+
74
+ const handler = createAppHandler({
75
+ pickedKeys,
76
+ tools: {
77
+ async create_ticket(call) {
78
+ const email = typeof call.arguments.email === 'string' ? call.arguments.email : ''
79
+ const subject = typeof call.arguments.subject === 'string' ? call.arguments.subject : ''
80
+ if (email === '') throw new Error('email is required')
81
+ if (call.run.test) return { ticketId: 'test-run' }
82
+
83
+ const response = await fetch('https://helpdesk.example.com/api/tickets', {
84
+ method: 'POST',
85
+ headers: {
86
+ 'content-type': 'application/json',
87
+ 'idempotency-key': call.idempotencyKey,
88
+ },
89
+ body: JSON.stringify({ email, subject, priority: call.arguments.priority ?? 'normal' }),
90
+ })
91
+ if (!response.ok) throw new Error(`The help desk answered ${response.status}`)
92
+ const ticket = await response.json()
93
+ return { ticketId: String(ticket.id), url: String(ticket.url) }
94
+ },
95
+ },
96
+ })
97
+
98
+ serve(handler, Number(port))
99
+ ```
100
+
101
+ `createAppHandler` checks that Picked signed the request. It then calls the function and
102
+ answers with `{ "output": … }`.
103
+
104
+ Use these fields of the call:
105
+
106
+ - **`call.arguments`.** The arguments of the node, after Picked resolves them.
107
+ - **`call.idempotencyKey`.** The same value for each send of one call. See step 3.
108
+ - **`call.run.test`.** `true` in a test run. See step 4.
109
+ - **`call.token`.** A token for the Picked API, with the scopes of the installation.
110
+
111
+ [Workflow tool calls](/reference/apps/protocol#workflow-tool-calls) gives each field.
112
+
113
+ Check the type of each argument before you use it. An argument that is not `required` can
114
+ be absent.
115
+
116
+ Picked checks the output against `output` in the manifest. If a value is absent and not
117
+ `optional`, or has a different type, the node fails.
118
+
119
+ ## 3. Make writes safe to repeat
120
+
121
+ Picked can send one call more than one time. It sends the call again after a `408`, a
122
+ `429` or a `5xx` answer, or when it gets no answer. Each send of one call has the same
123
+ `idempotencyKey`. Picked also sends the key in the `idempotency-key` header.
124
+
125
+ Do one of these:
126
+
127
+ - If the service that you write to accepts an `idempotency-key` header, send `call.idempotencyKey`
128
+ to it. The example in step 2 sends it in the `idempotency-key` header.
129
+ - If not, record the output of each call. Before you write, look for a record with the
130
+ key. If you find one, return that output.
131
+
132
+ The key is `<runId>:<nodeId>`. A node that runs two times in one run, in a loop, sends the
133
+ same key two times. If the loop calls the tool for different work, add the arguments to
134
+ the record key.
135
+
136
+ To keep the record in Picked, use a label of the installation.
137
+ [How to store data for an installation](/how-to/apps/store-data-for-an-installation) shows
138
+ how.
139
+
140
+ ## 4. Handle a test run
141
+
142
+ In a test run, `call.run.test` is `true`:
143
+
144
+ - Picked calls a `read` tool, as in a live run.
145
+ - Picked does not call a `write` tool, unless the person turns on **Execute writes**.
146
+
147
+ When **Execute writes** is on, the writes are real. If your tool must not write during a
148
+ test, return an example output when `call.run.test` is `true`. The example in step 2
149
+ returns `{ ticketId: 'test-run' }`.
150
+
151
+ ## 5. Fail a call
152
+
153
+ To fail a call, throw an error. `createAppHandler` answers with status `422` and
154
+ `{ "error": { "message": "…" } }`. The node fails. For a node `ticket`, the message is:
155
+
156
+ ```text
157
+ Node 'ticket' ('app.my-app.create_ticket') failed: the app answered 422
158
+ ```
159
+
160
+ The run records the status of the answer. It does not record your message. Log the message
161
+ on your server, so that you can find the reason.
162
+
163
+ The failure of the node has a `retryable` value. Picked sets it from the status of the
164
+ answer, and does not read a `retryable` field in your answer. The value is `false` for a
165
+ `422` answer. Therefore, it is `false` for each failure from a tool of `@picked/app-kit`.
166
+ [Workflow tool calls](/reference/apps/protocol#workflow-tool-calls) gives the result of each
167
+ answer.
168
+
169
+ A workflow can test `retryable` and call the tool again. Refer to
170
+ [How to retry a failed call with a loop](/how-to/retry-a-failed-call-with-a-loop).
171
+
172
+ ## 6. Deploy and use the tool
173
+
174
+ 1. Deploy a new version of the manifest.
175
+ 2. Upgrade each installation to that version.
176
+
177
+ Each installation adds the tool to its project. The name of the tool is
178
+ `app.<installation slug>.<name>`, for example `app.my-app.create_ticket`. Its label is
179
+ `<app name>: <label>`, for example `My App: Create ticket`.
180
+ [Tools in a project](/reference/apps/manifest#tools-in-a-project) gives the scope that the
181
+ tool needs.
182
+
183
+ A workflow author finds the tool by its label in the workflow editor.
184
+ [How to use an app tool in a workflow](/how-to/apps/use-an-app-tool-in-a-workflow) shows
185
+ the steps.
186
+
187
+ When you remove or change a tool, upgrade the installations. Each installation uses the
188
+ tools of the version that it uses.
189
+
190
+ ## Related
191
+
192
+ - [App manifest](/reference/apps/manifest#workflow-tools) — every field of a tool.
193
+ - [App protocol](/reference/apps/protocol#workflow-tool-calls) — the call, the answer, and
194
+ the resends.
195
+ - [How to test an app](/how-to/apps/test-an-app) — call a tool in a test with `toolCall`.
196
+ - [How to read project data from an app](/how-to/apps/read-project-data-from-an-app) —
197
+ call the Picked API with the token of the call.
198
+ - [Workflow tools](/reference/dashboard/workflow-tools) — the tools that a workflow can
199
+ call.
@@ -0,0 +1,210 @@
1
+ ---
2
+ type: how-to
3
+ layout: docs
4
+ ---
5
+
6
+ # How to give the AI agent tools from an app
7
+
8
+ This guide shows you how to add an MCP server to your app. The **Agent** panel of the
9
+ dashboard, the AI agent, can then call the tools of your server in each project that has
10
+ the app.
11
+
12
+ You need an app with a server that uses `@picked/app-kit`, and a public `https` URL for
13
+ that server. [Build and install your first app](/tutorials/first-app) makes these.
14
+
15
+ ## 1. Declare the MCP server
16
+
17
+ Add `mcp` to the manifest, with the full URL of the MCP server in `url`. The URL can be on
18
+ the host of the `endpoint` or on a different host. A manifest with `mcp` does not need an
19
+ `endpoint`.
20
+
21
+ ```ts [picked.config.ts]
22
+ import { defineConfig } from '@picked/config/define'
23
+
24
+ export default defineConfig({
25
+ app: {
26
+ handle: 'my-app',
27
+ name: 'My App',
28
+ endpoint: 'https://my-app.example.com',
29
+ scopes: ['organization_leads/read'],
30
+ mcp: { url: 'https://my-app.example.com/mcp' },
31
+ },
32
+ })
33
+ ```
34
+
35
+ Add to `scopes` each scope that your tools need to call the Picked API. Step 4 tells how
36
+ the tools get a token.
37
+
38
+ ## 2. Write the MCP server
39
+
40
+ Write a function that answers one MCP request. It gets the request and the installation
41
+ that the request is for. It returns a `Response`.
42
+
43
+ The server below answers `JSON-RPC` over the `Streamable HTTP` transport of MCP. It answers
44
+ with JSON, and it has one tool.
45
+
46
+ ```ts [src/mcp.ts]
47
+ import { json, type InstallationRef } from '@picked/app-kit/server'
48
+
49
+ const TOOLS = [
50
+ {
51
+ name: 'project_summary',
52
+ description: 'Gives the ID of the project and of the installation.',
53
+ inputSchema: { type: 'object', properties: {} },
54
+ },
55
+ ]
56
+
57
+ export async function handleMcp(
58
+ request: Request,
59
+ installation: InstallationRef,
60
+ ): Promise<Response> {
61
+ if (request.method === 'GET') return new Response(null, { status: 405 })
62
+ if (request.method === 'DELETE') return new Response(null, { status: 200 })
63
+
64
+ const body = await request.json()
65
+ // A notification has no id and gets no answer.
66
+ if (body.id === undefined) return new Response(null, { status: 202 })
67
+
68
+ const answer = (result: unknown): Response => json({ jsonrpc: '2.0', id: body.id, result })
69
+ switch (body.method) {
70
+ case 'initialize':
71
+ return answer({
72
+ protocolVersion: body.params?.protocolVersion ?? '2025-06-18',
73
+ capabilities: { tools: {} },
74
+ serverInfo: { name: 'my-app', version: '1.0.0' },
75
+ })
76
+ case 'ping':
77
+ return answer({})
78
+ case 'tools/list':
79
+ return answer({ tools: TOOLS })
80
+ case 'tools/call':
81
+ if (body.params?.name !== 'project_summary') {
82
+ return answer({ isError: true, content: [{ type: 'text', text: 'Unknown tool' }] })
83
+ }
84
+ return answer({
85
+ content: [
86
+ {
87
+ type: 'text',
88
+ text: JSON.stringify({
89
+ projectId: installation.projectId,
90
+ installationId: installation.installationId,
91
+ }),
92
+ },
93
+ ],
94
+ })
95
+ default:
96
+ return json({
97
+ jsonrpc: '2.0',
98
+ id: body.id,
99
+ error: { code: -32601, message: 'Method not found' },
100
+ })
101
+ }
102
+ }
103
+ ```
104
+
105
+ The example app in the Picked repository, `fixtures/app-lead-insights`, has a full server
106
+ with two tools in `src/mcp.ts`.
107
+
108
+ ## 3. Give the server to the handler
109
+
110
+ Give `createAppHandler` an `mcp` option. Its `path` is the path of `mcp.url` in the
111
+ manifest, `/mcp` in this example:
112
+
113
+ ```ts [src/server.ts]
114
+ import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
115
+ import { serve } from '@picked/app-kit/node'
116
+ import { handleMcp } from './mcp'
117
+
118
+ const port = process.env.APP_PORT ?? '4103'
119
+ const { pickedKeys } = readAppEnvironment(process.env, port)
120
+
121
+ const handler = createAppHandler({
122
+ pickedKeys,
123
+ mcp: { path: '/mcp', handle: handleMcp },
124
+ })
125
+
126
+ serve(handler, Number(port))
127
+ ```
128
+
129
+ For each request to `path`, `createAppHandler` checks the `picked-signature` header. This header
130
+ signs the `picked-context` header together with the body. If the check fails, it answers
131
+ `401` with `{ "error": "invalid_signature" }`, and does not call `handle`. If the check
132
+ passes, it reads the installation from `picked-context` and calls `handle`.
133
+
134
+ The installation comes only from `picked-context`. The MCP body does not identify it.
135
+
136
+ ## 4. Call Picked from a tool
137
+
138
+ The installation that `handle` gets has a `token`. The token expires after one hour, and it
139
+ has the scopes of the installation. To call the Picked API with it, make an installation
140
+ client:
141
+
142
+ ```ts [src/leads.ts]
143
+ import { graphql, type ResultOf } from '@picked/admin-api-client'
144
+ import {
145
+ installationClientFor,
146
+ readAppEnvironment,
147
+ type InstallationRef,
148
+ } from '@picked/app-kit/server'
149
+
150
+ const { privateKey } = readAppEnvironment(process.env, process.env.APP_PORT ?? '4103')
151
+
152
+ const RecentLeadsDocument = graphql(`
153
+ query RecentLeads($projectId: ID!) {
154
+ project(input: { id: $projectId }) {
155
+ leads(first: 10) {
156
+ nodes {
157
+ id
158
+ createdAt
159
+ }
160
+ }
161
+ }
162
+ }
163
+ `)
164
+
165
+ export async function recentLeads(
166
+ installation: InstallationRef,
167
+ ): Promise<ResultOf<typeof RecentLeadsDocument>> {
168
+ const client = installationClientFor(installation, { privateKey })
169
+ return client.request(RecentLeadsDocument, { projectId: installation.projectId })
170
+ }
171
+ ```
172
+
173
+ `graphql` comes from the `@picked/admin-api-client` package.
174
+
175
+ Call `recentLeads(installation)` in the `tools/call` branch of `handleMcp`, and put the
176
+ result in the `text` of the answer.
177
+
178
+ Return an error from a tool as a result with `isError: true`. Then the AI agent can read the
179
+ message.
180
+
181
+ [How to read project data from an app](/how-to/apps/read-project-data-from-an-app) tells
182
+ more about the installation client.
183
+
184
+ ## 5. Deploy and try the server
185
+
186
+ 1. Deploy a new version of the manifest.
187
+ 2. Upgrade the installation to the new version.
188
+ 3. In the project, open the **Agent** panel.
189
+ 4. Ask for a task that a tool of your server does.
190
+
191
+ If the project does not have the app, install it in place of step 2.
192
+
193
+ The **Agent** panel finds the MCP server of each app that the project has. You do not add
194
+ the server to the project. The panel shows each server by the name of the app.
195
+
196
+ The person must hold `organization_apps/read` on the project. Picked sends no request from
197
+ a person without it.
198
+
199
+ If the panel cannot reach your server, it shows `Couldn't reach <app name>: <message>`.
200
+
201
+ ## Related
202
+
203
+ - [App manifest](/reference/apps/manifest#mcp-server) — the `mcp` field.
204
+ - [App protocol](/reference/apps/protocol#mcp-requests) — the headers, the signed text and the
205
+ limits of MCP requests.
206
+ - [`@picked/app-kit`](/reference/apps/app-kit) — `createAppHandler` and
207
+ `installationClientFor`.
208
+ - [How to test an app](/how-to/apps/test-an-app) — send a signed request in a test.
209
+ - [About app authentication](/explanation/app-authentication) — why Picked signs the
210
+ installation with the body.