@codebolt/codeboltjs 1.1.95 → 1.1.96

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.
@@ -33,15 +33,24 @@ import http from "http";
33
33
  import { fetch } from "undici";
34
34
  import { loadEsm } from "load-esm";
35
35
 
36
+ /**
37
+ * Type definition for SSE Server that can be closed.
38
+ */
36
39
  export type SSEServer = {
37
40
  close: () => Promise<void>;
38
41
  };
39
42
 
43
+ /**
44
+ * Event types for the FastMCP event emitter.
45
+ */
40
46
  type FastMCPEvents<T extends FastMCPSessionAuth> = {
41
47
  connect: (event: { session: FastMCPSession<T> }) => void;
42
48
  disconnect: (event: { session: FastMCPSession<T> }) => void;
43
49
  };
44
50
 
51
+ /**
52
+ * Event types for FastMCPSession event emitter.
53
+ */
45
54
  type FastMCPSessionEvents = {
46
55
  rootsChanged: (event: { roots: Root[] }) => void;
47
56
  error: (event: { error: Error }) => void;
@@ -49,6 +58,9 @@ type FastMCPSessionEvents = {
49
58
 
50
59
  /**
51
60
  * Generates an image content object from a URL, file path, or buffer.
61
+ *
62
+ * @param input - The input source for the image (URL, file path, or buffer)
63
+ * @returns Promise with the image content object
52
64
  */
53
65
  export const imageContent = async (
54
66
  input: { url: string } | { path: string } | { buffer: Buffer },
@@ -75,7 +87,7 @@ export const imageContent = async (
75
87
 
76
88
  const { fileTypeFromBuffer } = await loadEsm('file-type');
77
89
  const mimeType = await fileTypeFromBuffer(rawData);
78
- console.log(mimeType);
90
+
79
91
 
80
92
  const base64Data = rawData.toString("base64");
81
93
 
@@ -86,6 +98,9 @@ export const imageContent = async (
86
98
  } as const;
87
99
  };
88
100
 
101
+ /**
102
+ * Base class for FastMCP errors.
103
+ */
89
104
  abstract class FastMCPError extends Error {
90
105
  public constructor(message?: string) {
91
106
  super(message);
@@ -93,13 +108,29 @@ abstract class FastMCPError extends Error {
93
108
  }
94
109
  }
95
110
 
111
+ /**
112
+ * Type for extra data in errors.
113
+ */
96
114
  type Extra = unknown;
97
115
 
116
+ /**
117
+ * Type for a record of extra data.
118
+ */
98
119
  type Extras = Record<string, Extra>;
99
120
 
121
+ /**
122
+ * Error class for unexpected state conditions.
123
+ */
100
124
  export class UnexpectedStateError extends FastMCPError {
125
+ /** Additional context for the error */
101
126
  public extras?: Extras;
102
127
 
128
+ /**
129
+ * Creates a new UnexpectedStateError.
130
+ *
131
+ * @param message - Error message
132
+ * @param extras - Additional context for the error
133
+ */
103
134
  public constructor(message: string, extras?: Extras) {
104
135
  super(message);
105
136
  this.name = new.target.name;
@@ -108,19 +139,31 @@ export class UnexpectedStateError extends FastMCPError {
108
139
  }
109
140
 
110
141
  /**
111
- * An error that is meant to be surfaced to the user.
142
+ * Error that is meant to be surfaced to the user.
112
143
  */
113
144
  export class UserError extends UnexpectedStateError {}
114
145
 
146
+ /**
147
+ * Type for tool parameters schema.
148
+ */
115
149
  type ToolParameters = z.ZodTypeAny;
116
150
 
151
+ /**
152
+ * Type for literal values.
153
+ */
117
154
  type Literal = boolean | null | number | string | undefined;
118
155
 
156
+ /**
157
+ * Type for serializable values that can be passed around.
158
+ */
119
159
  type SerializableValue =
120
160
  | Literal
121
161
  | SerializableValue[]
122
162
  | { [key: string]: SerializableValue };
123
163
 
164
+ /**
165
+ * Type for reporting progress of operations.
166
+ */
124
167
  type Progress = {
125
168
  /**
126
169
  * The progress thus far. This should increase every time progress is made, even if the total is unknown.
@@ -132,9 +175,15 @@ type Progress = {
132
175
  total?: number;
133
176
  };
134
177
 
178
+ /**
179
+ * Context object passed to tool execution functions.
180
+ */
135
181
  type Context<T extends FastMCPSessionAuth> = {
182
+ /** The session authentication context */
136
183
  session: T | undefined;
184
+ /** Function to report progress of operations */
137
185
  reportProgress: (progress: Progress) => Promise<void>;
186
+ /** Logging functions */
138
187
  log: {
139
188
  debug: (message: string, data?: SerializableValue) => void;
140
189
  error: (message: string, data?: SerializableValue) => void;
@@ -143,8 +192,13 @@ type Context<T extends FastMCPSessionAuth> = {
143
192
  };
144
193
  };
145
194
 
195
+ /**
196
+ * Type for text content in messages.
197
+ */
146
198
  type TextContent = {
199
+ /** Always "text" for text content */
147
200
  type: "text";
201
+ /** The text content */
148
202
  text: string;
149
203
  };
150
204
 
@@ -158,9 +212,15 @@ const TextContentZodSchema = z
158
212
  })
159
213
  .strict() satisfies z.ZodType<TextContent>;
160
214
 
215
+ /**
216
+ * Type for image content in messages.
217
+ */
161
218
  type ImageContent = {
219
+ /** Always "image" for image content */
162
220
  type: "image";
221
+ /** Base64-encoded image data */
163
222
  data: string;
223
+ /** The MIME type of the image */
164
224
  mimeType: string;
165
225
  };
166
226
 
@@ -178,6 +238,9 @@ const ImageContentZodSchema = z
178
238
  })
179
239
  .strict() satisfies z.ZodType<ImageContent>;
180
240
 
241
+ /**
242
+ * Union type for content in messages.
243
+ */
181
244
  type Content = TextContent | ImageContent;
182
245
 
183
246
  const ContentZodSchema = z.discriminatedUnion("type", [
@@ -185,8 +248,13 @@ const ContentZodSchema = z.discriminatedUnion("type", [
185
248
  ImageContentZodSchema,
186
249
  ]) satisfies z.ZodType<Content>;
187
250
 
251
+ /**
252
+ * Type for content results from tool executions.
253
+ */
188
254
  type ContentResult = {
255
+ /** Array of content blocks */
189
256
  content: Content[];
257
+ /** Whether this result represents an error */
190
258
  isError?: boolean;
191
259
  };
192
260
 
@@ -197,13 +265,20 @@ const ContentResultZodSchema = z
197
265
  })
198
266
  .strict() satisfies z.ZodType<ContentResult>;
199
267
 
268
+ /**
269
+ * Type for completion results.
270
+ */
200
271
  type Completion = {
272
+ /** Array of completion values */
201
273
  values: string[];
274
+ /** The total number of completions available */
202
275
  total?: number;
276
+ /** Whether there are more completions available */
203
277
  hasMore?: boolean;
204
278
  };
205
279
 
206
280
  /**
281
+ * Schema for completion results.
207
282
  * https://github.com/modelcontextprotocol/typescript-sdk/blob/3164da64d085ec4e022ae881329eee7b72f208d4/src/types.ts#L983-L1003
208
283
  */
209
284
  const CompletionZodSchema = z.object({
@@ -221,16 +296,26 @@ const CompletionZodSchema = z.object({
221
296
  hasMore: z.optional(z.boolean()),
222
297
  }) satisfies z.ZodType<Completion>;
223
298
 
299
+ /**
300
+ * Type for a tool that can be executed.
301
+ */
224
302
  type Tool<T extends FastMCPSessionAuth, Params extends ToolParameters = ToolParameters> = {
303
+ /** Name of the tool */
225
304
  name: string;
305
+ /** Optional description of the tool's functionality */
226
306
  description?: string;
307
+ /** Optional parameters schema for the tool */
227
308
  parameters?: Params;
309
+ /** Function to execute the tool */
228
310
  execute: (
229
311
  args: z.infer<Params>,
230
312
  context: Context<T>,
231
313
  ) => Promise<string | ContentResult | TextContent | ImageContent>;
232
314
  };
233
315
 
316
+ /**
317
+ * Type for resource results.
318
+ */
234
319
  type ResourceResult =
235
320
  | {
236
321
  text: string;
@@ -239,68 +324,119 @@ type ResourceResult =
239
324
  blob: string;
240
325
  };
241
326
 
327
+ /**
328
+ * Type for input resource template arguments.
329
+ */
242
330
  type InputResourceTemplateArgument = Readonly<{
243
331
  name: string;
244
332
  description?: string;
245
333
  complete?: ArgumentValueCompleter;
246
334
  }>;
247
335
 
336
+ /**
337
+ * Type for resource template arguments.
338
+ */
248
339
  type ResourceTemplateArgument = Readonly<{
249
340
  name: string;
250
341
  description?: string;
251
342
  complete?: ArgumentValueCompleter;
252
343
  }>;
253
344
 
345
+ /**
346
+ * Type for resource templates.
347
+ */
254
348
  type ResourceTemplate<
255
349
  Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],
256
350
  > = {
351
+ /** URI template for the resource */
257
352
  uriTemplate: string;
353
+ /** Name of the resource template */
258
354
  name: string;
355
+ /** Optional description */
259
356
  description?: string;
357
+ /** Optional MIME type */
260
358
  mimeType?: string;
359
+ /** Arguments for the template */
261
360
  arguments: Arguments;
361
+ /** Optional completion function */
262
362
  complete?: (name: string, value: string) => Promise<Completion>;
363
+ /** Function to load the resource */
263
364
  load: (
264
365
  args: ResourceTemplateArgumentsToObject<Arguments>,
265
366
  ) => Promise<ResourceResult>;
266
367
  };
267
368
 
369
+ /**
370
+ * Type transformation from argument array to object.
371
+ */
268
372
  type ResourceTemplateArgumentsToObject<T extends { name: string }[]> = {
269
373
  [K in T[number]["name"]]: string;
270
374
  };
271
375
 
376
+ /**
377
+ * Type for input resource templates.
378
+ */
272
379
  type InputResourceTemplate<
273
380
  Arguments extends ResourceTemplateArgument[] = ResourceTemplateArgument[],
274
381
  > = {
382
+ /** URI template for the resource */
275
383
  uriTemplate: string;
384
+ /** Name of the template */
276
385
  name: string;
386
+ /** Optional description */
277
387
  description?: string;
388
+ /** Optional MIME type */
278
389
  mimeType?: string;
390
+ /** Arguments for the template */
279
391
  arguments: Arguments;
392
+ /** Function to load the resource */
280
393
  load: (
281
394
  args: ResourceTemplateArgumentsToObject<Arguments>,
282
395
  ) => Promise<ResourceResult>;
283
396
  };
284
397
 
398
+ /**
399
+ * Type for a resource.
400
+ */
285
401
  type Resource = {
402
+ /** URI of the resource */
286
403
  uri: string;
404
+ /** Name of the resource */
287
405
  name: string;
406
+ /** Optional description */
288
407
  description?: string;
408
+ /** Optional MIME type */
289
409
  mimeType?: string;
410
+ /** Function to load the resource */
290
411
  load: () => Promise<ResourceResult | ResourceResult[]>;
412
+ /** Optional completion function */
291
413
  complete?: (name: string, value: string) => Promise<Completion>;
292
414
  };
293
415
 
416
+ /**
417
+ * Type for argument value completion.
418
+ */
294
419
  type ArgumentValueCompleter = (value: string) => Promise<Completion>;
295
420
 
421
+ /**
422
+ * Type for input prompt arguments.
423
+ */
296
424
  type InputPromptArgument = Readonly<{
425
+ /** Name of the argument */
297
426
  name: string;
427
+ /** Optional description */
298
428
  description?: string;
429
+ /** Whether the argument is required */
299
430
  required?: boolean;
431
+ /** Optional completion function */
300
432
  complete?: ArgumentValueCompleter;
433
+ /** Optional enum of possible values */
301
434
  enum?: string[];
302
435
  }>;
303
436
 
437
+ /**
438
+ * Type transformation from prompt arguments to object.
439
+ */
304
440
  type PromptArgumentsToObject<T extends { name: string; required?: boolean }[]> =
305
441
  {
306
442
  [K in T[number]["name"]]: Extract<
@@ -311,41 +447,73 @@ type PromptArgumentsToObject<T extends { name: string; required?: boolean }[]> =
311
447
  : string | undefined;
312
448
  };
313
449
 
450
+ /**
451
+ * Type for input prompts.
452
+ */
314
453
  type InputPrompt<
315
454
  Arguments extends InputPromptArgument[] = InputPromptArgument[],
316
455
  Args = PromptArgumentsToObject<Arguments>,
317
456
  > = {
457
+ /** Name of the prompt */
318
458
  name: string;
459
+ /** Optional description */
319
460
  description?: string;
461
+ /** Optional arguments */
320
462
  arguments?: InputPromptArgument[];
463
+ /** Function to load the prompt */
321
464
  load: (args: Args) => Promise<string>;
322
465
  };
323
466
 
467
+ /**
468
+ * Type for prompt arguments.
469
+ */
324
470
  type PromptArgument = Readonly<{
471
+ /** Name of the argument */
325
472
  name: string;
473
+ /** Optional description */
326
474
  description?: string;
475
+ /** Whether the argument is required */
327
476
  required?: boolean;
477
+ /** Optional completion function */
328
478
  complete?: ArgumentValueCompleter;
479
+ /** Optional enum of possible values */
329
480
  enum?: string[];
330
481
  }>;
331
482
 
483
+ /**
484
+ * Type for prompts.
485
+ */
332
486
  type Prompt<
333
487
  Arguments extends PromptArgument[] = PromptArgument[],
334
488
  Args = PromptArgumentsToObject<Arguments>,
335
489
  > = {
490
+ /** Optional arguments */
336
491
  arguments?: PromptArgument[];
492
+ /** Optional completion function */
337
493
  complete?: (name: string, value: string) => Promise<Completion>;
494
+ /** Optional description */
338
495
  description?: string;
496
+ /** Function to load the prompt */
339
497
  load: (args: Args) => Promise<string>;
498
+ /** Name of the prompt */
340
499
  name: string;
341
500
  };
342
501
 
502
+ /**
503
+ * Type for server options.
504
+ */
343
505
  type ServerOptions<T extends FastMCPSessionAuth> = {
506
+ /** Name of the server */
344
507
  name: string;
508
+ /** Version of the server */
345
509
  version: `${number}.${number}.${number}`;
510
+ /** Optional authentication function */
346
511
  authenticate?: Authenticate<T>;
347
512
  };
348
513
 
514
+ /**
515
+ * Type for logging levels.
516
+ */
349
517
  type LoggingLevel =
350
518
  | "debug"
351
519
  | "info"
@@ -362,15 +530,29 @@ const FastMCPSessionEventEmitterBase: {
362
530
 
363
531
  class FastMCPSessionEventEmitter extends FastMCPSessionEventEmitterBase {}
364
532
 
533
+ /**
534
+ * Type for sampling responses.
535
+ */
365
536
  type SamplingResponse = {
537
+ /** The model used */
366
538
  model: string;
539
+ /** Optional reason for stopping */
367
540
  stopReason?: "endTurn" | "stopSequence" | "maxTokens" | string;
541
+ /** Role of the message */
368
542
  role: "user" | "assistant";
543
+ /** Content of the message */
369
544
  content: TextContent | ImageContent;
370
545
  };
371
546
 
547
+ /**
548
+ * Type for session authentication.
549
+ */
372
550
  type FastMCPSessionAuth = Record<string, unknown> | undefined;
373
551
 
552
+ /**
553
+ * Class representing a FastMCP session.
554
+ * Manages communication between the client and server.
555
+ */
374
556
  export class FastMCPSession<T extends FastMCPSessionAuth = FastMCPSessionAuth> extends FastMCPSessionEventEmitter {
375
557
  #capabilities: ServerCapabilities = {};
376
558
  #clientCapabilities?: ClientCapabilities;
@@ -382,6 +564,11 @@ export class FastMCPSession<T extends FastMCPSessionAuth = FastMCPSessionAuth> e
382
564
  #server: Server;
383
565
  #auth: T | undefined;
384
566
 
567
+ /**
568
+ * Creates a new FastMCPSession.
569
+ *
570
+ * @param options - Configuration options for the session
571
+ */
385
572
  constructor({
386
573
  auth,
387
574
  name,
@@ -555,7 +742,7 @@ export class FastMCPSession<T extends FastMCPSessionAuth = FastMCPSessionAuth> e
555
742
 
556
743
  while (attempt++ < 10) {
557
744
  const capabilities = await this.#server.getClientCapabilities();
558
- console.log("capabilities", capabilities);
745
+
559
746
 
560
747
  if (capabilities) {
561
748
  this.#clientCapabilities = capabilities;
@@ -567,7 +754,7 @@ export class FastMCPSession<T extends FastMCPSessionAuth = FastMCPSessionAuth> e
567
754
  }
568
755
 
569
756
  if (!this.#clientCapabilities) {
570
- console.warn('[warning] toolBox could not infer client capabilities')
757
+ // console.warn('[warning] toolBox could not infer client capabilities')
571
758
  }
572
759
 
573
760
  if (this.#clientCapabilities?.roots) {
@@ -1027,6 +1214,10 @@ class FastMCPEventEmitter extends FastMCPEventEmitterBase {}
1027
1214
 
1028
1215
  type Authenticate<T> = (request: http.IncomingMessage) => Promise<T>;
1029
1216
 
1217
+ /**
1218
+ * Class representing a toolbox for FastMCP.
1219
+ * Manages tools, resources, and prompts for a Model Context Protocol server.
1220
+ */
1030
1221
  export class ToolBox<T extends Record<string, unknown> | undefined = undefined> extends FastMCPEventEmitter {
1031
1222
  #options: ServerOptions<T>;
1032
1223
  #prompts: InputPrompt[] = [];
@@ -1037,6 +1228,11 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1037
1228
  #tools: Tool<T>[] = [];
1038
1229
  #authenticate: Authenticate<T> | undefined;
1039
1230
 
1231
+ /**
1232
+ * Creates a new ToolBox instance.
1233
+ *
1234
+ * @param options - Configuration options for the toolbox
1235
+ */
1040
1236
  constructor(public options: ServerOptions<T>) {
1041
1237
  super();
1042
1238
 
@@ -1044,12 +1240,17 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1044
1240
  this.#authenticate = options.authenticate;
1045
1241
  }
1046
1242
 
1243
+ /**
1244
+ * Gets all active sessions.
1245
+ */
1047
1246
  public get sessions(): FastMCPSession<T>[] {
1048
1247
  return this.#sessions;
1049
1248
  }
1050
1249
 
1051
1250
  /**
1052
1251
  * Adds a tool to the server.
1252
+ *
1253
+ * @param tool - The tool to add
1053
1254
  */
1054
1255
  public addTool<Params extends ToolParameters>(tool: Tool<T, Params>) {
1055
1256
  this.#tools.push(tool as unknown as Tool<T>);
@@ -1057,6 +1258,8 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1057
1258
 
1058
1259
  /**
1059
1260
  * Adds a resource to the server.
1261
+ *
1262
+ * @param resource - The resource to add
1060
1263
  */
1061
1264
  public addResource(resource: Resource) {
1062
1265
  this.#resources.push(resource);
@@ -1064,6 +1267,8 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1064
1267
 
1065
1268
  /**
1066
1269
  * Adds a resource template to the server.
1270
+ *
1271
+ * @param resource - The resource template to add
1067
1272
  */
1068
1273
  public addResourceTemplate<
1069
1274
  const Args extends InputResourceTemplateArgument[],
@@ -1073,6 +1278,8 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1073
1278
 
1074
1279
  /**
1075
1280
  * Adds a prompt to the server.
1281
+ *
1282
+ * @param prompt - The prompt to add
1076
1283
  */
1077
1284
  public addPrompt<const Args extends InputPromptArgument[]>(
1078
1285
  prompt: InputPrompt<Args>,
@@ -1082,6 +1289,8 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1082
1289
 
1083
1290
  /**
1084
1291
  * Starts the server.
1292
+ *
1293
+ * @param options - Options for the server transport
1085
1294
  */
1086
1295
  public async start(
1087
1296
  options:
@@ -1130,7 +1339,9 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1130
1339
  }
1131
1340
 
1132
1341
  /**
1133
- * Starts the server.
1342
+ * Activates the server.
1343
+ *
1344
+ * @param options - Options for the server transport
1134
1345
  */
1135
1346
  public async activate(
1136
1347
  options:
@@ -1144,7 +1355,6 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1144
1355
  ) {
1145
1356
  if (options.transportType === "stdio") {
1146
1357
  const transport = new StdioServerTransport();
1147
- // console.log("tools", this.#tools);
1148
1358
 
1149
1359
  const session = new FastMCPSession({
1150
1360
  name: this.#options.name,
@@ -1154,7 +1364,7 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1154
1364
  resourcesTemplates: this.#resourcesTemplates,
1155
1365
  prompts: this.#prompts,
1156
1366
  });
1157
- // console.log("session", session);
1367
+
1158
1368
  await session.connect(transport);
1159
1369
 
1160
1370
  this.#sessions.push(session);
@@ -1165,42 +1375,10 @@ export class ToolBox<T extends Record<string, unknown> | undefined = undefined>
1165
1375
 
1166
1376
  console.info(`server is running on stdio`);
1167
1377
  } else if (options.transportType === "sse") {
1168
- // this.#sseServer = await startSSEServer<FastMCPSession>({
1169
- // endpoint: options.sse.endpoint as `/${string}`,
1170
- // port: options.sse.port,
1171
- // createServer: async () => {
1172
- // return new FastMCPSession({
1173
- // name: this.#options.name,
1174
- // version: this.#options.version,
1175
- // tools: this.#tools,
1176
- // resources: this.#resources,
1177
- // resourcesTemplates: this.#resourcesTemplates,
1178
- // prompts: this.#prompts,
1179
- // });
1180
- // },
1181
- // onClose: (session) => {
1182
- // this.emit("disconnect", {
1183
- // session,
1184
- // });
1185
- // },
1186
- // onConnect: async (session) => {
1187
- // this.#sessions.push(session);
1188
-
1189
- // this.emit("connect", {
1190
- // session,
1191
- // });
1192
- // },
1193
- // });
1194
-
1195
- // console.error(
1196
- // `server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`,
1197
- // );
1378
+ // Implementation for SSE transport
1198
1379
  } else {
1199
1380
  throw new Error("Invalid transport type");
1200
1381
  }
1201
1382
  }
1202
-
1203
-
1204
-
1205
1383
  }
1206
1384