@codebolt/codeboltjs 1.1.88 → 1.1.90
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/.github/workflows/publish-to-npm.yml +1 -1
- package/index.d.ts +6 -3
- package/index.js +1 -1
- package/modules/agent.d.ts +26 -1
- package/modules/agent.js +65 -4
- package/modules/agentlib/agent.d.ts +23 -0
- package/modules/agentlib/agent.js +175 -0
- package/modules/agentlib/systemprompt.d.ts +20 -0
- package/modules/agentlib/systemprompt.js +48 -0
- package/modules/agentlib/taskInstruction.d.ts +22 -0
- package/modules/agentlib/taskInstruction.js +37 -0
- package/modules/agentlib/usermessage.d.ts +22 -0
- package/modules/agentlib/usermessage.js +70 -0
- package/modules/chat.d.ts +11 -1
- package/modules/chat.js +52 -12
- package/modules/fs.d.ts +1 -1
- package/modules/mcp.js +0 -15
- package/modules/toolBox.d.ts +262 -0
- package/modules/toolBox.js +720 -0
- package/modules/websocket.d.ts +1 -1
- package/modules/websocket.js +13 -11
- package/package.json +28 -4
- package/src/index.ts +6 -2
- package/src/modules/agent.ts +70 -4
- package/src/modules/agentlib/agent.ts +226 -0
- package/src/modules/agentlib/package-lock.json +282 -0
- package/src/modules/agentlib/package.json +6 -0
- package/src/modules/agentlib/systemprompt.ts +55 -0
- package/src/modules/agentlib/taskInstruction.ts +66 -0
- package/src/modules/agentlib/usermessage.ts +97 -0
- package/src/modules/chat.ts +56 -15
- package/src/modules/fs.ts +1 -1
- package/src/modules/mcp.ts +5 -15
- package/src/modules/toolBox.ts +1164 -0
- package/src/modules/websocket.ts +20 -13
- package/src/utils.ts +5 -0
- package/utils.d.ts +5 -0
- package/utils.js +13 -0
|
@@ -0,0 +1,1164 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import {
|
|
4
|
+
CallToolRequestSchema,
|
|
5
|
+
ClientCapabilities,
|
|
6
|
+
CompleteRequestSchema,
|
|
7
|
+
CreateMessageRequestSchema,
|
|
8
|
+
ErrorCode,
|
|
9
|
+
GetPromptRequestSchema,
|
|
10
|
+
ListPromptsRequestSchema,
|
|
11
|
+
ListResourcesRequestSchema,
|
|
12
|
+
ListResourceTemplatesRequestSchema,
|
|
13
|
+
ListToolsRequestSchema,
|
|
14
|
+
McpError,
|
|
15
|
+
ReadResourceRequestSchema,
|
|
16
|
+
Root,
|
|
17
|
+
RootsListChangedNotificationSchema,
|
|
18
|
+
ServerCapabilities,
|
|
19
|
+
SetLevelRequestSchema,
|
|
20
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
21
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
import { setTimeout as delay } from "timers/promises";
|
|
24
|
+
import { readFile } from "fs/promises";
|
|
25
|
+
// import { fileTypeFromBuffer } from "file-type";
|
|
26
|
+
// type FileTypeModule = typeof import('file-type');
|
|
27
|
+
import { StrictEventEmitter } from "strict-event-emitter-types";
|
|
28
|
+
import { EventEmitter } from "events";
|
|
29
|
+
import Fuse from "fuse.js";
|
|
30
|
+
// import { startSSEServer } from "mcp-proxy";
|
|
31
|
+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
32
|
+
import parseURITemplate from "uri-templates";
|
|
33
|
+
import { loadEsm } from "load-esm";
|
|
34
|
+
|
|
35
|
+
export type SSEServer = {
|
|
36
|
+
close: () => Promise<void>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type FastMCPEvents = {
|
|
40
|
+
connect: (event: { session: FastMCPSession }) => void;
|
|
41
|
+
disconnect: (event: { session: FastMCPSession }) => void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type FastMCPSessionEvents = {
|
|
45
|
+
rootsChanged: (event: { roots: Root[] }) => void;
|
|
46
|
+
error: (event: { error: Error }) => void;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Generates an image content object from a URL, file path, or buffer.
|
|
51
|
+
*/
|
|
52
|
+
export const imageContent = async (
|
|
53
|
+
input: { url: string } | { path: string } | { buffer: Buffer },
|
|
54
|
+
): Promise<ImageContent> => {
|
|
55
|
+
let rawData: Buffer;
|
|
56
|
+
|
|
57
|
+
if ("url" in input) {
|
|
58
|
+
const response = await fetch(input.url);
|
|
59
|
+
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
throw new Error(`Failed to fetch image from URL: ${response.statusText}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
rawData = Buffer.from(await response.arrayBuffer());
|
|
65
|
+
} else if ("path" in input) {
|
|
66
|
+
rawData = await readFile(input.path);
|
|
67
|
+
} else if ("buffer" in input) {
|
|
68
|
+
rawData = input.buffer;
|
|
69
|
+
} else {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"Invalid input: Provide a valid 'url', 'path', or 'buffer'",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
const { fileTypeFromBuffer } = await loadEsm('file-type');
|
|
75
|
+
const mimeType = await fileTypeFromBuffer(rawData);
|
|
76
|
+
console.log(mimeType);
|
|
77
|
+
|
|
78
|
+
const base64Data = rawData.toString("base64");
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
type: "image",
|
|
82
|
+
data: base64Data,
|
|
83
|
+
mimeType: mimeType?.mime ?? "image/png",
|
|
84
|
+
} as const;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
abstract class FastMCPError extends Error {
|
|
88
|
+
public constructor(message?: string) {
|
|
89
|
+
super(message);
|
|
90
|
+
this.name = new.target.name;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type Extra = unknown;
|
|
95
|
+
|
|
96
|
+
type Extras = Record<string, Extra>;
|
|
97
|
+
|
|
98
|
+
export class UnexpectedStateError extends FastMCPError {
|
|
99
|
+
public extras?: Extras;
|
|
100
|
+
|
|
101
|
+
public constructor(message: string, extras?: Extras) {
|
|
102
|
+
super(message);
|
|
103
|
+
this.name = new.target.name;
|
|
104
|
+
this.extras = extras;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* An error that is meant to be surfaced to the user.
|
|
110
|
+
*/
|
|
111
|
+
export class UserError extends UnexpectedStateError { }
|
|
112
|
+
|
|
113
|
+
type ToolParameters = z.ZodTypeAny;
|
|
114
|
+
|
|
115
|
+
type Literal = boolean | null | number | string | undefined;
|
|
116
|
+
|
|
117
|
+
type SerializableValue =
|
|
118
|
+
| Literal
|
|
119
|
+
| SerializableValue[]
|
|
120
|
+
| { [key: string]: SerializableValue };
|
|
121
|
+
|
|
122
|
+
type Progress = {
|
|
123
|
+
/**
|
|
124
|
+
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
|
|
125
|
+
*/
|
|
126
|
+
progress: number;
|
|
127
|
+
/**
|
|
128
|
+
* Total number of items to process (or total progress required), if known.
|
|
129
|
+
*/
|
|
130
|
+
total?: number;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
type Context = {
|
|
134
|
+
reportProgress: (progress: Progress) => Promise<void>;
|
|
135
|
+
log: {
|
|
136
|
+
debug: (message: string, data?: SerializableValue) => void;
|
|
137
|
+
error: (message: string, data?: SerializableValue) => void;
|
|
138
|
+
info: (message: string, data?: SerializableValue) => void;
|
|
139
|
+
warn: (message: string, data?: SerializableValue) => void;
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
type TextContent = {
|
|
144
|
+
type: "text";
|
|
145
|
+
text: string;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const TextContentZodSchema = z
|
|
149
|
+
.object({
|
|
150
|
+
type: z.literal("text"),
|
|
151
|
+
/**
|
|
152
|
+
* The text content of the message.
|
|
153
|
+
*/
|
|
154
|
+
text: z.string(),
|
|
155
|
+
})
|
|
156
|
+
.strict() satisfies z.ZodType<TextContent>;
|
|
157
|
+
|
|
158
|
+
type ImageContent = {
|
|
159
|
+
type: "image";
|
|
160
|
+
data: string;
|
|
161
|
+
mimeType: string;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const ImageContentZodSchema = z
|
|
165
|
+
.object({
|
|
166
|
+
type: z.literal("image"),
|
|
167
|
+
/**
|
|
168
|
+
* The base64-encoded image data.
|
|
169
|
+
*/
|
|
170
|
+
data: z.string().base64(),
|
|
171
|
+
/**
|
|
172
|
+
* The MIME type of the image. Different providers may support different image types.
|
|
173
|
+
*/
|
|
174
|
+
mimeType: z.string(),
|
|
175
|
+
})
|
|
176
|
+
.strict() satisfies z.ZodType<ImageContent>;
|
|
177
|
+
|
|
178
|
+
type Content = TextContent | ImageContent;
|
|
179
|
+
|
|
180
|
+
const ContentZodSchema = z.discriminatedUnion("type", [
|
|
181
|
+
TextContentZodSchema,
|
|
182
|
+
ImageContentZodSchema,
|
|
183
|
+
]) satisfies z.ZodType<Content>;
|
|
184
|
+
|
|
185
|
+
type ContentResult = {
|
|
186
|
+
content: Content[];
|
|
187
|
+
isError?: boolean;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const ContentResultZodSchema = z
|
|
191
|
+
.object({
|
|
192
|
+
content: ContentZodSchema.array(),
|
|
193
|
+
isError: z.boolean().optional(),
|
|
194
|
+
})
|
|
195
|
+
.strict() satisfies z.ZodType<ContentResult>;
|
|
196
|
+
|
|
197
|
+
type Completion = {
|
|
198
|
+
values: string[];
|
|
199
|
+
total?: number;
|
|
200
|
+
hasMore?: boolean;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* https://github.com/modelcontextprotocol/typescript-sdk/blob/3164da64d085ec4e022ae881329eee7b72f208d4/src/types.ts#L983-L1003
|
|
205
|
+
*/
|
|
206
|
+
const CompletionZodSchema = z.object({
|
|
207
|
+
/**
|
|
208
|
+
* An array of completion values. Must not exceed 100 items.
|
|
209
|
+
*/
|
|
210
|
+
values: z.array(z.string()).max(100),
|
|
211
|
+
/**
|
|
212
|
+
* The total number of completion options available. This can exceed the number of values actually sent in the response.
|
|
213
|
+
*/
|
|
214
|
+
total: z.optional(z.number().int()),
|
|
215
|
+
/**
|
|
216
|
+
* Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.
|
|
217
|
+
*/
|
|
218
|
+
hasMore: z.optional(z.boolean()),
|
|
219
|
+
}) satisfies z.ZodType<Completion>;
|
|
220
|
+
|
|
221
|
+
type Tool<Params extends ToolParameters = ToolParameters> = {
|
|
222
|
+
name: string;
|
|
223
|
+
description?: string;
|
|
224
|
+
parameters?: Params;
|
|
225
|
+
execute: (
|
|
226
|
+
args: z.infer<Params>,
|
|
227
|
+
context: Context,
|
|
228
|
+
) => Promise<string | ContentResult | TextContent | ImageContent>;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
type ResourceResult =
|
|
232
|
+
| {
|
|
233
|
+
text: string;
|
|
234
|
+
}
|
|
235
|
+
| {
|
|
236
|
+
blob: string;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
type InputResourceTemplateArgument = Readonly<{
|
|
240
|
+
name: string;
|
|
241
|
+
description?: string;
|
|
242
|
+
complete?: ArgumentValueCompleter;
|
|
243
|
+
}>;
|
|
244
|
+
|
|
245
|
+
type ResourceTemplateArgument = Readonly<{
|
|
246
|
+
name: string;
|
|
247
|
+
description?: string;
|
|
248
|
+
complete?: ArgumentValueCompleter;
|
|
249
|
+
}>;
|
|
250
|
+
|
|
251
|
+
type ResourceTemplate<
|
|
252
|
+
Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],
|
|
253
|
+
> = {
|
|
254
|
+
uriTemplate: string;
|
|
255
|
+
name: string;
|
|
256
|
+
description?: string;
|
|
257
|
+
mimeType?: string;
|
|
258
|
+
arguments: Arguments;
|
|
259
|
+
complete?: (name: string, value: string) => Promise<Completion>;
|
|
260
|
+
load: (
|
|
261
|
+
args: ResourceTemplateArgumentsToObject<Arguments>,
|
|
262
|
+
) => Promise<ResourceResult>;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
type ResourceTemplateArgumentsToObject<T extends { name: string }[]> = {
|
|
266
|
+
[K in T[number]["name"]]: string;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
type InputResourceTemplate<
|
|
270
|
+
Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],
|
|
271
|
+
> = {
|
|
272
|
+
uriTemplate: string;
|
|
273
|
+
name: string;
|
|
274
|
+
description?: string;
|
|
275
|
+
mimeType?: string;
|
|
276
|
+
arguments: Arguments;
|
|
277
|
+
load: (
|
|
278
|
+
args: ResourceTemplateArgumentsToObject<Arguments>,
|
|
279
|
+
) => Promise<ResourceResult>;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
type Resource = {
|
|
283
|
+
uri: string;
|
|
284
|
+
name: string;
|
|
285
|
+
description?: string;
|
|
286
|
+
mimeType?: string;
|
|
287
|
+
load: () => Promise<ResourceResult | ResourceResult[]>;
|
|
288
|
+
complete?: (name: string, value: string) => Promise<Completion>;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
type ArgumentValueCompleter = (value: string) => Promise<Completion>;
|
|
292
|
+
|
|
293
|
+
type InputPromptArgument = Readonly<{
|
|
294
|
+
name: string;
|
|
295
|
+
description?: string;
|
|
296
|
+
required?: boolean;
|
|
297
|
+
complete?: ArgumentValueCompleter;
|
|
298
|
+
enum?: string[];
|
|
299
|
+
}>;
|
|
300
|
+
|
|
301
|
+
type PromptArgumentsToObject<T extends { name: string; required?: boolean }[]> =
|
|
302
|
+
{
|
|
303
|
+
[K in T[number]["name"]]: Extract<
|
|
304
|
+
T[number],
|
|
305
|
+
{ name: K }
|
|
306
|
+
>["required"] extends true
|
|
307
|
+
? string
|
|
308
|
+
: string | undefined;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
type InputPrompt<
|
|
312
|
+
Arguments extends InputPromptArgument[] = InputPromptArgument[],
|
|
313
|
+
Args = PromptArgumentsToObject<Arguments>,
|
|
314
|
+
> = {
|
|
315
|
+
name: string;
|
|
316
|
+
description?: string;
|
|
317
|
+
arguments?: InputPromptArgument[];
|
|
318
|
+
load: (args: Args) => Promise<string>;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
type PromptArgument = Readonly<{
|
|
322
|
+
name: string;
|
|
323
|
+
description?: string;
|
|
324
|
+
required?: boolean;
|
|
325
|
+
complete?: ArgumentValueCompleter;
|
|
326
|
+
enum?: string[];
|
|
327
|
+
}>;
|
|
328
|
+
|
|
329
|
+
type Prompt<
|
|
330
|
+
Arguments extends PromptArgument[] = PromptArgument[],
|
|
331
|
+
Args = PromptArgumentsToObject<Arguments>,
|
|
332
|
+
> = {
|
|
333
|
+
arguments?: PromptArgument[];
|
|
334
|
+
complete?: (name: string, value: string) => Promise<Completion>;
|
|
335
|
+
description?: string;
|
|
336
|
+
load: (args: Args) => Promise<string>;
|
|
337
|
+
name: string;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
type ServerOptions = {
|
|
341
|
+
name: string;
|
|
342
|
+
version: `${number}.${number}.${number}`;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
type LoggingLevel =
|
|
346
|
+
| "debug"
|
|
347
|
+
| "info"
|
|
348
|
+
| "notice"
|
|
349
|
+
| "warning"
|
|
350
|
+
| "error"
|
|
351
|
+
| "critical"
|
|
352
|
+
| "alert"
|
|
353
|
+
| "emergency";
|
|
354
|
+
|
|
355
|
+
const FastMCPSessionEventEmitterBase: {
|
|
356
|
+
new(): StrictEventEmitter<EventEmitter, FastMCPSessionEvents>;
|
|
357
|
+
} = EventEmitter;
|
|
358
|
+
|
|
359
|
+
class FastMCPSessionEventEmitter extends FastMCPSessionEventEmitterBase { }
|
|
360
|
+
|
|
361
|
+
type SamplingResponse = {
|
|
362
|
+
model: string;
|
|
363
|
+
stopReason?: "endTurn" | "stopSequence" | "maxTokens" | string;
|
|
364
|
+
role: "user" | "assistant";
|
|
365
|
+
content: TextContent | ImageContent;
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
export class FastMCPSession extends FastMCPSessionEventEmitter {
|
|
369
|
+
#capabilities: ServerCapabilities = {};
|
|
370
|
+
#clientCapabilities?: ClientCapabilities;
|
|
371
|
+
#loggingLevel: LoggingLevel = "info";
|
|
372
|
+
#prompts: Prompt[] = [];
|
|
373
|
+
#resources: Resource[] = [];
|
|
374
|
+
#resourceTemplates: ResourceTemplate[] = [];
|
|
375
|
+
#roots: Root[] = [];
|
|
376
|
+
#server: Server;
|
|
377
|
+
|
|
378
|
+
constructor({
|
|
379
|
+
name,
|
|
380
|
+
version,
|
|
381
|
+
tools,
|
|
382
|
+
resources,
|
|
383
|
+
resourcesTemplates,
|
|
384
|
+
prompts,
|
|
385
|
+
}: {
|
|
386
|
+
name: string;
|
|
387
|
+
version: string;
|
|
388
|
+
tools: Tool[];
|
|
389
|
+
resources: Resource[];
|
|
390
|
+
resourcesTemplates: InputResourceTemplate[];
|
|
391
|
+
prompts: Prompt[];
|
|
392
|
+
}) {
|
|
393
|
+
super();
|
|
394
|
+
|
|
395
|
+
if (tools.length) {
|
|
396
|
+
this.#capabilities.tools = {};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (resources.length || resourcesTemplates.length) {
|
|
400
|
+
this.#capabilities.resources = {};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (prompts.length) {
|
|
404
|
+
for (const prompt of prompts) {
|
|
405
|
+
this.addPrompt(prompt);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
this.#capabilities.prompts = {};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
this.#capabilities.logging = {};
|
|
412
|
+
|
|
413
|
+
this.#server = new Server(
|
|
414
|
+
{ name: name, version: version },
|
|
415
|
+
{ capabilities: this.#capabilities },
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
this.setupErrorHandling();
|
|
419
|
+
this.setupLoggingHandlers();
|
|
420
|
+
this.setupRootsHandlers();
|
|
421
|
+
this.setupCompleteHandlers();
|
|
422
|
+
|
|
423
|
+
if (tools.length) {
|
|
424
|
+
this.setupToolHandlers(tools);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (resources.length || resourcesTemplates.length) {
|
|
428
|
+
for (const resource of resources) {
|
|
429
|
+
this.addResource(resource);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.setupResourceHandlers(resources);
|
|
433
|
+
|
|
434
|
+
if (resourcesTemplates.length) {
|
|
435
|
+
for (const resourceTemplate of resourcesTemplates) {
|
|
436
|
+
this.addResourceTemplate(resourceTemplate);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
this.setupResourceTemplateHandlers(resourcesTemplates);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (prompts.length) {
|
|
444
|
+
this.setupPromptHandlers(prompts);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private addResource(inputResource: Resource) {
|
|
449
|
+
this.#resources.push(inputResource);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private addResourceTemplate(inputResourceTemplate: InputResourceTemplate) {
|
|
453
|
+
const completers: Record<string, ArgumentValueCompleter> = {};
|
|
454
|
+
|
|
455
|
+
for (const argument of inputResourceTemplate.arguments ?? []) {
|
|
456
|
+
if (argument.complete) {
|
|
457
|
+
completers[argument.name] = argument.complete;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const resourceTemplate = {
|
|
462
|
+
...inputResourceTemplate,
|
|
463
|
+
complete: async (name: string, value: string) => {
|
|
464
|
+
if (completers[name]) {
|
|
465
|
+
return await completers[name](value);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
values: [],
|
|
470
|
+
};
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
this.#resourceTemplates.push(resourceTemplate);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
private addPrompt(inputPrompt: InputPrompt) {
|
|
478
|
+
const completers: Record<string, ArgumentValueCompleter> = {};
|
|
479
|
+
const enums: Record<string, string[]> = {};
|
|
480
|
+
|
|
481
|
+
for (const argument of inputPrompt.arguments ?? []) {
|
|
482
|
+
if (argument.complete) {
|
|
483
|
+
completers[argument.name] = argument.complete;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (argument.enum) {
|
|
487
|
+
enums[argument.name] = argument.enum;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const prompt = {
|
|
492
|
+
...inputPrompt,
|
|
493
|
+
complete: async (name: string, value: string) => {
|
|
494
|
+
if (completers[name]) {
|
|
495
|
+
return await completers[name](value);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (enums[name]) {
|
|
499
|
+
const fuse = new Fuse(enums[name], {
|
|
500
|
+
keys: ["value"],
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const result = fuse.search(value);
|
|
504
|
+
|
|
505
|
+
return {
|
|
506
|
+
values: result.map((item) => item.item),
|
|
507
|
+
total: result.length,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return {
|
|
512
|
+
values: [],
|
|
513
|
+
};
|
|
514
|
+
},
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
this.#prompts.push(prompt);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
public get clientCapabilities(): ClientCapabilities | null {
|
|
521
|
+
return this.#clientCapabilities ?? null;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
public get server(): Server {
|
|
525
|
+
return this.#server;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
#pingInterval: ReturnType<typeof setInterval> | null = null;
|
|
529
|
+
|
|
530
|
+
public async requestSampling(
|
|
531
|
+
message: z.infer<typeof CreateMessageRequestSchema>["params"],
|
|
532
|
+
): Promise<SamplingResponse> {
|
|
533
|
+
return this.#server.createMessage(message);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
public async connect(transport: Transport) {
|
|
537
|
+
if (this.#server.transport) {
|
|
538
|
+
throw new UnexpectedStateError("Server is already connected");
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
await this.#server.connect(transport);
|
|
542
|
+
|
|
543
|
+
let attempt = 0;
|
|
544
|
+
|
|
545
|
+
while (attempt++ < 10) {
|
|
546
|
+
const capabilities = await this.#server.getClientCapabilities();
|
|
547
|
+
|
|
548
|
+
if (capabilities) {
|
|
549
|
+
this.#clientCapabilities = capabilities;
|
|
550
|
+
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
await delay(100);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if (!this.#clientCapabilities) {
|
|
558
|
+
throw new UnexpectedStateError("Server did not connect");
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (this.#clientCapabilities?.roots) {
|
|
562
|
+
const roots = await this.#server.listRoots();
|
|
563
|
+
|
|
564
|
+
this.#roots = roots.roots;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
this.#pingInterval = setInterval(async () => {
|
|
568
|
+
try {
|
|
569
|
+
await this.#server.ping();
|
|
570
|
+
} catch (error) {
|
|
571
|
+
this.emit("error", {
|
|
572
|
+
error: error as Error,
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
}, 1000);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
public get roots(): Root[] {
|
|
579
|
+
return this.#roots;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
public async close() {
|
|
583
|
+
if (this.#pingInterval) {
|
|
584
|
+
clearInterval(this.#pingInterval);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
await this.#server.close();
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
private setupErrorHandling() {
|
|
591
|
+
this.#server.onerror = (error) => {
|
|
592
|
+
console.error("[MCP Error]", error);
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
public get loggingLevel(): LoggingLevel {
|
|
597
|
+
return this.#loggingLevel;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
private setupCompleteHandlers() {
|
|
601
|
+
this.#server.setRequestHandler(CompleteRequestSchema, async (request) => {
|
|
602
|
+
if (request.params.ref.type === "ref/prompt") {
|
|
603
|
+
const prompt = this.#prompts.find(
|
|
604
|
+
(prompt) => prompt.name === request.params.ref.name,
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
if (!prompt) {
|
|
608
|
+
throw new UnexpectedStateError("Unknown prompt", {
|
|
609
|
+
request,
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (!prompt.complete) {
|
|
614
|
+
throw new UnexpectedStateError("Prompt does not support completion", {
|
|
615
|
+
request,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const completion = CompletionZodSchema.parse(
|
|
620
|
+
await prompt.complete(
|
|
621
|
+
request.params.argument.name,
|
|
622
|
+
request.params.argument.value,
|
|
623
|
+
),
|
|
624
|
+
);
|
|
625
|
+
|
|
626
|
+
return {
|
|
627
|
+
completion,
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
if (request.params.ref.type === "ref/resource") {
|
|
632
|
+
const resource = this.#resourceTemplates.find(
|
|
633
|
+
(resource) => resource.uriTemplate === request.params.ref.uri,
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
if (!resource) {
|
|
637
|
+
throw new UnexpectedStateError("Unknown resource", {
|
|
638
|
+
request,
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
if (!("uriTemplate" in resource)) {
|
|
643
|
+
throw new UnexpectedStateError("Unexpected resource");
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
if (!resource.complete) {
|
|
647
|
+
throw new UnexpectedStateError(
|
|
648
|
+
"Resource does not support completion",
|
|
649
|
+
{
|
|
650
|
+
request,
|
|
651
|
+
},
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
const completion = CompletionZodSchema.parse(
|
|
656
|
+
await resource.complete(
|
|
657
|
+
request.params.argument.name,
|
|
658
|
+
request.params.argument.value,
|
|
659
|
+
),
|
|
660
|
+
);
|
|
661
|
+
|
|
662
|
+
return {
|
|
663
|
+
completion,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
throw new UnexpectedStateError("Unexpected completion request", {
|
|
668
|
+
request,
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
private setupRootsHandlers() {
|
|
674
|
+
this.#server.setNotificationHandler(
|
|
675
|
+
RootsListChangedNotificationSchema,
|
|
676
|
+
() => {
|
|
677
|
+
this.#server.listRoots().then((roots) => {
|
|
678
|
+
this.#roots = roots.roots;
|
|
679
|
+
|
|
680
|
+
this.emit("rootsChanged", {
|
|
681
|
+
roots: roots.roots,
|
|
682
|
+
});
|
|
683
|
+
});
|
|
684
|
+
},
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
private setupLoggingHandlers() {
|
|
689
|
+
this.#server.setRequestHandler(SetLevelRequestSchema, (request) => {
|
|
690
|
+
this.#loggingLevel = request.params.level;
|
|
691
|
+
|
|
692
|
+
return {};
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
private setupToolHandlers(tools: Tool[]) {
|
|
697
|
+
this.#server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
698
|
+
return {
|
|
699
|
+
tools: tools.map((tool) => {
|
|
700
|
+
return {
|
|
701
|
+
name: tool.name,
|
|
702
|
+
description: tool.description,
|
|
703
|
+
inputSchema: tool.parameters
|
|
704
|
+
? zodToJsonSchema(tool.parameters)
|
|
705
|
+
: undefined,
|
|
706
|
+
};
|
|
707
|
+
}),
|
|
708
|
+
};
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
this.#server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
712
|
+
const tool = tools.find((tool) => tool.name === request.params.name);
|
|
713
|
+
|
|
714
|
+
if (!tool) {
|
|
715
|
+
throw new McpError(
|
|
716
|
+
ErrorCode.MethodNotFound,
|
|
717
|
+
`Unknown tool: ${request.params.name}`,
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
let args: any = undefined;
|
|
722
|
+
|
|
723
|
+
if (tool.parameters) {
|
|
724
|
+
const parsed = tool.parameters.safeParse(request.params.arguments);
|
|
725
|
+
|
|
726
|
+
if (!parsed.success) {
|
|
727
|
+
throw new McpError(
|
|
728
|
+
ErrorCode.InvalidParams,
|
|
729
|
+
`Invalid ${request.params.name} parameters`,
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
args = parsed.data;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const progressToken = request.params?._meta?.progressToken;
|
|
737
|
+
|
|
738
|
+
let result: ContentResult;
|
|
739
|
+
|
|
740
|
+
try {
|
|
741
|
+
const reportProgress = async (progress: Progress) => {
|
|
742
|
+
await this.#server.notification({
|
|
743
|
+
method: "notifications/progress",
|
|
744
|
+
params: {
|
|
745
|
+
...progress,
|
|
746
|
+
progressToken,
|
|
747
|
+
},
|
|
748
|
+
});
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
const log = {
|
|
752
|
+
debug: (message: string, context?: SerializableValue) => {
|
|
753
|
+
this.#server.sendLoggingMessage({
|
|
754
|
+
level: "debug",
|
|
755
|
+
data: {
|
|
756
|
+
message,
|
|
757
|
+
context,
|
|
758
|
+
},
|
|
759
|
+
});
|
|
760
|
+
},
|
|
761
|
+
error: (message: string, context?: SerializableValue) => {
|
|
762
|
+
this.#server.sendLoggingMessage({
|
|
763
|
+
level: "error",
|
|
764
|
+
data: {
|
|
765
|
+
message,
|
|
766
|
+
context,
|
|
767
|
+
},
|
|
768
|
+
});
|
|
769
|
+
},
|
|
770
|
+
info: (message: string, context?: SerializableValue) => {
|
|
771
|
+
this.#server.sendLoggingMessage({
|
|
772
|
+
level: "info",
|
|
773
|
+
data: {
|
|
774
|
+
message,
|
|
775
|
+
context,
|
|
776
|
+
},
|
|
777
|
+
});
|
|
778
|
+
},
|
|
779
|
+
warn: (message: string, context?: SerializableValue) => {
|
|
780
|
+
this.#server.sendLoggingMessage({
|
|
781
|
+
level: "warning",
|
|
782
|
+
data: {
|
|
783
|
+
message,
|
|
784
|
+
context,
|
|
785
|
+
},
|
|
786
|
+
});
|
|
787
|
+
},
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
const maybeStringResult = await tool.execute(args, {
|
|
791
|
+
reportProgress,
|
|
792
|
+
log,
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
if (typeof maybeStringResult === "string") {
|
|
796
|
+
result = ContentResultZodSchema.parse({
|
|
797
|
+
content: [{ type: "text", text: maybeStringResult }],
|
|
798
|
+
});
|
|
799
|
+
} else if ("type" in maybeStringResult) {
|
|
800
|
+
result = ContentResultZodSchema.parse({
|
|
801
|
+
content: [maybeStringResult],
|
|
802
|
+
});
|
|
803
|
+
} else {
|
|
804
|
+
result = ContentResultZodSchema.parse(maybeStringResult);
|
|
805
|
+
}
|
|
806
|
+
} catch (error) {
|
|
807
|
+
if (error instanceof UserError) {
|
|
808
|
+
return {
|
|
809
|
+
content: [{ type: "text", text: error.message }],
|
|
810
|
+
isError: true,
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return {
|
|
815
|
+
content: [{ type: "text", text: `Error: ${error}` }],
|
|
816
|
+
isError: true,
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
return result;
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
private setupResourceHandlers(resources: Resource[]) {
|
|
825
|
+
this.#server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
|
826
|
+
return {
|
|
827
|
+
resources: resources.map((resource) => {
|
|
828
|
+
return {
|
|
829
|
+
uri: resource.uri,
|
|
830
|
+
name: resource.name,
|
|
831
|
+
mimeType: resource.mimeType,
|
|
832
|
+
};
|
|
833
|
+
}),
|
|
834
|
+
};
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
this.#server.setRequestHandler(
|
|
838
|
+
ReadResourceRequestSchema,
|
|
839
|
+
async (request) => {
|
|
840
|
+
if ("uri" in request.params) {
|
|
841
|
+
const resource = resources.find(
|
|
842
|
+
(resource) =>
|
|
843
|
+
"uri" in resource && resource.uri === request.params.uri,
|
|
844
|
+
);
|
|
845
|
+
|
|
846
|
+
if (!resource) {
|
|
847
|
+
for (const resourceTemplate of this.#resourceTemplates) {
|
|
848
|
+
const uriTemplate = parseURITemplate(
|
|
849
|
+
resourceTemplate.uriTemplate,
|
|
850
|
+
);
|
|
851
|
+
|
|
852
|
+
const match = uriTemplate.fromUri(request.params.uri);
|
|
853
|
+
|
|
854
|
+
if (!match) {
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
const uri = uriTemplate.fill(match);
|
|
859
|
+
|
|
860
|
+
const result = await resourceTemplate.load(match);
|
|
861
|
+
|
|
862
|
+
return {
|
|
863
|
+
contents: [
|
|
864
|
+
{
|
|
865
|
+
uri: uri,
|
|
866
|
+
mimeType: resourceTemplate.mimeType,
|
|
867
|
+
name: resourceTemplate.name,
|
|
868
|
+
...result,
|
|
869
|
+
},
|
|
870
|
+
],
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
throw new McpError(
|
|
875
|
+
ErrorCode.MethodNotFound,
|
|
876
|
+
`Unknown resource: ${request.params.uri}`,
|
|
877
|
+
);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
if (!("uri" in resource)) {
|
|
881
|
+
throw new UnexpectedStateError("Resource does not support reading");
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
let maybeArrayResult: Awaited<ReturnType<Resource["load"]>>;
|
|
885
|
+
|
|
886
|
+
try {
|
|
887
|
+
maybeArrayResult = await resource.load();
|
|
888
|
+
} catch (error) {
|
|
889
|
+
throw new McpError(
|
|
890
|
+
ErrorCode.InternalError,
|
|
891
|
+
`Error reading resource: ${error}`,
|
|
892
|
+
{
|
|
893
|
+
uri: resource.uri,
|
|
894
|
+
},
|
|
895
|
+
);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
if (Array.isArray(maybeArrayResult)) {
|
|
899
|
+
return {
|
|
900
|
+
contents: maybeArrayResult.map((result) => ({
|
|
901
|
+
uri: resource.uri,
|
|
902
|
+
mimeType: resource.mimeType,
|
|
903
|
+
name: resource.name,
|
|
904
|
+
...result,
|
|
905
|
+
})),
|
|
906
|
+
};
|
|
907
|
+
} else {
|
|
908
|
+
return {
|
|
909
|
+
contents: [
|
|
910
|
+
{
|
|
911
|
+
uri: resource.uri,
|
|
912
|
+
mimeType: resource.mimeType,
|
|
913
|
+
name: resource.name,
|
|
914
|
+
...maybeArrayResult,
|
|
915
|
+
},
|
|
916
|
+
],
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
throw new UnexpectedStateError("Unknown resource request", {
|
|
922
|
+
request,
|
|
923
|
+
});
|
|
924
|
+
},
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
private setupResourceTemplateHandlers(resourceTemplates: ResourceTemplate[]) {
|
|
929
|
+
this.#server.setRequestHandler(
|
|
930
|
+
ListResourceTemplatesRequestSchema,
|
|
931
|
+
async () => {
|
|
932
|
+
return {
|
|
933
|
+
resourceTemplates: resourceTemplates.map((resourceTemplate) => {
|
|
934
|
+
return {
|
|
935
|
+
name: resourceTemplate.name,
|
|
936
|
+
uriTemplate: resourceTemplate.uriTemplate,
|
|
937
|
+
};
|
|
938
|
+
}),
|
|
939
|
+
};
|
|
940
|
+
},
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
private setupPromptHandlers(prompts: Prompt[]) {
|
|
945
|
+
this.#server.setRequestHandler(ListPromptsRequestSchema, async () => {
|
|
946
|
+
return {
|
|
947
|
+
prompts: prompts.map((prompt) => {
|
|
948
|
+
return {
|
|
949
|
+
name: prompt.name,
|
|
950
|
+
description: prompt.description,
|
|
951
|
+
arguments: prompt.arguments,
|
|
952
|
+
complete: prompt.complete,
|
|
953
|
+
};
|
|
954
|
+
}),
|
|
955
|
+
};
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
this.#server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
959
|
+
const prompt = prompts.find(
|
|
960
|
+
(prompt) => prompt.name === request.params.name,
|
|
961
|
+
);
|
|
962
|
+
|
|
963
|
+
if (!prompt) {
|
|
964
|
+
throw new McpError(
|
|
965
|
+
ErrorCode.MethodNotFound,
|
|
966
|
+
`Unknown prompt: ${request.params.name}`,
|
|
967
|
+
);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
const args = request.params.arguments;
|
|
971
|
+
|
|
972
|
+
for (const arg of prompt.arguments ?? []) {
|
|
973
|
+
if (arg.required && !(args && arg.name in args)) {
|
|
974
|
+
throw new McpError(
|
|
975
|
+
ErrorCode.InvalidRequest,
|
|
976
|
+
`Missing required argument: ${arg.name}`,
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
let result: Awaited<ReturnType<Prompt["load"]>>;
|
|
982
|
+
|
|
983
|
+
try {
|
|
984
|
+
result = await prompt.load(args as Record<string, string | undefined>);
|
|
985
|
+
} catch (error) {
|
|
986
|
+
throw new McpError(
|
|
987
|
+
ErrorCode.InternalError,
|
|
988
|
+
`Error loading prompt: ${error}`,
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
return {
|
|
993
|
+
description: prompt.description,
|
|
994
|
+
messages: [
|
|
995
|
+
{
|
|
996
|
+
role: "user",
|
|
997
|
+
content: { type: "text", text: result },
|
|
998
|
+
},
|
|
999
|
+
],
|
|
1000
|
+
};
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
const FastMCPEventEmitterBase: {
|
|
1006
|
+
new(): StrictEventEmitter<EventEmitter, FastMCPEvents>;
|
|
1007
|
+
} = EventEmitter;
|
|
1008
|
+
|
|
1009
|
+
class FastMCPEventEmitter extends FastMCPEventEmitterBase { }
|
|
1010
|
+
|
|
1011
|
+
export class ToolBox extends FastMCPEventEmitter {
|
|
1012
|
+
#options: ServerOptions;
|
|
1013
|
+
#prompts: InputPrompt[] = [];
|
|
1014
|
+
#resources: Resource[] = [];
|
|
1015
|
+
#resourcesTemplates: InputResourceTemplate[] = [];
|
|
1016
|
+
#sessions: FastMCPSession[] = [];
|
|
1017
|
+
#sseServer: SSEServer | null = null;
|
|
1018
|
+
#tools: Tool[] = [];
|
|
1019
|
+
|
|
1020
|
+
constructor(public options: ServerOptions) {
|
|
1021
|
+
super();
|
|
1022
|
+
|
|
1023
|
+
this.#options = options;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
public get sessions(): FastMCPSession[] {
|
|
1027
|
+
return this.#sessions;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/**
|
|
1031
|
+
* Adds a tool to the server.
|
|
1032
|
+
*/
|
|
1033
|
+
public addTool<Params extends ToolParameters>(tool: Tool<Params>) {
|
|
1034
|
+
this.#tools.push(tool as unknown as Tool);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Adds a resource to the server.
|
|
1039
|
+
*/
|
|
1040
|
+
public addResource(resource: Resource) {
|
|
1041
|
+
this.#resources.push(resource);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* Adds a resource template to the server.
|
|
1046
|
+
*/
|
|
1047
|
+
public addResourceTemplate<
|
|
1048
|
+
const Args extends InputResourceTemplateArgument[],
|
|
1049
|
+
>(resource: InputResourceTemplate<Args>) {
|
|
1050
|
+
this.#resourcesTemplates.push(resource);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* Adds a prompt to the server.
|
|
1055
|
+
*/
|
|
1056
|
+
public addPrompt<const Args extends InputPromptArgument[]>(
|
|
1057
|
+
prompt: InputPrompt<Args>,
|
|
1058
|
+
) {
|
|
1059
|
+
this.#prompts.push(prompt);
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/**
|
|
1063
|
+
* Starts the server.
|
|
1064
|
+
*/
|
|
1065
|
+
public async activate(
|
|
1066
|
+
options:
|
|
1067
|
+
| { transportType: "stdio" }
|
|
1068
|
+
| {
|
|
1069
|
+
transportType: "sse";
|
|
1070
|
+
sse: { endpoint: `/${string}`; port: number };
|
|
1071
|
+
} = {
|
|
1072
|
+
transportType: "stdio",
|
|
1073
|
+
},
|
|
1074
|
+
) {
|
|
1075
|
+
if (options.transportType === "stdio") {
|
|
1076
|
+
const transport = new StdioServerTransport();
|
|
1077
|
+
|
|
1078
|
+
const session = new FastMCPSession({
|
|
1079
|
+
name: this.#options.name,
|
|
1080
|
+
version: this.#options.version,
|
|
1081
|
+
tools: this.#tools,
|
|
1082
|
+
resources: this.#resources,
|
|
1083
|
+
resourcesTemplates: this.#resourcesTemplates,
|
|
1084
|
+
prompts: this.#prompts,
|
|
1085
|
+
});
|
|
1086
|
+
console.log("session", session);
|
|
1087
|
+
await session.connect(transport);
|
|
1088
|
+
|
|
1089
|
+
this.#sessions.push(session);
|
|
1090
|
+
|
|
1091
|
+
this.emit("connect", {
|
|
1092
|
+
session,
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
console.info(`server is running on stdio`);
|
|
1096
|
+
} else if (options.transportType === "sse") {
|
|
1097
|
+
// this.#sseServer = await startSSEServer<FastMCPSession>({
|
|
1098
|
+
// endpoint: options.sse.endpoint as `/${string}`,
|
|
1099
|
+
// port: options.sse.port,
|
|
1100
|
+
// createServer: async () => {
|
|
1101
|
+
// return new FastMCPSession({
|
|
1102
|
+
// name: this.#options.name,
|
|
1103
|
+
// version: this.#options.version,
|
|
1104
|
+
// tools: this.#tools,
|
|
1105
|
+
// resources: this.#resources,
|
|
1106
|
+
// resourcesTemplates: this.#resourcesTemplates,
|
|
1107
|
+
// prompts: this.#prompts,
|
|
1108
|
+
// });
|
|
1109
|
+
// },
|
|
1110
|
+
// onClose: (session) => {
|
|
1111
|
+
// this.emit("disconnect", {
|
|
1112
|
+
// session,
|
|
1113
|
+
// });
|
|
1114
|
+
// },
|
|
1115
|
+
// onConnect: async (session) => {
|
|
1116
|
+
// this.#sessions.push(session);
|
|
1117
|
+
|
|
1118
|
+
// this.emit("connect", {
|
|
1119
|
+
// session,
|
|
1120
|
+
// });
|
|
1121
|
+
// },
|
|
1122
|
+
// });
|
|
1123
|
+
|
|
1124
|
+
// console.error(
|
|
1125
|
+
// `server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`,
|
|
1126
|
+
// );
|
|
1127
|
+
} else {
|
|
1128
|
+
throw new Error("Invalid transport type");
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
/**
|
|
1133
|
+
* Stops the server.
|
|
1134
|
+
*/
|
|
1135
|
+
public async stop() {
|
|
1136
|
+
if (this.#sseServer) {
|
|
1137
|
+
this.#sseServer.close();
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
// public async activate() {
|
|
1142
|
+
|
|
1143
|
+
// const transport = new StdioServerTransport();
|
|
1144
|
+
|
|
1145
|
+
// const session = new FastMCPSession({
|
|
1146
|
+
// name: this.#options.name,
|
|
1147
|
+
// version: this.#options.version,
|
|
1148
|
+
// tools: this.#tools,
|
|
1149
|
+
// resources: this.#resources,
|
|
1150
|
+
// resourcesTemplates: this.#resourcesTemplates,
|
|
1151
|
+
// prompts: this.#prompts,
|
|
1152
|
+
// });
|
|
1153
|
+
|
|
1154
|
+
// await session.connect(transport);
|
|
1155
|
+
|
|
1156
|
+
// this.#sessions.push(session);
|
|
1157
|
+
|
|
1158
|
+
// this.emit("connect", {
|
|
1159
|
+
// session,
|
|
1160
|
+
// });
|
|
1161
|
+
|
|
1162
|
+
// console.info(`server is running on stdio`);
|
|
1163
|
+
// }
|
|
1164
|
+
}
|