holon-sh 0.0.1-dev.11 → 0.0.1-dev.13
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/README.md +2 -30
- package/dist/ext/events.d.ts +15 -155
- package/dist/ext/events.d.ts.map +1 -1
- package/dist/ext/events.js +103 -218
- package/dist/ext/events.js.map +1 -1
- package/dist/ext/files.client.d.ts +1 -1
- package/dist/ext/files.client.d.ts.map +1 -1
- package/dist/ext/files.client.js +2 -2
- package/dist/ext/files.client.js.map +1 -1
- package/dist/protocol.js +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +20 -13
- package/dist/runtime.js.map +1 -1
- package/package.json +1 -2
- package/src/ext/events.ts +140 -328
- package/src/ext/files.client.ts +2 -2
- package/src/protocol.ts +1 -1
- package/src/runtime.ts +20 -14
- package/dist/ext/webhooks.d.ts +0 -18
- package/dist/ext/webhooks.d.ts.map +0 -1
- package/dist/ext/webhooks.js +0 -527
- package/dist/ext/webhooks.js.map +0 -1
- package/src/ext/webhooks.ts +0 -512
package/dist/ext/events.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
// The server side of MCP Events, as the Triggers & Events Working Group's design sketch defines it:
|
|
2
2
|
// https://github.com/modelcontextprotocol/experimental-ext-triggers-events/blob/main/docs/design-sketch-proposal.md
|
|
3
|
-
// The draft is experimental, so this lives under ext and may change with it.
|
|
4
|
-
//
|
|
5
|
-
// that reaches the event types through the internals below. Names follow the sketch, and types are the MCP SDK's own
|
|
6
|
-
// wherever it has them.
|
|
3
|
+
// The draft is experimental, so this lives under ext and may change with it. It serves the poll and push delivery
|
|
4
|
+
// modes. Names follow the sketch, and types are the MCP SDK's own wherever it has them.
|
|
7
5
|
import { ProtocolError, ProtocolErrorCode, SUBSCRIPTION_ID_META_KEY } from "@modelcontextprotocol/server";
|
|
8
|
-
import { webhooks } from "./webhooks.js";
|
|
9
6
|
/** The identifier a server declares the Events extension under, in capabilities.extensions. */
|
|
10
7
|
export const EVENTS_EXTENSION = "io.modelcontextprotocol/events";
|
|
11
|
-
/** Reaches what a delivery mode needs of the events it serves. Not for applications. */
|
|
12
|
-
export const INTERNALS = Symbol.for("holon-sh.events.internals");
|
|
13
8
|
const INVALID_PARAMS = ProtocolErrorCode.InvalidParams;
|
|
14
9
|
const INTERNAL_ERROR = ProtocolErrorCode.InternalError;
|
|
15
10
|
// The sketch's general-purpose codes, provisional until the specification allocates them.
|
|
@@ -43,7 +38,7 @@ const caller = (context) => {
|
|
|
43
38
|
};
|
|
44
39
|
const hex = (bytes) => [...bytes].map(byte => byte.toString(16).padStart(2, "0")).join("");
|
|
45
40
|
// A subscription's id is the same for the same caller, event type and arguments, so it survives reconnects and poll
|
|
46
|
-
// leases
|
|
41
|
+
// leases. It is made with the global crypto, which needs no node:crypto at load.
|
|
47
42
|
const identify = async (who, name, args) => {
|
|
48
43
|
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify([who, name, canonical(args)])));
|
|
49
44
|
return `sub_${hex(new Uint8Array(digest, 0, 16))}`;
|
|
@@ -53,11 +48,11 @@ const unique = () => `sub_${hex(crypto.getRandomValues(new Uint8Array(16)))}`;
|
|
|
53
48
|
// A teardown that fails has nobody to tell, and must not take the process down.
|
|
54
49
|
const settle = (run) => { Promise.resolve().then(run).catch(() => { }); };
|
|
55
50
|
const integer = (value, minimum) => value === undefined || Number.isInteger(value) && value >= minimum;
|
|
56
|
-
|
|
57
|
-
// emit buffer.
|
|
51
|
+
const OFFERED = ["poll", "push"];
|
|
52
|
+
// What each type keeps of what it emits, for polls and reconnecting streams: the sketch's emit buffer.
|
|
58
53
|
const KEPT_EVENTS = 10_000;
|
|
59
54
|
const KEPT_AGE = 10 * 60_000;
|
|
60
|
-
// How long a poll is told to wait before the next
|
|
55
|
+
// How long a poll is told to wait before the next.
|
|
61
56
|
const INTERVAL = 1000;
|
|
62
57
|
// How often a stream hears from the server when nothing happens, as the sketch recommends.
|
|
63
58
|
const HEARTBEAT = 30_000;
|
|
@@ -69,17 +64,20 @@ const MAX_SUBSCRIPTIONS = 10_000;
|
|
|
69
64
|
const MAX_SUBSCRIPTIONS_PER_CALLER = 1000;
|
|
70
65
|
// How many event types events/list returns at a time.
|
|
71
66
|
const PAGE_SIZE = 100;
|
|
67
|
+
/**
|
|
68
|
+
* What a stream sends before it ends and its client resumes from its cursor. The draft leaves flow control to the
|
|
69
|
+
* transport, but the MCP SDK's HTTP transport queues whatever a stream sends without waiting for its reader:
|
|
70
|
+
* https://github.com/modelcontextprotocol/experimental-ext-triggers-events/blob/main/docs/design-sketch-proposal.md#note-on-flow-control
|
|
71
|
+
*/
|
|
72
|
+
const STREAM_BUDGET = 2 ** 20;
|
|
72
73
|
/**
|
|
73
74
|
* Event types a process emits and the MCP servers it creates serve, in the poll and push delivery modes of MCP Events,
|
|
74
75
|
* the draft extension of the Triggers & Events Working Group. Each type keeps the last ten thousand events it emits,
|
|
75
76
|
* for ten minutes, for polls and for streams that reconnect: those cursors last as long as the process, and after a
|
|
76
|
-
* restart a client is told that events were skipped. A
|
|
77
|
-
*
|
|
78
|
-
* the server every thirty seconds.
|
|
77
|
+
* restart a client is told that events were skipped. A poll is told to come back every second, and a stream hears
|
|
78
|
+
* from the server every thirty seconds.
|
|
79
79
|
*/
|
|
80
|
-
export function createEvents(
|
|
81
|
-
const modes = webhookTtl === undefined ? [] : [webhooks({ ...webhook, ttl: webhookTtl, heartbeat: HEARTBEAT })];
|
|
82
|
-
const offered = ["poll", "push", ...modes.map(mode => mode.name)];
|
|
80
|
+
export function createEvents() {
|
|
83
81
|
// Cursors are positions in this process's memory. One from another process names nothing here. The name is made
|
|
84
82
|
// without node:crypto, which is not there when a server is evaluated for its contract.
|
|
85
83
|
const epoch = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
|
|
@@ -88,33 +86,21 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
88
86
|
let subscribed = 0;
|
|
89
87
|
const callers = new Map();
|
|
90
88
|
const position = (type, sequence = type.sequence) => `${epoch}.${sequence}`;
|
|
91
|
-
//
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
// the events still kept starts from the oldest kept, and says that events were skipped.
|
|
102
|
-
const since = (state, subscription, cursor, maxAgeMs, limit = Infinity) => {
|
|
103
|
-
const type = state;
|
|
104
|
-
const now = { events: [], start: position(type), cursor: position(type), hasMore: false };
|
|
105
|
-
if (cursor == null)
|
|
106
|
-
return { ...now, truncated: false };
|
|
107
|
-
const [process, text] = cursor.split(".");
|
|
108
|
-
const sequence = Number(text);
|
|
109
|
-
if (process !== epoch || !Number.isInteger(sequence) || sequence > type.sequence)
|
|
110
|
-
return { ...now, truncated: true };
|
|
111
|
-
const floor = maxAgeMs === undefined ? -Infinity : Date.now() - maxAgeMs;
|
|
112
|
-
const later = type.kept.filter(kept => kept.sequence > sequence);
|
|
89
|
+
// What a subscription is owed after a cursor, up to `until`: the events for it, in order, where delivery starts,
|
|
90
|
+
// and where it now stands. A null cursor starts where the subscription was opened, and so does one from another
|
|
91
|
+
// process, which says that events were skipped. One past the events still kept starts from the oldest kept, and
|
|
92
|
+
// says so too.
|
|
93
|
+
const since = (type, subscription, cursor, { maxAgeMs, maxEvents: limit = Infinity, opened = type.sequence, until = type.sequence } = {}) => {
|
|
94
|
+
const [process, text] = cursor?.split(".") ?? [];
|
|
95
|
+
const resumed = cursor != null && process === epoch && Number.isInteger(Number(text)) && Number(text) <= type.sequence;
|
|
96
|
+
const sequence = resumed ? Number(text) : opened;
|
|
97
|
+
const floor = maxAgeMs === undefined || !resumed ? -Infinity : Date.now() - maxAgeMs;
|
|
98
|
+
const later = type.kept.filter(kept => kept.sequence > sequence && kept.sequence <= until);
|
|
113
99
|
const recent = later.filter(kept => kept.at >= floor);
|
|
114
|
-
const truncated = sequence < type.dropped || recent.length < later.length;
|
|
100
|
+
const truncated = cursor != null && !resumed || sequence < type.dropped || recent.length < later.length;
|
|
115
101
|
const events = [];
|
|
116
102
|
const failures = [];
|
|
117
|
-
let reached = recent.length ? recent[0].sequence - 1 :
|
|
103
|
+
let reached = recent.length ? recent[0].sequence - 1 : until;
|
|
118
104
|
const start = position(type, reached);
|
|
119
105
|
for (const kept of recent) {
|
|
120
106
|
if (events.length === limit)
|
|
@@ -124,49 +110,14 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
124
110
|
if (event)
|
|
125
111
|
events.push(event);
|
|
126
112
|
}
|
|
127
|
-
return { events, failures, start, cursor: position(type), truncated, hasMore: false };
|
|
128
|
-
};
|
|
129
|
-
// What a subscription is owed after a cursor from the type's check function. The cursor is the author's own. Each
|
|
130
|
-
// event is placed at its own cursor when it has one, so a batch can be cut short; otherwise at the cursor before
|
|
131
|
-
// the batch, which is safe to resume from. After a gap, delivery starts from the position the check reached.
|
|
132
|
-
const checked = async (type, { arguments: args, context }, cursor, maxAgeMs, limit = Infinity) => {
|
|
133
|
-
const result = await type.check(cursor ?? null, args, context);
|
|
134
|
-
const next = result?.cursor ?? null;
|
|
135
|
-
// A null cursor asks for the current position and nothing before it, except of a source that has no positions:
|
|
136
|
-
// each check of that is asked with null, and answers with what is new.
|
|
137
|
-
if (cursor == null && next !== null)
|
|
138
|
-
return { events: [], start: next, cursor: next, truncated: false, hasMore: false };
|
|
139
|
-
const items = result?.events ?? [];
|
|
140
|
-
const floor = maxAgeMs === undefined || cursor == null ? -Infinity : Date.now() - maxAgeMs;
|
|
141
|
-
let truncated = next !== null && Boolean(result.truncated);
|
|
142
|
-
const events = [];
|
|
143
|
-
const failures = [];
|
|
144
|
-
let reached = cursor ?? null;
|
|
145
|
-
let cuttable = false;
|
|
146
|
-
const answer = (end, hasMore) => ({ events, failures, start: truncated || !events.length ? end : cursor ?? null, cursor: end, truncated, hasMore });
|
|
147
|
-
for (const [index, item] of items.entries()) {
|
|
148
|
-
if (events.length === limit && cuttable)
|
|
149
|
-
return answer(reached, true);
|
|
150
|
-
const after = index === items.length - 1 ? next : item.cursor ?? reached;
|
|
151
|
-
const { eventId = crypto.randomUUID(), timestamp = new Date().toISOString(), data, _meta } = item;
|
|
152
|
-
if (Date.parse(timestamp) < floor)
|
|
153
|
-
truncated = next !== null;
|
|
154
|
-
else if (serialisable(type, eventId, { data, _meta }, error => failures.push(error)))
|
|
155
|
-
events.push({ eventId, name: type.name, timestamp, data, ...(_meta && { _meta }), cursor: after });
|
|
156
|
-
reached = after;
|
|
157
|
-
cuttable = item.cursor != null;
|
|
158
|
-
}
|
|
159
|
-
return answer(next, Boolean(result.hasMore));
|
|
113
|
+
return { events, failures, start, cursor: position(type, until), truncated, hasMore: false };
|
|
160
114
|
};
|
|
161
|
-
// What a subscription is owed after a cursor, whichever way its type learns of events.
|
|
162
|
-
const read = (type, subscription, cursor, { maxAgeMs, maxEvents } = {}) => type.check ? checked(type, subscription, cursor, maxAgeMs, maxEvents) : since(type, subscription, cursor, maxAgeMs, maxEvents);
|
|
163
115
|
// The occurrence a subscription receives, or nothing when it is not for it. An event emitted to one subscription
|
|
164
116
|
// is for that one alone, and its arguments are not asked.
|
|
165
117
|
// An event that cannot be shaped for a subscription, because `match` or `transform` throws or what `transform`
|
|
166
118
|
// returns cannot be sent as JSON, is skipped for that subscription alone, so its cursor moves past it. It is
|
|
167
119
|
// logged, and `failed` hears of it so a stream can say so once.
|
|
168
|
-
const shape = (
|
|
169
|
-
const type = state;
|
|
120
|
+
const shape = (type, { sequence, target, occurrence }, { id, untargetable, arguments: args, context }, failed) => {
|
|
170
121
|
let data;
|
|
171
122
|
try {
|
|
172
123
|
if (target !== undefined ? untargetable || target !== id : type.match && !type.match(occurrence.data, args, context))
|
|
@@ -176,25 +127,12 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
176
127
|
JSON.stringify(data);
|
|
177
128
|
}
|
|
178
129
|
catch (error) {
|
|
179
|
-
|
|
130
|
+
const { eventId } = occurrence;
|
|
131
|
+
console.error(`holon-sh/ext/events: event ${eventId} of ${type.name} was skipped for a subscription:`, error);
|
|
132
|
+
failed({ code: INTERNAL_ERROR, message: `Event ${eventId} could not be delivered and was skipped`, data: { eventId, reason: String(error?.message ?? error) } });
|
|
180
133
|
return undefined;
|
|
181
134
|
}
|
|
182
|
-
return { ...occurrence, data,
|
|
183
|
-
};
|
|
184
|
-
const skipped = (type, eventId, error, failed) => {
|
|
185
|
-
console.error(`holon-sh/ext/events: event ${eventId} of ${type.name} was skipped for a subscription:`, error);
|
|
186
|
-
failed?.({ code: INTERNAL_ERROR, message: `Event ${eventId} could not be delivered and was skipped`, data: { eventId, reason: String(error?.message ?? error) } });
|
|
187
|
-
};
|
|
188
|
-
// What a check function found is skipped in the same way when it cannot be sent as JSON.
|
|
189
|
-
const serialisable = (type, eventId, value, failed) => {
|
|
190
|
-
try {
|
|
191
|
-
JSON.stringify(value);
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
catch (error) {
|
|
195
|
-
skipped(type, eventId, error, failed);
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
135
|
+
return { ...occurrence, data, cursor: position(type, sequence) };
|
|
198
136
|
};
|
|
199
137
|
// The event type and arguments a request names, checked as the sketch's errors require, in its order: the type,
|
|
200
138
|
// the delivery mode, the request's own fields, the arguments, and then whether the caller may subscribe. A caller
|
|
@@ -203,7 +141,7 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
203
141
|
const type = types.get(name);
|
|
204
142
|
if (!type)
|
|
205
143
|
throw refusal(removed);
|
|
206
|
-
if (
|
|
144
|
+
if (!type.delivery.includes(delivery))
|
|
207
145
|
throw refusal({ code: UNSUPPORTED, message: "Unsupported", data: { feature: "deliveryMode", value: delivery } });
|
|
208
146
|
if (cursor != null && typeof cursor !== "string")
|
|
209
147
|
throw new ProtocolError(INVALID_PARAMS, "cursor must be a string or null");
|
|
@@ -229,12 +167,11 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
229
167
|
for (const held of [...type.subscriptions.get(id)?.held ?? []])
|
|
230
168
|
held.end(error);
|
|
231
169
|
};
|
|
232
|
-
// Begins holding a subscription, by a stream
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
//
|
|
236
|
-
const open = async (
|
|
237
|
-
const type = state;
|
|
170
|
+
// Begins holding a subscription, by a stream or a poll lease: counted against the limits first, so a refused one
|
|
171
|
+
// provisions nothing. The type's onSubscribe is called when the first hold of an id begins, and its onUnsubscribe
|
|
172
|
+
// when the last ends, so two streams of one subscription set it up once. `end` is how the holder is told the app
|
|
173
|
+
// terminated it; `close` is how the holder says its hold is over.
|
|
174
|
+
const open = async (type, { id, delivery, arguments: args, context }, end) => {
|
|
238
175
|
if (types.get(type.name) !== type)
|
|
239
176
|
throw refusal(removed);
|
|
240
177
|
// Callers without an identity cannot be told apart, so they count together as one.
|
|
@@ -255,7 +192,7 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
255
192
|
}
|
|
256
193
|
const current = entry;
|
|
257
194
|
let closed = false;
|
|
258
|
-
const held = { id,
|
|
195
|
+
const held = { id, arguments: args, context, end, close() {
|
|
259
196
|
if (closed)
|
|
260
197
|
return;
|
|
261
198
|
closed = true;
|
|
@@ -306,13 +243,12 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
306
243
|
lent = undefined;
|
|
307
244
|
}
|
|
308
245
|
if (!lent) {
|
|
309
|
-
const fresh = { type }
|
|
246
|
+
const fresh = { type, opening: open(type, { ...subscribed, id: key, delivery: "poll" }, error => {
|
|
247
|
+
fresh.ended = error;
|
|
248
|
+
fresh.release?.();
|
|
249
|
+
}) };
|
|
310
250
|
lent = fresh;
|
|
311
251
|
leases.set(key, fresh);
|
|
312
|
-
fresh.opening = open(type, { ...subscribed, id: key, delivery: "poll" }, error => {
|
|
313
|
-
fresh.ended = error;
|
|
314
|
-
fresh.release?.();
|
|
315
|
-
});
|
|
316
252
|
fresh.opening.then(held => {
|
|
317
253
|
if (shared)
|
|
318
254
|
held.untargetable = true;
|
|
@@ -338,30 +274,20 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
338
274
|
current.timer.unref?.();
|
|
339
275
|
return held;
|
|
340
276
|
};
|
|
341
|
-
// What a delivery mode works with: `subscription` checks a request, names its event type and the subscription's
|
|
342
|
-
// id, `open` begins holding a subscription, `read` gives a subscription what it is owed after a cursor (`since` and
|
|
343
|
-
// `shape` for emitted events alone), `position` is a type's cursor, a type's `sinks` hear what is emitted, and its
|
|
344
|
-
// `subscriptions` are the ones held by every mode, by id.
|
|
345
|
-
const internals = { types, subscription, open, read, since, shape, position };
|
|
346
277
|
return {
|
|
347
|
-
[INTERNALS]: internals,
|
|
348
278
|
/**
|
|
349
279
|
* Declares an event type and returns the function that emits it. `match` decides which subscriptions receive an
|
|
350
|
-
* event from their arguments, and `transform` shapes its data for each.
|
|
351
|
-
* cursor, for event types whose source keeps its own history.
|
|
280
|
+
* event from their arguments, and `transform` shapes its data for each.
|
|
352
281
|
*/
|
|
353
|
-
event(name, { description, inputSchema, payloadSchema, _meta, match, transform,
|
|
282
|
+
event(name, { description, inputSchema, payloadSchema, _meta, match, transform, delivery = OFFERED, authorize, onSubscribe, onUnsubscribe }) {
|
|
354
283
|
if (types.has(name))
|
|
355
284
|
throw new Error(`Event ${name} is already declared.`);
|
|
356
|
-
const unknown = delivery.filter(mode => !
|
|
285
|
+
const unknown = delivery.filter(mode => !OFFERED.includes(mode));
|
|
357
286
|
if (unknown.length || !delivery.length)
|
|
358
|
-
throw new Error(`Event ${name} is delivered by ${delivery.join(", ") || "nothing"}, but only ${
|
|
359
|
-
// Webhook delivery sends what is emitted, and does not ask a check function.
|
|
360
|
-
if (check && delivery.includes("webhook"))
|
|
361
|
-
throw new Error(`Event ${name} has a check function, which webhook delivery does not read.`);
|
|
287
|
+
throw new Error(`Event ${name} is delivered by ${delivery.join(", ") || "nothing"}, but only ${OFFERED.join(", ")} are served.`);
|
|
362
288
|
const type = {
|
|
363
|
-
name, order: ++declared, description, inputSchema, payloadSchema, _meta, match, transform,
|
|
364
|
-
sequence: 0, dropped: 0, kept: [], sinks: new Set(), subscriptions: new Map(),
|
|
289
|
+
name, order: ++declared, description, inputSchema, payloadSchema, _meta, delivery, match, transform, authorize, onSubscribe, onUnsubscribe,
|
|
290
|
+
sequence: 0, dropped: 0, kept: [], sinks: new Set(), subscriptions: new Map(),
|
|
365
291
|
};
|
|
366
292
|
types.set(name, type);
|
|
367
293
|
return {
|
|
@@ -369,26 +295,15 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
369
295
|
const occurrence = { eventId, name, timestamp, data };
|
|
370
296
|
// Data that cannot be sent as JSON is refused here, before anything is kept or sent.
|
|
371
297
|
JSON.stringify(occurrence);
|
|
372
|
-
// A type with a check function is asked for what polls are owed, so what it emits is pushed and not kept.
|
|
373
|
-
if (type.check) {
|
|
374
|
-
// An event for one subscription that has no stream to take it is lost.
|
|
375
|
-
if (target !== undefined && ![...type.subscriptions.get(target)?.held ?? []].some(held => held.delivery !== "poll"))
|
|
376
|
-
owe(type, target);
|
|
377
|
-
for (const sink of type.sinks)
|
|
378
|
-
sink({ target, occurrence });
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
298
|
// An event for one subscription is kept like any other, so the subscription has it when it comes back.
|
|
382
299
|
const kept = { sequence: ++type.sequence, at: Date.now(), target, occurrence };
|
|
383
300
|
type.kept.push(kept);
|
|
384
301
|
while (type.kept.length > KEPT_EVENTS || type.kept[0].at < kept.at - KEPT_AGE)
|
|
385
302
|
type.dropped = type.kept.shift().sequence;
|
|
386
303
|
for (const sink of type.sinks)
|
|
387
|
-
sink(
|
|
304
|
+
sink();
|
|
388
305
|
},
|
|
389
306
|
cursor() {
|
|
390
|
-
if (type.check)
|
|
391
|
-
throw new Error(`Event ${name} has a check function, whose cursor is its source's own.`);
|
|
392
307
|
return position(type);
|
|
393
308
|
},
|
|
394
309
|
terminate(id, error = forbidden) {
|
|
@@ -408,9 +323,8 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
408
323
|
},
|
|
409
324
|
/** Serves the declared event types on an MCP server: events/list, events/poll and events/stream. */
|
|
410
325
|
serve(mcp) {
|
|
411
|
-
const underlying = "server" in mcp ? mcp.server : mcp;
|
|
412
326
|
// The custom methods are registered through the parts of the server this module uses.
|
|
413
|
-
const server =
|
|
327
|
+
const server = ("server" in mcp ? mcp.server : mcp);
|
|
414
328
|
// The sketch declares capabilities.events. The MCP SDK's client drops top-level capabilities it does not know,
|
|
415
329
|
// so the extension is declared under capabilities.extensions too, where SDK clients read it.
|
|
416
330
|
// Clients are never told the event types changed: over Holon's stateless HTTP, the notification reaches none.
|
|
@@ -440,32 +354,43 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
440
354
|
...(start + PAGE_SIZE < all.length && { nextCursor: String(page.at(-1).order) }),
|
|
441
355
|
};
|
|
442
356
|
});
|
|
443
|
-
for (const mode of modes)
|
|
444
|
-
mode.serve(underlying, internals);
|
|
445
357
|
server.setRequestHandler("events/poll", { params }, async (request, ctx) => {
|
|
446
358
|
const { type, subscription: subscribed } = await subscription(request, ctx.http, "poll");
|
|
359
|
+
// Taken before the lease, which waits for onSubscribe. The sketch's poll from null returns no events and a
|
|
360
|
+
// fresh cursor: this one is where the subscription opened, so the next poll has what onSubscribe emitted.
|
|
361
|
+
const opened = type.sequence;
|
|
447
362
|
const held = await lease(type, subscribed);
|
|
448
363
|
// Each poll is answered for its own caller, with the lease's id.
|
|
449
|
-
const { events, start, failures,
|
|
450
|
-
const lost = !held.untargetable && type.owed.delete(held.id);
|
|
364
|
+
const { events, start, failures, ...rest } = since(type, { ...subscribed, id: held.id, untargetable: held.untargetable }, request.cursor, { maxAgeMs: request.maxAgeMs, maxEvents: request.maxEvents, opened, ...(request.cursor == null && { until: opened }) });
|
|
451
365
|
// A poll carries its cursor once, for the whole response.
|
|
452
|
-
return { events: events.map(({ cursor, ...event }) => event), ...rest,
|
|
366
|
+
return { events: events.map(({ cursor, ...event }) => event), ...rest, nextPollMs: INTERVAL };
|
|
453
367
|
});
|
|
454
368
|
server.setRequestHandler("events/stream", { params }, async (request, ctx) => {
|
|
455
|
-
const { type
|
|
456
|
-
const type = state;
|
|
369
|
+
const { type, subscription: subscribed } = await subscription(request, ctx.http, "push");
|
|
457
370
|
const { signal } = ctx.mcpReq;
|
|
458
371
|
const meta = { [SUBSCRIPTION_ID_META_KEY]: ctx.mcpReq.id };
|
|
459
372
|
let done = false;
|
|
460
373
|
let ending;
|
|
374
|
+
// Set when the app terminates the stream: the last event emitted before, which the stream still sends.
|
|
375
|
+
let until;
|
|
461
376
|
let wake = () => { };
|
|
462
377
|
const finish = () => {
|
|
463
378
|
done = true;
|
|
464
379
|
wake();
|
|
465
380
|
};
|
|
381
|
+
// What is emitted from here on is the stream's, so it has what onSubscribe emits to it.
|
|
382
|
+
const opened = type.sequence;
|
|
466
383
|
const held = await open(type, { ...subscribed, delivery: "push" }, error => {
|
|
384
|
+
// Lost access ends delivery at once, even while what came before is still being sent. Otherwise only the
|
|
385
|
+
// first end counts, and the stream first sends what was emitted before it.
|
|
386
|
+
const lost = error.code === FORBIDDEN;
|
|
387
|
+
if (ending && !lost)
|
|
388
|
+
return;
|
|
467
389
|
ending = error;
|
|
468
|
-
|
|
390
|
+
if (lost)
|
|
391
|
+
return finish();
|
|
392
|
+
until = type.sequence;
|
|
393
|
+
wake();
|
|
469
394
|
});
|
|
470
395
|
// A client that left while the subscription was being set up is not waited for.
|
|
471
396
|
if (signal.aborted) {
|
|
@@ -479,32 +404,30 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
479
404
|
}
|
|
480
405
|
signal.addEventListener("abort", finish, { once: true });
|
|
481
406
|
// A stream that cannot be written to is over.
|
|
407
|
+
let sent = 0;
|
|
482
408
|
const notify = async (method, body) => {
|
|
483
409
|
if (signal.aborted)
|
|
484
410
|
return;
|
|
411
|
+
const params = { ...body, _meta: meta };
|
|
485
412
|
try {
|
|
486
|
-
await ctx.mcpReq.notify({ method: `notifications/events/${method}`, params
|
|
413
|
+
await ctx.mcpReq.notify({ method: `notifications/events/${method}`, params });
|
|
487
414
|
}
|
|
488
415
|
catch {
|
|
489
416
|
finish();
|
|
490
417
|
}
|
|
418
|
+
if ((sent += JSON.stringify(params).length) > STREAM_BUDGET)
|
|
419
|
+
finish();
|
|
491
420
|
};
|
|
492
|
-
// The stream follows its cursor
|
|
493
|
-
//
|
|
494
|
-
//
|
|
421
|
+
// The stream follows its cursor, and emitted events wake it. What it is owed waits in the buffer, not here, so
|
|
422
|
+
// a slow client is not owed more memory than one batch; when what it is owed is no longer kept, it is told of
|
|
423
|
+
// the gap.
|
|
495
424
|
let cursor = request.cursor ?? null;
|
|
496
425
|
let behind = true;
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
pushed.push(kept);
|
|
503
|
-
if (pushed.length > KEPT_EVENTS)
|
|
504
|
-
pushed.shift();
|
|
505
|
-
}
|
|
506
|
-
else
|
|
507
|
-
behind = true;
|
|
426
|
+
const sink = () => {
|
|
427
|
+
// Nothing emitted after the app terminated the stream is for it.
|
|
428
|
+
if (until !== undefined)
|
|
429
|
+
return;
|
|
430
|
+
behind = true;
|
|
508
431
|
wake();
|
|
509
432
|
};
|
|
510
433
|
type.sinks.add(sink);
|
|
@@ -529,71 +452,36 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
529
452
|
}, HEARTBEAT);
|
|
530
453
|
let first = true;
|
|
531
454
|
try {
|
|
532
|
-
let due = 0;
|
|
533
455
|
while (!done) {
|
|
534
|
-
if (
|
|
456
|
+
if (behind) {
|
|
535
457
|
behind = false;
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
await notify("error", { error
|
|
545
|
-
|
|
546
|
-
due = Date.now() + type.interval;
|
|
547
|
-
if (result) {
|
|
548
|
-
// A check function's gap is told after the events that follow it, at the position after them, since
|
|
549
|
-
// the position before them is not known and the client's cursor must not go back. The stream is
|
|
550
|
-
// confirmed before anything else is said on it.
|
|
551
|
-
const after = type.check && result.truncated && !first;
|
|
552
|
-
const truncated = result.truncated || first && type.owed.delete(held.id);
|
|
553
|
-
if (first || truncated && !after)
|
|
554
|
-
await notify("active", { cursor: type.check && result.truncated ? cursor : result.start, truncated });
|
|
555
|
-
if (first)
|
|
556
|
-
beat = heartbeats();
|
|
557
|
-
first = false;
|
|
558
|
-
for (const error of result.failures ?? [])
|
|
559
|
-
await notify("error", { error });
|
|
560
|
-
for (const event of result.events) {
|
|
561
|
-
if (done)
|
|
562
|
-
break;
|
|
563
|
-
await notify("event", event);
|
|
564
|
-
cursor = event.cursor ?? null;
|
|
565
|
-
}
|
|
458
|
+
const result = since(type, held, cursor, { ...(first && request.maxAgeMs !== undefined && { maxAgeMs: request.maxAgeMs }), opened, until });
|
|
459
|
+
// The stream is confirmed before anything else is said on it.
|
|
460
|
+
if (first || result.truncated)
|
|
461
|
+
await notify("active", { cursor: result.start, truncated: result.truncated });
|
|
462
|
+
if (first)
|
|
463
|
+
beat = heartbeats();
|
|
464
|
+
first = false;
|
|
465
|
+
for (const error of result.failures)
|
|
466
|
+
await notify("error", { error });
|
|
467
|
+
for (const event of result.events) {
|
|
566
468
|
if (done)
|
|
567
469
|
break;
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
await notify("active", { cursor, truncated: true });
|
|
571
|
-
if (result.hasMore) {
|
|
572
|
-
due = 0;
|
|
573
|
-
behind = true;
|
|
574
|
-
}
|
|
470
|
+
await notify("event", event);
|
|
471
|
+
cursor = event.cursor ?? null;
|
|
575
472
|
}
|
|
473
|
+
if (done)
|
|
474
|
+
break;
|
|
475
|
+
cursor = result.cursor;
|
|
576
476
|
}
|
|
577
|
-
//
|
|
578
|
-
|
|
579
|
-
let failure;
|
|
580
|
-
const event = shape(type, pushed.shift(), held, error => { failure = error; });
|
|
581
|
-
if (failure)
|
|
582
|
-
await notify("error", { error: failure });
|
|
583
|
-
if (event)
|
|
584
|
-
await notify("event", { ...event, cursor });
|
|
585
|
-
}
|
|
586
|
-
if (done)
|
|
477
|
+
// Terminated by the app, the stream ends once it has sent what was emitted before.
|
|
478
|
+
if (done || until !== undefined && !behind)
|
|
587
479
|
break;
|
|
588
|
-
let timer;
|
|
589
480
|
await new Promise(resolve => {
|
|
590
481
|
wake = resolve;
|
|
591
|
-
if (done ||
|
|
482
|
+
if (done || behind)
|
|
592
483
|
resolve();
|
|
593
|
-
else if (type.check)
|
|
594
|
-
timer = setTimeout(resolve, Math.max(0, due - Date.now()));
|
|
595
484
|
});
|
|
596
|
-
clearTimeout(timer);
|
|
597
485
|
wake = () => { };
|
|
598
486
|
}
|
|
599
487
|
if (ending) {
|
|
@@ -605,9 +493,6 @@ export function createEvents({ webhookTtl, webhook } = {}) {
|
|
|
605
493
|
finally {
|
|
606
494
|
clearInterval(beat);
|
|
607
495
|
type.sinks.delete(sink);
|
|
608
|
-
// What was emitted to this subscription alone and not sent is owed to it as a gap when it comes back.
|
|
609
|
-
if (held.id === subscribed.lease && pushed.some(kept => kept.target === held.id))
|
|
610
|
-
owe(type, held.id);
|
|
611
496
|
held.close();
|
|
612
497
|
}
|
|
613
498
|
// The final frame, sent when the server ends the stream; when the client did, nobody is listening.
|