@decocms/bindings 1.0.1-alpha.18 → 1.0.1-alpha.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/bindings",
3
- "version": "1.0.1-alpha.18",
3
+ "version": "1.0.1-alpha.19",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "check": "tsc --noEmit",
package/src/index.ts CHANGED
@@ -20,3 +20,43 @@ export {
20
20
  type RegistryAppCollectionEntity,
21
21
  REGISTRY_APP_BINDING,
22
22
  } from "./well-known/registry";
23
+
24
+ // Re-export event subscriber binding types (for connections that receive events)
25
+ export {
26
+ CloudEventSchema,
27
+ type CloudEvent,
28
+ OnEventsInputSchema,
29
+ type OnEventsInput,
30
+ OnEventsOutputSchema,
31
+ type OnEventsOutput,
32
+ EVENT_SUBSCRIBER_BINDING,
33
+ EventSubscriberBinding,
34
+ type EventSubscriberBindingClient,
35
+ } from "./well-known/event-subscriber";
36
+
37
+ // Re-export event bus binding types (for interacting with an event bus)
38
+ export {
39
+ EventPublishInputSchema,
40
+ type EventPublishInput,
41
+ EventPublishOutputSchema,
42
+ type EventPublishOutput,
43
+ EventSubscribeInputSchema,
44
+ type EventSubscribeInput,
45
+ EventSubscribeOutputSchema,
46
+ type EventSubscribeOutput,
47
+ EventUnsubscribeInputSchema,
48
+ type EventUnsubscribeInput,
49
+ EventUnsubscribeOutputSchema,
50
+ type EventUnsubscribeOutput,
51
+ EventCancelInputSchema,
52
+ type EventCancelInput,
53
+ EventCancelOutputSchema,
54
+ type EventCancelOutput,
55
+ EventAckInputSchema,
56
+ type EventAckInput,
57
+ EventAckOutputSchema,
58
+ type EventAckOutput,
59
+ EVENT_BUS_BINDING,
60
+ EventBusBinding,
61
+ type EventBusBindingClient,
62
+ } from "./well-known/event-bus";
@@ -0,0 +1,335 @@
1
+ /**
2
+ * Event Bus Well-Known Binding
3
+ *
4
+ * Defines the interface for interacting with an event bus via MCP.
5
+ * Any MCP that implements this binding can publish events and manage subscriptions.
6
+ *
7
+ * This binding includes:
8
+ * - EVENT_PUBLISH: Publish an event to the bus
9
+ * - EVENT_SUBSCRIBE: Subscribe to events of a specific type
10
+ * - EVENT_UNSUBSCRIBE: Remove a subscription
11
+ *
12
+ * Events follow the CloudEvents v1.0 specification.
13
+ * @see https://cloudevents.io/
14
+ */
15
+
16
+ import { z } from "zod";
17
+ import { bindingClient, type ToolBinder } from "../core/binder";
18
+
19
+ // ============================================================================
20
+ // Publish Schemas
21
+ // ============================================================================
22
+
23
+ /**
24
+ * EVENT_PUBLISH Input Schema
25
+ *
26
+ * Input for publishing an event.
27
+ * Note: `source` is automatically set by the event bus from the caller's connection ID.
28
+ */
29
+ export const EventPublishInputSchema = z.object({
30
+ /** Event type (e.g., "order.created", "user.signup") */
31
+ type: z.string().min(1).max(255).describe("Event type identifier"),
32
+
33
+ /** Optional subject/resource identifier */
34
+ subject: z
35
+ .string()
36
+ .max(255)
37
+ .optional()
38
+ .describe("Subject/resource identifier (e.g., order ID)"),
39
+
40
+ /** Event payload (any JSON value) */
41
+ data: z.unknown().optional().describe("Event payload"),
42
+
43
+ /**
44
+ * Optional scheduled delivery time (ISO 8601 timestamp).
45
+ * If provided, the event will not be delivered until this time.
46
+ * If omitted, the event is delivered immediately.
47
+ * Cannot be used together with `cron`.
48
+ */
49
+ deliverAt: z
50
+ .string()
51
+ .datetime()
52
+ .optional()
53
+ .describe(
54
+ "Scheduled delivery time (ISO 8601). Omit for immediate delivery.",
55
+ ),
56
+
57
+ /**
58
+ * Optional cron expression for recurring events.
59
+ * If provided, the event will be delivered repeatedly according to the schedule.
60
+ * Uses standard cron syntax (5 or 6 fields).
61
+ * Cannot be used together with `deliverAt`.
62
+ *
63
+ * Examples:
64
+ * - "0 9 * * 1" - Every Monday at 9:00 AM
65
+ * - "0 0 1 * *" - First day of every month at midnight
66
+ * - "0/15 * * * *" - Every 15 minutes
67
+ */
68
+ cron: z
69
+ .string()
70
+ .max(100)
71
+ .optional()
72
+ .describe(
73
+ "Cron expression for recurring delivery. Use EVENT_CANCEL to stop.",
74
+ ),
75
+ });
76
+
77
+ export type EventPublishInput = z.infer<typeof EventPublishInputSchema>;
78
+
79
+ /**
80
+ * EVENT_PUBLISH Output Schema
81
+ */
82
+ export const EventPublishOutputSchema = z.object({
83
+ /** Created event ID */
84
+ id: z.string().describe("Unique event ID"),
85
+
86
+ /** Event type */
87
+ type: z.string().describe("Event type"),
88
+
89
+ /** Source connection ID */
90
+ source: z.string().describe("Source connection ID"),
91
+
92
+ /** Event timestamp (ISO 8601) */
93
+ time: z.string().describe("Event timestamp"),
94
+ });
95
+
96
+ export type EventPublishOutput = z.infer<typeof EventPublishOutputSchema>;
97
+
98
+ // ============================================================================
99
+ // Subscribe Schemas
100
+ // ============================================================================
101
+
102
+ /**
103
+ * EVENT_SUBSCRIBE Input Schema
104
+ *
105
+ * Input for subscribing to events.
106
+ * The subscriber connection ID is automatically set from the caller's token.
107
+ */
108
+ export const EventSubscribeInputSchema = z.object({
109
+ /** Event type pattern to match */
110
+ eventType: z.string().min(1).max(255).describe("Event type to subscribe to"),
111
+
112
+ /** Optional: Only receive events from this publisher connection */
113
+ publisher: z
114
+ .string()
115
+ .optional()
116
+ .describe("Filter events by publisher connection ID"),
117
+
118
+ /** Optional: JSONPath filter expression on event data */
119
+ filter: z
120
+ .string()
121
+ .max(1000)
122
+ .optional()
123
+ .describe("JSONPath filter expression on event data"),
124
+ });
125
+
126
+ export type EventSubscribeInput = z.infer<typeof EventSubscribeInputSchema>;
127
+
128
+ /**
129
+ * EVENT_SUBSCRIBE Output Schema
130
+ */
131
+ export const EventSubscribeOutputSchema = z.object({
132
+ /** Created subscription */
133
+ subscription: z.object({
134
+ /** Subscription ID */
135
+ id: z.string().describe("Subscription ID"),
136
+
137
+ /** Subscriber connection ID */
138
+ connectionId: z.string().describe("Subscriber connection ID"),
139
+
140
+ /** Event type pattern */
141
+ eventType: z.string().describe("Event type pattern"),
142
+
143
+ /** Publisher connection filter */
144
+ publisher: z.string().nullable().describe("Publisher connection filter"),
145
+
146
+ /** JSONPath filter */
147
+ filter: z.string().nullable().describe("JSONPath filter expression"),
148
+
149
+ /** Whether subscription is enabled */
150
+ enabled: z.boolean().describe("Whether subscription is enabled"),
151
+
152
+ /** Created timestamp */
153
+ createdAt: z.union([z.string(), z.date()]).describe("Created timestamp"),
154
+
155
+ /** Updated timestamp */
156
+ updatedAt: z.union([z.string(), z.date()]).describe("Updated timestamp"),
157
+ }),
158
+ });
159
+
160
+ export type EventSubscribeOutput = z.infer<typeof EventSubscribeOutputSchema>;
161
+
162
+ // ============================================================================
163
+ // Unsubscribe Schemas
164
+ // ============================================================================
165
+
166
+ /**
167
+ * EVENT_UNSUBSCRIBE Input Schema
168
+ */
169
+ export const EventUnsubscribeInputSchema = z.object({
170
+ /** Subscription ID to remove */
171
+ subscriptionId: z.string().describe("Subscription ID to remove"),
172
+ });
173
+
174
+ export type EventUnsubscribeInput = z.infer<typeof EventUnsubscribeInputSchema>;
175
+
176
+ /**
177
+ * EVENT_UNSUBSCRIBE Output Schema
178
+ */
179
+ export const EventUnsubscribeOutputSchema = z.object({
180
+ /** Success status */
181
+ success: z.boolean().describe("Whether unsubscribe was successful"),
182
+
183
+ /** Subscription ID that was removed */
184
+ subscriptionId: z.string().describe("Subscription ID that was removed"),
185
+ });
186
+
187
+ export type EventUnsubscribeOutput = z.infer<
188
+ typeof EventUnsubscribeOutputSchema
189
+ >;
190
+
191
+ // ============================================================================
192
+ // Cancel Schemas (for stopping recurring events)
193
+ // ============================================================================
194
+
195
+ /**
196
+ * EVENT_CANCEL Input Schema
197
+ *
198
+ * Input for cancelling a recurring event.
199
+ * Only the publisher connection can cancel its own events.
200
+ */
201
+ export const EventCancelInputSchema = z.object({
202
+ /** Event ID to cancel */
203
+ eventId: z.string().describe("Event ID to cancel"),
204
+ });
205
+
206
+ export type EventCancelInput = z.infer<typeof EventCancelInputSchema>;
207
+
208
+ /**
209
+ * EVENT_CANCEL Output Schema
210
+ */
211
+ export const EventCancelOutputSchema = z.object({
212
+ /** Success status */
213
+ success: z.boolean().describe("Whether cancellation was successful"),
214
+
215
+ /** Event ID that was cancelled */
216
+ eventId: z.string().describe("Event ID that was cancelled"),
217
+ });
218
+
219
+ export type EventCancelOutput = z.infer<typeof EventCancelOutputSchema>;
220
+
221
+ // ============================================================================
222
+ // Ack Schemas (for acknowledging async event processing)
223
+ // ============================================================================
224
+
225
+ /**
226
+ * EVENT_ACK Input Schema
227
+ *
228
+ * Input for acknowledging an event delivery.
229
+ * Used when ON_EVENTS returns retryAfter - the subscriber must call EVENT_ACK
230
+ * to confirm successful processing, otherwise the event will be re-delivered.
231
+ *
232
+ * The subscriber connection ID is determined from the caller's token.
233
+ */
234
+ export const EventAckInputSchema = z.object({
235
+ /** Event ID to acknowledge */
236
+ eventId: z.string().describe("Event ID to acknowledge"),
237
+ });
238
+
239
+ export type EventAckInput = z.infer<typeof EventAckInputSchema>;
240
+
241
+ /**
242
+ * EVENT_ACK Output Schema
243
+ */
244
+ export const EventAckOutputSchema = z.object({
245
+ /** Success status */
246
+ success: z.boolean().describe("Whether ACK was successful"),
247
+
248
+ /** Event ID that was acknowledged */
249
+ eventId: z.string().describe("Event ID that was acknowledged"),
250
+ });
251
+
252
+ export type EventAckOutput = z.infer<typeof EventAckOutputSchema>;
253
+
254
+ // ============================================================================
255
+ // Event Bus Binding
256
+ // ============================================================================
257
+
258
+ /**
259
+ * Event Bus Binding
260
+ *
261
+ * Defines the interface for interacting with an event bus.
262
+ * Implementations must provide PUBLISH, SUBSCRIBE, UNSUBSCRIBE, CANCEL, and ACK tools.
263
+ *
264
+ * Required tools:
265
+ * - EVENT_PUBLISH: Publish an event (supports one-time, scheduled, and recurring via cron)
266
+ * - EVENT_SUBSCRIBE: Subscribe to events
267
+ * - EVENT_UNSUBSCRIBE: Remove a subscription
268
+ * - EVENT_CANCEL: Cancel a recurring event (stops future deliveries)
269
+ * - EVENT_ACK: Acknowledge event delivery (for async processing with retryAfter)
270
+ */
271
+ export const EVENT_BUS_BINDING = [
272
+ {
273
+ name: "EVENT_PUBLISH" as const,
274
+ inputSchema: EventPublishInputSchema,
275
+ outputSchema: EventPublishOutputSchema,
276
+ },
277
+ {
278
+ name: "EVENT_SUBSCRIBE" as const,
279
+ inputSchema: EventSubscribeInputSchema,
280
+ outputSchema: EventSubscribeOutputSchema,
281
+ },
282
+ {
283
+ name: "EVENT_UNSUBSCRIBE" as const,
284
+ inputSchema: EventUnsubscribeInputSchema,
285
+ outputSchema: EventUnsubscribeOutputSchema,
286
+ },
287
+ {
288
+ name: "EVENT_CANCEL" as const,
289
+ inputSchema: EventCancelInputSchema,
290
+ outputSchema: EventCancelOutputSchema,
291
+ },
292
+ {
293
+ name: "EVENT_ACK" as const,
294
+ inputSchema: EventAckInputSchema,
295
+ outputSchema: EventAckOutputSchema,
296
+ },
297
+ ] satisfies ToolBinder[];
298
+
299
+ /**
300
+ * Event Bus Binding Client
301
+ *
302
+ * Use this to create a client for interacting with an event bus.
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * import { EventBusBinding } from "@decocms/bindings/event-bus";
307
+ *
308
+ * // For a connection
309
+ * const client = EventBusBinding.forConnection(connection);
310
+ *
311
+ * // Publish an event
312
+ * const event = await client.EVENT_PUBLISH({
313
+ * type: "order.created",
314
+ * data: { orderId: "123" }
315
+ * });
316
+ *
317
+ * // Subscribe to events
318
+ * const sub = await client.EVENT_SUBSCRIBE({
319
+ * eventType: "order.created"
320
+ * });
321
+ *
322
+ * // Unsubscribe
323
+ * await client.EVENT_UNSUBSCRIBE({
324
+ * subscriptionId: sub.subscription.id
325
+ * });
326
+ * ```
327
+ */
328
+ export const EventBusBinding = bindingClient(EVENT_BUS_BINDING);
329
+
330
+ /**
331
+ * Type helper for the Event Bus binding client
332
+ */
333
+ export type EventBusBindingClient = ReturnType<
334
+ typeof EventBusBinding.forConnection
335
+ >;
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Event Subscriber Well-Known Binding
3
+ *
4
+ * Defines the interface for MCP connections that can receive events.
5
+ * Any MCP that implements this binding can receive batched CloudEvents
6
+ * from the MCP Mesh event bus.
7
+ *
8
+ * This binding includes:
9
+ * - ON_EVENTS: Receive a batch of CloudEvents
10
+ *
11
+ * Events follow the CloudEvents v1.0 specification.
12
+ * @see https://cloudevents.io/
13
+ */
14
+
15
+ import { z } from "zod";
16
+ import { bindingClient, type ToolBinder } from "../core/binder";
17
+
18
+ /**
19
+ * CloudEvent Schema
20
+ *
21
+ * Follows CloudEvents v1.0 specification.
22
+ * Required attributes: id, source, type, specversion
23
+ * Optional attributes: time, subject, datacontenttype, dataschema, data
24
+ *
25
+ * @see https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md
26
+ */
27
+ export const CloudEventSchema = z.object({
28
+ /** CloudEvents specification version (always "1.0") */
29
+ specversion: z.literal("1.0").describe("CloudEvents specification version"),
30
+
31
+ /** Unique identifier for this event */
32
+ id: z
33
+ .string()
34
+ .describe("Unique identifier for this event (UUID recommended)"),
35
+
36
+ /**
37
+ * Source of the event - in MCP Mesh, this is the connection ID of the publisher.
38
+ * Format: URI-reference identifying the context in which an event happened.
39
+ */
40
+ source: z.string().describe("Connection ID of the event publisher"),
41
+
42
+ /**
43
+ * Event type identifier.
44
+ * Should be a reverse-DNS name like "com.example.order.created"
45
+ */
46
+ type: z
47
+ .string()
48
+ .describe("Event type (e.g., 'order.created', 'user.signup')"),
49
+
50
+ /** Timestamp of when the event occurred (ISO 8601 format) */
51
+ time: z
52
+ .string()
53
+ .datetime()
54
+ .optional()
55
+ .describe("Timestamp of when the event occurred (ISO 8601)"),
56
+
57
+ /**
58
+ * Subject of the event in the context of the event producer.
59
+ * Can be used to identify the resource the event is about.
60
+ */
61
+ subject: z
62
+ .string()
63
+ .optional()
64
+ .describe("Subject/resource identifier (e.g., order ID, user ID)"),
65
+
66
+ /** Content type of the data attribute (e.g., "application/json") */
67
+ datacontenttype: z
68
+ .string()
69
+ .optional()
70
+ .default("application/json")
71
+ .describe("Content type of the data attribute"),
72
+
73
+ /** Schema URI for the data attribute */
74
+ dataschema: z
75
+ .string()
76
+ .url()
77
+ .optional()
78
+ .describe("URI to the schema for the data attribute"),
79
+
80
+ /** Event payload - can be any JSON value */
81
+ data: z.unknown().optional().describe("Event payload (any JSON value)"),
82
+ });
83
+
84
+ /**
85
+ * CloudEvent type - inferred from schema
86
+ */
87
+ export type CloudEvent = z.infer<typeof CloudEventSchema>;
88
+
89
+ /**
90
+ * ON_EVENTS Input Schema
91
+ *
92
+ * Accepts a batch of CloudEvents for processing.
93
+ */
94
+ export const OnEventsInputSchema = z.object({
95
+ /** Array of CloudEvents to process */
96
+ events: z
97
+ .array(CloudEventSchema)
98
+ .min(1)
99
+ .describe("Batch of CloudEvents to process"),
100
+ });
101
+
102
+ /**
103
+ * ON_EVENTS Input type
104
+ */
105
+ export type OnEventsInput = z.infer<typeof OnEventsInputSchema>;
106
+
107
+ /**
108
+ * ON_EVENTS Output Schema
109
+ *
110
+ * Returns success status. If success=true, all events in the batch
111
+ * are considered delivered and will be marked as such by the event bus.
112
+ *
113
+ * Alternatively, return retryAfter (ms) to request re-delivery after a delay.
114
+ * Events with retryAfter stay pending until explicitly ACKed via EVENT_ACK.
115
+ * This enables async processing patterns where the subscriber needs time to process.
116
+ */
117
+ export const OnEventsOutputSchema = z.object({
118
+ /** Whether all events were successfully processed */
119
+ success: z
120
+ .boolean()
121
+ .describe("True if all events were successfully processed"),
122
+
123
+ /** Optional error message if success=false */
124
+ error: z.string().optional().describe("Error message if processing failed"),
125
+
126
+ /** Optional count of successfully processed events */
127
+ processedCount: z
128
+ .number()
129
+ .int()
130
+ .min(0)
131
+ .optional()
132
+ .describe("Number of events successfully processed"),
133
+
134
+ /**
135
+ * Request re-delivery after this many milliseconds.
136
+ * Events remain pending until explicitly ACKed via EVENT_ACK.
137
+ * Does not count toward max retry attempts.
138
+ */
139
+ retryAfter: z
140
+ .number()
141
+ .int()
142
+ .positive()
143
+ .optional()
144
+ .describe(
145
+ "Request re-delivery after this many ms. Call EVENT_ACK to mark delivered.",
146
+ ),
147
+ });
148
+
149
+ /**
150
+ * ON_EVENTS Output type
151
+ */
152
+ export type OnEventsOutput = z.infer<typeof OnEventsOutputSchema>;
153
+
154
+ /**
155
+ * Event Subscriber Binding
156
+ *
157
+ * Defines the interface for MCP connections that can receive events.
158
+ * Implementations must provide the ON_EVENTS tool to receive batched CloudEvents.
159
+ *
160
+ * Required tools:
161
+ * - ON_EVENTS: Receive and process a batch of CloudEvents
162
+ */
163
+ export const EVENT_SUBSCRIBER_BINDING = [
164
+ {
165
+ name: "ON_EVENTS" as const,
166
+ inputSchema: OnEventsInputSchema,
167
+ outputSchema: OnEventsOutputSchema,
168
+ },
169
+ ] satisfies ToolBinder[];
170
+
171
+ /**
172
+ * Event Subscriber Binding Client
173
+ *
174
+ * Use this to create a client for calling ON_EVENTS on subscriber connections.
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * import { EventSubscriberBinding } from "@decocms/bindings/event-subscriber";
179
+ *
180
+ * // For a connection
181
+ * const client = EventSubscriberBinding.forConnection(connection);
182
+ * const result = await client.ON_EVENTS({ events: [...] });
183
+ *
184
+ * // For an MCP client
185
+ * const client = EventSubscriberBinding.forClient(mcpClient);
186
+ * const result = await client.ON_EVENTS({ events: [...] });
187
+ * ```
188
+ */
189
+ export const EventSubscriberBinding = bindingClient(EVENT_SUBSCRIBER_BINDING);
190
+
191
+ /**
192
+ * Type helper for the Event Subscriber binding client
193
+ */
194
+ export type EventSubscriberBindingClient = ReturnType<
195
+ typeof EventSubscriberBinding.forConnection
196
+ >;