@notifkit/mcp 0.1.5 → 0.1.6

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 (41) hide show
  1. package/README.md +1 -1
  2. package/dist/tools/operations.d.ts +4 -0
  3. package/dist/tools/operations.d.ts.map +1 -0
  4. package/dist/tools/operations.js +54 -0
  5. package/dist/tools/operations.js.map +1 -0
  6. package/dist/tools/projects.d.ts +4 -0
  7. package/dist/tools/projects.d.ts.map +1 -0
  8. package/dist/tools/projects.js +88 -0
  9. package/dist/tools/projects.js.map +1 -0
  10. package/dist/tools/sending.d.ts +4 -0
  11. package/dist/tools/sending.d.ts.map +1 -0
  12. package/dist/tools/sending.js +375 -0
  13. package/dist/tools/sending.js.map +1 -0
  14. package/dist/tools/shared.d.ts +17 -0
  15. package/dist/tools/shared.d.ts.map +1 -0
  16. package/dist/tools/shared.js +110 -0
  17. package/dist/tools/shared.js.map +1 -0
  18. package/dist/tools/templates.d.ts +4 -0
  19. package/dist/tools/templates.d.ts.map +1 -0
  20. package/dist/tools/templates.js +149 -0
  21. package/dist/tools/templates.js.map +1 -0
  22. package/dist/tools/users.d.ts +4 -0
  23. package/dist/tools/users.d.ts.map +1 -0
  24. package/dist/tools/users.js +251 -0
  25. package/dist/tools/users.js.map +1 -0
  26. package/dist/tools/workflows.d.ts +4 -0
  27. package/dist/tools/workflows.d.ts.map +1 -0
  28. package/dist/tools/workflows.js +115 -0
  29. package/dist/tools/workflows.js.map +1 -0
  30. package/dist/tools.d.ts.map +1 -1
  31. package/dist/tools.js +12 -1124
  32. package/dist/tools.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/tools/operations.ts +84 -0
  35. package/src/tools/projects.ts +131 -0
  36. package/src/tools/sending.ts +484 -0
  37. package/src/tools/shared.ts +129 -0
  38. package/src/tools/templates.ts +206 -0
  39. package/src/tools/users.ts +330 -0
  40. package/src/tools/workflows.ts +158 -0
  41. package/src/tools.ts +12 -1502
@@ -0,0 +1,206 @@
1
+ import { z } from "zod";
2
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ import type { NotifkitApi } from "../client.js";
4
+ import { CHANNELS, handler, renderContentNode } from "./shared.js";
5
+
6
+ export function registerTemplatesTools(server: McpServer, api: NotifkitApi): void {
7
+ server.registerTool(
8
+ "list_templates",
9
+ {
10
+ title: "List templates",
11
+ description:
12
+ "List registered templates with their channel and content. Supports filtering by channel, " +
13
+ "topic opt-out category, or limiting result count.",
14
+ inputSchema: {
15
+ limit: z
16
+ .number()
17
+ .int()
18
+ .min(1)
19
+ .max(100)
20
+ .optional()
21
+ .describe("Maximum number of templates to return."),
22
+ channel: z
23
+ .enum(CHANNELS)
24
+ .optional()
25
+ .describe("Only list templates that render for this channel."),
26
+ topic: z.string().optional().describe("Only list templates tagged with this topic."),
27
+ },
28
+ annotations: { title: "List templates", readOnlyHint: true },
29
+ },
30
+ handler(async (args) => api.get("/v1/templates", args)),
31
+ );
32
+
33
+ server.registerTool(
34
+ "get_template",
35
+ {
36
+ title: "Get a template",
37
+ description:
38
+ "Fetch one template by id, including its full per-channel content and any AI prompts.",
39
+ inputSchema: { id: z.string().min(1).describe("Template id.") },
40
+ annotations: { title: "Get a template", readOnlyHint: true },
41
+ },
42
+ handler(async (args) => api.get(`/v1/templates/${encodeURIComponent(args.id)}`)),
43
+ );
44
+
45
+ server.registerTool(
46
+ "upsert_template",
47
+ {
48
+ title: "Create or update a template",
49
+ description:
50
+ "Create a template, or overwrite an existing one with the same id. Content is a per-channel object " +
51
+ "whose values may contain {{variable}} placeholders filled by send_notification's `data`; escaping " +
52
+ "is decided by the destination field. Because this overwrites, call get_template first when " +
53
+ "editing one that already exists.",
54
+ inputSchema: {
55
+ id: z.string().min(1).describe("Template id, e.g. 'order-shipped'."),
56
+ channel: z.enum(CHANNELS).describe("Channel this template renders for."),
57
+ topic: z
58
+ .union([z.string().min(1), z.array(z.string().min(1))])
59
+ .optional()
60
+ .describe("Topic(s) this template belongs to, used for per-user topic opt-outs."),
61
+ content: z
62
+ .record(z.string(), z.unknown())
63
+ .describe("Channel-specific fields, e.g. { subject, body } for email."),
64
+ aiPrompts: z
65
+ .record(z.string(), z.string())
66
+ .optional()
67
+ .describe("Optional per-field LLM prompts applied before rendering."),
68
+ },
69
+ annotations: {
70
+ title: "Create or update a template",
71
+ readOnlyHint: false,
72
+ idempotentHint: true,
73
+ },
74
+ },
75
+ handler(async (args) => api.put("/v1/templates", { templates: [args] })),
76
+ );
77
+
78
+ server.registerTool(
79
+ "delete_template",
80
+ {
81
+ title: "Delete a template",
82
+ description: "Permanently delete a registered template by its ID.",
83
+ inputSchema: {
84
+ id: z.string().min(1).describe("Template id to delete."),
85
+ },
86
+ annotations: {
87
+ title: "Delete a template",
88
+ readOnlyHint: false,
89
+ destructiveHint: true,
90
+ idempotentHint: true,
91
+ },
92
+ },
93
+ handler(async (args) => {
94
+ await api.delete(`/v1/templates/${encodeURIComponent(args.id)}`);
95
+ return `Deleted template ${args.id}.`;
96
+ }),
97
+ );
98
+
99
+ const previewTemplateHandler = async (args: {
100
+ id?: string;
101
+ content?: Record<string, unknown>;
102
+ data?: Record<string, unknown>;
103
+ channel?: (typeof CHANNELS)[number];
104
+ }) => {
105
+ if (!args.id && !args.content) {
106
+ throw new Error("Provide either a template `id` or raw `content` to preview.");
107
+ }
108
+
109
+ let rawContent = args.content;
110
+ let templateChannel = args.channel;
111
+
112
+ if (args.id) {
113
+ const tmpl = await api.get<{
114
+ id: string;
115
+ channel?: (typeof CHANNELS)[number];
116
+ content: Record<string, unknown>;
117
+ }>(`/v1/templates/${encodeURIComponent(args.id)}`);
118
+ if (!rawContent) {
119
+ rawContent = tmpl.content;
120
+ }
121
+ if (!templateChannel && tmpl.channel) {
122
+ templateChannel = tmpl.channel;
123
+ }
124
+ }
125
+
126
+ const vars = args.data ?? {};
127
+ const rendered = renderContentNode(rawContent ?? {}, vars, "text");
128
+
129
+ const jsonStr = JSON.stringify(rawContent ?? {});
130
+ const placeholderMatches = Array.from(jsonStr.matchAll(/\{\{\{?(\w+)\}?\}\}/g)).map(
131
+ (m) => m[1]!,
132
+ );
133
+ const placeholders = Array.from(new Set(placeholderMatches));
134
+ const resolvedVariables = placeholders.filter((p) => p in vars);
135
+ const unresolvedVariables = placeholders.filter((p) => !(p in vars));
136
+
137
+ return {
138
+ ...(args.id ? { templateId: args.id } : {}),
139
+ channel: templateChannel ?? "email",
140
+ rendered,
141
+ resolvedVariables,
142
+ unresolvedVariables: unresolvedVariables.length > 0 ? unresolvedVariables : undefined,
143
+ };
144
+ };
145
+
146
+ server.registerTool(
147
+ "preview_template",
148
+ {
149
+ title: "Preview a template",
150
+ description:
151
+ "Dry-run render a template with sample variable data before sending. Pass an existing template " +
152
+ "`id` or supply raw `content` directly. Interpolates {{variable}} placeholders and returns the " +
153
+ "rendered output alongside resolved/unresolved variable names.",
154
+ inputSchema: {
155
+ id: z.string().optional().describe("Template id to fetch and preview."),
156
+ content: z
157
+ .record(z.string(), z.unknown())
158
+ .optional()
159
+ .describe(
160
+ "Raw template content object (e.g. { subject, body }) to render without saving.",
161
+ ),
162
+ data: z
163
+ .record(z.string(), z.unknown())
164
+ .optional()
165
+ .describe("Sample variable data to interpolate into the template's placeholders."),
166
+ channel: z
167
+ .enum(CHANNELS)
168
+ .optional()
169
+ .describe(
170
+ "Target channel (e.g. 'email', 'sms', 'push'). Defaults to the template's channel.",
171
+ ),
172
+ },
173
+ annotations: { title: "Preview a template", readOnlyHint: true },
174
+ },
175
+ handler(previewTemplateHandler),
176
+ );
177
+
178
+ /** @deprecated Same handler as preview_template; removed at the next minor version. */
179
+ server.registerTool(
180
+ "render_template",
181
+ {
182
+ title: "Render template content",
183
+ description:
184
+ "Deprecated: use preview_template, which does the same thing; this tool will be removed. " +
185
+ "Render template content with variable data without dispatching any notifications. " +
186
+ "Accepts either an existing template `id` or raw `content` object.",
187
+ inputSchema: {
188
+ id: z.string().optional().describe("Template id to fetch and render."),
189
+ content: z
190
+ .record(z.string(), z.unknown())
191
+ .optional()
192
+ .describe("Raw template content object to render."),
193
+ data: z
194
+ .record(z.string(), z.unknown())
195
+ .optional()
196
+ .describe("Variables to interpolate into placeholders."),
197
+ channel: z
198
+ .enum(CHANNELS)
199
+ .optional()
200
+ .describe("Target channel. Defaults to template channel."),
201
+ },
202
+ annotations: { title: "Render template content", readOnlyHint: true },
203
+ },
204
+ handler(previewTemplateHandler),
205
+ );
206
+ }
@@ -0,0 +1,330 @@
1
+ import { z } from "zod";
2
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ import type { NotifkitApi } from "../client.js";
4
+ import { CHANNELS, CONTACT_CHANNELS, handler } from "./shared.js";
5
+
6
+ export function registerUsersTools(server: McpServer, api: NotifkitApi): void {
7
+ server.registerTool(
8
+ "list_users",
9
+ {
10
+ title: "List users",
11
+ description:
12
+ "Page through the project's users with comprehensive filtering. Supports filtering by search " +
13
+ "keyword (external ID or email), segment tag, language, timezone, or channel contact. " +
14
+ "Results are paginated — pass the returned `nextCursor` back as `cursor`. For one known user " +
15
+ "prefer get_user, which also returns contacts and preferences.",
16
+ inputSchema: {
17
+ limit: z.number().int().min(1).max(200).optional().describe("Users per page."),
18
+ cursor: z
19
+ .string()
20
+ .optional()
21
+ .describe("Pagination cursor from a previous call's `nextCursor`."),
22
+ search: z
23
+ .string()
24
+ .optional()
25
+ .describe("Filter users whose external ID or email contains this search keyword."),
26
+ segment: z.string().optional().describe("Only list users assigned to this segment tag."),
27
+ language: z
28
+ .string()
29
+ .optional()
30
+ .describe("Filter users by language tag, e.g. 'en', 'es', 'fr'."),
31
+ timezone: z
32
+ .string()
33
+ .optional()
34
+ .describe("Filter users by IANA timezone, e.g. 'Europe/London', 'America/New_York'."),
35
+ channel: z
36
+ .enum(CONTACT_CHANNELS)
37
+ .optional()
38
+ .describe(
39
+ "Only list users who have a registered contact for this channel " +
40
+ "(email, sms, push, webhook, telegram, discord, whatsapp, slack).",
41
+ ),
42
+ },
43
+ annotations: { title: "List users", readOnlyHint: true },
44
+ },
45
+ handler(async (args) => api.get("/v1/users", args)),
46
+ );
47
+
48
+ server.registerTool(
49
+ "get_user",
50
+ {
51
+ title: "Get a user",
52
+ description:
53
+ "Fetch one user with their contacts, segments, and notification preferences. Call this before " +
54
+ "sending to a single user when you need to know which channels they can actually be reached on.",
55
+ inputSchema: { id: z.string().min(1).describe("User id.") },
56
+ annotations: { title: "Get a user", readOnlyHint: true },
57
+ },
58
+ handler(async (args) => api.get(`/v1/users/${encodeURIComponent(args.id)}/details`)),
59
+ );
60
+
61
+ server.registerTool(
62
+ "upsert_user",
63
+ {
64
+ title: "Create or update a user",
65
+ description:
66
+ "Create a user profile, or update an existing one with the same id. Sets contact targets, segment " +
67
+ "tags, timezone, and preferences in one call. Segments assigned here are what send_notification's " +
68
+ "`segment` targeting matches on. Timezone drives quiet-hours maths, so set it when known.",
69
+ inputSchema: {
70
+ id: z.string().min(1).describe("Your own stable user id, e.g. 'usr_123'."),
71
+ contacts: z
72
+ .array(
73
+ z.object({
74
+ channel: z.enum(CHANNELS).describe("Contact delivery channel."),
75
+ target: z.string().min(1).describe("Destination address, phone, token, or URL."),
76
+ label: z.string().optional().describe("Friendly label for this contact."),
77
+ isPrimary: z
78
+ .boolean()
79
+ .optional()
80
+ .describe("Whether this is the primary contact for this channel."),
81
+ }),
82
+ )
83
+ .optional()
84
+ .describe(
85
+ "Array of user contacts across any supported channels (preferred over legacy email/phone/pushToken).",
86
+ ),
87
+ email: z
88
+ .union([z.string().min(1), z.array(z.string().min(1))])
89
+ .optional()
90
+ .describe("One email address or several."),
91
+ phone: z
92
+ .union([z.string().min(1), z.array(z.string().min(1))])
93
+ .optional()
94
+ .describe("One phone number or several, in E.164 form."),
95
+ pushToken: z
96
+ .union([z.string().min(1), z.array(z.string().min(1))])
97
+ .optional()
98
+ .describe("One device push token or several."),
99
+ segments: z
100
+ .array(z.string().min(1))
101
+ .optional()
102
+ .describe("Segment tags to assign to this user."),
103
+ language: z
104
+ .string()
105
+ .optional()
106
+ .describe("BCP 47 language tag used to pick localised content."),
107
+ timezone: z
108
+ .string()
109
+ .optional()
110
+ .describe("IANA timezone, e.g. 'Europe/London'. Drives quiet hours."),
111
+ preferences: z
112
+ .object({
113
+ channels: z
114
+ .record(z.string(), z.boolean())
115
+ .optional()
116
+ .describe("Per-channel opt-in map, e.g. { email: true, sms: false }."),
117
+ topics: z.record(z.string(), z.boolean()).optional().describe("Per-topic opt-in map."),
118
+ quietHours: z
119
+ .array(
120
+ z.object({
121
+ start: z.string().describe("HH:MM, 24h, in the user's timezone."),
122
+ end: z.string().describe("HH:MM, 24h, in the user's timezone."),
123
+ }),
124
+ )
125
+ .optional()
126
+ .describe("Windows during which non-critical sends are deferred."),
127
+ })
128
+ .optional()
129
+ .describe("Consent and quiet-hours settings."),
130
+ },
131
+ annotations: { title: "Create or update a user", readOnlyHint: false, idempotentHint: true },
132
+ },
133
+ handler(async (args) => api.post("/v1/users", args)),
134
+ );
135
+
136
+ server.registerTool(
137
+ "update_user",
138
+ {
139
+ title: "Update a user",
140
+ description:
141
+ "Partially update an existing user profile (e.g. language, timezone, segment tags, preferences, or append new contacts) without replacing unspecified fields.",
142
+ inputSchema: {
143
+ id: z.string().min(1).describe("User id."),
144
+ contacts: z
145
+ .array(
146
+ z.object({
147
+ channel: z.enum(CHANNELS).describe("Contact delivery channel."),
148
+ target: z.string().min(1).describe("Destination address, phone, token, or URL."),
149
+ label: z.string().optional().describe("Friendly label for this contact."),
150
+ isPrimary: z
151
+ .boolean()
152
+ .optional()
153
+ .describe("Whether this is the primary contact for this channel."),
154
+ }),
155
+ )
156
+ .optional()
157
+ .describe("Array of user contacts across any supported channels to append or update."),
158
+ email: z
159
+ .union([z.string().min(1), z.array(z.string().min(1))])
160
+ .optional()
161
+ .describe("One email address or several."),
162
+ phone: z
163
+ .union([z.string().min(1), z.array(z.string().min(1))])
164
+ .optional()
165
+ .describe("One phone number or several, in E.164 form."),
166
+ pushToken: z
167
+ .union([z.string().min(1), z.array(z.string().min(1))])
168
+ .optional()
169
+ .describe("One device push token or several."),
170
+ segments: z
171
+ .array(z.string().min(1))
172
+ .optional()
173
+ .describe("Segment tags to assign to this user."),
174
+ language: z
175
+ .string()
176
+ .optional()
177
+ .describe("BCP 47 language tag used to pick localised content."),
178
+ timezone: z
179
+ .string()
180
+ .optional()
181
+ .describe("IANA timezone, e.g. 'Europe/London'. Drives quiet hours."),
182
+ preferences: z
183
+ .object({
184
+ channels: z
185
+ .record(z.string(), z.boolean())
186
+ .optional()
187
+ .describe("Per-channel opt-in map, e.g. { email: true, sms: false }."),
188
+ topics: z.record(z.string(), z.boolean()).optional().describe("Per-topic opt-in map."),
189
+ quietHours: z
190
+ .array(
191
+ z.object({
192
+ start: z.string().describe("HH:MM, 24h, in the user's timezone."),
193
+ end: z.string().describe("HH:MM, 24h, in the user's timezone."),
194
+ }),
195
+ )
196
+ .optional()
197
+ .describe("Windows during which non-critical sends are deferred."),
198
+ })
199
+ .optional()
200
+ .describe("Consent and quiet-hours settings."),
201
+ },
202
+ annotations: { title: "Update a user", readOnlyHint: false, idempotentHint: true },
203
+ },
204
+ handler(async ({ id, ...body }) => api.patch(`/v1/users/${encodeURIComponent(id)}`, body)),
205
+ );
206
+
207
+ server.registerTool(
208
+ "delete_user",
209
+ {
210
+ title: "Delete a user",
211
+ description:
212
+ "Permanently delete a user profile and all associated contacts and preferences by user id.",
213
+ inputSchema: {
214
+ id: z.string().min(1).describe("User id to delete."),
215
+ },
216
+ annotations: {
217
+ title: "Delete a user",
218
+ readOnlyHint: false,
219
+ destructiveHint: true,
220
+ idempotentHint: true,
221
+ },
222
+ },
223
+ handler(async (args) => {
224
+ await api.delete(`/v1/users/${encodeURIComponent(args.id)}`);
225
+ return `Deleted user ${args.id}.`;
226
+ }),
227
+ );
228
+
229
+ server.registerTool(
230
+ "get_user_contacts",
231
+ {
232
+ title: "Get user contacts",
233
+ description: "Fetch all registered contact addresses and tokens for a specific user.",
234
+ inputSchema: {
235
+ userId: z.string().min(1).describe("User id."),
236
+ },
237
+ annotations: { title: "Get user contacts", readOnlyHint: true },
238
+ },
239
+ handler(async (args) => api.get(`/v1/users/${encodeURIComponent(args.userId)}/contacts`)),
240
+ );
241
+
242
+ server.registerTool(
243
+ "add_user_contact",
244
+ {
245
+ title: "Add a contact to a user",
246
+ description:
247
+ "Add one addressable target (email address, phone number, push token, or webhook URL) to an " +
248
+ "existing user without replacing their other contacts. Prefer this over upsert_user when adding a " +
249
+ "second device or address to someone who already exists.",
250
+ inputSchema: {
251
+ userId: z.string().min(1).describe("Id of the user to add the contact to."),
252
+ channel: z.enum(CONTACT_CHANNELS).describe("Channel this target belongs to."),
253
+ target: z.string().min(1).describe("The address itself: email, phone, push token, or URL."),
254
+ },
255
+ annotations: { title: "Add a contact to a user", readOnlyHint: false },
256
+ },
257
+ handler(async ({ userId, ...body }) =>
258
+ api.post(`/v1/users/${encodeURIComponent(userId)}/contacts`, body),
259
+ ),
260
+ );
261
+
262
+ server.registerTool(
263
+ "delete_user_contact",
264
+ {
265
+ title: "Delete a user contact",
266
+ description:
267
+ "Remove one contact destination (email, phone, push token, or webhook URL) from a user profile.",
268
+ inputSchema: {
269
+ userId: z.string().min(1).describe("User id."),
270
+ channel: z.enum(CONTACT_CHANNELS).describe("Channel of the contact to delete."),
271
+ target: z.string().min(1).describe("The address/number/token/URL to remove."),
272
+ },
273
+ annotations: {
274
+ title: "Delete a user contact",
275
+ readOnlyHint: false,
276
+ destructiveHint: true,
277
+ idempotentHint: true,
278
+ },
279
+ },
280
+ handler(async (args) => {
281
+ await api.delete(
282
+ `/v1/users/${encodeURIComponent(args.userId)}/contacts/${encodeURIComponent(args.channel)}/${encodeURIComponent(args.target)}`,
283
+ );
284
+ return `Deleted ${args.channel} contact '${args.target}' for user ${args.userId}.`;
285
+ }),
286
+ );
287
+
288
+ server.registerTool(
289
+ "get_user_preferences",
290
+ {
291
+ title: "Get user preferences",
292
+ description:
293
+ "Fetch notification channel/topic opt-in preferences and quiet hours for a user.",
294
+ inputSchema: {
295
+ userId: z.string().min(1).describe("User id."),
296
+ },
297
+ annotations: { title: "Get user preferences", readOnlyHint: true },
298
+ },
299
+ handler(async (args) => api.get(`/v1/users/${encodeURIComponent(args.userId)}/preferences`)),
300
+ );
301
+
302
+ server.registerTool(
303
+ "update_user_preferences",
304
+ {
305
+ title: "Update user preferences",
306
+ description: "Update notification channel/topic opt-ins and quiet hours settings for a user.",
307
+ inputSchema: {
308
+ userId: z.string().min(1).describe("User id."),
309
+ channels: z
310
+ .record(z.string(), z.boolean())
311
+ .optional()
312
+ .describe("Per-channel opt-in map, e.g. { email: true, sms: false }."),
313
+ topics: z.record(z.string(), z.boolean()).optional().describe("Per-topic opt-in map."),
314
+ quietHours: z
315
+ .array(
316
+ z.object({
317
+ start: z.string().describe("HH:MM, 24h, in the user's timezone."),
318
+ end: z.string().describe("HH:MM, 24h, in the user's timezone."),
319
+ }),
320
+ )
321
+ .optional()
322
+ .describe("Windows during which non-critical sends are deferred."),
323
+ },
324
+ annotations: { title: "Update user preferences", readOnlyHint: false, idempotentHint: true },
325
+ },
326
+ handler(async ({ userId, ...preferences }) =>
327
+ api.patch(`/v1/users/${encodeURIComponent(userId)}/preferences`, preferences),
328
+ ),
329
+ );
330
+ }
@@ -0,0 +1,158 @@
1
+ import { z } from "zod";
2
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ import type { NotifkitApi } from "../client.js";
4
+ import { handler } from "./shared.js";
5
+
6
+ export function registerWorkflowsTools(server: McpServer, api: NotifkitApi): void {
7
+ server.registerTool(
8
+ "create_workflow",
9
+ {
10
+ title: "Create a workflow",
11
+ description:
12
+ "Define a named multi-step sequence — the way to build a drip, an onboarding series, or a reminder " +
13
+ "chain rather than a single send. Steps run in order and are one of: `notify` (send, using the " +
14
+ "same fields as send_notification), `wait` (pause for a duration like '1h' or '3d'), or " +
15
+ "`waitForEvent` (block until ingest_event reports a named event, with an optional timeout). A " +
16
+ "notify step with no target inherits the user the instance was triggered for. Creating a workflow " +
17
+ "does not run it — call trigger_workflow per user.",
18
+ inputSchema: {
19
+ name: z.string().min(1).describe("Workflow name, used later by trigger_workflow."),
20
+ steps: z
21
+ .array(
22
+ z.union([
23
+ z.object({
24
+ action: z.literal("notify"),
25
+ payload: z
26
+ .record(z.string(), z.unknown())
27
+ .describe(
28
+ "Same shape as send_notification's arguments. Omit the target to use the instance's own user.",
29
+ ),
30
+ }),
31
+ z.object({
32
+ action: z.literal("wait"),
33
+ duration: z.string().describe("Duration string, e.g. '5m', '1h', '3d'."),
34
+ }),
35
+ z.object({
36
+ action: z.literal("waitForEvent"),
37
+ event: z.string().describe("Event name to wait for, as passed to ingest_event."),
38
+ options: z
39
+ .object({ timeout: z.string().optional().describe("e.g. '24h'.") })
40
+ .optional()
41
+ .describe("Give a timeout so the instance cannot wait forever."),
42
+ }),
43
+ ]),
44
+ )
45
+ .min(1)
46
+ .describe("Ordered steps."),
47
+ },
48
+ annotations: { title: "Create a workflow", readOnlyHint: false, idempotentHint: true },
49
+ },
50
+ handler(async (args) => api.post("/v1/workflows", args)),
51
+ );
52
+
53
+ server.registerTool(
54
+ "list_workflows",
55
+ {
56
+ title: "List workflows",
57
+ description:
58
+ "List registered workflow definitions and their steps. Call this to find the name to pass to " +
59
+ "trigger_workflow, or to check a sequence before triggering it. Supports filtering by search keyword.",
60
+ inputSchema: {
61
+ limit: z
62
+ .number()
63
+ .int()
64
+ .min(1)
65
+ .max(100)
66
+ .optional()
67
+ .describe("Maximum number of workflow definitions to return."),
68
+ search: z
69
+ .string()
70
+ .optional()
71
+ .describe("Filter workflow definitions whose name contains this search keyword."),
72
+ },
73
+ annotations: { title: "List workflows", readOnlyHint: true },
74
+ },
75
+ handler(async (args) => api.get("/v1/workflows", args)),
76
+ );
77
+
78
+ server.registerTool(
79
+ "trigger_workflow",
80
+ {
81
+ title: "Trigger a workflow",
82
+ description:
83
+ "Start one run of a registered workflow, optionally for a specific user, and return the instance " +
84
+ "id. Each call starts an independent instance, so triggering the same workflow twice for one user " +
85
+ "enrols them twice. Track the returned instance with get_workflow_run.",
86
+ inputSchema: {
87
+ name: z.string().min(1).describe("Name of the workflow to run."),
88
+ user: z
89
+ .string()
90
+ .min(1)
91
+ .optional()
92
+ .describe("User id this run is for. Notify steps default to them."),
93
+ input: z
94
+ .record(z.string(), z.unknown())
95
+ .optional()
96
+ .describe("Data made available to the run's steps."),
97
+ },
98
+ annotations: { title: "Trigger a workflow", readOnlyHint: false, idempotentHint: false },
99
+ },
100
+ handler(async (args) => api.post("/v1/workflows/trigger", args)),
101
+ );
102
+
103
+ server.registerTool(
104
+ "get_workflow_run",
105
+ {
106
+ title: "Get a workflow run",
107
+ description:
108
+ "Fetch one workflow instance by id: which step it is on, its status, and what it is waiting for. " +
109
+ "Use this to check on a run started by trigger_workflow.",
110
+ inputSchema: { instanceId: z.string().min(1).describe("Workflow instance id.") },
111
+ annotations: { title: "Get a workflow run", readOnlyHint: true },
112
+ },
113
+ handler(async (args) =>
114
+ api.get(`/v1/workflows/instances/${encodeURIComponent(args.instanceId)}`),
115
+ ),
116
+ );
117
+
118
+ server.registerTool(
119
+ "cancel_workflow_run",
120
+ {
121
+ title: "Cancel a workflow run",
122
+ description:
123
+ "Stop a running or suspended workflow instance so its remaining steps never fire. This does not " +
124
+ "recall notifications already sent, and it cannot be undone — confirm with the user before " +
125
+ "cancelling a run you did not start in this session.",
126
+ inputSchema: { instanceId: z.string().min(1).describe("Workflow instance id to cancel.") },
127
+ annotations: {
128
+ title: "Cancel a workflow run",
129
+ readOnlyHint: false,
130
+ destructiveHint: true,
131
+ idempotentHint: true,
132
+ },
133
+ },
134
+ handler(async (args) => {
135
+ await api.request("DELETE", `/v1/workflows/instances/${encodeURIComponent(args.instanceId)}`);
136
+ return `Cancelled workflow instance ${args.instanceId}.`;
137
+ }),
138
+ );
139
+
140
+ server.registerTool(
141
+ "ingest_event",
142
+ {
143
+ title: "Ingest an event",
144
+ description:
145
+ "Report that something happened in your product. Workflow instances parked on a matching " +
146
+ "`waitForEvent` step resume. Call this to unblock a waiting sequence, e.g. emitting 'order.paid' " +
147
+ "so an abandoned-cart workflow stops before its reminder fires.",
148
+ inputSchema: {
149
+ name: z.string().min(1).describe("Event name, matching a workflow's waitForEvent step."),
150
+ properties: z
151
+ .record(z.string(), z.unknown())
152
+ .describe("Event payload. Include the user id the event is about."),
153
+ },
154
+ annotations: { title: "Ingest an event", readOnlyHint: false, idempotentHint: false },
155
+ },
156
+ handler(async (args) => api.post("/v1/events", args)),
157
+ );
158
+ }