mcp-use 1.34.2 → 1.34.3

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.
Files changed (41) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/{chunk-QNMWBM3V.js → chunk-4GUL52TF.js} +1 -1
  3. package/dist/{chunk-3L7F47SI.js → chunk-BJIP3NFE.js} +4 -4
  4. package/dist/{chunk-Z3NHDOLS.js → chunk-CQPVGLM4.js} +1 -1
  5. package/dist/{chunk-P2JIVJY6.js → chunk-CXHJETCQ.js} +4 -4
  6. package/dist/{chunk-VJAQ2A7N.js → chunk-GTLSCTCO.js} +1 -1
  7. package/dist/{chunk-EJOL74UE.js → chunk-J67NWN5H.js} +1 -1
  8. package/dist/{chunk-6DJM56C3.js → chunk-KWYZMUVB.js} +2 -2
  9. package/dist/chunk-KX7P6L43.js +171 -0
  10. package/dist/{chunk-VT4KQHJI.js → chunk-MCYFETK3.js} +2 -2
  11. package/dist/{chunk-DENFIIV2.js → chunk-NJPLVA2I.js} +1 -1
  12. package/dist/chunk-NQYSWKWJ.js +2609 -0
  13. package/dist/{chunk-OYFQOSSW.js → chunk-NYIMA6MZ.js} +1 -1
  14. package/dist/chunk-PJB2MFKZ.js +954 -0
  15. package/dist/chunk-PSD6ATRG.js +562 -0
  16. package/dist/{chunk-T3QVQIYU.js → chunk-QLKY4PK3.js} +5 -5
  17. package/dist/chunk-SUVUANZ3.js +594 -0
  18. package/dist/chunk-XHP35F4S.js +204 -0
  19. package/dist/chunk-ZLHL3BY6.js +2690 -0
  20. package/dist/{client-24ODJASB.js → client-VKWZPQCL.js} +4 -4
  21. package/dist/index.cjs +1 -1
  22. package/dist/index.js +6 -6
  23. package/dist/src/agents/index.cjs +1 -1
  24. package/dist/src/agents/index.js +4 -4
  25. package/dist/src/browser-agent.cjs +1 -1
  26. package/dist/src/browser-agent.js +2 -2
  27. package/dist/src/browser.cjs +1 -1
  28. package/dist/src/browser.js +4 -4
  29. package/dist/src/client.cjs +1 -1
  30. package/dist/src/client.js +4 -4
  31. package/dist/src/react/index.cjs +1 -1
  32. package/dist/src/react/index.js +4 -4
  33. package/dist/src/server/index.cjs +1 -1
  34. package/dist/src/server/index.js +7 -7
  35. package/dist/src/version.d.ts +1 -1
  36. package/dist/{stdio-CG6YESIU.js → stdio-37X7ETM7.js} +3 -3
  37. package/dist/{stdio-KZLWEEYB.js → stdio-CSGVA5IJ.js} +2 -2
  38. package/dist/stdio-FUBSRH66.js +13 -0
  39. package/dist/{tool-execution-helpers-GVP22F32.js → tool-execution-helpers-4LIAFMYM.js} +2 -2
  40. package/dist/tool-execution-helpers-HPVG5WVB.js +27 -0
  41. package/package.json +3 -3
@@ -0,0 +1,562 @@
1
+ import {
2
+ Telemetry
3
+ } from "./chunk-BJIP3NFE.js";
4
+ import {
5
+ logger
6
+ } from "./chunk-QWQYAQCK.js";
7
+ import {
8
+ __name
9
+ } from "./chunk-3GQAWCBQ.js";
10
+
11
+ // src/connectors/base.ts
12
+ import {
13
+ CreateMessageRequestSchema,
14
+ ElicitRequestSchema,
15
+ ListRootsRequestSchema
16
+ } from "@modelcontextprotocol/sdk/types.js";
17
+ var BaseConnector = class {
18
+ static {
19
+ __name(this, "BaseConnector");
20
+ }
21
+ client = null;
22
+ connectionManager = null;
23
+ toolsCache = null;
24
+ capabilitiesCache = null;
25
+ serverInfoCache = null;
26
+ connected = false;
27
+ opts;
28
+ notificationHandlers = [];
29
+ rootsCache = [];
30
+ constructor(opts = {}) {
31
+ const finalOpts = {
32
+ ...opts,
33
+ onSampling: opts.onSampling ?? opts.samplingCallback,
34
+ onElicitation: opts.onElicitation ?? opts.elicitationCallback
35
+ };
36
+ if (opts.samplingCallback && !opts.onSampling) {
37
+ logger.warn(
38
+ '[BaseConnector] The "samplingCallback" option is deprecated. Use "onSampling" instead.'
39
+ );
40
+ }
41
+ if (opts.elicitationCallback && !opts.onElicitation) {
42
+ console.warn(
43
+ '[BaseConnector] The "elicitationCallback" option is deprecated. Use "onElicitation" instead.'
44
+ );
45
+ }
46
+ this.opts = finalOpts;
47
+ if (finalOpts.roots) {
48
+ this.rootsCache = [...finalOpts.roots];
49
+ }
50
+ if (finalOpts.onNotification) {
51
+ this.notificationHandlers.push(finalOpts.onNotification);
52
+ }
53
+ }
54
+ /**
55
+ * Track connector initialization event
56
+ * Should be called by subclasses after successful connection
57
+ */
58
+ trackConnectorInit(data) {
59
+ const connectorType = this.constructor.name;
60
+ Telemetry.getInstance().trackConnectorInit({
61
+ connectorType,
62
+ ...data
63
+ }).catch((e) => logger.debug(`Failed to track connector init: ${e}`));
64
+ }
65
+ /**
66
+ * Register a handler for server notifications
67
+ *
68
+ * @param handler - Function to call when a notification is received
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * connector.onNotification((notification) => {
73
+ * console.log(`Received: ${notification.method}`, notification.params);
74
+ * });
75
+ * ```
76
+ */
77
+ onNotification(handler) {
78
+ this.notificationHandlers.push(handler);
79
+ if (this.client) {
80
+ this.setupNotificationHandler();
81
+ }
82
+ }
83
+ /**
84
+ * Internal: wire notification handlers to the SDK client
85
+ * Includes automatic handling for list_changed notifications per MCP spec
86
+ */
87
+ setupNotificationHandler() {
88
+ if (!this.client) return;
89
+ const forwardToUserHandlers = /* @__PURE__ */ __name(async (notification) => {
90
+ for (const handler of this.notificationHandlers) {
91
+ try {
92
+ await handler(notification);
93
+ } catch (err) {
94
+ logger.error("Error in notification handler:", err);
95
+ }
96
+ }
97
+ }, "forwardToUserHandlers");
98
+ this.client.fallbackNotificationHandler = async (notification) => {
99
+ switch (notification.method) {
100
+ case "notifications/tools/list_changed":
101
+ await this.refreshToolsCache();
102
+ break;
103
+ case "notifications/resources/list_changed":
104
+ await this.onResourcesListChanged();
105
+ break;
106
+ case "notifications/prompts/list_changed":
107
+ await this.onPromptsListChanged();
108
+ break;
109
+ default:
110
+ break;
111
+ }
112
+ await forwardToUserHandlers(notification);
113
+ };
114
+ const client = this.client;
115
+ const handlersMap = client._notificationHandlers;
116
+ for (const method of [
117
+ "notifications/progress",
118
+ "notifications/cancelled"
119
+ ]) {
120
+ const originalHandler = handlersMap.get(method);
121
+ if (originalHandler) {
122
+ handlersMap.set(method, async (notification) => {
123
+ await originalHandler(notification);
124
+ await forwardToUserHandlers(notification);
125
+ });
126
+ }
127
+ }
128
+ }
129
+ /**
130
+ * Auto-refresh tools cache when server sends tools/list_changed notification
131
+ */
132
+ async refreshToolsCache() {
133
+ if (!this.client) return;
134
+ try {
135
+ logger.debug(
136
+ "[Auto] Refreshing tools cache due to list_changed notification"
137
+ );
138
+ const result = await this.client.listTools();
139
+ this.toolsCache = result.tools ?? [];
140
+ logger.debug(
141
+ `[Auto] Refreshed tools cache: ${this.toolsCache.length} tools`
142
+ );
143
+ } catch (err) {
144
+ logger.warn("[Auto] Failed to refresh tools cache:", err);
145
+ }
146
+ }
147
+ /**
148
+ * Called when server sends resources/list_changed notification
149
+ * Resources aren't cached by default, but we log for user awareness
150
+ */
151
+ async onResourcesListChanged() {
152
+ logger.debug(
153
+ "[Auto] Resources list changed - clients should re-fetch if needed"
154
+ );
155
+ }
156
+ /**
157
+ * Called when server sends prompts/list_changed notification
158
+ * Prompts aren't cached by default, but we log for user awareness
159
+ */
160
+ async onPromptsListChanged() {
161
+ logger.debug(
162
+ "[Auto] Prompts list changed - clients should re-fetch if needed"
163
+ );
164
+ }
165
+ /**
166
+ * Set roots and notify the server.
167
+ * Roots represent directories or files that the client has access to.
168
+ *
169
+ * @param roots - Array of Root objects with `uri` (must start with "file://") and optional `name`
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * await connector.setRoots([
174
+ * { uri: "file:///home/user/project", name: "My Project" },
175
+ * { uri: "file:///home/user/data" }
176
+ * ]);
177
+ * ```
178
+ */
179
+ async setRoots(roots) {
180
+ this.rootsCache = [...roots];
181
+ if (this.client) {
182
+ logger.debug(
183
+ `Sending roots/list_changed notification with ${roots.length} root(s)`
184
+ );
185
+ await this.client.sendRootsListChanged();
186
+ }
187
+ }
188
+ /**
189
+ * Get the current roots.
190
+ */
191
+ getRoots() {
192
+ return [...this.rootsCache];
193
+ }
194
+ /**
195
+ * Internal: set up roots/list request handler.
196
+ * This is called after the client connects to register the handler for server requests.
197
+ */
198
+ setupRootsHandler() {
199
+ if (!this.client) return;
200
+ this.client.setRequestHandler(
201
+ ListRootsRequestSchema,
202
+ async (_request, _extra) => {
203
+ logger.debug(
204
+ `Server requested roots list, returning ${this.rootsCache.length} root(s)`
205
+ );
206
+ return { roots: this.rootsCache };
207
+ }
208
+ );
209
+ }
210
+ /**
211
+ * Internal: set up sampling/createMessage request handler.
212
+ * This is called after the client connects to register the handler for sampling requests.
213
+ */
214
+ setupSamplingHandler() {
215
+ if (!this.client) {
216
+ logger.debug("setupSamplingHandler: No client available");
217
+ return;
218
+ }
219
+ const samplingCallback = this.opts.onSampling ?? this.opts.samplingCallback;
220
+ if (!samplingCallback) {
221
+ logger.debug("setupSamplingHandler: No sampling callback provided");
222
+ return;
223
+ }
224
+ logger.debug("setupSamplingHandler: Setting up sampling request handler");
225
+ this.client.setRequestHandler(
226
+ CreateMessageRequestSchema,
227
+ async (request, _extra) => {
228
+ logger.debug("Server requested sampling, forwarding to callback");
229
+ return await samplingCallback(request.params);
230
+ }
231
+ );
232
+ logger.debug(
233
+ "setupSamplingHandler: Sampling handler registered successfully"
234
+ );
235
+ }
236
+ /**
237
+ * Internal: set up elicitation/create request handler.
238
+ * This is called after the client connects to register the handler for elicitation requests.
239
+ */
240
+ setupElicitationHandler() {
241
+ if (!this.client) {
242
+ logger.debug("setupElicitationHandler: No client available");
243
+ return;
244
+ }
245
+ const elicitationCallback = this.opts.onElicitation ?? this.opts.elicitationCallback;
246
+ if (!elicitationCallback) {
247
+ logger.debug("setupElicitationHandler: No elicitation callback provided");
248
+ return;
249
+ }
250
+ logger.debug(
251
+ "setupElicitationHandler: Setting up elicitation request handler"
252
+ );
253
+ this.client.setRequestHandler(
254
+ ElicitRequestSchema,
255
+ async (request, _extra) => {
256
+ logger.debug("Server requested elicitation, forwarding to callback");
257
+ return await elicitationCallback(request.params);
258
+ }
259
+ );
260
+ logger.debug(
261
+ "setupElicitationHandler: Elicitation handler registered successfully"
262
+ );
263
+ }
264
+ /** Disconnect and release resources. */
265
+ async disconnect() {
266
+ if (!this.connected) {
267
+ logger.debug("Not connected to MCP implementation");
268
+ return;
269
+ }
270
+ logger.debug("Disconnecting from MCP implementation");
271
+ await this.cleanupResources();
272
+ this.connected = false;
273
+ logger.debug("Disconnected from MCP implementation");
274
+ }
275
+ /** Check if the client is connected */
276
+ get isClientConnected() {
277
+ return this.client != null;
278
+ }
279
+ /**
280
+ * Initialise the MCP session **after** `connect()` has succeeded.
281
+ *
282
+ * In the SDK, `Client.connect(transport)` automatically performs the
283
+ * protocol‑level `initialize` handshake, so we only need to cache the list of
284
+ * tools and expose some server info.
285
+ */
286
+ async initialize(defaultRequestOptions = this.opts.defaultRequestOptions ?? {}) {
287
+ if (!this.client) {
288
+ throw new Error("MCP client is not connected");
289
+ }
290
+ logger.debug("Caching server capabilities & tools");
291
+ const capabilities = this.client.getServerCapabilities();
292
+ this.capabilitiesCache = capabilities || null;
293
+ const serverInfo = this.client.getServerVersion();
294
+ this.serverInfoCache = serverInfo || null;
295
+ try {
296
+ const listToolsRes = await this.client.listTools(
297
+ void 0,
298
+ defaultRequestOptions
299
+ );
300
+ this.toolsCache = listToolsRes.tools ?? [];
301
+ logger.debug(`Fetched ${this.toolsCache.length} tools from server`);
302
+ } catch (err) {
303
+ const error = err;
304
+ if (error.code === -32601) {
305
+ logger.debug("Server does not implement tools/list, assuming no tools");
306
+ } else {
307
+ logger.debug("Failed to list tools, assuming empty:", error.message);
308
+ }
309
+ this.toolsCache = [];
310
+ }
311
+ logger.debug("Server capabilities:", capabilities);
312
+ logger.debug("Server info:", serverInfo);
313
+ return capabilities;
314
+ }
315
+ /** Lazily expose the cached tools list. */
316
+ get tools() {
317
+ if (!this.toolsCache) {
318
+ throw new Error("MCP client is not initialized; call initialize() first");
319
+ }
320
+ return this.toolsCache;
321
+ }
322
+ /** Expose cached server capabilities. */
323
+ get serverCapabilities() {
324
+ return this.capabilitiesCache || {};
325
+ }
326
+ /** Expose cached server info. */
327
+ get serverInfo() {
328
+ return this.serverInfoCache;
329
+ }
330
+ /** Call a tool on the server. */
331
+ async callTool(name, args, options) {
332
+ if (!this.client) {
333
+ throw new Error("MCP client is not connected");
334
+ }
335
+ const enhancedOptions = options ? { ...options } : void 0;
336
+ if (enhancedOptions?.resetTimeoutOnProgress && !enhancedOptions.onprogress) {
337
+ enhancedOptions.onprogress = () => {
338
+ };
339
+ logger.debug(
340
+ `[BaseConnector] Added onprogress callback for tool '${name}' to enable progressToken`
341
+ );
342
+ }
343
+ logger.debug(`Calling tool '${name}' with args`, args);
344
+ const res = await this.client.callTool(
345
+ { name, arguments: args },
346
+ void 0,
347
+ enhancedOptions
348
+ );
349
+ logger.debug(`Tool '${name}' returned`, res);
350
+ return res;
351
+ }
352
+ /**
353
+ * List all available tools from the MCP server.
354
+ * This method fetches fresh tools from the server, unlike the `tools` getter which returns cached tools.
355
+ *
356
+ * @param options - Optional request options
357
+ * @returns Array of available tools
358
+ */
359
+ async listTools(options) {
360
+ if (!this.client) {
361
+ throw new Error("MCP client is not connected");
362
+ }
363
+ logger.debug("[listTools] Fetching fresh tools from server...");
364
+ const result = await this.client.listTools(void 0, options);
365
+ const tools = result.tools ? [...result.tools] : [];
366
+ logger.debug(
367
+ `[listTools] Returned ${tools.length} tools:`,
368
+ tools.map((t) => t.name)
369
+ );
370
+ return tools;
371
+ }
372
+ /**
373
+ * List resources from the server with optional pagination
374
+ *
375
+ * @param cursor - Optional cursor for pagination
376
+ * @param options - Request options
377
+ * @returns Resource list with optional nextCursor for pagination
378
+ */
379
+ async listResources(cursor, options) {
380
+ if (!this.client) {
381
+ throw new Error("MCP client is not connected");
382
+ }
383
+ logger.debug("Listing resources", cursor ? `with cursor: ${cursor}` : "");
384
+ return await this.client.listResources({ cursor }, options);
385
+ }
386
+ /**
387
+ * List all resources from the server, automatically handling pagination
388
+ *
389
+ * @param options - Request options
390
+ * @returns Complete list of all resources
391
+ */
392
+ async listAllResources(options) {
393
+ if (!this.client) {
394
+ throw new Error("MCP client is not connected");
395
+ }
396
+ if (!this.capabilitiesCache?.resources) {
397
+ logger.debug("Server does not advertise resources capability, skipping");
398
+ return { resources: [] };
399
+ }
400
+ try {
401
+ logger.debug("Listing all resources (with auto-pagination)");
402
+ const allResources = [];
403
+ let cursor = void 0;
404
+ do {
405
+ const result = await this.client.listResources({ cursor }, options);
406
+ allResources.push(...result.resources || []);
407
+ cursor = result.nextCursor;
408
+ } while (cursor);
409
+ return { resources: allResources };
410
+ } catch (err) {
411
+ const error = err;
412
+ if (error.code === -32601) {
413
+ logger.debug("Server advertised resources but method not found");
414
+ return { resources: [] };
415
+ }
416
+ throw err;
417
+ }
418
+ }
419
+ /**
420
+ * List resource templates from the server
421
+ *
422
+ * @param options - Request options
423
+ * @returns List of available resource templates
424
+ */
425
+ async listResourceTemplates(options) {
426
+ if (!this.client) {
427
+ throw new Error("MCP client is not connected");
428
+ }
429
+ logger.debug("Listing resource templates");
430
+ return await this.client.listResourceTemplates(void 0, options);
431
+ }
432
+ /**
433
+ * Request completion suggestions for a prompt or resource template argument
434
+ *
435
+ * @param params - Completion request parameters
436
+ * @param options - Request options
437
+ * @returns Completion suggestions from the server
438
+ */
439
+ async complete(params, options) {
440
+ if (!this.client) {
441
+ throw new Error("MCP client is not connected");
442
+ }
443
+ logger.debug("[complete] Requesting completions for:", params.ref);
444
+ const result = await this.client.complete(params, options);
445
+ logger.debug(
446
+ `[complete] Received ${result.completion.values.length} suggestions`
447
+ );
448
+ return result;
449
+ }
450
+ /** Read a resource by URI. */
451
+ async readResource(uri, options) {
452
+ if (!this.client) {
453
+ throw new Error("MCP client is not connected");
454
+ }
455
+ logger.debug(`Reading resource ${uri}`);
456
+ const res = await this.client.readResource({ uri }, options);
457
+ return res;
458
+ }
459
+ /**
460
+ * Subscribe to resource updates
461
+ *
462
+ * @param uri - URI of the resource to subscribe to
463
+ * @param options - Request options
464
+ */
465
+ async subscribeToResource(uri, options) {
466
+ if (!this.client) {
467
+ throw new Error("MCP client is not connected");
468
+ }
469
+ logger.debug(`Subscribing to resource: ${uri}`);
470
+ return await this.client.subscribeResource({ uri }, options);
471
+ }
472
+ /**
473
+ * Unsubscribe from resource updates
474
+ *
475
+ * @param uri - URI of the resource to unsubscribe from
476
+ * @param options - Request options
477
+ */
478
+ async unsubscribeFromResource(uri, options) {
479
+ if (!this.client) {
480
+ throw new Error("MCP client is not connected");
481
+ }
482
+ logger.debug(`Unsubscribing from resource: ${uri}`);
483
+ return await this.client.unsubscribeResource({ uri }, options);
484
+ }
485
+ async listPrompts() {
486
+ if (!this.client) {
487
+ throw new Error("MCP client is not connected");
488
+ }
489
+ if (!this.capabilitiesCache?.prompts) {
490
+ logger.debug("Server does not advertise prompts capability, skipping");
491
+ return { prompts: [] };
492
+ }
493
+ try {
494
+ logger.debug("Listing prompts");
495
+ return await this.client.listPrompts();
496
+ } catch (err) {
497
+ const error = err;
498
+ if (error.code === -32601) {
499
+ logger.debug("Server advertised prompts but method not found");
500
+ return { prompts: [] };
501
+ }
502
+ throw err;
503
+ }
504
+ }
505
+ async getPrompt(name, args) {
506
+ if (!this.client) {
507
+ throw new Error("MCP client is not connected");
508
+ }
509
+ logger.debug(`Getting prompt ${name}`);
510
+ return await this.client.getPrompt({ name, arguments: args });
511
+ }
512
+ /** Send a raw request through the client. */
513
+ async request(method, params = null, options) {
514
+ if (!this.client) {
515
+ throw new Error("MCP client is not connected");
516
+ }
517
+ logger.debug(`Sending raw request '${method}' with params`, params);
518
+ return await this.client.request(
519
+ { method, params: params ?? {} },
520
+ void 0,
521
+ options
522
+ );
523
+ }
524
+ /**
525
+ * Helper to tear down the client & connection manager safely.
526
+ */
527
+ async cleanupResources() {
528
+ const issues = [];
529
+ if (this.client) {
530
+ try {
531
+ if (typeof this.client.close === "function") {
532
+ await this.client.close();
533
+ }
534
+ } catch (e) {
535
+ const msg = `Error closing client: ${e}`;
536
+ logger.warn(msg);
537
+ issues.push(msg);
538
+ } finally {
539
+ this.client = null;
540
+ }
541
+ }
542
+ if (this.connectionManager) {
543
+ try {
544
+ await this.connectionManager.stop();
545
+ } catch (e) {
546
+ const msg = `Error stopping connection manager: ${e}`;
547
+ logger.warn(msg);
548
+ issues.push(msg);
549
+ } finally {
550
+ this.connectionManager = null;
551
+ }
552
+ }
553
+ this.toolsCache = null;
554
+ if (issues.length) {
555
+ logger.warn(`Resource cleanup finished with ${issues.length} issue(s)`);
556
+ }
557
+ }
558
+ };
559
+
560
+ export {
561
+ BaseConnector
562
+ };
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  CODE_MODE_AGENT_PROMPT
3
- } from "./chunk-EJOL74UE.js";
4
- import {
5
- LangChainAdapter
6
- } from "./chunk-27JERD25.js";
3
+ } from "./chunk-J67NWN5H.js";
7
4
  import {
8
5
  Telemetry,
9
6
  extractModelInfo,
10
7
  getPackageVersion
11
- } from "./chunk-3L7F47SI.js";
8
+ } from "./chunk-BJIP3NFE.js";
9
+ import {
10
+ LangChainAdapter
11
+ } from "./chunk-27JERD25.js";
12
12
  import {
13
13
  logger
14
14
  } from "./chunk-QWQYAQCK.js";