@codebolt/codeboltjs 1.1.92 → 1.1.95
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/index.d.ts +15 -8
- package/index.js +3 -3
- package/modules/agentlib/agent.d.ts +2 -2
- package/modules/agentlib/agent.js +92 -16
- package/modules/agentlib/usermessage.d.ts +11 -1
- package/modules/agentlib/usermessage.js +8 -4
- package/modules/mcp.d.ts +1 -0
- package/modules/mcp.js +24 -0
- package/modules/toolBox.bkp.d.ts +262 -0
- package/modules/toolBox.bkp.js +721 -0
- package/modules/toolBox.d.ts +36 -17
- package/modules/toolBox.js +62 -21
- package/modules/tools.d.ts +16 -0
- package/modules/tools.js +197 -0
- package/package.json +3 -2
- package/src/index.ts +3 -3
- package/src/modules/agentlib/agent.ts +111 -23
- package/src/modules/agentlib/usermessage.ts +23 -10
- package/src/modules/toolBox.bkp.ts +1165 -0
- package/src/modules/toolBox.ts +108 -66
- package/src/modules/tools.ts +187 -0
- package/src/modules/mcp.ts +0 -117
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
var _FastMCPSession_capabilities, _FastMCPSession_clientCapabilities, _FastMCPSession_loggingLevel, _FastMCPSession_prompts, _FastMCPSession_resources, _FastMCPSession_resourceTemplates, _FastMCPSession_roots, _FastMCPSession_server, _FastMCPSession_pingInterval, _ToolBox_options, _ToolBox_prompts, _ToolBox_resources, _ToolBox_resourcesTemplates, _ToolBox_sessions, _ToolBox_sseServer, _ToolBox_tools;
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.ToolBox = exports.FastMCPSession = exports.UserError = exports.UnexpectedStateError = exports.imageContent = void 0;
|
|
19
|
+
const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
|
|
20
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
21
|
+
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
22
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
23
|
+
const zod_1 = require("zod");
|
|
24
|
+
const promises_1 = require("timers/promises");
|
|
25
|
+
const promises_2 = require("fs/promises");
|
|
26
|
+
const events_1 = require("events");
|
|
27
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
28
|
+
const uri_templates_1 = __importDefault(require("uri-templates"));
|
|
29
|
+
const load_esm_1 = require("load-esm");
|
|
30
|
+
/**
|
|
31
|
+
* Generates an image content object from a URL, file path, or buffer.
|
|
32
|
+
*/
|
|
33
|
+
const imageContent = async (input) => {
|
|
34
|
+
var _a;
|
|
35
|
+
let rawData;
|
|
36
|
+
if ("url" in input) {
|
|
37
|
+
const response = await fetch(input.url);
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
throw new Error(`Failed to fetch image from URL: ${response.statusText}`);
|
|
40
|
+
}
|
|
41
|
+
rawData = Buffer.from(await response.arrayBuffer());
|
|
42
|
+
}
|
|
43
|
+
else if ("path" in input) {
|
|
44
|
+
rawData = await (0, promises_2.readFile)(input.path);
|
|
45
|
+
}
|
|
46
|
+
else if ("buffer" in input) {
|
|
47
|
+
rawData = input.buffer;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
throw new Error("Invalid input: Provide a valid 'url', 'path', or 'buffer'");
|
|
51
|
+
}
|
|
52
|
+
const { fileTypeFromBuffer } = await (0, load_esm_1.loadEsm)('file-type');
|
|
53
|
+
const mimeType = await fileTypeFromBuffer(rawData);
|
|
54
|
+
console.log(mimeType);
|
|
55
|
+
const base64Data = rawData.toString("base64");
|
|
56
|
+
return {
|
|
57
|
+
type: "image",
|
|
58
|
+
data: base64Data,
|
|
59
|
+
mimeType: (_a = mimeType === null || mimeType === void 0 ? void 0 : mimeType.mime) !== null && _a !== void 0 ? _a : "image/png",
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
exports.imageContent = imageContent;
|
|
63
|
+
class FastMCPError extends Error {
|
|
64
|
+
constructor(message) {
|
|
65
|
+
super(message);
|
|
66
|
+
this.name = new.target.name;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
class UnexpectedStateError extends FastMCPError {
|
|
70
|
+
constructor(message, extras) {
|
|
71
|
+
super(message);
|
|
72
|
+
this.name = new.target.name;
|
|
73
|
+
this.extras = extras;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.UnexpectedStateError = UnexpectedStateError;
|
|
77
|
+
/**
|
|
78
|
+
* An error that is meant to be surfaced to the user.
|
|
79
|
+
*/
|
|
80
|
+
class UserError extends UnexpectedStateError {
|
|
81
|
+
}
|
|
82
|
+
exports.UserError = UserError;
|
|
83
|
+
const TextContentZodSchema = zod_1.z
|
|
84
|
+
.object({
|
|
85
|
+
type: zod_1.z.literal("text"),
|
|
86
|
+
/**
|
|
87
|
+
* The text content of the message.
|
|
88
|
+
*/
|
|
89
|
+
text: zod_1.z.string(),
|
|
90
|
+
})
|
|
91
|
+
.strict();
|
|
92
|
+
const ImageContentZodSchema = zod_1.z
|
|
93
|
+
.object({
|
|
94
|
+
type: zod_1.z.literal("image"),
|
|
95
|
+
/**
|
|
96
|
+
* The base64-encoded image data.
|
|
97
|
+
*/
|
|
98
|
+
data: zod_1.z.string().base64(),
|
|
99
|
+
/**
|
|
100
|
+
* The MIME type of the image. Different providers may support different image types.
|
|
101
|
+
*/
|
|
102
|
+
mimeType: zod_1.z.string(),
|
|
103
|
+
})
|
|
104
|
+
.strict();
|
|
105
|
+
const ContentZodSchema = zod_1.z.discriminatedUnion("type", [
|
|
106
|
+
TextContentZodSchema,
|
|
107
|
+
ImageContentZodSchema,
|
|
108
|
+
]);
|
|
109
|
+
const ContentResultZodSchema = zod_1.z
|
|
110
|
+
.object({
|
|
111
|
+
content: ContentZodSchema.array(),
|
|
112
|
+
isError: zod_1.z.boolean().optional(),
|
|
113
|
+
})
|
|
114
|
+
.strict();
|
|
115
|
+
/**
|
|
116
|
+
* https://github.com/modelcontextprotocol/typescript-sdk/blob/3164da64d085ec4e022ae881329eee7b72f208d4/src/types.ts#L983-L1003
|
|
117
|
+
*/
|
|
118
|
+
const CompletionZodSchema = zod_1.z.object({
|
|
119
|
+
/**
|
|
120
|
+
* An array of completion values. Must not exceed 100 items.
|
|
121
|
+
*/
|
|
122
|
+
values: zod_1.z.array(zod_1.z.string()).max(100),
|
|
123
|
+
/**
|
|
124
|
+
* The total number of completion options available. This can exceed the number of values actually sent in the response.
|
|
125
|
+
*/
|
|
126
|
+
total: zod_1.z.optional(zod_1.z.number().int()),
|
|
127
|
+
/**
|
|
128
|
+
* Indicates whether there are additional completion options beyond those provided in the current response, even if the exact total is unknown.
|
|
129
|
+
*/
|
|
130
|
+
hasMore: zod_1.z.optional(zod_1.z.boolean()),
|
|
131
|
+
});
|
|
132
|
+
const FastMCPSessionEventEmitterBase = events_1.EventEmitter;
|
|
133
|
+
class FastMCPSessionEventEmitter extends FastMCPSessionEventEmitterBase {
|
|
134
|
+
}
|
|
135
|
+
class FastMCPSession extends FastMCPSessionEventEmitter {
|
|
136
|
+
constructor({ name, version, tools, resources, resourcesTemplates, prompts, }) {
|
|
137
|
+
super();
|
|
138
|
+
_FastMCPSession_capabilities.set(this, {});
|
|
139
|
+
_FastMCPSession_clientCapabilities.set(this, void 0);
|
|
140
|
+
_FastMCPSession_loggingLevel.set(this, "info");
|
|
141
|
+
_FastMCPSession_prompts.set(this, []);
|
|
142
|
+
_FastMCPSession_resources.set(this, []);
|
|
143
|
+
_FastMCPSession_resourceTemplates.set(this, []);
|
|
144
|
+
_FastMCPSession_roots.set(this, []);
|
|
145
|
+
_FastMCPSession_server.set(this, void 0);
|
|
146
|
+
_FastMCPSession_pingInterval.set(this, null);
|
|
147
|
+
if (tools.length) {
|
|
148
|
+
__classPrivateFieldGet(this, _FastMCPSession_capabilities, "f").tools = {};
|
|
149
|
+
}
|
|
150
|
+
if (resources.length || resourcesTemplates.length) {
|
|
151
|
+
__classPrivateFieldGet(this, _FastMCPSession_capabilities, "f").resources = {};
|
|
152
|
+
}
|
|
153
|
+
if (prompts.length) {
|
|
154
|
+
for (const prompt of prompts) {
|
|
155
|
+
this.addPrompt(prompt);
|
|
156
|
+
}
|
|
157
|
+
__classPrivateFieldGet(this, _FastMCPSession_capabilities, "f").prompts = {};
|
|
158
|
+
}
|
|
159
|
+
__classPrivateFieldGet(this, _FastMCPSession_capabilities, "f").logging = {};
|
|
160
|
+
__classPrivateFieldSet(this, _FastMCPSession_server, new index_js_1.Server({ name: name, version: version }, { capabilities: __classPrivateFieldGet(this, _FastMCPSession_capabilities, "f") }), "f");
|
|
161
|
+
this.setupErrorHandling();
|
|
162
|
+
this.setupLoggingHandlers();
|
|
163
|
+
this.setupRootsHandlers();
|
|
164
|
+
this.setupCompleteHandlers();
|
|
165
|
+
if (tools.length) {
|
|
166
|
+
this.setupToolHandlers(tools);
|
|
167
|
+
}
|
|
168
|
+
if (resources.length || resourcesTemplates.length) {
|
|
169
|
+
for (const resource of resources) {
|
|
170
|
+
this.addResource(resource);
|
|
171
|
+
}
|
|
172
|
+
this.setupResourceHandlers(resources);
|
|
173
|
+
if (resourcesTemplates.length) {
|
|
174
|
+
for (const resourceTemplate of resourcesTemplates) {
|
|
175
|
+
this.addResourceTemplate(resourceTemplate);
|
|
176
|
+
}
|
|
177
|
+
this.setupResourceTemplateHandlers(resourcesTemplates);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (prompts.length) {
|
|
181
|
+
this.setupPromptHandlers(prompts);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
addResource(inputResource) {
|
|
185
|
+
__classPrivateFieldGet(this, _FastMCPSession_resources, "f").push(inputResource);
|
|
186
|
+
}
|
|
187
|
+
addResourceTemplate(inputResourceTemplate) {
|
|
188
|
+
var _a;
|
|
189
|
+
const completers = {};
|
|
190
|
+
for (const argument of (_a = inputResourceTemplate.arguments) !== null && _a !== void 0 ? _a : []) {
|
|
191
|
+
if (argument.complete) {
|
|
192
|
+
completers[argument.name] = argument.complete;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const resourceTemplate = {
|
|
196
|
+
...inputResourceTemplate,
|
|
197
|
+
complete: async (name, value) => {
|
|
198
|
+
if (completers[name]) {
|
|
199
|
+
return await completers[name](value);
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
values: [],
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
__classPrivateFieldGet(this, _FastMCPSession_resourceTemplates, "f").push(resourceTemplate);
|
|
207
|
+
}
|
|
208
|
+
addPrompt(inputPrompt) {
|
|
209
|
+
var _a;
|
|
210
|
+
const completers = {};
|
|
211
|
+
const enums = {};
|
|
212
|
+
for (const argument of (_a = inputPrompt.arguments) !== null && _a !== void 0 ? _a : []) {
|
|
213
|
+
if (argument.complete) {
|
|
214
|
+
completers[argument.name] = argument.complete;
|
|
215
|
+
}
|
|
216
|
+
if (argument.enum) {
|
|
217
|
+
enums[argument.name] = argument.enum;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const prompt = {
|
|
221
|
+
...inputPrompt,
|
|
222
|
+
complete: async (name, value) => {
|
|
223
|
+
if (completers[name]) {
|
|
224
|
+
return await completers[name](value);
|
|
225
|
+
}
|
|
226
|
+
if (enums[name]) {
|
|
227
|
+
const fuse = new fuse_js_1.default(enums[name], {
|
|
228
|
+
keys: ["value"],
|
|
229
|
+
});
|
|
230
|
+
const result = fuse.search(value);
|
|
231
|
+
return {
|
|
232
|
+
values: result.map((item) => item.item),
|
|
233
|
+
total: result.length,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
values: [],
|
|
238
|
+
};
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
__classPrivateFieldGet(this, _FastMCPSession_prompts, "f").push(prompt);
|
|
242
|
+
}
|
|
243
|
+
get clientCapabilities() {
|
|
244
|
+
var _a;
|
|
245
|
+
return (_a = __classPrivateFieldGet(this, _FastMCPSession_clientCapabilities, "f")) !== null && _a !== void 0 ? _a : null;
|
|
246
|
+
}
|
|
247
|
+
get server() {
|
|
248
|
+
return __classPrivateFieldGet(this, _FastMCPSession_server, "f");
|
|
249
|
+
}
|
|
250
|
+
async requestSampling(message) {
|
|
251
|
+
return __classPrivateFieldGet(this, _FastMCPSession_server, "f").createMessage(message);
|
|
252
|
+
}
|
|
253
|
+
async connect(transport) {
|
|
254
|
+
var _a;
|
|
255
|
+
if (__classPrivateFieldGet(this, _FastMCPSession_server, "f").transport) {
|
|
256
|
+
throw new UnexpectedStateError("Server is already connected");
|
|
257
|
+
}
|
|
258
|
+
await __classPrivateFieldGet(this, _FastMCPSession_server, "f").connect(transport);
|
|
259
|
+
let attempt = 0;
|
|
260
|
+
while (attempt++ < 10) {
|
|
261
|
+
const capabilities = await __classPrivateFieldGet(this, _FastMCPSession_server, "f").getClientCapabilities();
|
|
262
|
+
if (capabilities) {
|
|
263
|
+
__classPrivateFieldSet(this, _FastMCPSession_clientCapabilities, capabilities, "f");
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
await (0, promises_1.setTimeout)(100);
|
|
267
|
+
}
|
|
268
|
+
if (!__classPrivateFieldGet(this, _FastMCPSession_clientCapabilities, "f")) {
|
|
269
|
+
throw new UnexpectedStateError("Server did not connect");
|
|
270
|
+
}
|
|
271
|
+
if ((_a = __classPrivateFieldGet(this, _FastMCPSession_clientCapabilities, "f")) === null || _a === void 0 ? void 0 : _a.roots) {
|
|
272
|
+
const roots = await __classPrivateFieldGet(this, _FastMCPSession_server, "f").listRoots();
|
|
273
|
+
__classPrivateFieldSet(this, _FastMCPSession_roots, roots.roots, "f");
|
|
274
|
+
}
|
|
275
|
+
__classPrivateFieldSet(this, _FastMCPSession_pingInterval, setInterval(async () => {
|
|
276
|
+
try {
|
|
277
|
+
await __classPrivateFieldGet(this, _FastMCPSession_server, "f").ping();
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
this.emit("error", {
|
|
281
|
+
error: error,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}, 1000), "f");
|
|
285
|
+
}
|
|
286
|
+
get roots() {
|
|
287
|
+
return __classPrivateFieldGet(this, _FastMCPSession_roots, "f");
|
|
288
|
+
}
|
|
289
|
+
async close() {
|
|
290
|
+
if (__classPrivateFieldGet(this, _FastMCPSession_pingInterval, "f")) {
|
|
291
|
+
clearInterval(__classPrivateFieldGet(this, _FastMCPSession_pingInterval, "f"));
|
|
292
|
+
}
|
|
293
|
+
await __classPrivateFieldGet(this, _FastMCPSession_server, "f").close();
|
|
294
|
+
}
|
|
295
|
+
setupErrorHandling() {
|
|
296
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").onerror = (error) => {
|
|
297
|
+
console.error("[MCP Error]", error);
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
get loggingLevel() {
|
|
301
|
+
return __classPrivateFieldGet(this, _FastMCPSession_loggingLevel, "f");
|
|
302
|
+
}
|
|
303
|
+
setupCompleteHandlers() {
|
|
304
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.CompleteRequestSchema, async (request) => {
|
|
305
|
+
if (request.params.ref.type === "ref/prompt") {
|
|
306
|
+
const prompt = __classPrivateFieldGet(this, _FastMCPSession_prompts, "f").find((prompt) => prompt.name === request.params.ref.name);
|
|
307
|
+
if (!prompt) {
|
|
308
|
+
throw new UnexpectedStateError("Unknown prompt", {
|
|
309
|
+
request,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (!prompt.complete) {
|
|
313
|
+
throw new UnexpectedStateError("Prompt does not support completion", {
|
|
314
|
+
request,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
const completion = CompletionZodSchema.parse(await prompt.complete(request.params.argument.name, request.params.argument.value));
|
|
318
|
+
return {
|
|
319
|
+
completion,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
if (request.params.ref.type === "ref/resource") {
|
|
323
|
+
const resource = __classPrivateFieldGet(this, _FastMCPSession_resourceTemplates, "f").find((resource) => resource.uriTemplate === request.params.ref.uri);
|
|
324
|
+
if (!resource) {
|
|
325
|
+
throw new UnexpectedStateError("Unknown resource", {
|
|
326
|
+
request,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
if (!("uriTemplate" in resource)) {
|
|
330
|
+
throw new UnexpectedStateError("Unexpected resource");
|
|
331
|
+
}
|
|
332
|
+
if (!resource.complete) {
|
|
333
|
+
throw new UnexpectedStateError("Resource does not support completion", {
|
|
334
|
+
request,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
const completion = CompletionZodSchema.parse(await resource.complete(request.params.argument.name, request.params.argument.value));
|
|
338
|
+
return {
|
|
339
|
+
completion,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
throw new UnexpectedStateError("Unexpected completion request", {
|
|
343
|
+
request,
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
setupRootsHandlers() {
|
|
348
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setNotificationHandler(types_js_1.RootsListChangedNotificationSchema, () => {
|
|
349
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").listRoots().then((roots) => {
|
|
350
|
+
__classPrivateFieldSet(this, _FastMCPSession_roots, roots.roots, "f");
|
|
351
|
+
this.emit("rootsChanged", {
|
|
352
|
+
roots: roots.roots,
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
setupLoggingHandlers() {
|
|
358
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.SetLevelRequestSchema, (request) => {
|
|
359
|
+
__classPrivateFieldSet(this, _FastMCPSession_loggingLevel, request.params.level, "f");
|
|
360
|
+
return {};
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
setupToolHandlers(tools) {
|
|
364
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.ListToolsRequestSchema, async () => {
|
|
365
|
+
return {
|
|
366
|
+
tools: tools.map((tool) => {
|
|
367
|
+
return {
|
|
368
|
+
name: tool.name,
|
|
369
|
+
description: tool.description,
|
|
370
|
+
inputSchema: tool.parameters
|
|
371
|
+
? (0, zod_to_json_schema_1.zodToJsonSchema)(tool.parameters)
|
|
372
|
+
: undefined,
|
|
373
|
+
};
|
|
374
|
+
}),
|
|
375
|
+
};
|
|
376
|
+
});
|
|
377
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
378
|
+
var _a, _b;
|
|
379
|
+
const tool = tools.find((tool) => tool.name === request.params.name);
|
|
380
|
+
if (!tool) {
|
|
381
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`);
|
|
382
|
+
}
|
|
383
|
+
let args = undefined;
|
|
384
|
+
if (tool.parameters) {
|
|
385
|
+
const parsed = tool.parameters.safeParse(request.params.arguments);
|
|
386
|
+
if (!parsed.success) {
|
|
387
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidParams, `Invalid ${request.params.name} parameters`);
|
|
388
|
+
}
|
|
389
|
+
args = parsed.data;
|
|
390
|
+
}
|
|
391
|
+
const progressToken = (_b = (_a = request.params) === null || _a === void 0 ? void 0 : _a._meta) === null || _b === void 0 ? void 0 : _b.progressToken;
|
|
392
|
+
let result;
|
|
393
|
+
try {
|
|
394
|
+
const reportProgress = async (progress) => {
|
|
395
|
+
await __classPrivateFieldGet(this, _FastMCPSession_server, "f").notification({
|
|
396
|
+
method: "notifications/progress",
|
|
397
|
+
params: {
|
|
398
|
+
...progress,
|
|
399
|
+
progressToken,
|
|
400
|
+
},
|
|
401
|
+
});
|
|
402
|
+
};
|
|
403
|
+
const log = {
|
|
404
|
+
debug: (message, context) => {
|
|
405
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").sendLoggingMessage({
|
|
406
|
+
level: "debug",
|
|
407
|
+
data: {
|
|
408
|
+
message,
|
|
409
|
+
context,
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
},
|
|
413
|
+
error: (message, context) => {
|
|
414
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").sendLoggingMessage({
|
|
415
|
+
level: "error",
|
|
416
|
+
data: {
|
|
417
|
+
message,
|
|
418
|
+
context,
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
},
|
|
422
|
+
info: (message, context) => {
|
|
423
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").sendLoggingMessage({
|
|
424
|
+
level: "info",
|
|
425
|
+
data: {
|
|
426
|
+
message,
|
|
427
|
+
context,
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
},
|
|
431
|
+
warn: (message, context) => {
|
|
432
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").sendLoggingMessage({
|
|
433
|
+
level: "warning",
|
|
434
|
+
data: {
|
|
435
|
+
message,
|
|
436
|
+
context,
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
},
|
|
440
|
+
};
|
|
441
|
+
const maybeStringResult = await tool.execute(args, {
|
|
442
|
+
reportProgress,
|
|
443
|
+
log,
|
|
444
|
+
});
|
|
445
|
+
if (typeof maybeStringResult === "string") {
|
|
446
|
+
result = ContentResultZodSchema.parse({
|
|
447
|
+
content: [{ type: "text", text: maybeStringResult }],
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
else if ("type" in maybeStringResult) {
|
|
451
|
+
result = ContentResultZodSchema.parse({
|
|
452
|
+
content: [maybeStringResult],
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
result = ContentResultZodSchema.parse(maybeStringResult);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
catch (error) {
|
|
460
|
+
if (error instanceof UserError) {
|
|
461
|
+
return {
|
|
462
|
+
content: [{ type: "text", text: error.message }],
|
|
463
|
+
isError: true,
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
content: [{ type: "text", text: `Error: ${error}` }],
|
|
468
|
+
isError: true,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
return result;
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
setupResourceHandlers(resources) {
|
|
475
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.ListResourcesRequestSchema, async () => {
|
|
476
|
+
return {
|
|
477
|
+
resources: resources.map((resource) => {
|
|
478
|
+
return {
|
|
479
|
+
uri: resource.uri,
|
|
480
|
+
name: resource.name,
|
|
481
|
+
mimeType: resource.mimeType,
|
|
482
|
+
};
|
|
483
|
+
}),
|
|
484
|
+
};
|
|
485
|
+
});
|
|
486
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.ReadResourceRequestSchema, async (request) => {
|
|
487
|
+
if ("uri" in request.params) {
|
|
488
|
+
const resource = resources.find((resource) => "uri" in resource && resource.uri === request.params.uri);
|
|
489
|
+
if (!resource) {
|
|
490
|
+
for (const resourceTemplate of __classPrivateFieldGet(this, _FastMCPSession_resourceTemplates, "f")) {
|
|
491
|
+
const uriTemplate = (0, uri_templates_1.default)(resourceTemplate.uriTemplate);
|
|
492
|
+
const match = uriTemplate.fromUri(request.params.uri);
|
|
493
|
+
if (!match) {
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const uri = uriTemplate.fill(match);
|
|
497
|
+
const result = await resourceTemplate.load(match);
|
|
498
|
+
return {
|
|
499
|
+
contents: [
|
|
500
|
+
{
|
|
501
|
+
uri: uri,
|
|
502
|
+
mimeType: resourceTemplate.mimeType,
|
|
503
|
+
name: resourceTemplate.name,
|
|
504
|
+
...result,
|
|
505
|
+
},
|
|
506
|
+
],
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.MethodNotFound, `Unknown resource: ${request.params.uri}`);
|
|
510
|
+
}
|
|
511
|
+
if (!("uri" in resource)) {
|
|
512
|
+
throw new UnexpectedStateError("Resource does not support reading");
|
|
513
|
+
}
|
|
514
|
+
let maybeArrayResult;
|
|
515
|
+
try {
|
|
516
|
+
maybeArrayResult = await resource.load();
|
|
517
|
+
}
|
|
518
|
+
catch (error) {
|
|
519
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InternalError, `Error reading resource: ${error}`, {
|
|
520
|
+
uri: resource.uri,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
if (Array.isArray(maybeArrayResult)) {
|
|
524
|
+
return {
|
|
525
|
+
contents: maybeArrayResult.map((result) => ({
|
|
526
|
+
uri: resource.uri,
|
|
527
|
+
mimeType: resource.mimeType,
|
|
528
|
+
name: resource.name,
|
|
529
|
+
...result,
|
|
530
|
+
})),
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
return {
|
|
535
|
+
contents: [
|
|
536
|
+
{
|
|
537
|
+
uri: resource.uri,
|
|
538
|
+
mimeType: resource.mimeType,
|
|
539
|
+
name: resource.name,
|
|
540
|
+
...maybeArrayResult,
|
|
541
|
+
},
|
|
542
|
+
],
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
throw new UnexpectedStateError("Unknown resource request", {
|
|
547
|
+
request,
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
setupResourceTemplateHandlers(resourceTemplates) {
|
|
552
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.ListResourceTemplatesRequestSchema, async () => {
|
|
553
|
+
return {
|
|
554
|
+
resourceTemplates: resourceTemplates.map((resourceTemplate) => {
|
|
555
|
+
return {
|
|
556
|
+
name: resourceTemplate.name,
|
|
557
|
+
uriTemplate: resourceTemplate.uriTemplate,
|
|
558
|
+
};
|
|
559
|
+
}),
|
|
560
|
+
};
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
setupPromptHandlers(prompts) {
|
|
564
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.ListPromptsRequestSchema, async () => {
|
|
565
|
+
return {
|
|
566
|
+
prompts: prompts.map((prompt) => {
|
|
567
|
+
return {
|
|
568
|
+
name: prompt.name,
|
|
569
|
+
description: prompt.description,
|
|
570
|
+
arguments: prompt.arguments,
|
|
571
|
+
complete: prompt.complete,
|
|
572
|
+
};
|
|
573
|
+
}),
|
|
574
|
+
};
|
|
575
|
+
});
|
|
576
|
+
__classPrivateFieldGet(this, _FastMCPSession_server, "f").setRequestHandler(types_js_1.GetPromptRequestSchema, async (request) => {
|
|
577
|
+
var _a;
|
|
578
|
+
const prompt = prompts.find((prompt) => prompt.name === request.params.name);
|
|
579
|
+
if (!prompt) {
|
|
580
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.MethodNotFound, `Unknown prompt: ${request.params.name}`);
|
|
581
|
+
}
|
|
582
|
+
const args = request.params.arguments;
|
|
583
|
+
for (const arg of (_a = prompt.arguments) !== null && _a !== void 0 ? _a : []) {
|
|
584
|
+
if (arg.required && !(args && arg.name in args)) {
|
|
585
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, `Missing required argument: ${arg.name}`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
let result;
|
|
589
|
+
try {
|
|
590
|
+
result = await prompt.load(args);
|
|
591
|
+
}
|
|
592
|
+
catch (error) {
|
|
593
|
+
throw new types_js_1.McpError(types_js_1.ErrorCode.InternalError, `Error loading prompt: ${error}`);
|
|
594
|
+
}
|
|
595
|
+
return {
|
|
596
|
+
description: prompt.description,
|
|
597
|
+
messages: [
|
|
598
|
+
{
|
|
599
|
+
role: "user",
|
|
600
|
+
content: { type: "text", text: result },
|
|
601
|
+
},
|
|
602
|
+
],
|
|
603
|
+
};
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
exports.FastMCPSession = FastMCPSession;
|
|
608
|
+
_FastMCPSession_capabilities = new WeakMap(), _FastMCPSession_clientCapabilities = new WeakMap(), _FastMCPSession_loggingLevel = new WeakMap(), _FastMCPSession_prompts = new WeakMap(), _FastMCPSession_resources = new WeakMap(), _FastMCPSession_resourceTemplates = new WeakMap(), _FastMCPSession_roots = new WeakMap(), _FastMCPSession_server = new WeakMap(), _FastMCPSession_pingInterval = new WeakMap();
|
|
609
|
+
const FastMCPEventEmitterBase = events_1.EventEmitter;
|
|
610
|
+
class FastMCPEventEmitter extends FastMCPEventEmitterBase {
|
|
611
|
+
}
|
|
612
|
+
class ToolBox extends FastMCPEventEmitter {
|
|
613
|
+
constructor(options) {
|
|
614
|
+
super();
|
|
615
|
+
this.options = options;
|
|
616
|
+
_ToolBox_options.set(this, void 0);
|
|
617
|
+
_ToolBox_prompts.set(this, []);
|
|
618
|
+
_ToolBox_resources.set(this, []);
|
|
619
|
+
_ToolBox_resourcesTemplates.set(this, []);
|
|
620
|
+
_ToolBox_sessions.set(this, []);
|
|
621
|
+
_ToolBox_sseServer.set(this, null);
|
|
622
|
+
_ToolBox_tools.set(this, []);
|
|
623
|
+
__classPrivateFieldSet(this, _ToolBox_options, options, "f");
|
|
624
|
+
}
|
|
625
|
+
get sessions() {
|
|
626
|
+
return __classPrivateFieldGet(this, _ToolBox_sessions, "f");
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Adds a tool to the server.
|
|
630
|
+
*/
|
|
631
|
+
addTool(tool) {
|
|
632
|
+
__classPrivateFieldGet(this, _ToolBox_tools, "f").push(tool);
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Adds a resource to the server.
|
|
636
|
+
*/
|
|
637
|
+
addResource(resource) {
|
|
638
|
+
__classPrivateFieldGet(this, _ToolBox_resources, "f").push(resource);
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Adds a resource template to the server.
|
|
642
|
+
*/
|
|
643
|
+
addResourceTemplate(resource) {
|
|
644
|
+
__classPrivateFieldGet(this, _ToolBox_resourcesTemplates, "f").push(resource);
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Adds a prompt to the server.
|
|
648
|
+
*/
|
|
649
|
+
addPrompt(prompt) {
|
|
650
|
+
__classPrivateFieldGet(this, _ToolBox_prompts, "f").push(prompt);
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Starts the server.
|
|
654
|
+
*/
|
|
655
|
+
async activate(options = {
|
|
656
|
+
transportType: "stdio",
|
|
657
|
+
}) {
|
|
658
|
+
if (options.transportType === "stdio") {
|
|
659
|
+
const transport = new stdio_js_1.StdioServerTransport();
|
|
660
|
+
// console.log("tools", this.#tools);
|
|
661
|
+
const session = new FastMCPSession({
|
|
662
|
+
name: __classPrivateFieldGet(this, _ToolBox_options, "f").name,
|
|
663
|
+
version: __classPrivateFieldGet(this, _ToolBox_options, "f").version,
|
|
664
|
+
tools: __classPrivateFieldGet(this, _ToolBox_tools, "f"),
|
|
665
|
+
resources: __classPrivateFieldGet(this, _ToolBox_resources, "f"),
|
|
666
|
+
resourcesTemplates: __classPrivateFieldGet(this, _ToolBox_resourcesTemplates, "f"),
|
|
667
|
+
prompts: __classPrivateFieldGet(this, _ToolBox_prompts, "f"),
|
|
668
|
+
});
|
|
669
|
+
// console.log("session", session);
|
|
670
|
+
await session.connect(transport);
|
|
671
|
+
__classPrivateFieldGet(this, _ToolBox_sessions, "f").push(session);
|
|
672
|
+
this.emit("connect", {
|
|
673
|
+
session,
|
|
674
|
+
});
|
|
675
|
+
console.info(`server is running on stdio`);
|
|
676
|
+
}
|
|
677
|
+
else if (options.transportType === "sse") {
|
|
678
|
+
// this.#sseServer = await startSSEServer<FastMCPSession>({
|
|
679
|
+
// endpoint: options.sse.endpoint as `/${string}`,
|
|
680
|
+
// port: options.sse.port,
|
|
681
|
+
// createServer: async () => {
|
|
682
|
+
// return new FastMCPSession({
|
|
683
|
+
// name: this.#options.name,
|
|
684
|
+
// version: this.#options.version,
|
|
685
|
+
// tools: this.#tools,
|
|
686
|
+
// resources: this.#resources,
|
|
687
|
+
// resourcesTemplates: this.#resourcesTemplates,
|
|
688
|
+
// prompts: this.#prompts,
|
|
689
|
+
// });
|
|
690
|
+
// },
|
|
691
|
+
// onClose: (session) => {
|
|
692
|
+
// this.emit("disconnect", {
|
|
693
|
+
// session,
|
|
694
|
+
// });
|
|
695
|
+
// },
|
|
696
|
+
// onConnect: async (session) => {
|
|
697
|
+
// this.#sessions.push(session);
|
|
698
|
+
// this.emit("connect", {
|
|
699
|
+
// session,
|
|
700
|
+
// });
|
|
701
|
+
// },
|
|
702
|
+
// });
|
|
703
|
+
// console.error(
|
|
704
|
+
// `server is running on SSE at http://localhost:${options.sse.port}${options.sse.endpoint}`,
|
|
705
|
+
// );
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
throw new Error("Invalid transport type");
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Stops the server.
|
|
713
|
+
*/
|
|
714
|
+
async stop() {
|
|
715
|
+
if (__classPrivateFieldGet(this, _ToolBox_sseServer, "f")) {
|
|
716
|
+
__classPrivateFieldGet(this, _ToolBox_sseServer, "f").close();
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
exports.ToolBox = ToolBox;
|
|
721
|
+
_ToolBox_options = new WeakMap(), _ToolBox_prompts = new WeakMap(), _ToolBox_resources = new WeakMap(), _ToolBox_resourcesTemplates = new WeakMap(), _ToolBox_sessions = new WeakMap(), _ToolBox_sseServer = new WeakMap(), _ToolBox_tools = new WeakMap();
|