@jangmanj/mcp 0.0.0-stage → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +337 -2
- package/adapters/pi/bridge.ts +721 -0
- package/adapters/pi/config.ts +364 -0
- package/adapters/pi/content-offloaded.ts +53 -0
- package/adapters/pi/content.ts +78 -0
- package/adapters/pi/diagnostics.ts +26 -0
- package/adapters/pi/elicitation.ts +133 -0
- package/adapters/pi/index.ts +210 -0
- package/adapters/pi/oauth.ts +219 -0
- package/adapters/pi/pi-api.ts +133 -0
- package/adapters/pi/resources.ts +177 -0
- package/adapters/pi/result-files.ts +32 -0
- package/adapters/pi/tasks.ts +49 -0
- package/dist/auth/client-credentials.d.ts +65 -0
- package/dist/auth/client-credentials.js +388 -0
- package/dist/auth/discovery.d.ts +28 -0
- package/dist/auth/discovery.js +125 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/node-ports.d.ts +8 -0
- package/dist/auth/node-ports.js +67 -0
- package/dist/auth/protocol.d.ts +12 -0
- package/dist/auth/protocol.js +39 -0
- package/dist/auth/provider.d.ts +114 -0
- package/dist/auth/provider.js +407 -0
- package/dist/auth/safe-fetch.d.ts +14 -0
- package/dist/auth/safe-fetch.js +130 -0
- package/dist/client/events.d.ts +33 -0
- package/dist/client/events.js +84 -0
- package/dist/client/index.d.ts +65 -0
- package/dist/client/index.js +128 -0
- package/dist/core/auth-types.d.ts +36 -0
- package/dist/core/auth-types.js +11 -0
- package/dist/core/bearer-challenge.d.ts +3 -0
- package/dist/core/bearer-challenge.js +63 -0
- package/dist/core/cancel.d.ts +70 -0
- package/dist/core/cancel.js +177 -0
- package/dist/core/extension.d.ts +33 -0
- package/dist/core/extension.js +145 -0
- package/dist/core/index.d.ts +73 -0
- package/dist/core/index.js +129 -0
- package/dist/core/jsonrpc.d.ts +49 -0
- package/dist/core/jsonrpc.js +80 -0
- package/dist/core/lifecycle.d.ts +128 -0
- package/dist/core/lifecycle.js +251 -0
- package/dist/core/outcome.d.ts +98 -0
- package/dist/core/outcome.js +32 -0
- package/dist/core/ports.d.ts +144 -0
- package/dist/core/ports.js +67 -0
- package/dist/core/progress.d.ts +81 -0
- package/dist/core/progress.js +205 -0
- package/dist/core/request.d.ts +108 -0
- package/dist/core/request.js +578 -0
- package/dist/core/schema/index.d.ts +1 -0
- package/dist/core/schema/index.js +1 -0
- package/dist/core/schema/types.d.ts +4154 -0
- package/dist/core/schema/types.js +6 -0
- package/dist/core/telemetry.d.ts +25 -0
- package/dist/core/telemetry.js +37 -0
- package/dist/ext-events/index.d.ts +79 -0
- package/dist/ext-events/index.js +218 -0
- package/dist/ext-events/model.d.ts +10 -0
- package/dist/ext-events/model.js +6 -0
- package/dist/ext-events/push.d.ts +18 -0
- package/dist/ext-events/push.js +101 -0
- package/dist/ext-events/stream.d.ts +6 -0
- package/dist/ext-events/stream.js +60 -0
- package/dist/ext-server-cards/index.d.ts +107 -0
- package/dist/ext-server-cards/index.js +249 -0
- package/dist/ext-skills/cache.d.ts +20 -0
- package/dist/ext-skills/cache.js +101 -0
- package/dist/ext-skills/client.d.ts +82 -0
- package/dist/ext-skills/client.js +292 -0
- package/dist/ext-skills/extension.d.ts +16 -0
- package/dist/ext-skills/extension.js +29 -0
- package/dist/ext-skills/index.d.ts +4 -0
- package/dist/ext-skills/index.js +4 -0
- package/dist/ext-skills/verify.d.ts +57 -0
- package/dist/ext-skills/verify.js +168 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mrtr-cache/cache.d.ts +51 -0
- package/dist/mrtr-cache/cache.js +295 -0
- package/dist/mrtr-cache/client.d.ts +24 -0
- package/dist/mrtr-cache/client.js +44 -0
- package/dist/mrtr-cache/common.d.ts +10 -0
- package/dist/mrtr-cache/common.js +9 -0
- package/dist/mrtr-cache/elicitation.d.ts +42 -0
- package/dist/mrtr-cache/elicitation.js +221 -0
- package/dist/mrtr-cache/index.d.ts +9 -0
- package/dist/mrtr-cache/index.js +8 -0
- package/dist/mrtr-cache/mrtr.d.ts +37 -0
- package/dist/mrtr-cache/mrtr.js +250 -0
- package/dist/mrtr-cache/refresh.d.ts +8 -0
- package/dist/mrtr-cache/refresh.js +30 -0
- package/dist/mrtr-cache/stream.d.ts +5 -0
- package/dist/mrtr-cache/stream.js +18 -0
- package/dist/mrtr-cache/subscriptions.d.ts +96 -0
- package/dist/mrtr-cache/subscriptions.js +288 -0
- package/dist/mrtr-cache/tasks.d.ts +103 -0
- package/dist/mrtr-cache/tasks.js +444 -0
- package/dist/ops/common.d.ts +18 -0
- package/dist/ops/common.js +39 -0
- package/dist/ops/completion.d.ts +44 -0
- package/dist/ops/completion.js +65 -0
- package/dist/ops/content.d.ts +40 -0
- package/dist/ops/content.js +117 -0
- package/dist/ops/icons.d.ts +29 -0
- package/dist/ops/icons.js +82 -0
- package/dist/ops/index.d.ts +30 -0
- package/dist/ops/index.js +28 -0
- package/dist/ops/pagination.d.ts +32 -0
- package/dist/ops/pagination.js +65 -0
- package/dist/ops/prompts.d.ts +32 -0
- package/dist/ops/prompts.js +68 -0
- package/dist/ops/resources.d.ts +42 -0
- package/dist/ops/resources.js +104 -0
- package/dist/ops/schema.d.ts +39 -0
- package/dist/ops/schema.js +163 -0
- package/dist/ops/tools.d.ts +88 -0
- package/dist/ops/tools.js +169 -0
- package/dist/ops/uri-template.d.ts +3 -0
- package/dist/ops/uri-template.js +101 -0
- package/dist/server/index.d.ts +5 -0
- package/dist/server/index.js +5 -0
- package/dist/server/mrtr.d.ts +38 -0
- package/dist/server/mrtr.js +99 -0
- package/dist/server/protocol.d.ts +21 -0
- package/dist/server/protocol.js +49 -0
- package/dist/server/server.d.ts +105 -0
- package/dist/server/server.js +383 -0
- package/dist/server/skills.d.ts +45 -0
- package/dist/server/skills.js +73 -0
- package/dist/server/uri.d.ts +2 -0
- package/dist/server/uri.js +27 -0
- package/dist/server-events/bus.d.ts +40 -0
- package/dist/server-events/bus.js +84 -0
- package/dist/server-events/events.d.ts +36 -0
- package/dist/server-events/events.js +108 -0
- package/dist/server-events/index.d.ts +6 -0
- package/dist/server-events/index.js +6 -0
- package/dist/server-events/notices.d.ts +24 -0
- package/dist/server-events/notices.js +31 -0
- package/dist/server-events/subscriptions.d.ts +33 -0
- package/dist/server-events/subscriptions.js +77 -0
- package/dist/server-events/tasks.d.ts +43 -0
- package/dist/server-events/tasks.js +101 -0
- package/dist/server-events/types.d.ts +25 -0
- package/dist/server-events/types.js +34 -0
- package/dist/server-transport/auth.d.ts +20 -0
- package/dist/server-transport/auth.js +29 -0
- package/dist/server-transport/channel.d.ts +17 -0
- package/dist/server-transport/channel.js +47 -0
- package/dist/server-transport/http.d.ts +19 -0
- package/dist/server-transport/http.js +209 -0
- package/dist/server-transport/index.d.ts +4 -0
- package/dist/server-transport/index.js +4 -0
- package/dist/server-transport/stdio.d.ts +16 -0
- package/dist/server-transport/stdio.js +83 -0
- package/dist/telemetry-otel/index.d.ts +64 -0
- package/dist/telemetry-otel/index.js +196 -0
- package/dist/testing/index.d.ts +36 -0
- package/dist/testing/index.js +140 -0
- package/dist/transport-http/headers.d.ts +24 -0
- package/dist/transport-http/headers.js +140 -0
- package/dist/transport-http/index.d.ts +3 -0
- package/dist/transport-http/index.js +3 -0
- package/dist/transport-http/sse.d.ts +33 -0
- package/dist/transport-http/sse.js +152 -0
- package/dist/transport-http/transport.d.ts +66 -0
- package/dist/transport-http/transport.js +854 -0
- package/dist/transport-stdio/framing.d.ts +50 -0
- package/dist/transport-stdio/framing.js +154 -0
- package/dist/transport-stdio/index.d.ts +4 -0
- package/dist/transport-stdio/index.js +4 -0
- package/dist/transport-stdio/stderr-ring.d.ts +27 -0
- package/dist/transport-stdio/stderr-ring.js +105 -0
- package/dist/transport-stdio/transport.d.ts +142 -0
- package/dist/transport-stdio/transport.js +743 -0
- package/dist/transport-stdio/write-queue.d.ts +48 -0
- package/dist/transport-stdio/write-queue.js +147 -0
- package/package.json +134 -4
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { McpOutcomeError, NotificationQueue, systemClock, } from "../core/index.js";
|
|
2
|
+
import { object } from "./common.js";
|
|
3
|
+
import { elicit } from "./elicitation.js";
|
|
4
|
+
export const TASKS_ID = "io.modelcontextprotocol/tasks";
|
|
5
|
+
export class MemoryTaskStore {
|
|
6
|
+
records = new Map();
|
|
7
|
+
put(r) {
|
|
8
|
+
this.records.set(JSON.stringify([r.serverKey, r.taskId]), { ...r });
|
|
9
|
+
}
|
|
10
|
+
get(s, t) {
|
|
11
|
+
return this.records.get(JSON.stringify([s, t]));
|
|
12
|
+
}
|
|
13
|
+
list(s) {
|
|
14
|
+
return [...this.records.values()].filter((r) => r.serverKey === s);
|
|
15
|
+
}
|
|
16
|
+
delete(s, t) {
|
|
17
|
+
this.records.delete(JSON.stringify([s, t]));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const duration = (n) => n === undefined || (typeof n === "number" && Number.isSafeInteger(n) && n >= 0);
|
|
21
|
+
function valid(x) {
|
|
22
|
+
return (object(x) &&
|
|
23
|
+
typeof x.taskId === "string" &&
|
|
24
|
+
x.taskId.trim().length > 0 &&
|
|
25
|
+
["working", "input_required", "completed", "failed", "cancelled"].includes(String(x.status)) &&
|
|
26
|
+
(x.ttl === null || duration(x.ttl)) &&
|
|
27
|
+
duration(x.ttlMs) &&
|
|
28
|
+
duration(x.pollIntervalMs) &&
|
|
29
|
+
(x.result === undefined || object(x.result)) &&
|
|
30
|
+
(x.error === undefined || object(x.error)) &&
|
|
31
|
+
(x.inputRequests === undefined ||
|
|
32
|
+
(object(x.inputRequests) &&
|
|
33
|
+
Object.values(x.inputRequests).every((v) => object(v) && typeof v.method === "string" && object(v.params)))));
|
|
34
|
+
}
|
|
35
|
+
function isTerminal(status) {
|
|
36
|
+
return status === "completed" || status === "failed" || status === "cancelled";
|
|
37
|
+
}
|
|
38
|
+
function extractSignal(opts) {
|
|
39
|
+
if (!opts)
|
|
40
|
+
return undefined;
|
|
41
|
+
return opts instanceof AbortSignal ? opts : opts.signal;
|
|
42
|
+
}
|
|
43
|
+
const meta = (method) => ({
|
|
44
|
+
requestId: 0,
|
|
45
|
+
method,
|
|
46
|
+
binding: "extension",
|
|
47
|
+
attempts: 0,
|
|
48
|
+
});
|
|
49
|
+
const violation = (method, detail) => ({
|
|
50
|
+
kind: "protocol-violation",
|
|
51
|
+
detail,
|
|
52
|
+
meta: meta(method),
|
|
53
|
+
});
|
|
54
|
+
export class TasksRuntime {
|
|
55
|
+
opts;
|
|
56
|
+
store;
|
|
57
|
+
serverKey;
|
|
58
|
+
clock;
|
|
59
|
+
listeners = new Map();
|
|
60
|
+
watchers = new Map();
|
|
61
|
+
states = new Map();
|
|
62
|
+
constructor(opts = {}) {
|
|
63
|
+
this.opts = opts;
|
|
64
|
+
this.store = opts.store ?? new MemoryTaskStore();
|
|
65
|
+
this.serverKey = opts.serverKey ?? "default";
|
|
66
|
+
this.clock = opts.clock ?? systemClock;
|
|
67
|
+
}
|
|
68
|
+
refusal(method) {
|
|
69
|
+
const absent = () => ({
|
|
70
|
+
kind: "capability-absent",
|
|
71
|
+
detail: TASKS_ID,
|
|
72
|
+
meta: meta(method),
|
|
73
|
+
});
|
|
74
|
+
if (this.opts.healthy === false)
|
|
75
|
+
return absent();
|
|
76
|
+
const caps = typeof this.opts.peerCapabilities === "function"
|
|
77
|
+
? this.opts.peerCapabilities()
|
|
78
|
+
: this.opts.peerCapabilities;
|
|
79
|
+
const exts = caps?.extensions;
|
|
80
|
+
if (exts !== undefined && !object(exts))
|
|
81
|
+
return violation(method, "invalid extensions");
|
|
82
|
+
if (!exts || !Object.hasOwn(exts, TASKS_ID))
|
|
83
|
+
return this.opts.force ? undefined : absent();
|
|
84
|
+
const s = exts[TASKS_ID];
|
|
85
|
+
if (!object(s) ||
|
|
86
|
+
Object.keys(s).some((k) => k !== "revision") ||
|
|
87
|
+
("revision" in s && typeof s.revision !== "string")) {
|
|
88
|
+
return violation(method, "invalid task settings");
|
|
89
|
+
}
|
|
90
|
+
if (s.revision !== undefined && s.revision !== "2026-07-28")
|
|
91
|
+
return absent();
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
remember(s) {
|
|
95
|
+
this.states.set(s.taskId, structuredClone(s));
|
|
96
|
+
const old = this.store.get(this.serverKey, s.taskId);
|
|
97
|
+
const ttl = s.ttl === null ? undefined : s.ttlMs ?? s.ttl ?? old?.ttlMs;
|
|
98
|
+
const interval = s.pollIntervalMs ?? old?.pollIntervalMs;
|
|
99
|
+
this.store.put({
|
|
100
|
+
serverKey: this.serverKey,
|
|
101
|
+
taskId: s.taskId,
|
|
102
|
+
createdAt: old?.createdAt ?? this.clock.now(),
|
|
103
|
+
lastStatus: s.status,
|
|
104
|
+
...(ttl !== undefined && { ttlMs: ttl }),
|
|
105
|
+
...(interval !== undefined && { pollIntervalMs: interval }),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
latestState(id) {
|
|
109
|
+
return this.states.get(id);
|
|
110
|
+
}
|
|
111
|
+
emitUpdate(state) {
|
|
112
|
+
const group = this.watchers.get(state.taskId);
|
|
113
|
+
if (!group)
|
|
114
|
+
return;
|
|
115
|
+
const snapshot = structuredClone(state);
|
|
116
|
+
for (const fn of [...group])
|
|
117
|
+
fn(snapshot);
|
|
118
|
+
}
|
|
119
|
+
subscribe(id, fn) {
|
|
120
|
+
let group = this.watchers.get(id);
|
|
121
|
+
if (!group) {
|
|
122
|
+
group = new Set();
|
|
123
|
+
this.watchers.set(id, group);
|
|
124
|
+
}
|
|
125
|
+
group.add(fn);
|
|
126
|
+
return () => {
|
|
127
|
+
const cur = this.watchers.get(id);
|
|
128
|
+
if (!cur)
|
|
129
|
+
return;
|
|
130
|
+
cur.delete(fn);
|
|
131
|
+
if (cur.size === 0)
|
|
132
|
+
this.watchers.delete(id);
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
accept(s) {
|
|
136
|
+
const refused = this.refusal("task");
|
|
137
|
+
if (refused)
|
|
138
|
+
return refused;
|
|
139
|
+
if (!valid(s))
|
|
140
|
+
return violation("task", "invalid CreateTaskResult");
|
|
141
|
+
const cloned = structuredClone(s);
|
|
142
|
+
this.remember(cloned);
|
|
143
|
+
return {
|
|
144
|
+
kind: "task-handle",
|
|
145
|
+
handle: new TaskHandle(this, cloned.taskId, cloned),
|
|
146
|
+
meta: meta("task"),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
resume(id) {
|
|
150
|
+
return new TaskHandle(this, id, this.states.get(id));
|
|
151
|
+
}
|
|
152
|
+
resumeAll() {
|
|
153
|
+
return this.store.list(this.serverKey).map((r) => this.resume(r.taskId));
|
|
154
|
+
}
|
|
155
|
+
wait(id, opts) {
|
|
156
|
+
if (opts?.signal?.aborted)
|
|
157
|
+
return Promise.resolve();
|
|
158
|
+
const interval = this.store.get(this.serverKey, id)?.pollIntervalMs;
|
|
159
|
+
return new Promise((resolve) => {
|
|
160
|
+
const group = this.listeners.get(id) ?? new Set();
|
|
161
|
+
this.listeners.set(id, group);
|
|
162
|
+
let clear = () => { };
|
|
163
|
+
let done = false;
|
|
164
|
+
const onAbort = () => finish();
|
|
165
|
+
const finish = () => {
|
|
166
|
+
if (done)
|
|
167
|
+
return;
|
|
168
|
+
done = true;
|
|
169
|
+
clear();
|
|
170
|
+
opts?.signal?.removeEventListener("abort", onAbort);
|
|
171
|
+
group.delete(finish);
|
|
172
|
+
if (!group.size)
|
|
173
|
+
this.listeners.delete(id);
|
|
174
|
+
resolve();
|
|
175
|
+
};
|
|
176
|
+
group.add(finish);
|
|
177
|
+
opts?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
178
|
+
clear = this.clock.setTimeout(finish, interval ?? 1000);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
notify(n) {
|
|
182
|
+
if (n.method !== "notifications/tasks" ||
|
|
183
|
+
!object(n.params) ||
|
|
184
|
+
typeof n.params.taskId !== "string") {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const taskId = n.params.taskId;
|
|
188
|
+
let state;
|
|
189
|
+
if (valid(n.params)) {
|
|
190
|
+
state = structuredClone(n.params);
|
|
191
|
+
this.remember(state);
|
|
192
|
+
}
|
|
193
|
+
const watchGroup = this.watchers.get(taskId);
|
|
194
|
+
if (watchGroup) {
|
|
195
|
+
for (const fn of [...watchGroup])
|
|
196
|
+
fn(state ? structuredClone(state) : undefined);
|
|
197
|
+
}
|
|
198
|
+
const waitGroup = this.listeners.get(taskId);
|
|
199
|
+
if (waitGroup) {
|
|
200
|
+
for (const fn of [...waitGroup])
|
|
201
|
+
fn();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async listen(stream) {
|
|
205
|
+
for await (const n of stream) {
|
|
206
|
+
this.notify(n);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export class TaskHandle {
|
|
211
|
+
runtime;
|
|
212
|
+
id;
|
|
213
|
+
state;
|
|
214
|
+
constructor(runtime, id, state) {
|
|
215
|
+
this.runtime = runtime;
|
|
216
|
+
this.id = id;
|
|
217
|
+
this.state = state;
|
|
218
|
+
}
|
|
219
|
+
currentState() {
|
|
220
|
+
return this.runtime.latestState(this.id) ?? this.state;
|
|
221
|
+
}
|
|
222
|
+
status() {
|
|
223
|
+
return (this.currentState()?.status ??
|
|
224
|
+
this.runtime.store.get(this.runtime.serverKey, this.id)?.lastStatus);
|
|
225
|
+
}
|
|
226
|
+
async send(method, extra = {}, opts) {
|
|
227
|
+
const rt = this.runtime;
|
|
228
|
+
const denied = rt.refusal(method);
|
|
229
|
+
if (denied)
|
|
230
|
+
return denied;
|
|
231
|
+
if (!rt.opts.request)
|
|
232
|
+
return violation(method, "task request port missing");
|
|
233
|
+
const out = await rt.opts.request(method, { taskId: this.id, ...extra }, {
|
|
234
|
+
headersHint: { name: this.id },
|
|
235
|
+
idempotent: method === "tasks/get",
|
|
236
|
+
...(opts?.signal ? { signal: opts.signal } : {}),
|
|
237
|
+
});
|
|
238
|
+
const s = out.kind === "task-handle" && out.handle instanceof TaskHandle
|
|
239
|
+
? out.handle.state
|
|
240
|
+
: out.kind === "ok"
|
|
241
|
+
? out.value
|
|
242
|
+
: undefined;
|
|
243
|
+
if (out.kind !== "ok" && out.kind !== "task-handle")
|
|
244
|
+
return out;
|
|
245
|
+
if (!valid(s) || s.taskId !== this.id)
|
|
246
|
+
return violation(method, "invalid task response");
|
|
247
|
+
const cloned = structuredClone(s);
|
|
248
|
+
this.state = cloned;
|
|
249
|
+
rt.remember(cloned);
|
|
250
|
+
if (opts?.notifyWatchers)
|
|
251
|
+
rt.emitUpdate(cloned);
|
|
252
|
+
return { kind: "ok", value: cloned, meta: out.meta };
|
|
253
|
+
}
|
|
254
|
+
/** Fetches the current task state immediately on demand without pre-sleeping. */
|
|
255
|
+
get(opts) {
|
|
256
|
+
const signal = extractSignal(opts);
|
|
257
|
+
return this.send("tasks/get", {}, signal ? { signal } : undefined);
|
|
258
|
+
}
|
|
259
|
+
cancel(opts) {
|
|
260
|
+
const signal = extractSignal(opts);
|
|
261
|
+
return this.send("tasks/cancel", {}, { ...(signal ? { signal } : {}), notifyWatchers: true });
|
|
262
|
+
}
|
|
263
|
+
async update(inputResponses, opts) {
|
|
264
|
+
const signal = extractSignal(opts);
|
|
265
|
+
const denied = this.runtime.refusal("tasks/update");
|
|
266
|
+
if (denied)
|
|
267
|
+
return denied;
|
|
268
|
+
if (signal?.aborted) {
|
|
269
|
+
return { kind: "cancelled", wasSent: false, phase: "input", meta: meta("tasks/update") };
|
|
270
|
+
}
|
|
271
|
+
const requests = this.currentState()?.inputRequests;
|
|
272
|
+
if (!inputResponses) {
|
|
273
|
+
const elicOpts = this.runtime.opts.elicitation;
|
|
274
|
+
if (!requests || !elicOpts)
|
|
275
|
+
return violation("tasks/update", "missing task input");
|
|
276
|
+
const responses = Object.create(null);
|
|
277
|
+
const elicitSignal = signal ?? new AbortController().signal;
|
|
278
|
+
for (const [key, req] of Object.entries(requests)) {
|
|
279
|
+
if (req.method !== "elicitation/create") {
|
|
280
|
+
return violation("tasks/update", "unsupported task input");
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
responses[key] = await elicit(req.params, elicOpts, elicitSignal);
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
if (signal?.aborted) {
|
|
287
|
+
return {
|
|
288
|
+
kind: "cancelled",
|
|
289
|
+
wasSent: false,
|
|
290
|
+
phase: "input",
|
|
291
|
+
meta: meta("tasks/update"),
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
return violation("tasks/update", "elicitation failed");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
inputResponses = responses;
|
|
298
|
+
}
|
|
299
|
+
if (requests &&
|
|
300
|
+
(Object.keys(requests).length !== Object.keys(inputResponses).length ||
|
|
301
|
+
Object.keys(requests).some((k) => !Object.hasOwn(inputResponses, k)))) {
|
|
302
|
+
return violation("tasks/update", "input keys differ");
|
|
303
|
+
}
|
|
304
|
+
return this.send("tasks/update", { inputResponses }, { ...(signal ? { signal } : {}), notifyWatchers: true });
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Push-first watcher yielding task state updates. Uses `tasks/get` only initially
|
|
308
|
+
* (when state is unknown), on wake-only notifications without state, or when `fallbackPoll` is enabled.
|
|
309
|
+
*/
|
|
310
|
+
watch(opts = {}) {
|
|
311
|
+
const { signal, fallbackPoll = false } = opts;
|
|
312
|
+
const queue = new NotificationQueue();
|
|
313
|
+
let cleaned = false;
|
|
314
|
+
const unsub = this.runtime.subscribe(this.id, (update) => {
|
|
315
|
+
queue.push(update);
|
|
316
|
+
});
|
|
317
|
+
const onAbort = () => cleanup();
|
|
318
|
+
const cleanup = () => {
|
|
319
|
+
if (cleaned)
|
|
320
|
+
return;
|
|
321
|
+
cleaned = true;
|
|
322
|
+
signal?.removeEventListener("abort", onAbort);
|
|
323
|
+
unsub();
|
|
324
|
+
queue.close();
|
|
325
|
+
};
|
|
326
|
+
if (signal) {
|
|
327
|
+
if (signal.aborted)
|
|
328
|
+
cleanup();
|
|
329
|
+
else
|
|
330
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
331
|
+
}
|
|
332
|
+
const self = this;
|
|
333
|
+
return {
|
|
334
|
+
async *[Symbol.asyncIterator]() {
|
|
335
|
+
try {
|
|
336
|
+
if (signal?.aborted)
|
|
337
|
+
return;
|
|
338
|
+
let current = self.currentState();
|
|
339
|
+
if (!current) {
|
|
340
|
+
const out = await self.send("tasks/get", {}, signal ? { signal } : undefined);
|
|
341
|
+
if (signal?.aborted)
|
|
342
|
+
return;
|
|
343
|
+
if (out.kind !== "ok")
|
|
344
|
+
throw new McpOutcomeError(out);
|
|
345
|
+
current = out.value;
|
|
346
|
+
}
|
|
347
|
+
yield structuredClone(current);
|
|
348
|
+
if (isTerminal(current.status))
|
|
349
|
+
return;
|
|
350
|
+
const iter = queue[Symbol.asyncIterator]();
|
|
351
|
+
let pendingNext;
|
|
352
|
+
for (;;) {
|
|
353
|
+
if (signal?.aborted)
|
|
354
|
+
return;
|
|
355
|
+
pendingNext ??= iter.next();
|
|
356
|
+
let step;
|
|
357
|
+
if (fallbackPoll) {
|
|
358
|
+
const interval = self.runtime.store.get(self.runtime.serverKey, self.id)?.pollIntervalMs ??
|
|
359
|
+
current.pollIntervalMs ??
|
|
360
|
+
1000;
|
|
361
|
+
let clearPoll = () => { };
|
|
362
|
+
const pollPromise = new Promise((resolve) => {
|
|
363
|
+
clearPoll = self.runtime.clock.setTimeout(() => resolve("poll"), interval);
|
|
364
|
+
});
|
|
365
|
+
step = await Promise.race([pendingNext, pollPromise]);
|
|
366
|
+
clearPoll();
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
step = await pendingNext;
|
|
370
|
+
}
|
|
371
|
+
if (signal?.aborted)
|
|
372
|
+
return;
|
|
373
|
+
if (step === "poll") {
|
|
374
|
+
const out = await self.send("tasks/get", {}, signal ? { signal } : undefined);
|
|
375
|
+
if (signal?.aborted)
|
|
376
|
+
return;
|
|
377
|
+
if (out.kind !== "ok")
|
|
378
|
+
throw new McpOutcomeError(out);
|
|
379
|
+
current = out.value;
|
|
380
|
+
yield structuredClone(current);
|
|
381
|
+
if (isTerminal(current.status))
|
|
382
|
+
return;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
pendingNext = undefined;
|
|
386
|
+
if (step.done)
|
|
387
|
+
return;
|
|
388
|
+
if (step.value) {
|
|
389
|
+
self.state = step.value;
|
|
390
|
+
current = step.value;
|
|
391
|
+
yield structuredClone(current);
|
|
392
|
+
if (isTerminal(current.status))
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
const out = await self.send("tasks/get", {}, signal ? { signal } : undefined);
|
|
397
|
+
if (signal?.aborted)
|
|
398
|
+
return;
|
|
399
|
+
if (out.kind !== "ok")
|
|
400
|
+
throw new McpOutcomeError(out);
|
|
401
|
+
current = out.value;
|
|
402
|
+
yield structuredClone(current);
|
|
403
|
+
if (isTerminal(current.status))
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
finally {
|
|
409
|
+
cleanup();
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
export class TasksExtension extends TasksRuntime {
|
|
416
|
+
id = TASKS_ID;
|
|
417
|
+
capabilities(isWorking) {
|
|
418
|
+
return isWorking && this.opts.healthy !== false ? { extensions: { [this.id]: {} } } : {};
|
|
419
|
+
}
|
|
420
|
+
attach(host) {
|
|
421
|
+
if (this.opts.healthy === false)
|
|
422
|
+
return { dispose() { } };
|
|
423
|
+
const resultReg = host.registerResultType("task", (r) => this.accept(r));
|
|
424
|
+
let notifReg;
|
|
425
|
+
try {
|
|
426
|
+
notifReg = host.onNotification?.("notifications/tasks", (n) => {
|
|
427
|
+
this.notify(n);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
catch (err) {
|
|
431
|
+
resultReg.dispose();
|
|
432
|
+
throw err;
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
dispose() {
|
|
436
|
+
notifReg?.dispose();
|
|
437
|
+
resultReg.dispose();
|
|
438
|
+
},
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
export function createTasksExtension(opts) {
|
|
443
|
+
return new TasksExtension(opts);
|
|
444
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ClientRequest, Outcome, OutcomeMeta, RequestOptions } from "../core/index.js";
|
|
2
|
+
/** The slice of McpClient the operations use. */
|
|
3
|
+
export interface OpsClient {
|
|
4
|
+
request<T = Record<string, unknown>>(method: string, params?: Record<string, unknown>, opts?: RequestOptions): ClientRequest<T>;
|
|
5
|
+
}
|
|
6
|
+
export type Capabilities = Readonly<Record<string, unknown>>;
|
|
7
|
+
export type CapabilitySource = Capabilities | (() => Capabilities | undefined) | undefined;
|
|
8
|
+
export type NonOkKind = Exclude<Outcome<never>["kind"], "ok" | "mrtr-interim">;
|
|
9
|
+
export declare function localMeta(method: string): OutcomeMeta;
|
|
10
|
+
export declare function local<T>(kind: "capability-absent" | "invalid-arguments" | "invalid-schema" | "protocol-violation" | "limit-exceeded" | "local-error", method: string, detail: unknown): Outcome<T>;
|
|
11
|
+
/** Re-type an outcome by mapping its `ok` branch. */
|
|
12
|
+
export declare function mapOutcome<T, U>(o: Outcome<T>, f: (value: T, meta: OutcomeMeta) => Outcome<U>): Outcome<U>;
|
|
13
|
+
export declare function isObj(v: unknown): v is Record<string, unknown>;
|
|
14
|
+
/** Capability gate. Returns a local outcome only when the capability is known to be absent. */
|
|
15
|
+
export declare function gate<T>(source: CapabilitySource, capability: string, method: string): Outcome<T> | undefined;
|
|
16
|
+
/** Unknown discover data: a -32601 reply means the capability is absent. */
|
|
17
|
+
export declare function mapMethodNotFound<T>(o: Outcome<T>, capability: string): Outcome<T>;
|
|
18
|
+
export declare function utf8Length(v: unknown): number;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function localMeta(method) {
|
|
2
|
+
return { requestId: 0, method, binding: "local", attempts: 0 };
|
|
3
|
+
}
|
|
4
|
+
export function local(kind, method, detail) {
|
|
5
|
+
return { kind, detail, meta: localMeta(method) };
|
|
6
|
+
}
|
|
7
|
+
/** Re-type an outcome by mapping its `ok` branch. */
|
|
8
|
+
export function mapOutcome(o, f) {
|
|
9
|
+
if (o.kind === "ok")
|
|
10
|
+
return f(o.value, o.meta);
|
|
11
|
+
return o;
|
|
12
|
+
}
|
|
13
|
+
export function isObj(v) {
|
|
14
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
15
|
+
}
|
|
16
|
+
/** Capability gate. Returns a local outcome only when the capability is known to be absent. */
|
|
17
|
+
export function gate(source, capability, method) {
|
|
18
|
+
const caps = typeof source === "function" ? source() : source;
|
|
19
|
+
if (caps === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (caps[capability] === undefined || caps[capability] === null || caps[capability] === false) {
|
|
22
|
+
return local("capability-absent", method, { capability, reason: "not-declared", wire: false });
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/** Unknown discover data: a -32601 reply means the capability is absent. */
|
|
27
|
+
export function mapMethodNotFound(o, capability) {
|
|
28
|
+
if (o.kind === "jsonrpc-error" && o.code === -32601) {
|
|
29
|
+
return {
|
|
30
|
+
kind: "capability-absent",
|
|
31
|
+
detail: { capability, reason: "method-not-found", code: -32601, message: o.message, wire: true },
|
|
32
|
+
meta: o.meta,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return o;
|
|
36
|
+
}
|
|
37
|
+
export function utf8Length(v) {
|
|
38
|
+
return new TextEncoder().encode(typeof v === "string" ? v : (JSON.stringify(v) ?? "")).length;
|
|
39
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Outcome, RequestOptions } from "../core/index.js";
|
|
2
|
+
import { type CapabilitySource, type OpsClient } from "./common.js";
|
|
3
|
+
export declare const MAX_COMPLETION_VALUES = 100;
|
|
4
|
+
export type CompletionRef = {
|
|
5
|
+
readonly type: "ref/prompt";
|
|
6
|
+
readonly name: string;
|
|
7
|
+
} | {
|
|
8
|
+
readonly type: "ref/resource";
|
|
9
|
+
readonly uri: string;
|
|
10
|
+
};
|
|
11
|
+
export interface CompleteParams {
|
|
12
|
+
readonly ref: CompletionRef;
|
|
13
|
+
readonly argument: {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly value: string;
|
|
16
|
+
};
|
|
17
|
+
readonly context?: {
|
|
18
|
+
readonly arguments?: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface CompleteResult {
|
|
22
|
+
readonly values: string[];
|
|
23
|
+
readonly total?: number;
|
|
24
|
+
readonly hasMore?: boolean;
|
|
25
|
+
/** OPS-COMP-2: values were cut to 100. */
|
|
26
|
+
readonly truncated: boolean;
|
|
27
|
+
readonly originalCount: number;
|
|
28
|
+
}
|
|
29
|
+
/** Aborts the previous in-flight run for the same key. No timers (OPS-COMP-3). */
|
|
30
|
+
export interface LatestWins {
|
|
31
|
+
run<T>(key: string, fn: (signal: AbortSignal) => T): T;
|
|
32
|
+
}
|
|
33
|
+
export declare function latestWins(): LatestWins;
|
|
34
|
+
export declare class CompletionApi {
|
|
35
|
+
private readonly client;
|
|
36
|
+
private readonly opts;
|
|
37
|
+
private readonly lw;
|
|
38
|
+
constructor(client: OpsClient, opts?: {
|
|
39
|
+
readonly capabilities?: CapabilitySource;
|
|
40
|
+
});
|
|
41
|
+
complete(p: CompleteParams, opts?: RequestOptions): Promise<Outcome<CompleteResult>>;
|
|
42
|
+
/** Coalesce rapid requests for `key`: the previous in-flight completion is aborted. */
|
|
43
|
+
latestWins(key: string, p: CompleteParams, opts?: RequestOptions): Promise<Outcome<CompleteResult>>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { gate, isObj, mapMethodNotFound, mapOutcome } from "./common.js";
|
|
2
|
+
export const MAX_COMPLETION_VALUES = 100;
|
|
3
|
+
export function latestWins() {
|
|
4
|
+
const active = new Map();
|
|
5
|
+
return {
|
|
6
|
+
run(key, fn) {
|
|
7
|
+
active.get(key)?.abort("superseded");
|
|
8
|
+
const ac = new AbortController();
|
|
9
|
+
active.set(key, ac);
|
|
10
|
+
const result = fn(ac.signal);
|
|
11
|
+
const clear = () => {
|
|
12
|
+
if (active.get(key) === ac)
|
|
13
|
+
active.delete(key);
|
|
14
|
+
};
|
|
15
|
+
if (result && typeof result.then === "function") {
|
|
16
|
+
result.then(clear, clear);
|
|
17
|
+
}
|
|
18
|
+
else
|
|
19
|
+
clear();
|
|
20
|
+
return result;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export class CompletionApi {
|
|
25
|
+
client;
|
|
26
|
+
opts;
|
|
27
|
+
lw = latestWins();
|
|
28
|
+
constructor(client, opts = {}) {
|
|
29
|
+
this.client = client;
|
|
30
|
+
this.opts = opts;
|
|
31
|
+
}
|
|
32
|
+
async complete(p, opts) {
|
|
33
|
+
const blocked = gate(this.opts.capabilities, "completions", "completion/complete");
|
|
34
|
+
if (blocked)
|
|
35
|
+
return blocked;
|
|
36
|
+
const params = { ref: p.ref, argument: p.argument, ...(p.context ? { context: p.context } : {}) };
|
|
37
|
+
const o = await this.client.request("completion/complete", params, { ...opts, idempotent: true });
|
|
38
|
+
const m = mapOutcome(mapMethodNotFound(o, "completions"), (v, meta) => {
|
|
39
|
+
const c = v["completion"];
|
|
40
|
+
if (!isObj(c) || !Array.isArray(c["values"]))
|
|
41
|
+
return { kind: "protocol-violation", detail: { reason: "bad-completion" }, meta };
|
|
42
|
+
const all = c["values"];
|
|
43
|
+
const truncated = all.length > MAX_COMPLETION_VALUES;
|
|
44
|
+
return {
|
|
45
|
+
kind: "ok",
|
|
46
|
+
value: {
|
|
47
|
+
values: truncated ? all.slice(0, MAX_COMPLETION_VALUES) : all,
|
|
48
|
+
...(typeof c["total"] === "number" ? { total: c["total"] } : {}),
|
|
49
|
+
...(typeof c["hasMore"] === "boolean" ? { hasMore: c["hasMore"] } : {}),
|
|
50
|
+
truncated,
|
|
51
|
+
originalCount: all.length,
|
|
52
|
+
},
|
|
53
|
+
meta,
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
if (m.kind === "jsonrpc-error" && m.code === -32602) {
|
|
57
|
+
return { kind: "invalid-arguments", detail: { code: m.code, message: m.message, ...(m.data !== undefined ? { data: m.data } : {}) }, meta: m.meta };
|
|
58
|
+
}
|
|
59
|
+
return m;
|
|
60
|
+
}
|
|
61
|
+
/** Coalesce rapid requests for `key`: the previous in-flight completion is aborted. */
|
|
62
|
+
latestWins(key, p, opts) {
|
|
63
|
+
return this.lw.run(key, (signal) => this.complete(p, { ...opts, signal }));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare function isBase64(s: unknown): s is string;
|
|
2
|
+
export declare function isMimeType(s: unknown): s is string;
|
|
3
|
+
/** RFC 3986 URI syntax check (OPS-RES-6). */
|
|
4
|
+
export declare function isRfc3986Uri(s: unknown): s is string;
|
|
5
|
+
export type BlockType = "text" | "image" | "audio" | "resource_link" | "resource" | "unknown";
|
|
6
|
+
export interface ContentBlock {
|
|
7
|
+
readonly type: BlockType;
|
|
8
|
+
/** The block exactly as received. Never dropped, never rewritten. */
|
|
9
|
+
readonly raw: unknown;
|
|
10
|
+
/** The original `type` string when the block is unknown. */
|
|
11
|
+
readonly rawType?: string;
|
|
12
|
+
/** Flags such as unknown-type, invalid-base64, invalid-mimeType, invalid-uri, annotation-priority-out-of-range. */
|
|
13
|
+
readonly issues: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
export declare function checkAnnotations(a: unknown, issues: string[]): void;
|
|
16
|
+
export interface ResourceEntry {
|
|
17
|
+
readonly raw: Record<string, unknown>;
|
|
18
|
+
readonly uri: string;
|
|
19
|
+
readonly mimeType?: string;
|
|
20
|
+
readonly text?: string;
|
|
21
|
+
readonly blob?: string;
|
|
22
|
+
readonly issues: readonly string[];
|
|
23
|
+
}
|
|
24
|
+
/** Check one TextResourceContents / BlobResourceContents. Returns undefined when both or neither of text/blob exist. */
|
|
25
|
+
export declare function normalizeResourceContents(raw: unknown): ResourceEntry | undefined;
|
|
26
|
+
/** Normalize one content block (OPS-TOOLS-13/14). Unknown types are preserved and flagged. */
|
|
27
|
+
export declare function normalizeContentBlock(raw: unknown): ContentBlock;
|
|
28
|
+
export declare function normalizeContent(blocks: unknown): ContentBlock[];
|
|
29
|
+
/** Blocks whose data is unusable (invalid base64 or mime on image/audio, or blob). */
|
|
30
|
+
export declare function hasInvalidMedia(b: ContentBlock): boolean;
|
|
31
|
+
export interface Measure {
|
|
32
|
+
readonly totalBytes: number;
|
|
33
|
+
readonly blocks: readonly {
|
|
34
|
+
readonly type: string;
|
|
35
|
+
readonly bytes: number;
|
|
36
|
+
}[];
|
|
37
|
+
readonly structuredContentBytes: number;
|
|
38
|
+
}
|
|
39
|
+
/** OPS-TOOLS-15: bytes per block only. The library never truncates. */
|
|
40
|
+
export declare function measure(result: unknown): Measure;
|