dc-cli-mcp 0.0.1-alpha.4 → 0.0.2-alpha.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/es/index.mjs +2 -1256
- package/es/test.mjs +20 -978
- package/package.json +13 -16
- package/types/create.d.ts +9 -0
- package/types/injectInfo.json.d.ts +15 -0
- package/types/template.json.d.ts +155 -0
- package/es/stdio-ClkAdHuL.js +0 -13843
package/es/index.mjs
CHANGED
|
@@ -1,1257 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import B from "node:process";
|
|
5
|
-
import { injectInfo as y, FormNameEnum as T } from "create-done-coding";
|
|
6
|
-
import { log as L, params2cliParams as ye, initEnvConfig as Ce, EnvConfigCallModeEnum as ke } from "@done-coding/cli-utils";
|
|
7
|
-
import { execSync as Ee } from "node:child_process";
|
|
8
|
-
class Pe {
|
|
9
|
-
constructor(e) {
|
|
10
|
-
this._server = e;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Sends a request and returns an AsyncGenerator that yields response messages.
|
|
14
|
-
* The generator is guaranteed to end with either a 'result' or 'error' message.
|
|
15
|
-
*
|
|
16
|
-
* This method provides streaming access to request processing, allowing you to
|
|
17
|
-
* observe intermediate task status updates for task-augmented requests.
|
|
18
|
-
*
|
|
19
|
-
* @param request - The request to send
|
|
20
|
-
* @param resultSchema - Zod schema for validating the result
|
|
21
|
-
* @param options - Optional request options (timeout, signal, task creation params, etc.)
|
|
22
|
-
* @returns AsyncGenerator that yields ResponseMessage objects
|
|
23
|
-
*
|
|
24
|
-
* @experimental
|
|
25
|
-
*/
|
|
26
|
-
requestStream(e, t, s) {
|
|
27
|
-
return this._server.requestStream(e, t, s);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Gets the current status of a task.
|
|
31
|
-
*
|
|
32
|
-
* @param taskId - The task identifier
|
|
33
|
-
* @param options - Optional request options
|
|
34
|
-
* @returns The task status
|
|
35
|
-
*
|
|
36
|
-
* @experimental
|
|
37
|
-
*/
|
|
38
|
-
async getTask(e, t) {
|
|
39
|
-
return this._server.getTask({ taskId: e }, t);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Retrieves the result of a completed task.
|
|
43
|
-
*
|
|
44
|
-
* @param taskId - The task identifier
|
|
45
|
-
* @param resultSchema - Zod schema for validating the result
|
|
46
|
-
* @param options - Optional request options
|
|
47
|
-
* @returns The task result
|
|
48
|
-
*
|
|
49
|
-
* @experimental
|
|
50
|
-
*/
|
|
51
|
-
async getTaskResult(e, t, s) {
|
|
52
|
-
return this._server.getTaskResult({ taskId: e }, t, s);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Lists tasks with optional pagination.
|
|
56
|
-
*
|
|
57
|
-
* @param cursor - Optional pagination cursor
|
|
58
|
-
* @param options - Optional request options
|
|
59
|
-
* @returns List of tasks with optional next cursor
|
|
60
|
-
*
|
|
61
|
-
* @experimental
|
|
62
|
-
*/
|
|
63
|
-
async listTasks(e, t) {
|
|
64
|
-
return this._server.listTasks(e ? { cursor: e } : void 0, t);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Cancels a running task.
|
|
68
|
-
*
|
|
69
|
-
* @param taskId - The task identifier
|
|
70
|
-
* @param options - Optional request options
|
|
71
|
-
*
|
|
72
|
-
* @experimental
|
|
73
|
-
*/
|
|
74
|
-
async cancelTask(e, t) {
|
|
75
|
-
return this._server.cancelTask({ taskId: e }, t);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
class Ie extends Q {
|
|
79
|
-
/**
|
|
80
|
-
* Initializes this server with the given name and version information.
|
|
81
|
-
*/
|
|
82
|
-
constructor(e, t) {
|
|
83
|
-
super(t), this._serverInfo = e, this._loggingLevels = /* @__PURE__ */ new Map(), this.LOG_LEVEL_SEVERITY = new Map(q.options.map((s, r) => [s, r])), this.isMessageIgnored = (s, r) => {
|
|
84
|
-
const a = this._loggingLevels.get(r);
|
|
85
|
-
return a ? this.LOG_LEVEL_SEVERITY.get(s) < this.LOG_LEVEL_SEVERITY.get(a) : !1;
|
|
86
|
-
}, this._capabilities = (t == null ? void 0 : t.capabilities) ?? {}, this._instructions = t == null ? void 0 : t.instructions, this._jsonSchemaValidator = (t == null ? void 0 : t.jsonSchemaValidator) ?? new ee(), this.setRequestHandler(te, (s) => this._oninitialize(s)), this.setNotificationHandler(se, () => {
|
|
87
|
-
var s;
|
|
88
|
-
return (s = this.oninitialized) == null ? void 0 : s.call(this);
|
|
89
|
-
}), this._capabilities.logging && this.setRequestHandler(re, async (s, r) => {
|
|
90
|
-
var l;
|
|
91
|
-
const a = r.sessionId || ((l = r.requestInfo) == null ? void 0 : l.headers["mcp-session-id"]) || void 0, { level: i } = s.params, n = q.safeParse(i);
|
|
92
|
-
return n.success && this._loggingLevels.set(a, n.data), {};
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Access experimental features.
|
|
97
|
-
*
|
|
98
|
-
* WARNING: These APIs are experimental and may change without notice.
|
|
99
|
-
*
|
|
100
|
-
* @experimental
|
|
101
|
-
*/
|
|
102
|
-
get experimental() {
|
|
103
|
-
return this._experimental || (this._experimental = {
|
|
104
|
-
tasks: new Pe(this)
|
|
105
|
-
}), this._experimental;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Registers new capabilities. This can only be called before connecting to a transport.
|
|
109
|
-
*
|
|
110
|
-
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
|
|
111
|
-
*/
|
|
112
|
-
registerCapabilities(e) {
|
|
113
|
-
if (this.transport)
|
|
114
|
-
throw new Error("Cannot register capabilities after connecting to transport");
|
|
115
|
-
this._capabilities = ie(this._capabilities, e);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Override request handler registration to enforce server-side validation for tools/call.
|
|
119
|
-
*/
|
|
120
|
-
setRequestHandler(e, t) {
|
|
121
|
-
var n;
|
|
122
|
-
const s = C(e), r = s == null ? void 0 : s.method;
|
|
123
|
-
if (!r)
|
|
124
|
-
throw new Error("Schema is missing a method literal");
|
|
125
|
-
let a;
|
|
126
|
-
if (ae(r)) {
|
|
127
|
-
const l = r, c = (n = l._zod) == null ? void 0 : n.def;
|
|
128
|
-
a = (c == null ? void 0 : c.value) ?? l.value;
|
|
129
|
-
} else {
|
|
130
|
-
const l = r, c = l._def;
|
|
131
|
-
a = (c == null ? void 0 : c.value) ?? l.value;
|
|
132
|
-
}
|
|
133
|
-
if (typeof a != "string")
|
|
134
|
-
throw new Error("Schema method literal must be a string");
|
|
135
|
-
if (a === "tools/call") {
|
|
136
|
-
const l = async (c, u) => {
|
|
137
|
-
const d = E($, c);
|
|
138
|
-
if (!d.success) {
|
|
139
|
-
const f = d.error instanceof Error ? d.error.message : String(d.error);
|
|
140
|
-
throw new p(m.InvalidParams, `Invalid tools/call request: ${f}`);
|
|
141
|
-
}
|
|
142
|
-
const { params: w } = d.data, h = await Promise.resolve(t(c, u));
|
|
143
|
-
if (w.task) {
|
|
144
|
-
const f = E(he, h);
|
|
145
|
-
if (!f.success) {
|
|
146
|
-
const X = f.error instanceof Error ? f.error.message : String(f.error);
|
|
147
|
-
throw new p(m.InvalidParams, `Invalid task creation result: ${X}`);
|
|
148
|
-
}
|
|
149
|
-
return f.data;
|
|
150
|
-
}
|
|
151
|
-
const b = E(fe, h);
|
|
152
|
-
if (!b.success) {
|
|
153
|
-
const f = b.error instanceof Error ? b.error.message : String(b.error);
|
|
154
|
-
throw new p(m.InvalidParams, `Invalid tools/call result: ${f}`);
|
|
155
|
-
}
|
|
156
|
-
return b.data;
|
|
157
|
-
};
|
|
158
|
-
return super.setRequestHandler(e, l);
|
|
159
|
-
}
|
|
160
|
-
return super.setRequestHandler(e, t);
|
|
161
|
-
}
|
|
162
|
-
assertCapabilityForMethod(e) {
|
|
163
|
-
var t, s, r;
|
|
164
|
-
switch (e) {
|
|
165
|
-
case "sampling/createMessage":
|
|
166
|
-
if (!((t = this._clientCapabilities) != null && t.sampling))
|
|
167
|
-
throw new Error(`Client does not support sampling (required for ${e})`);
|
|
168
|
-
break;
|
|
169
|
-
case "elicitation/create":
|
|
170
|
-
if (!((s = this._clientCapabilities) != null && s.elicitation))
|
|
171
|
-
throw new Error(`Client does not support elicitation (required for ${e})`);
|
|
172
|
-
break;
|
|
173
|
-
case "roots/list":
|
|
174
|
-
if (!((r = this._clientCapabilities) != null && r.roots))
|
|
175
|
-
throw new Error(`Client does not support listing roots (required for ${e})`);
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
assertNotificationCapability(e) {
|
|
180
|
-
var t, s;
|
|
181
|
-
switch (e) {
|
|
182
|
-
case "notifications/message":
|
|
183
|
-
if (!this._capabilities.logging)
|
|
184
|
-
throw new Error(`Server does not support logging (required for ${e})`);
|
|
185
|
-
break;
|
|
186
|
-
case "notifications/resources/updated":
|
|
187
|
-
case "notifications/resources/list_changed":
|
|
188
|
-
if (!this._capabilities.resources)
|
|
189
|
-
throw new Error(`Server does not support notifying about resources (required for ${e})`);
|
|
190
|
-
break;
|
|
191
|
-
case "notifications/tools/list_changed":
|
|
192
|
-
if (!this._capabilities.tools)
|
|
193
|
-
throw new Error(`Server does not support notifying of tool list changes (required for ${e})`);
|
|
194
|
-
break;
|
|
195
|
-
case "notifications/prompts/list_changed":
|
|
196
|
-
if (!this._capabilities.prompts)
|
|
197
|
-
throw new Error(`Server does not support notifying of prompt list changes (required for ${e})`);
|
|
198
|
-
break;
|
|
199
|
-
case "notifications/elicitation/complete":
|
|
200
|
-
if (!((s = (t = this._clientCapabilities) == null ? void 0 : t.elicitation) != null && s.url))
|
|
201
|
-
throw new Error(`Client does not support URL elicitation (required for ${e})`);
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
assertRequestHandlerCapability(e) {
|
|
206
|
-
if (this._capabilities)
|
|
207
|
-
switch (e) {
|
|
208
|
-
case "completion/complete":
|
|
209
|
-
if (!this._capabilities.completions)
|
|
210
|
-
throw new Error(`Server does not support completions (required for ${e})`);
|
|
211
|
-
break;
|
|
212
|
-
case "logging/setLevel":
|
|
213
|
-
if (!this._capabilities.logging)
|
|
214
|
-
throw new Error(`Server does not support logging (required for ${e})`);
|
|
215
|
-
break;
|
|
216
|
-
case "prompts/get":
|
|
217
|
-
case "prompts/list":
|
|
218
|
-
if (!this._capabilities.prompts)
|
|
219
|
-
throw new Error(`Server does not support prompts (required for ${e})`);
|
|
220
|
-
break;
|
|
221
|
-
case "resources/list":
|
|
222
|
-
case "resources/templates/list":
|
|
223
|
-
case "resources/read":
|
|
224
|
-
if (!this._capabilities.resources)
|
|
225
|
-
throw new Error(`Server does not support resources (required for ${e})`);
|
|
226
|
-
break;
|
|
227
|
-
case "tools/call":
|
|
228
|
-
case "tools/list":
|
|
229
|
-
if (!this._capabilities.tools)
|
|
230
|
-
throw new Error(`Server does not support tools (required for ${e})`);
|
|
231
|
-
break;
|
|
232
|
-
case "tasks/get":
|
|
233
|
-
case "tasks/list":
|
|
234
|
-
case "tasks/result":
|
|
235
|
-
case "tasks/cancel":
|
|
236
|
-
if (!this._capabilities.tasks)
|
|
237
|
-
throw new Error(`Server does not support tasks capability (required for ${e})`);
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
assertTaskCapability(e) {
|
|
242
|
-
var t, s;
|
|
243
|
-
ne((s = (t = this._clientCapabilities) == null ? void 0 : t.tasks) == null ? void 0 : s.requests, e, "Client");
|
|
244
|
-
}
|
|
245
|
-
assertTaskHandlerCapability(e) {
|
|
246
|
-
var t;
|
|
247
|
-
this._capabilities && oe((t = this._capabilities.tasks) == null ? void 0 : t.requests, e, "Server");
|
|
248
|
-
}
|
|
249
|
-
async _oninitialize(e) {
|
|
250
|
-
const t = e.params.protocolVersion;
|
|
251
|
-
return this._clientCapabilities = e.params.capabilities, this._clientVersion = e.params.clientInfo, {
|
|
252
|
-
protocolVersion: me.includes(t) ? t : ce,
|
|
253
|
-
capabilities: this.getCapabilities(),
|
|
254
|
-
serverInfo: this._serverInfo,
|
|
255
|
-
...this._instructions && { instructions: this._instructions }
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
260
|
-
*/
|
|
261
|
-
getClientCapabilities() {
|
|
262
|
-
return this._clientCapabilities;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
266
|
-
*/
|
|
267
|
-
getClientVersion() {
|
|
268
|
-
return this._clientVersion;
|
|
269
|
-
}
|
|
270
|
-
getCapabilities() {
|
|
271
|
-
return this._capabilities;
|
|
272
|
-
}
|
|
273
|
-
async ping() {
|
|
274
|
-
return this.request({ method: "ping" }, le);
|
|
275
|
-
}
|
|
276
|
-
// Implementation
|
|
277
|
-
async createMessage(e, t) {
|
|
278
|
-
var s, r;
|
|
279
|
-
if ((e.tools || e.toolChoice) && !((r = (s = this._clientCapabilities) == null ? void 0 : s.sampling) != null && r.tools))
|
|
280
|
-
throw new Error("Client does not support sampling tools capability.");
|
|
281
|
-
if (e.messages.length > 0) {
|
|
282
|
-
const a = e.messages[e.messages.length - 1], i = Array.isArray(a.content) ? a.content : [a.content], n = i.some((d) => d.type === "tool_result"), l = e.messages.length > 1 ? e.messages[e.messages.length - 2] : void 0, c = l ? Array.isArray(l.content) ? l.content : [l.content] : [], u = c.some((d) => d.type === "tool_use");
|
|
283
|
-
if (n) {
|
|
284
|
-
if (i.some((d) => d.type !== "tool_result"))
|
|
285
|
-
throw new Error("The last message must contain only tool_result content if any is present");
|
|
286
|
-
if (!u)
|
|
287
|
-
throw new Error("tool_result blocks are not matching any tool_use from the previous message");
|
|
288
|
-
}
|
|
289
|
-
if (u) {
|
|
290
|
-
const d = new Set(c.filter((h) => h.type === "tool_use").map((h) => h.id)), w = new Set(i.filter((h) => h.type === "tool_result").map((h) => h.toolUseId));
|
|
291
|
-
if (d.size !== w.size || ![...d].every((h) => w.has(h)))
|
|
292
|
-
throw new Error("ids of tool_result blocks and tool_use blocks from previous message do not match");
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
return e.tools ? this.request({ method: "sampling/createMessage", params: e }, de, t) : this.request({ method: "sampling/createMessage", params: e }, ue, t);
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Creates an elicitation request for the given parameters.
|
|
299
|
-
* For backwards compatibility, `mode` may be omitted for form requests and will default to `'form'`.
|
|
300
|
-
* @param params The parameters for the elicitation request.
|
|
301
|
-
* @param options Optional request options.
|
|
302
|
-
* @returns The result of the elicitation request.
|
|
303
|
-
*/
|
|
304
|
-
async elicitInput(e, t) {
|
|
305
|
-
var r, a, i, n;
|
|
306
|
-
switch (e.mode ?? "form") {
|
|
307
|
-
case "url": {
|
|
308
|
-
if (!((a = (r = this._clientCapabilities) == null ? void 0 : r.elicitation) != null && a.url))
|
|
309
|
-
throw new Error("Client does not support url elicitation.");
|
|
310
|
-
const l = e;
|
|
311
|
-
return this.request({ method: "elicitation/create", params: l }, M, t);
|
|
312
|
-
}
|
|
313
|
-
case "form": {
|
|
314
|
-
if (!((n = (i = this._clientCapabilities) == null ? void 0 : i.elicitation) != null && n.form))
|
|
315
|
-
throw new Error("Client does not support form elicitation.");
|
|
316
|
-
const l = e.mode === "form" ? e : { ...e, mode: "form" }, c = await this.request({ method: "elicitation/create", params: l }, M, t);
|
|
317
|
-
if (c.action === "accept" && c.content && l.requestedSchema)
|
|
318
|
-
try {
|
|
319
|
-
const d = this._jsonSchemaValidator.getValidator(l.requestedSchema)(c.content);
|
|
320
|
-
if (!d.valid)
|
|
321
|
-
throw new p(m.InvalidParams, `Elicitation response content does not match requested schema: ${d.errorMessage}`);
|
|
322
|
-
} catch (u) {
|
|
323
|
-
throw u instanceof p ? u : new p(m.InternalError, `Error validating elicitation response: ${u instanceof Error ? u.message : String(u)}`);
|
|
324
|
-
}
|
|
325
|
-
return c;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Creates a reusable callback that, when invoked, will send a `notifications/elicitation/complete`
|
|
331
|
-
* notification for the specified elicitation ID.
|
|
332
|
-
*
|
|
333
|
-
* @param elicitationId The ID of the elicitation to mark as complete.
|
|
334
|
-
* @param options Optional notification options. Useful when the completion notification should be related to a prior request.
|
|
335
|
-
* @returns A function that emits the completion notification when awaited.
|
|
336
|
-
*/
|
|
337
|
-
createElicitationCompletionNotifier(e, t) {
|
|
338
|
-
var s, r;
|
|
339
|
-
if (!((r = (s = this._clientCapabilities) == null ? void 0 : s.elicitation) != null && r.url))
|
|
340
|
-
throw new Error("Client does not support URL elicitation (required for notifications/elicitation/complete)");
|
|
341
|
-
return () => this.notification({
|
|
342
|
-
method: "notifications/elicitation/complete",
|
|
343
|
-
params: {
|
|
344
|
-
elicitationId: e
|
|
345
|
-
}
|
|
346
|
-
}, t);
|
|
347
|
-
}
|
|
348
|
-
async listRoots(e, t) {
|
|
349
|
-
return this.request({ method: "roots/list", params: e }, pe, t);
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Sends a logging message to the client, if connected.
|
|
353
|
-
* Note: You only need to send the parameters object, not the entire JSON RPC message
|
|
354
|
-
* @see LoggingMessageNotification
|
|
355
|
-
* @param params
|
|
356
|
-
* @param sessionId optional for stateless and backward compatibility
|
|
357
|
-
*/
|
|
358
|
-
async sendLoggingMessage(e, t) {
|
|
359
|
-
if (this._capabilities.logging && !this.isMessageIgnored(e.level, t))
|
|
360
|
-
return this.notification({ method: "notifications/message", params: e });
|
|
361
|
-
}
|
|
362
|
-
async sendResourceUpdated(e) {
|
|
363
|
-
return this.notification({
|
|
364
|
-
method: "notifications/resources/updated",
|
|
365
|
-
params: e
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
async sendResourceListChanged() {
|
|
369
|
-
return this.notification({
|
|
370
|
-
method: "notifications/resources/list_changed"
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
async sendToolListChanged() {
|
|
374
|
-
return this.notification({ method: "notifications/tools/list_changed" });
|
|
375
|
-
}
|
|
376
|
-
async sendPromptListChanged() {
|
|
377
|
-
return this.notification({ method: "notifications/prompts/list_changed" });
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
const D = Symbol.for("mcp.completable");
|
|
381
|
-
function Z(o) {
|
|
382
|
-
return !!o && typeof o == "object" && D in o;
|
|
383
|
-
}
|
|
384
|
-
function Le(o) {
|
|
385
|
-
const e = o[D];
|
|
386
|
-
return e == null ? void 0 : e.complete;
|
|
387
|
-
}
|
|
388
|
-
var G;
|
|
389
|
-
(function(o) {
|
|
390
|
-
o.Completable = "McpCompletable";
|
|
391
|
-
})(G || (G = {}));
|
|
392
|
-
const $e = /^[A-Za-z0-9._-]{1,128}$/;
|
|
393
|
-
function He(o) {
|
|
394
|
-
const e = [];
|
|
395
|
-
if (o.length === 0)
|
|
396
|
-
return {
|
|
397
|
-
isValid: !1,
|
|
398
|
-
warnings: ["Tool name cannot be empty"]
|
|
399
|
-
};
|
|
400
|
-
if (o.length > 128)
|
|
401
|
-
return {
|
|
402
|
-
isValid: !1,
|
|
403
|
-
warnings: [`Tool name exceeds maximum length of 128 characters (current: ${o.length})`]
|
|
404
|
-
};
|
|
405
|
-
if (o.includes(" ") && e.push("Tool name contains spaces, which may cause parsing issues"), o.includes(",") && e.push("Tool name contains commas, which may cause parsing issues"), (o.startsWith("-") || o.endsWith("-")) && e.push("Tool name starts or ends with a dash, which may cause parsing issues in some contexts"), (o.startsWith(".") || o.endsWith(".")) && e.push("Tool name starts or ends with a dot, which may cause parsing issues in some contexts"), !$e.test(o)) {
|
|
406
|
-
const t = o.split("").filter((s) => !/[A-Za-z0-9._-]/.test(s)).filter((s, r, a) => a.indexOf(s) === r);
|
|
407
|
-
return e.push(`Tool name contains invalid characters: ${t.map((s) => `"${s}"`).join(", ")}`, "Allowed characters are: A-Z, a-z, 0-9, underscore (_), dash (-), and dot (.)"), {
|
|
408
|
-
isValid: !1,
|
|
409
|
-
warnings: e
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
return {
|
|
413
|
-
isValid: !0,
|
|
414
|
-
warnings: e
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
function qe(o, e) {
|
|
418
|
-
if (e.length > 0) {
|
|
419
|
-
console.warn(`Tool name validation warning for "${o}":`);
|
|
420
|
-
for (const t of e)
|
|
421
|
-
console.warn(` - ${t}`);
|
|
422
|
-
console.warn("Tool registration will proceed, but this may cause compatibility issues."), console.warn("Consider updating the tool name to conform to the MCP tool naming standard."), console.warn("See SEP: Specify Format for Tool Names (https://github.com/modelcontextprotocol/modelcontextprotocol/issues/986) for more details.");
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
function J(o) {
|
|
426
|
-
const e = He(o);
|
|
427
|
-
return qe(o, e.warnings), e.isValid;
|
|
428
|
-
}
|
|
429
|
-
class Me {
|
|
430
|
-
constructor(e) {
|
|
431
|
-
this._mcpServer = e;
|
|
432
|
-
}
|
|
433
|
-
registerToolTask(e, t, s) {
|
|
434
|
-
const r = { taskSupport: "required", ...t.execution };
|
|
435
|
-
if (r.taskSupport === "forbidden")
|
|
436
|
-
throw new Error(`Cannot register task-based tool '${e}' with taskSupport 'forbidden'. Use registerTool() instead.`);
|
|
437
|
-
return this._mcpServer._createRegisteredTool(e, t.title, t.description, t.inputSchema, t.outputSchema, t.annotations, r, t._meta, s);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
class je {
|
|
441
|
-
constructor(e, t) {
|
|
442
|
-
this._registeredResources = {}, this._registeredResourceTemplates = {}, this._registeredTools = {}, this._registeredPrompts = {}, this._toolHandlersInitialized = !1, this._completionHandlerInitialized = !1, this._resourceHandlersInitialized = !1, this._promptHandlersInitialized = !1, this.server = new Ie(e, t);
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Access experimental features.
|
|
446
|
-
*
|
|
447
|
-
* WARNING: These APIs are experimental and may change without notice.
|
|
448
|
-
*
|
|
449
|
-
* @experimental
|
|
450
|
-
*/
|
|
451
|
-
get experimental() {
|
|
452
|
-
return this._experimental || (this._experimental = {
|
|
453
|
-
tasks: new Me(this)
|
|
454
|
-
}), this._experimental;
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Attaches to the given transport, starts it, and starts listening for messages.
|
|
458
|
-
*
|
|
459
|
-
* The `server` object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
|
|
460
|
-
*/
|
|
461
|
-
async connect(e) {
|
|
462
|
-
return await this.server.connect(e);
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Closes the connection.
|
|
466
|
-
*/
|
|
467
|
-
async close() {
|
|
468
|
-
await this.server.close();
|
|
469
|
-
}
|
|
470
|
-
setToolRequestHandlers() {
|
|
471
|
-
this._toolHandlersInitialized || (this.server.assertCanSetRequestHandler(g(j)), this.server.assertCanSetRequestHandler(g($)), this.server.registerCapabilities({
|
|
472
|
-
tools: {
|
|
473
|
-
listChanged: !0
|
|
474
|
-
}
|
|
475
|
-
}), this.server.setRequestHandler(j, () => ({
|
|
476
|
-
tools: Object.entries(this._registeredTools).filter(([, e]) => e.enabled).map(([e, t]) => {
|
|
477
|
-
const s = {
|
|
478
|
-
name: e,
|
|
479
|
-
title: t.title,
|
|
480
|
-
description: t.description,
|
|
481
|
-
inputSchema: (() => {
|
|
482
|
-
const r = _(t.inputSchema);
|
|
483
|
-
return r ? O(r, {
|
|
484
|
-
strictUnions: !0,
|
|
485
|
-
pipeStrategy: "input"
|
|
486
|
-
}) : Oe;
|
|
487
|
-
})(),
|
|
488
|
-
annotations: t.annotations,
|
|
489
|
-
execution: t.execution,
|
|
490
|
-
_meta: t._meta
|
|
491
|
-
};
|
|
492
|
-
if (t.outputSchema) {
|
|
493
|
-
const r = _(t.outputSchema);
|
|
494
|
-
r && (s.outputSchema = O(r, {
|
|
495
|
-
strictUnions: !0,
|
|
496
|
-
pipeStrategy: "output"
|
|
497
|
-
}));
|
|
498
|
-
}
|
|
499
|
-
return s;
|
|
500
|
-
})
|
|
501
|
-
})), this.server.setRequestHandler($, async (e, t) => {
|
|
502
|
-
var s;
|
|
503
|
-
try {
|
|
504
|
-
const r = this._registeredTools[e.params.name];
|
|
505
|
-
if (!r)
|
|
506
|
-
throw new p(m.InvalidParams, `Tool ${e.params.name} not found`);
|
|
507
|
-
if (!r.enabled)
|
|
508
|
-
throw new p(m.InvalidParams, `Tool ${e.params.name} disabled`);
|
|
509
|
-
const a = !!e.params.task, i = (s = r.execution) == null ? void 0 : s.taskSupport, n = "createTask" in r.handler;
|
|
510
|
-
if ((i === "required" || i === "optional") && !n)
|
|
511
|
-
throw new p(m.InternalError, `Tool ${e.params.name} has taskSupport '${i}' but was not registered with registerToolTask`);
|
|
512
|
-
if (i === "required" && !a)
|
|
513
|
-
throw new p(m.MethodNotFound, `Tool ${e.params.name} requires task augmentation (taskSupport: 'required')`);
|
|
514
|
-
if (i === "optional" && !a && n)
|
|
515
|
-
return await this.handleAutomaticTaskPolling(r, e, t);
|
|
516
|
-
const l = await this.validateToolInput(r, e.params.arguments, e.params.name), c = await this.executeToolHandler(r, l, t);
|
|
517
|
-
return a || await this.validateToolOutput(r, c, e.params.name), c;
|
|
518
|
-
} catch (r) {
|
|
519
|
-
if (r instanceof p && r.code === m.UrlElicitationRequired)
|
|
520
|
-
throw r;
|
|
521
|
-
return this.createToolError(r instanceof Error ? r.message : String(r));
|
|
522
|
-
}
|
|
523
|
-
}), this._toolHandlersInitialized = !0);
|
|
524
|
-
}
|
|
525
|
-
/**
|
|
526
|
-
* Creates a tool error result.
|
|
527
|
-
*
|
|
528
|
-
* @param errorMessage - The error message.
|
|
529
|
-
* @returns The tool error result.
|
|
530
|
-
*/
|
|
531
|
-
createToolError(e) {
|
|
532
|
-
return {
|
|
533
|
-
content: [
|
|
534
|
-
{
|
|
535
|
-
type: "text",
|
|
536
|
-
text: e
|
|
537
|
-
}
|
|
538
|
-
],
|
|
539
|
-
isError: !0
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Validates tool input arguments against the tool's input schema.
|
|
544
|
-
*/
|
|
545
|
-
async validateToolInput(e, t, s) {
|
|
546
|
-
if (!e.inputSchema)
|
|
547
|
-
return;
|
|
548
|
-
const a = _(e.inputSchema) ?? e.inputSchema, i = await P(a, t);
|
|
549
|
-
if (!i.success) {
|
|
550
|
-
const n = "error" in i ? i.error : "Unknown error", l = I(n);
|
|
551
|
-
throw new p(m.InvalidParams, `Input validation error: Invalid arguments for tool ${s}: ${l}`);
|
|
552
|
-
}
|
|
553
|
-
return i.data;
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Validates tool output against the tool's output schema.
|
|
557
|
-
*/
|
|
558
|
-
async validateToolOutput(e, t, s) {
|
|
559
|
-
if (!e.outputSchema || !("content" in t) || t.isError)
|
|
560
|
-
return;
|
|
561
|
-
if (!t.structuredContent)
|
|
562
|
-
throw new p(m.InvalidParams, `Output validation error: Tool ${s} has an output schema but no structured content was provided`);
|
|
563
|
-
const r = _(e.outputSchema), a = await P(r, t.structuredContent);
|
|
564
|
-
if (!a.success) {
|
|
565
|
-
const i = "error" in a ? a.error : "Unknown error", n = I(i);
|
|
566
|
-
throw new p(m.InvalidParams, `Output validation error: Invalid structured content for tool ${s}: ${n}`);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
/**
|
|
570
|
-
* Executes a tool handler (either regular or task-based).
|
|
571
|
-
*/
|
|
572
|
-
async executeToolHandler(e, t, s) {
|
|
573
|
-
const r = e.handler;
|
|
574
|
-
if ("createTask" in r) {
|
|
575
|
-
if (!s.taskStore)
|
|
576
|
-
throw new Error("No task store provided.");
|
|
577
|
-
const i = { ...s, taskStore: s.taskStore };
|
|
578
|
-
if (e.inputSchema) {
|
|
579
|
-
const n = r;
|
|
580
|
-
return await Promise.resolve(n.createTask(t, i));
|
|
581
|
-
} else {
|
|
582
|
-
const n = r;
|
|
583
|
-
return await Promise.resolve(n.createTask(i));
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
if (e.inputSchema) {
|
|
587
|
-
const i = r;
|
|
588
|
-
return await Promise.resolve(i(t, s));
|
|
589
|
-
} else {
|
|
590
|
-
const i = r;
|
|
591
|
-
return await Promise.resolve(i(s));
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* Handles automatic task polling for tools with taskSupport 'optional'.
|
|
596
|
-
*/
|
|
597
|
-
async handleAutomaticTaskPolling(e, t, s) {
|
|
598
|
-
if (!s.taskStore)
|
|
599
|
-
throw new Error("No task store provided for task-capable tool.");
|
|
600
|
-
const r = await this.validateToolInput(e, t.params.arguments, t.params.name), a = e.handler, i = { ...s, taskStore: s.taskStore }, n = r ? await Promise.resolve(a.createTask(r, i)) : (
|
|
601
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
602
|
-
await Promise.resolve(a.createTask(i))
|
|
603
|
-
), l = n.task.taskId;
|
|
604
|
-
let c = n.task;
|
|
605
|
-
const u = c.pollInterval ?? 5e3;
|
|
606
|
-
for (; c.status !== "completed" && c.status !== "failed" && c.status !== "cancelled"; ) {
|
|
607
|
-
await new Promise((w) => setTimeout(w, u));
|
|
608
|
-
const d = await s.taskStore.getTask(l);
|
|
609
|
-
if (!d)
|
|
610
|
-
throw new p(m.InternalError, `Task ${l} not found during polling`);
|
|
611
|
-
c = d;
|
|
612
|
-
}
|
|
613
|
-
return await s.taskStore.getTaskResult(l);
|
|
614
|
-
}
|
|
615
|
-
setCompletionRequestHandler() {
|
|
616
|
-
this._completionHandlerInitialized || (this.server.assertCanSetRequestHandler(g(x)), this.server.registerCapabilities({
|
|
617
|
-
completions: {}
|
|
618
|
-
}), this.server.setRequestHandler(x, async (e) => {
|
|
619
|
-
switch (e.params.ref.type) {
|
|
620
|
-
case "ref/prompt":
|
|
621
|
-
return we(e), this.handlePromptCompletion(e, e.params.ref);
|
|
622
|
-
case "ref/resource":
|
|
623
|
-
return ge(e), this.handleResourceCompletion(e, e.params.ref);
|
|
624
|
-
default:
|
|
625
|
-
throw new p(m.InvalidParams, `Invalid completion reference: ${e.params.ref}`);
|
|
626
|
-
}
|
|
627
|
-
}), this._completionHandlerInitialized = !0);
|
|
628
|
-
}
|
|
629
|
-
async handlePromptCompletion(e, t) {
|
|
630
|
-
const s = this._registeredPrompts[t.name];
|
|
631
|
-
if (!s)
|
|
632
|
-
throw new p(m.InvalidParams, `Prompt ${t.name} not found`);
|
|
633
|
-
if (!s.enabled)
|
|
634
|
-
throw new p(m.InvalidParams, `Prompt ${t.name} disabled`);
|
|
635
|
-
if (!s.argsSchema)
|
|
636
|
-
return v;
|
|
637
|
-
const r = C(s.argsSchema), a = r == null ? void 0 : r[e.params.argument.name];
|
|
638
|
-
if (!Z(a))
|
|
639
|
-
return v;
|
|
640
|
-
const i = Le(a);
|
|
641
|
-
if (!i)
|
|
642
|
-
return v;
|
|
643
|
-
const n = await i(e.params.argument.value, e.params.context);
|
|
644
|
-
return W(n);
|
|
645
|
-
}
|
|
646
|
-
async handleResourceCompletion(e, t) {
|
|
647
|
-
const s = Object.values(this._registeredResourceTemplates).find((i) => i.resourceTemplate.uriTemplate.toString() === t.uri);
|
|
648
|
-
if (!s) {
|
|
649
|
-
if (this._registeredResources[t.uri])
|
|
650
|
-
return v;
|
|
651
|
-
throw new p(m.InvalidParams, `Resource template ${e.params.ref.uri} not found`);
|
|
652
|
-
}
|
|
653
|
-
const r = s.resourceTemplate.completeCallback(e.params.argument.name);
|
|
654
|
-
if (!r)
|
|
655
|
-
return v;
|
|
656
|
-
const a = await r(e.params.argument.value, e.params.context);
|
|
657
|
-
return W(a);
|
|
658
|
-
}
|
|
659
|
-
setResourceRequestHandlers() {
|
|
660
|
-
this._resourceHandlersInitialized || (this.server.assertCanSetRequestHandler(g(V)), this.server.assertCanSetRequestHandler(g(z)), this.server.assertCanSetRequestHandler(g(A)), this.server.registerCapabilities({
|
|
661
|
-
resources: {
|
|
662
|
-
listChanged: !0
|
|
663
|
-
}
|
|
664
|
-
}), this.server.setRequestHandler(V, async (e, t) => {
|
|
665
|
-
const s = Object.entries(this._registeredResources).filter(([a, i]) => i.enabled).map(([a, i]) => ({
|
|
666
|
-
uri: a,
|
|
667
|
-
name: i.name,
|
|
668
|
-
...i.metadata
|
|
669
|
-
})), r = [];
|
|
670
|
-
for (const a of Object.values(this._registeredResourceTemplates)) {
|
|
671
|
-
if (!a.resourceTemplate.listCallback)
|
|
672
|
-
continue;
|
|
673
|
-
const i = await a.resourceTemplate.listCallback(t);
|
|
674
|
-
for (const n of i.resources)
|
|
675
|
-
r.push({
|
|
676
|
-
...a.metadata,
|
|
677
|
-
// the defined resource metadata should override the template metadata if present
|
|
678
|
-
...n
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
return { resources: [...s, ...r] };
|
|
682
|
-
}), this.server.setRequestHandler(z, async () => ({ resourceTemplates: Object.entries(this._registeredResourceTemplates).map(([t, s]) => ({
|
|
683
|
-
name: t,
|
|
684
|
-
uriTemplate: s.resourceTemplate.uriTemplate.toString(),
|
|
685
|
-
...s.metadata
|
|
686
|
-
})) })), this.server.setRequestHandler(A, async (e, t) => {
|
|
687
|
-
const s = new URL(e.params.uri), r = this._registeredResources[s.toString()];
|
|
688
|
-
if (r) {
|
|
689
|
-
if (!r.enabled)
|
|
690
|
-
throw new p(m.InvalidParams, `Resource ${s} disabled`);
|
|
691
|
-
return r.readCallback(s, t);
|
|
692
|
-
}
|
|
693
|
-
for (const a of Object.values(this._registeredResourceTemplates)) {
|
|
694
|
-
const i = a.resourceTemplate.uriTemplate.match(s.toString());
|
|
695
|
-
if (i)
|
|
696
|
-
return a.readCallback(s, i, t);
|
|
697
|
-
}
|
|
698
|
-
throw new p(m.InvalidParams, `Resource ${s} not found`);
|
|
699
|
-
}), this._resourceHandlersInitialized = !0);
|
|
700
|
-
}
|
|
701
|
-
setPromptRequestHandlers() {
|
|
702
|
-
this._promptHandlersInitialized || (this.server.assertCanSetRequestHandler(g(N)), this.server.assertCanSetRequestHandler(g(U)), this.server.registerCapabilities({
|
|
703
|
-
prompts: {
|
|
704
|
-
listChanged: !0
|
|
705
|
-
}
|
|
706
|
-
}), this.server.setRequestHandler(N, () => ({
|
|
707
|
-
prompts: Object.entries(this._registeredPrompts).filter(([, e]) => e.enabled).map(([e, t]) => ({
|
|
708
|
-
name: e,
|
|
709
|
-
title: t.title,
|
|
710
|
-
description: t.description,
|
|
711
|
-
arguments: t.argsSchema ? Ve(t.argsSchema) : void 0
|
|
712
|
-
}))
|
|
713
|
-
})), this.server.setRequestHandler(U, async (e, t) => {
|
|
714
|
-
const s = this._registeredPrompts[e.params.name];
|
|
715
|
-
if (!s)
|
|
716
|
-
throw new p(m.InvalidParams, `Prompt ${e.params.name} not found`);
|
|
717
|
-
if (!s.enabled)
|
|
718
|
-
throw new p(m.InvalidParams, `Prompt ${e.params.name} disabled`);
|
|
719
|
-
if (s.argsSchema) {
|
|
720
|
-
const r = _(s.argsSchema), a = await P(r, e.params.arguments);
|
|
721
|
-
if (!a.success) {
|
|
722
|
-
const l = "error" in a ? a.error : "Unknown error", c = I(l);
|
|
723
|
-
throw new p(m.InvalidParams, `Invalid arguments for prompt ${e.params.name}: ${c}`);
|
|
724
|
-
}
|
|
725
|
-
const i = a.data, n = s.callback;
|
|
726
|
-
return await Promise.resolve(n(i, t));
|
|
727
|
-
} else {
|
|
728
|
-
const r = s.callback;
|
|
729
|
-
return await Promise.resolve(r(t));
|
|
730
|
-
}
|
|
731
|
-
}), this._promptHandlersInitialized = !0);
|
|
732
|
-
}
|
|
733
|
-
resource(e, t, ...s) {
|
|
734
|
-
let r;
|
|
735
|
-
typeof s[0] == "object" && (r = s.shift());
|
|
736
|
-
const a = s[0];
|
|
737
|
-
if (typeof t == "string") {
|
|
738
|
-
if (this._registeredResources[t])
|
|
739
|
-
throw new Error(`Resource ${t} is already registered`);
|
|
740
|
-
const i = this._createRegisteredResource(e, void 0, t, r, a);
|
|
741
|
-
return this.setResourceRequestHandlers(), this.sendResourceListChanged(), i;
|
|
742
|
-
} else {
|
|
743
|
-
if (this._registeredResourceTemplates[e])
|
|
744
|
-
throw new Error(`Resource template ${e} is already registered`);
|
|
745
|
-
const i = this._createRegisteredResourceTemplate(e, void 0, t, r, a);
|
|
746
|
-
return this.setResourceRequestHandlers(), this.sendResourceListChanged(), i;
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
registerResource(e, t, s, r) {
|
|
750
|
-
if (typeof t == "string") {
|
|
751
|
-
if (this._registeredResources[t])
|
|
752
|
-
throw new Error(`Resource ${t} is already registered`);
|
|
753
|
-
const a = this._createRegisteredResource(e, s.title, t, s, r);
|
|
754
|
-
return this.setResourceRequestHandlers(), this.sendResourceListChanged(), a;
|
|
755
|
-
} else {
|
|
756
|
-
if (this._registeredResourceTemplates[e])
|
|
757
|
-
throw new Error(`Resource template ${e} is already registered`);
|
|
758
|
-
const a = this._createRegisteredResourceTemplate(e, s.title, t, s, r);
|
|
759
|
-
return this.setResourceRequestHandlers(), this.sendResourceListChanged(), a;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
_createRegisteredResource(e, t, s, r, a) {
|
|
763
|
-
const i = {
|
|
764
|
-
name: e,
|
|
765
|
-
title: t,
|
|
766
|
-
metadata: r,
|
|
767
|
-
readCallback: a,
|
|
768
|
-
enabled: !0,
|
|
769
|
-
disable: () => i.update({ enabled: !1 }),
|
|
770
|
-
enable: () => i.update({ enabled: !0 }),
|
|
771
|
-
remove: () => i.update({ uri: null }),
|
|
772
|
-
update: (n) => {
|
|
773
|
-
typeof n.uri < "u" && n.uri !== s && (delete this._registeredResources[s], n.uri && (this._registeredResources[n.uri] = i)), typeof n.name < "u" && (i.name = n.name), typeof n.title < "u" && (i.title = n.title), typeof n.metadata < "u" && (i.metadata = n.metadata), typeof n.callback < "u" && (i.readCallback = n.callback), typeof n.enabled < "u" && (i.enabled = n.enabled), this.sendResourceListChanged();
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
return this._registeredResources[s] = i, i;
|
|
777
|
-
}
|
|
778
|
-
_createRegisteredResourceTemplate(e, t, s, r, a) {
|
|
779
|
-
const i = {
|
|
780
|
-
resourceTemplate: s,
|
|
781
|
-
title: t,
|
|
782
|
-
metadata: r,
|
|
783
|
-
readCallback: a,
|
|
784
|
-
enabled: !0,
|
|
785
|
-
disable: () => i.update({ enabled: !1 }),
|
|
786
|
-
enable: () => i.update({ enabled: !0 }),
|
|
787
|
-
remove: () => i.update({ name: null }),
|
|
788
|
-
update: (c) => {
|
|
789
|
-
typeof c.name < "u" && c.name !== e && (delete this._registeredResourceTemplates[e], c.name && (this._registeredResourceTemplates[c.name] = i)), typeof c.title < "u" && (i.title = c.title), typeof c.template < "u" && (i.resourceTemplate = c.template), typeof c.metadata < "u" && (i.metadata = c.metadata), typeof c.callback < "u" && (i.readCallback = c.callback), typeof c.enabled < "u" && (i.enabled = c.enabled), this.sendResourceListChanged();
|
|
790
|
-
}
|
|
791
|
-
};
|
|
792
|
-
this._registeredResourceTemplates[e] = i;
|
|
793
|
-
const n = s.uriTemplate.variableNames;
|
|
794
|
-
return Array.isArray(n) && n.some((c) => !!s.completeCallback(c)) && this.setCompletionRequestHandler(), i;
|
|
795
|
-
}
|
|
796
|
-
_createRegisteredPrompt(e, t, s, r, a) {
|
|
797
|
-
const i = {
|
|
798
|
-
title: t,
|
|
799
|
-
description: s,
|
|
800
|
-
argsSchema: r === void 0 ? void 0 : R(r),
|
|
801
|
-
callback: a,
|
|
802
|
-
enabled: !0,
|
|
803
|
-
disable: () => i.update({ enabled: !1 }),
|
|
804
|
-
enable: () => i.update({ enabled: !0 }),
|
|
805
|
-
remove: () => i.update({ name: null }),
|
|
806
|
-
update: (n) => {
|
|
807
|
-
typeof n.name < "u" && n.name !== e && (delete this._registeredPrompts[e], n.name && (this._registeredPrompts[n.name] = i)), typeof n.title < "u" && (i.title = n.title), typeof n.description < "u" && (i.description = n.description), typeof n.argsSchema < "u" && (i.argsSchema = R(n.argsSchema)), typeof n.callback < "u" && (i.callback = n.callback), typeof n.enabled < "u" && (i.enabled = n.enabled), this.sendPromptListChanged();
|
|
808
|
-
}
|
|
809
|
-
};
|
|
810
|
-
return this._registeredPrompts[e] = i, r && Object.values(r).some((l) => {
|
|
811
|
-
var u;
|
|
812
|
-
const c = l instanceof Te ? (u = l._def) == null ? void 0 : u.innerType : l;
|
|
813
|
-
return Z(c);
|
|
814
|
-
}) && this.setCompletionRequestHandler(), i;
|
|
815
|
-
}
|
|
816
|
-
_createRegisteredTool(e, t, s, r, a, i, n, l, c) {
|
|
817
|
-
J(e);
|
|
818
|
-
const u = {
|
|
819
|
-
title: t,
|
|
820
|
-
description: s,
|
|
821
|
-
inputSchema: F(r),
|
|
822
|
-
outputSchema: F(a),
|
|
823
|
-
annotations: i,
|
|
824
|
-
execution: n,
|
|
825
|
-
_meta: l,
|
|
826
|
-
handler: c,
|
|
827
|
-
enabled: !0,
|
|
828
|
-
disable: () => u.update({ enabled: !1 }),
|
|
829
|
-
enable: () => u.update({ enabled: !0 }),
|
|
830
|
-
remove: () => u.update({ name: null }),
|
|
831
|
-
update: (d) => {
|
|
832
|
-
typeof d.name < "u" && d.name !== e && (typeof d.name == "string" && J(d.name), delete this._registeredTools[e], d.name && (this._registeredTools[d.name] = u)), typeof d.title < "u" && (u.title = d.title), typeof d.description < "u" && (u.description = d.description), typeof d.paramsSchema < "u" && (u.inputSchema = R(d.paramsSchema)), typeof d.outputSchema < "u" && (u.outputSchema = R(d.outputSchema)), typeof d.callback < "u" && (u.handler = d.callback), typeof d.annotations < "u" && (u.annotations = d.annotations), typeof d._meta < "u" && (u._meta = d._meta), typeof d.enabled < "u" && (u.enabled = d.enabled), this.sendToolListChanged();
|
|
833
|
-
}
|
|
834
|
-
};
|
|
835
|
-
return this._registeredTools[e] = u, this.setToolRequestHandlers(), this.sendToolListChanged(), u;
|
|
836
|
-
}
|
|
837
|
-
/**
|
|
838
|
-
* tool() implementation. Parses arguments passed to overrides defined above.
|
|
839
|
-
*/
|
|
840
|
-
tool(e, ...t) {
|
|
841
|
-
if (this._registeredTools[e])
|
|
842
|
-
throw new Error(`Tool ${e} is already registered`);
|
|
843
|
-
let s, r, a, i;
|
|
844
|
-
if (typeof t[0] == "string" && (s = t.shift()), t.length > 1) {
|
|
845
|
-
const l = t[0];
|
|
846
|
-
H(l) ? (r = t.shift(), t.length > 1 && typeof t[0] == "object" && t[0] !== null && !H(t[0]) && (i = t.shift())) : typeof l == "object" && l !== null && (i = t.shift());
|
|
847
|
-
}
|
|
848
|
-
const n = t[0];
|
|
849
|
-
return this._createRegisteredTool(e, void 0, s, r, a, i, { taskSupport: "forbidden" }, void 0, n);
|
|
850
|
-
}
|
|
851
|
-
/**
|
|
852
|
-
* Registers a tool with a config object and callback.
|
|
853
|
-
*/
|
|
854
|
-
registerTool(e, t, s) {
|
|
855
|
-
if (this._registeredTools[e])
|
|
856
|
-
throw new Error(`Tool ${e} is already registered`);
|
|
857
|
-
const { title: r, description: a, inputSchema: i, outputSchema: n, annotations: l, _meta: c } = t;
|
|
858
|
-
return this._createRegisteredTool(e, r, a, i, n, l, { taskSupport: "forbidden" }, c, s);
|
|
859
|
-
}
|
|
860
|
-
prompt(e, ...t) {
|
|
861
|
-
if (this._registeredPrompts[e])
|
|
862
|
-
throw new Error(`Prompt ${e} is already registered`);
|
|
863
|
-
let s;
|
|
864
|
-
typeof t[0] == "string" && (s = t.shift());
|
|
865
|
-
let r;
|
|
866
|
-
t.length > 1 && (r = t.shift());
|
|
867
|
-
const a = t[0], i = this._createRegisteredPrompt(e, void 0, s, r, a);
|
|
868
|
-
return this.setPromptRequestHandlers(), this.sendPromptListChanged(), i;
|
|
869
|
-
}
|
|
870
|
-
/**
|
|
871
|
-
* Registers a prompt with a config object and callback.
|
|
872
|
-
*/
|
|
873
|
-
registerPrompt(e, t, s) {
|
|
874
|
-
if (this._registeredPrompts[e])
|
|
875
|
-
throw new Error(`Prompt ${e} is already registered`);
|
|
876
|
-
const { title: r, description: a, argsSchema: i } = t, n = this._createRegisteredPrompt(e, r, a, i, s);
|
|
877
|
-
return this.setPromptRequestHandlers(), this.sendPromptListChanged(), n;
|
|
878
|
-
}
|
|
879
|
-
/**
|
|
880
|
-
* Checks if the server is connected to a transport.
|
|
881
|
-
* @returns True if the server is connected
|
|
882
|
-
*/
|
|
883
|
-
isConnected() {
|
|
884
|
-
return this.server.transport !== void 0;
|
|
885
|
-
}
|
|
886
|
-
/**
|
|
887
|
-
* Sends a logging message to the client, if connected.
|
|
888
|
-
* Note: You only need to send the parameters object, not the entire JSON RPC message
|
|
889
|
-
* @see LoggingMessageNotification
|
|
890
|
-
* @param params
|
|
891
|
-
* @param sessionId optional for stateless and backward compatibility
|
|
892
|
-
*/
|
|
893
|
-
async sendLoggingMessage(e, t) {
|
|
894
|
-
return this.server.sendLoggingMessage(e, t);
|
|
895
|
-
}
|
|
896
|
-
/**
|
|
897
|
-
* Sends a resource list changed event to the client, if connected.
|
|
898
|
-
*/
|
|
899
|
-
sendResourceListChanged() {
|
|
900
|
-
this.isConnected() && this.server.sendResourceListChanged();
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Sends a tool list changed event to the client, if connected.
|
|
904
|
-
*/
|
|
905
|
-
sendToolListChanged() {
|
|
906
|
-
this.isConnected() && this.server.sendToolListChanged();
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* Sends a prompt list changed event to the client, if connected.
|
|
910
|
-
*/
|
|
911
|
-
sendPromptListChanged() {
|
|
912
|
-
this.isConnected() && this.server.sendPromptListChanged();
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
const Oe = {
|
|
916
|
-
type: "object",
|
|
917
|
-
properties: {}
|
|
918
|
-
};
|
|
919
|
-
function K(o) {
|
|
920
|
-
return o !== null && typeof o == "object" && "parse" in o && typeof o.parse == "function" && "safeParse" in o && typeof o.safeParse == "function";
|
|
921
|
-
}
|
|
922
|
-
function xe(o) {
|
|
923
|
-
return "_def" in o || "_zod" in o || K(o);
|
|
924
|
-
}
|
|
925
|
-
function H(o) {
|
|
926
|
-
return typeof o != "object" || o === null || xe(o) ? !1 : Object.keys(o).length === 0 ? !0 : Object.values(o).some(K);
|
|
927
|
-
}
|
|
928
|
-
function F(o) {
|
|
929
|
-
if (o)
|
|
930
|
-
return H(o) ? R(o) : o;
|
|
931
|
-
}
|
|
932
|
-
function Ve(o) {
|
|
933
|
-
const e = C(o);
|
|
934
|
-
return e ? Object.entries(e).map(([t, s]) => {
|
|
935
|
-
const r = _e(s), a = ve(s);
|
|
936
|
-
return {
|
|
937
|
-
name: t,
|
|
938
|
-
description: r,
|
|
939
|
-
required: !a
|
|
940
|
-
};
|
|
941
|
-
}) : [];
|
|
942
|
-
}
|
|
943
|
-
function g(o) {
|
|
944
|
-
const e = C(o), t = e == null ? void 0 : e.method;
|
|
945
|
-
if (!t)
|
|
946
|
-
throw new Error("Schema is missing a method literal");
|
|
947
|
-
const s = be(t);
|
|
948
|
-
if (typeof s == "string")
|
|
949
|
-
return s;
|
|
950
|
-
throw new Error("Schema method literal must be a string");
|
|
951
|
-
}
|
|
952
|
-
function W(o) {
|
|
953
|
-
return {
|
|
954
|
-
completion: {
|
|
955
|
-
values: o.slice(0, 100),
|
|
956
|
-
total: o.length,
|
|
957
|
-
hasMore: o.length > 100
|
|
958
|
-
}
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
const v = {
|
|
962
|
-
completion: {
|
|
963
|
-
values: [],
|
|
964
|
-
hasMore: !1
|
|
965
|
-
}
|
|
966
|
-
};
|
|
967
|
-
class ze {
|
|
968
|
-
constructor(e = B.stdin, t = B.stdout) {
|
|
969
|
-
this._stdin = e, this._stdout = t, this._readBuffer = new Re(), this._started = !1, this._ondata = (s) => {
|
|
970
|
-
this._readBuffer.append(s), this.processReadBuffer();
|
|
971
|
-
}, this._onerror = (s) => {
|
|
972
|
-
var r;
|
|
973
|
-
(r = this.onerror) == null || r.call(this, s);
|
|
974
|
-
};
|
|
975
|
-
}
|
|
976
|
-
/**
|
|
977
|
-
* Starts listening for messages on stdin.
|
|
978
|
-
*/
|
|
979
|
-
async start() {
|
|
980
|
-
if (this._started)
|
|
981
|
-
throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");
|
|
982
|
-
this._started = !0, this._stdin.on("data", this._ondata), this._stdin.on("error", this._onerror);
|
|
983
|
-
}
|
|
984
|
-
processReadBuffer() {
|
|
985
|
-
var e, t;
|
|
986
|
-
for (; ; )
|
|
987
|
-
try {
|
|
988
|
-
const s = this._readBuffer.readMessage();
|
|
989
|
-
if (s === null)
|
|
990
|
-
break;
|
|
991
|
-
(e = this.onmessage) == null || e.call(this, s);
|
|
992
|
-
} catch (s) {
|
|
993
|
-
(t = this.onerror) == null || t.call(this, s);
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
async close() {
|
|
997
|
-
var t;
|
|
998
|
-
this._stdin.off("data", this._ondata), this._stdin.off("error", this._onerror), this._stdin.listenerCount("data") === 0 && this._stdin.pause(), this._readBuffer.clear(), (t = this.onclose) == null || t.call(this);
|
|
999
|
-
}
|
|
1000
|
-
send(e) {
|
|
1001
|
-
return new Promise((t) => {
|
|
1002
|
-
const s = Se(e);
|
|
1003
|
-
this._stdout.write(s) ? t() : this._stdout.once("drain", t);
|
|
1004
|
-
});
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
const Y = {
|
|
1008
|
-
name: "dc-cli-mcp",
|
|
1009
|
-
version: "0.0.1-alpha.4"
|
|
1010
|
-
}, Ae = {
|
|
1011
|
-
templateList: [
|
|
1012
|
-
{
|
|
1013
|
-
name: "template-npm-common",
|
|
1014
|
-
description: "通用的npm工具包ts模板",
|
|
1015
|
-
url: "https://gitee.com/justsosu/template-npm-common.git",
|
|
1016
|
-
branch: [
|
|
1017
|
-
{
|
|
1018
|
-
name: "template/standalone",
|
|
1019
|
-
description: "独立项目(自带工程化配置)"
|
|
1020
|
-
},
|
|
1021
|
-
{
|
|
1022
|
-
name: "template/workspace",
|
|
1023
|
-
description: "workspace项目(不带工程化配置)"
|
|
1024
|
-
}
|
|
1025
|
-
]
|
|
1026
|
-
},
|
|
1027
|
-
{
|
|
1028
|
-
name: "template-npm-node-common",
|
|
1029
|
-
description: "node环境运行通用的npm工具包ts模板",
|
|
1030
|
-
url: "https://gitee.com/justsosu/template-npm-node-common.git",
|
|
1031
|
-
branch: [
|
|
1032
|
-
{
|
|
1033
|
-
name: "template/standalone",
|
|
1034
|
-
description: "独立项目(自带工程化配置)"
|
|
1035
|
-
},
|
|
1036
|
-
{
|
|
1037
|
-
name: "template/workspace",
|
|
1038
|
-
description: "workspace项目(不带工程化配置)"
|
|
1039
|
-
}
|
|
1040
|
-
]
|
|
1041
|
-
},
|
|
1042
|
-
{
|
|
1043
|
-
name: "template-npm-node-cli",
|
|
1044
|
-
description: "node环境运行的npm cli工具包ts模板",
|
|
1045
|
-
url: "https://gitee.com/justsosu/template-npm-node-cli.git",
|
|
1046
|
-
branch: [
|
|
1047
|
-
{
|
|
1048
|
-
name: "template/standalone",
|
|
1049
|
-
description: "独立项目(自带工程化配置)"
|
|
1050
|
-
},
|
|
1051
|
-
{
|
|
1052
|
-
name: "template/workspace",
|
|
1053
|
-
description: "workspace项目(不带工程化配置)"
|
|
1054
|
-
}
|
|
1055
|
-
]
|
|
1056
|
-
},
|
|
1057
|
-
{
|
|
1058
|
-
name: "template-node-server",
|
|
1059
|
-
description: "node环境服务ts模板",
|
|
1060
|
-
url: "https://gitee.com/justsosu/template-node-server.git",
|
|
1061
|
-
branch: [
|
|
1062
|
-
{
|
|
1063
|
-
name: "template/standalone",
|
|
1064
|
-
description: "独立项目(自带工程化配置)"
|
|
1065
|
-
},
|
|
1066
|
-
{
|
|
1067
|
-
name: "template/workspace",
|
|
1068
|
-
description: "workspace项目(不带工程化配置)"
|
|
1069
|
-
}
|
|
1070
|
-
]
|
|
1071
|
-
},
|
|
1072
|
-
{
|
|
1073
|
-
name: "template-web-admin-vue3",
|
|
1074
|
-
description: "web后台管理系统vue3项目模板",
|
|
1075
|
-
instances: [],
|
|
1076
|
-
url: "https://gitee.com/justsosu/template-web-pc-vue3.git",
|
|
1077
|
-
branch: [
|
|
1078
|
-
{
|
|
1079
|
-
name: "master",
|
|
1080
|
-
description: "后台管理系统项目-独立项目"
|
|
1081
|
-
},
|
|
1082
|
-
{
|
|
1083
|
-
name: "subpackage",
|
|
1084
|
-
description: "后台管理系统项目-lerna管理的monorepo子项目"
|
|
1085
|
-
}
|
|
1086
|
-
]
|
|
1087
|
-
},
|
|
1088
|
-
{
|
|
1089
|
-
name: "template-web-mobile-vue3",
|
|
1090
|
-
description: "web移动端vue3项目",
|
|
1091
|
-
instances: [],
|
|
1092
|
-
url: "https://gitee.com/justsosu/template-web-m-vue3.git"
|
|
1093
|
-
},
|
|
1094
|
-
{
|
|
1095
|
-
name: "template-uni-mobile-vue3",
|
|
1096
|
-
description: "uni移动端vue3项目",
|
|
1097
|
-
instances: [],
|
|
1098
|
-
url: "https://gitee.com/justsosu/template-uni-m-vue3.git"
|
|
1099
|
-
},
|
|
1100
|
-
{
|
|
1101
|
-
name: "template-web-mobile-react",
|
|
1102
|
-
description: "web移动端react项目",
|
|
1103
|
-
instances: [],
|
|
1104
|
-
url: "https://gitee.com/justsosu/template-web-m-react.git"
|
|
1105
|
-
},
|
|
1106
|
-
{
|
|
1107
|
-
name: "template-node-express",
|
|
1108
|
-
description: "node express项目模板",
|
|
1109
|
-
instances: [],
|
|
1110
|
-
url: "https://gitee.com/justsosu/template-node-express.git"
|
|
1111
|
-
},
|
|
1112
|
-
{
|
|
1113
|
-
name: "template-monorepo",
|
|
1114
|
-
description: "lerna管理的monorepo项目模板",
|
|
1115
|
-
instances: [
|
|
1116
|
-
"done-coding-request",
|
|
1117
|
-
"done-coding-cli",
|
|
1118
|
-
"done-coding-x"
|
|
1119
|
-
],
|
|
1120
|
-
url: "https://gitee.com/justsosu/template-monorepo.git",
|
|
1121
|
-
branch: [
|
|
1122
|
-
{
|
|
1123
|
-
name: "master",
|
|
1124
|
-
description: "lerna管理的通用monorepo项目"
|
|
1125
|
-
},
|
|
1126
|
-
{
|
|
1127
|
-
name: "components",
|
|
1128
|
-
description: "lerna管理的vue3 monorepo项目"
|
|
1129
|
-
}
|
|
1130
|
-
]
|
|
1131
|
-
},
|
|
1132
|
-
{
|
|
1133
|
-
name: "template-web-site-vue3",
|
|
1134
|
-
description: "web网站vue3项目模板",
|
|
1135
|
-
instances: [],
|
|
1136
|
-
url: "https://gitee.com/justsosu/template-web-site-vue3.git"
|
|
1137
|
-
},
|
|
1138
|
-
{
|
|
1139
|
-
name: "template-components-vue3",
|
|
1140
|
-
description: "vue3组件库项目模板",
|
|
1141
|
-
instances: [],
|
|
1142
|
-
url: "https://gitee.com/justsosu/template-components-vue3.git",
|
|
1143
|
-
branch: [
|
|
1144
|
-
{
|
|
1145
|
-
name: "master",
|
|
1146
|
-
description: "vue3组件库项目-独立项目"
|
|
1147
|
-
},
|
|
1148
|
-
{
|
|
1149
|
-
name: "subpackage",
|
|
1150
|
-
description: "vue3组件库项目-lerna管理的monorepo子项目"
|
|
1151
|
-
}
|
|
1152
|
-
]
|
|
1153
|
-
},
|
|
1154
|
-
{
|
|
1155
|
-
name: "template-micro-app-vue3",
|
|
1156
|
-
description: "微前端框架(micro-app[京东])项目模板",
|
|
1157
|
-
instances: [],
|
|
1158
|
-
url: "https://gitee.com/justsosu/template-micro-app.git",
|
|
1159
|
-
branch: "vue3"
|
|
1160
|
-
}
|
|
1161
|
-
]
|
|
1162
|
-
}, Ne = (o) => o.registerTool(
|
|
1163
|
-
y.cliConfig.moduleName,
|
|
1164
|
-
{
|
|
1165
|
-
title: `创建项目工具 - ${y.name}`,
|
|
1166
|
-
description: "根据用户提供的项目名称、模板地址和分支创建新项目",
|
|
1167
|
-
inputSchema: S.object({
|
|
1168
|
-
[T.PROJECT_NAME]: S.string().min(1, "项目名称不能为空").describe("项目名称"),
|
|
1169
|
-
[T.TEMPLATE_GIT_PATH]: S.string().min(1, "模板仓库地址不能为空").describe("模板仓库地址"),
|
|
1170
|
-
[T.TEMPLATE_GIT_BRANCH]: S.string().optional().describe("对应的 Git 分支名称")
|
|
1171
|
-
})
|
|
1172
|
-
},
|
|
1173
|
-
async (e) => {
|
|
1174
|
-
L.info(27, e);
|
|
1175
|
-
try {
|
|
1176
|
-
const t = {
|
|
1177
|
-
...e,
|
|
1178
|
-
simple: !0
|
|
1179
|
-
};
|
|
1180
|
-
L.info(37, process.env);
|
|
1181
|
-
const r = Ee(
|
|
1182
|
-
`node /Users/supengfei/Documents/code/project/done-coding-cli/packages/create/es/cli.mjs ${ye(t)}`,
|
|
1183
|
-
{
|
|
1184
|
-
cwd: process.cwd(),
|
|
1185
|
-
env: process.env
|
|
1186
|
-
}
|
|
1187
|
-
);
|
|
1188
|
-
return L.info(42, JSON.stringify(r.toString())), {
|
|
1189
|
-
content: [
|
|
1190
|
-
{
|
|
1191
|
-
type: "text",
|
|
1192
|
-
text: `✅ 项目 [${e[T.PROJECT_NAME]}] 创建成功!`
|
|
1193
|
-
}
|
|
1194
|
-
]
|
|
1195
|
-
};
|
|
1196
|
-
} catch (t) {
|
|
1197
|
-
return {
|
|
1198
|
-
isError: !0,
|
|
1199
|
-
content: [
|
|
1200
|
-
{ type: "text", text: `❌ 创建失败: ${(t == null ? void 0 : t.message) || t}` }
|
|
1201
|
-
]
|
|
1202
|
-
};
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
), Ue = (o) => o.registerResource(
|
|
1206
|
-
"template-list",
|
|
1207
|
-
// 1. name: 唯一标识符
|
|
1208
|
-
`resource:///${y.cliConfig.moduleName}/templates`,
|
|
1209
|
-
// 2. uriOrTemplate: 资源访问的 URI
|
|
1210
|
-
{
|
|
1211
|
-
title: "可用项目模板列表",
|
|
1212
|
-
// 3. config: 属性名是 title 而不是 name
|
|
1213
|
-
description: "包含所有可用的项目模板信息、仓库地址及可选分支"
|
|
1214
|
-
},
|
|
1215
|
-
async (e) => ({
|
|
1216
|
-
contents: [
|
|
1217
|
-
// 注意:新版 SDK 通常使用 contents (复数)
|
|
1218
|
-
{
|
|
1219
|
-
uri: e.href,
|
|
1220
|
-
text: JSON.stringify(Ae.templateList, null, 2),
|
|
1221
|
-
mimeType: "application/json"
|
|
1222
|
-
}
|
|
1223
|
-
]
|
|
1224
|
-
})
|
|
1225
|
-
), Be = (o) => o.registerPrompt(
|
|
1226
|
-
`${y.cliConfig.moduleName}/create-project-assistant`,
|
|
1227
|
-
{
|
|
1228
|
-
title: "创建项目助手",
|
|
1229
|
-
// 将 name 修改为 title
|
|
1230
|
-
description: "引导用户从模板列表中选择并初始化项目"
|
|
1231
|
-
},
|
|
1232
|
-
async () => ({
|
|
1233
|
-
messages: [
|
|
1234
|
-
{
|
|
1235
|
-
role: "user",
|
|
1236
|
-
// 注意:部分版本 SDK 只有 user 和 assistant 角色,system 有时需写在外面
|
|
1237
|
-
content: {
|
|
1238
|
-
type: "text",
|
|
1239
|
-
text: "你是一个专业的项目初始化助手... (此处省略之前的 prompt 内容)"
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1242
|
-
]
|
|
1243
|
-
})
|
|
1244
|
-
);
|
|
1245
|
-
Ce({
|
|
1246
|
-
callMode: ke.MCP,
|
|
1247
|
-
consoleLog: !1
|
|
1248
|
-
});
|
|
1249
|
-
const k = new je({
|
|
1250
|
-
name: Y.name,
|
|
1251
|
-
version: Y.version
|
|
1252
|
-
});
|
|
1253
|
-
Ne(k);
|
|
1254
|
-
Ue(k);
|
|
1255
|
-
Be(k);
|
|
1256
|
-
const Ze = new ze();
|
|
1257
|
-
await k.connect(Ze);
|
|
2
|
+
import { setupMcpServer as e } from "create-done-coding";
|
|
3
|
+
await e();
|