mcp-use 1.11.0-canary.9 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/{chunk-N3DO4P2L.js → chunk-A4QJRN7Z.js} +2 -2
- package/dist/{chunk-UCPSHMNO.js → chunk-B7AGEK7F.js} +1 -1
- package/dist/{chunk-HU2DGJ5J.js → chunk-GN5HOAV3.js} +491 -5
- package/dist/{chunk-D3CNYAYE.js → chunk-QPIDKGV4.js} +199 -8
- package/dist/{chunk-ZFZPZ4GE.js → chunk-V77WS6CS.js} +9 -0
- package/dist/{chunk-5QFJZ7H3.js → chunk-VRHAF2WT.js} +10 -4
- package/dist/{chunk-F3BZFJCD.js → chunk-Y2HHHJQB.js} +10 -5
- package/dist/{chunk-Q5LZL6BH.js → chunk-ZLZOOXMJ.js} +96 -43
- package/dist/index.cjs +122 -47
- package/dist/index.js +15 -19
- package/dist/src/agents/index.cjs +108 -44
- package/dist/src/agents/index.d.ts +1 -1
- package/dist/src/agents/index.d.ts.map +1 -1
- package/dist/src/agents/index.js +7 -11
- package/dist/src/agents/mcp_agent.d.ts.map +1 -1
- package/dist/src/{client/prompts.d.ts → agents/prompts/index.d.ts} +3 -3
- package/dist/src/agents/prompts/index.d.ts.map +1 -0
- package/dist/src/browser.cjs +115 -45
- package/dist/src/browser.js +10 -13
- package/dist/src/client/browser.d.ts.map +1 -1
- package/dist/src/client.cjs +108 -43
- package/dist/src/client.d.ts +2 -0
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +6 -5
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/connectors/http.d.ts +2 -0
- package/dist/src/connectors/http.d.ts.map +1 -1
- package/dist/src/react/index.cjs +119 -46
- package/dist/src/react/index.js +7 -8
- package/dist/src/react/useMcp.d.ts.map +1 -1
- package/dist/src/server/index.cjs +178 -113
- package/dist/src/server/index.js +92 -80
- package/dist/src/server/types/widget.d.ts +2 -2
- package/dist/src/server/types/widget.d.ts.map +1 -1
- package/dist/src/server/widgets/mount-widgets-dev.d.ts.map +1 -1
- package/dist/src/server/widgets/ui-resource-registration.d.ts.map +1 -1
- package/dist/src/telemetry/telemetry.d.ts +1 -0
- package/dist/src/telemetry/telemetry.d.ts.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/{tool-execution-helpers-MXVN6YNU.js → tool-execution-helpers-ZUA5D5IO.js} +2 -2
- package/package.json +49 -52
- package/dist/chunk-ESMOFYJ6.js +0 -2638
- package/dist/chunk-M7WATKYM.js +0 -204
- package/dist/chunk-OWPXM4QQ.js +0 -12
- package/dist/chunk-WW3A2EKQ.js +0 -1055
- package/dist/chunk-XEFWIBQF.js +0 -491
- package/dist/src/client/prompts.cjs +0 -407
- package/dist/src/client/prompts.d.ts.map +0 -1
- package/dist/src/client/prompts.js +0 -11
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MCPClient
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-QPIDKGV4.js";
|
|
4
4
|
import {
|
|
5
5
|
LangChainAdapter
|
|
6
6
|
} from "./chunk-MFSO5PUW.js";
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
Telemetry,
|
|
9
9
|
extractModelInfo,
|
|
10
10
|
getPackageVersion
|
|
11
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-ZLZOOXMJ.js";
|
|
12
12
|
import {
|
|
13
13
|
logger
|
|
14
14
|
} from "./chunk-FRUZDWXH.js";
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Tel
|
|
6
|
-
} from "./chunk-Q5LZL6BH.js";
|
|
2
|
+
Tel,
|
|
3
|
+
Telemetry
|
|
4
|
+
} from "./chunk-ZLZOOXMJ.js";
|
|
7
5
|
import {
|
|
8
6
|
logger
|
|
9
7
|
} from "./chunk-FRUZDWXH.js";
|
|
@@ -412,6 +410,484 @@ var BaseMCPClient = class {
|
|
|
412
410
|
}
|
|
413
411
|
};
|
|
414
412
|
|
|
413
|
+
// src/connectors/base.ts
|
|
414
|
+
import {
|
|
415
|
+
ListRootsRequestSchema,
|
|
416
|
+
CreateMessageRequestSchema,
|
|
417
|
+
ElicitRequestSchema
|
|
418
|
+
} from "@mcp-use/modelcontextprotocol-sdk/types.js";
|
|
419
|
+
var BaseConnector = class {
|
|
420
|
+
static {
|
|
421
|
+
__name(this, "BaseConnector");
|
|
422
|
+
}
|
|
423
|
+
client = null;
|
|
424
|
+
connectionManager = null;
|
|
425
|
+
toolsCache = null;
|
|
426
|
+
capabilitiesCache = null;
|
|
427
|
+
serverInfoCache = null;
|
|
428
|
+
connected = false;
|
|
429
|
+
opts;
|
|
430
|
+
notificationHandlers = [];
|
|
431
|
+
rootsCache = [];
|
|
432
|
+
constructor(opts = {}) {
|
|
433
|
+
this.opts = opts;
|
|
434
|
+
if (opts.roots) {
|
|
435
|
+
this.rootsCache = [...opts.roots];
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Track connector initialization event
|
|
440
|
+
* Should be called by subclasses after successful connection
|
|
441
|
+
*/
|
|
442
|
+
trackConnectorInit(data) {
|
|
443
|
+
const connectorType = this.constructor.name;
|
|
444
|
+
Telemetry.getInstance().trackConnectorInit({
|
|
445
|
+
connectorType,
|
|
446
|
+
...data
|
|
447
|
+
}).catch((e) => logger.debug(`Failed to track connector init: ${e}`));
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Register a handler for server notifications
|
|
451
|
+
*
|
|
452
|
+
* @param handler - Function to call when a notification is received
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```typescript
|
|
456
|
+
* connector.onNotification((notification) => {
|
|
457
|
+
* console.log(`Received: ${notification.method}`, notification.params);
|
|
458
|
+
* });
|
|
459
|
+
* ```
|
|
460
|
+
*/
|
|
461
|
+
onNotification(handler) {
|
|
462
|
+
this.notificationHandlers.push(handler);
|
|
463
|
+
if (this.client) {
|
|
464
|
+
this.setupNotificationHandler();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Internal: wire notification handlers to the SDK client
|
|
469
|
+
* Includes automatic handling for list_changed notifications per MCP spec
|
|
470
|
+
*/
|
|
471
|
+
setupNotificationHandler() {
|
|
472
|
+
if (!this.client) return;
|
|
473
|
+
this.client.fallbackNotificationHandler = async (notification) => {
|
|
474
|
+
switch (notification.method) {
|
|
475
|
+
case "notifications/tools/list_changed":
|
|
476
|
+
await this.refreshToolsCache();
|
|
477
|
+
break;
|
|
478
|
+
case "notifications/resources/list_changed":
|
|
479
|
+
await this.onResourcesListChanged();
|
|
480
|
+
break;
|
|
481
|
+
case "notifications/prompts/list_changed":
|
|
482
|
+
await this.onPromptsListChanged();
|
|
483
|
+
break;
|
|
484
|
+
default:
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
for (const handler of this.notificationHandlers) {
|
|
488
|
+
try {
|
|
489
|
+
await handler(notification);
|
|
490
|
+
} catch (err) {
|
|
491
|
+
logger.error("Error in notification handler:", err);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Auto-refresh tools cache when server sends tools/list_changed notification
|
|
498
|
+
*/
|
|
499
|
+
async refreshToolsCache() {
|
|
500
|
+
if (!this.client) return;
|
|
501
|
+
try {
|
|
502
|
+
logger.debug(
|
|
503
|
+
"[Auto] Refreshing tools cache due to list_changed notification"
|
|
504
|
+
);
|
|
505
|
+
const result = await this.client.listTools();
|
|
506
|
+
this.toolsCache = result.tools ?? [];
|
|
507
|
+
logger.debug(
|
|
508
|
+
`[Auto] Refreshed tools cache: ${this.toolsCache.length} tools`
|
|
509
|
+
);
|
|
510
|
+
} catch (err) {
|
|
511
|
+
logger.warn("[Auto] Failed to refresh tools cache:", err);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Called when server sends resources/list_changed notification
|
|
516
|
+
* Resources aren't cached by default, but we log for user awareness
|
|
517
|
+
*/
|
|
518
|
+
async onResourcesListChanged() {
|
|
519
|
+
logger.debug(
|
|
520
|
+
"[Auto] Resources list changed - clients should re-fetch if needed"
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Called when server sends prompts/list_changed notification
|
|
525
|
+
* Prompts aren't cached by default, but we log for user awareness
|
|
526
|
+
*/
|
|
527
|
+
async onPromptsListChanged() {
|
|
528
|
+
logger.debug(
|
|
529
|
+
"[Auto] Prompts list changed - clients should re-fetch if needed"
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Set roots and notify the server.
|
|
534
|
+
* Roots represent directories or files that the client has access to.
|
|
535
|
+
*
|
|
536
|
+
* @param roots - Array of Root objects with `uri` (must start with "file://") and optional `name`
|
|
537
|
+
*
|
|
538
|
+
* @example
|
|
539
|
+
* ```typescript
|
|
540
|
+
* await connector.setRoots([
|
|
541
|
+
* { uri: "file:///home/user/project", name: "My Project" },
|
|
542
|
+
* { uri: "file:///home/user/data" }
|
|
543
|
+
* ]);
|
|
544
|
+
* ```
|
|
545
|
+
*/
|
|
546
|
+
async setRoots(roots) {
|
|
547
|
+
this.rootsCache = [...roots];
|
|
548
|
+
if (this.client) {
|
|
549
|
+
logger.debug(
|
|
550
|
+
`Sending roots/list_changed notification with ${roots.length} root(s)`
|
|
551
|
+
);
|
|
552
|
+
await this.client.sendRootsListChanged();
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Get the current roots.
|
|
557
|
+
*/
|
|
558
|
+
getRoots() {
|
|
559
|
+
return [...this.rootsCache];
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Internal: set up roots/list request handler.
|
|
563
|
+
* This is called after the client connects to register the handler for server requests.
|
|
564
|
+
*/
|
|
565
|
+
setupRootsHandler() {
|
|
566
|
+
if (!this.client) return;
|
|
567
|
+
this.client.setRequestHandler(
|
|
568
|
+
ListRootsRequestSchema,
|
|
569
|
+
async (_request, _extra) => {
|
|
570
|
+
logger.debug(
|
|
571
|
+
`Server requested roots list, returning ${this.rootsCache.length} root(s)`
|
|
572
|
+
);
|
|
573
|
+
return { roots: this.rootsCache };
|
|
574
|
+
}
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Internal: set up sampling/createMessage request handler.
|
|
579
|
+
* This is called after the client connects to register the handler for sampling requests.
|
|
580
|
+
*/
|
|
581
|
+
setupSamplingHandler() {
|
|
582
|
+
if (!this.client) {
|
|
583
|
+
logger.debug("setupSamplingHandler: No client available");
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (!this.opts.samplingCallback) {
|
|
587
|
+
logger.debug("setupSamplingHandler: No sampling callback provided");
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
logger.debug("setupSamplingHandler: Setting up sampling request handler");
|
|
591
|
+
this.client.setRequestHandler(
|
|
592
|
+
CreateMessageRequestSchema,
|
|
593
|
+
async (request, _extra) => {
|
|
594
|
+
logger.debug("Server requested sampling, forwarding to callback");
|
|
595
|
+
return await this.opts.samplingCallback(request.params);
|
|
596
|
+
}
|
|
597
|
+
);
|
|
598
|
+
logger.debug(
|
|
599
|
+
"setupSamplingHandler: Sampling handler registered successfully"
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Internal: set up elicitation/create request handler.
|
|
604
|
+
* This is called after the client connects to register the handler for elicitation requests.
|
|
605
|
+
*/
|
|
606
|
+
setupElicitationHandler() {
|
|
607
|
+
if (!this.client) {
|
|
608
|
+
logger.debug("setupElicitationHandler: No client available");
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (!this.opts.elicitationCallback) {
|
|
612
|
+
logger.debug("setupElicitationHandler: No elicitation callback provided");
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
logger.debug(
|
|
616
|
+
"setupElicitationHandler: Setting up elicitation request handler"
|
|
617
|
+
);
|
|
618
|
+
this.client.setRequestHandler(
|
|
619
|
+
ElicitRequestSchema,
|
|
620
|
+
async (request, _extra) => {
|
|
621
|
+
logger.debug("Server requested elicitation, forwarding to callback");
|
|
622
|
+
return await this.opts.elicitationCallback(request.params);
|
|
623
|
+
}
|
|
624
|
+
);
|
|
625
|
+
logger.debug(
|
|
626
|
+
"setupElicitationHandler: Elicitation handler registered successfully"
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
/** Disconnect and release resources. */
|
|
630
|
+
async disconnect() {
|
|
631
|
+
if (!this.connected) {
|
|
632
|
+
logger.debug("Not connected to MCP implementation");
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
logger.debug("Disconnecting from MCP implementation");
|
|
636
|
+
await this.cleanupResources();
|
|
637
|
+
this.connected = false;
|
|
638
|
+
logger.debug("Disconnected from MCP implementation");
|
|
639
|
+
}
|
|
640
|
+
/** Check if the client is connected */
|
|
641
|
+
get isClientConnected() {
|
|
642
|
+
return this.client != null;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Initialise the MCP session **after** `connect()` has succeeded.
|
|
646
|
+
*
|
|
647
|
+
* In the SDK, `Client.connect(transport)` automatically performs the
|
|
648
|
+
* protocol‑level `initialize` handshake, so we only need to cache the list of
|
|
649
|
+
* tools and expose some server info.
|
|
650
|
+
*/
|
|
651
|
+
async initialize(defaultRequestOptions = this.opts.defaultRequestOptions ?? {}) {
|
|
652
|
+
if (!this.client) {
|
|
653
|
+
throw new Error("MCP client is not connected");
|
|
654
|
+
}
|
|
655
|
+
logger.debug("Caching server capabilities & tools");
|
|
656
|
+
const capabilities = this.client.getServerCapabilities();
|
|
657
|
+
this.capabilitiesCache = capabilities || null;
|
|
658
|
+
const serverInfo = this.client.getServerVersion();
|
|
659
|
+
this.serverInfoCache = serverInfo || null;
|
|
660
|
+
const listToolsRes = await this.client.listTools(
|
|
661
|
+
void 0,
|
|
662
|
+
defaultRequestOptions
|
|
663
|
+
);
|
|
664
|
+
this.toolsCache = listToolsRes.tools ?? [];
|
|
665
|
+
logger.debug(`Fetched ${this.toolsCache.length} tools from server`);
|
|
666
|
+
logger.debug("Server capabilities:", capabilities);
|
|
667
|
+
logger.debug("Server info:", serverInfo);
|
|
668
|
+
return capabilities;
|
|
669
|
+
}
|
|
670
|
+
/** Lazily expose the cached tools list. */
|
|
671
|
+
get tools() {
|
|
672
|
+
if (!this.toolsCache) {
|
|
673
|
+
throw new Error("MCP client is not initialized; call initialize() first");
|
|
674
|
+
}
|
|
675
|
+
return this.toolsCache;
|
|
676
|
+
}
|
|
677
|
+
/** Expose cached server capabilities. */
|
|
678
|
+
get serverCapabilities() {
|
|
679
|
+
return this.capabilitiesCache || {};
|
|
680
|
+
}
|
|
681
|
+
/** Expose cached server info. */
|
|
682
|
+
get serverInfo() {
|
|
683
|
+
return this.serverInfoCache;
|
|
684
|
+
}
|
|
685
|
+
/** Call a tool on the server. */
|
|
686
|
+
async callTool(name, args, options) {
|
|
687
|
+
if (!this.client) {
|
|
688
|
+
throw new Error("MCP client is not connected");
|
|
689
|
+
}
|
|
690
|
+
const enhancedOptions = options ? { ...options } : void 0;
|
|
691
|
+
if (enhancedOptions?.resetTimeoutOnProgress && !enhancedOptions.onprogress) {
|
|
692
|
+
enhancedOptions.onprogress = () => {
|
|
693
|
+
};
|
|
694
|
+
logger.debug(
|
|
695
|
+
`[BaseConnector] Added onprogress callback for tool '${name}' to enable progressToken`
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
logger.debug(`Calling tool '${name}' with args`, args);
|
|
699
|
+
const res = await this.client.callTool(
|
|
700
|
+
{ name, arguments: args },
|
|
701
|
+
void 0,
|
|
702
|
+
enhancedOptions
|
|
703
|
+
);
|
|
704
|
+
logger.debug(`Tool '${name}' returned`, res);
|
|
705
|
+
return res;
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* List all available tools from the MCP server.
|
|
709
|
+
* This method fetches fresh tools from the server, unlike the `tools` getter which returns cached tools.
|
|
710
|
+
*
|
|
711
|
+
* @param options - Optional request options
|
|
712
|
+
* @returns Array of available tools
|
|
713
|
+
*/
|
|
714
|
+
async listTools(options) {
|
|
715
|
+
if (!this.client) {
|
|
716
|
+
throw new Error("MCP client is not connected");
|
|
717
|
+
}
|
|
718
|
+
const result = await this.client.listTools(void 0, options);
|
|
719
|
+
return result.tools ?? [];
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* List resources from the server with optional pagination
|
|
723
|
+
*
|
|
724
|
+
* @param cursor - Optional cursor for pagination
|
|
725
|
+
* @param options - Request options
|
|
726
|
+
* @returns Resource list with optional nextCursor for pagination
|
|
727
|
+
*/
|
|
728
|
+
async listResources(cursor, options) {
|
|
729
|
+
if (!this.client) {
|
|
730
|
+
throw new Error("MCP client is not connected");
|
|
731
|
+
}
|
|
732
|
+
logger.debug("Listing resources", cursor ? `with cursor: ${cursor}` : "");
|
|
733
|
+
return await this.client.listResources({ cursor }, options);
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* List all resources from the server, automatically handling pagination
|
|
737
|
+
*
|
|
738
|
+
* @param options - Request options
|
|
739
|
+
* @returns Complete list of all resources
|
|
740
|
+
*/
|
|
741
|
+
async listAllResources(options) {
|
|
742
|
+
if (!this.client) {
|
|
743
|
+
throw new Error("MCP client is not connected");
|
|
744
|
+
}
|
|
745
|
+
if (!this.capabilitiesCache?.resources) {
|
|
746
|
+
logger.debug("Server does not advertise resources capability, skipping");
|
|
747
|
+
return { resources: [] };
|
|
748
|
+
}
|
|
749
|
+
try {
|
|
750
|
+
logger.debug("Listing all resources (with auto-pagination)");
|
|
751
|
+
const allResources = [];
|
|
752
|
+
let cursor = void 0;
|
|
753
|
+
do {
|
|
754
|
+
const result = await this.client.listResources({ cursor }, options);
|
|
755
|
+
allResources.push(...result.resources || []);
|
|
756
|
+
cursor = result.nextCursor;
|
|
757
|
+
} while (cursor);
|
|
758
|
+
return { resources: allResources };
|
|
759
|
+
} catch (err) {
|
|
760
|
+
const error = err;
|
|
761
|
+
if (error.code === -32601) {
|
|
762
|
+
logger.debug("Server advertised resources but method not found");
|
|
763
|
+
return { resources: [] };
|
|
764
|
+
}
|
|
765
|
+
throw err;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* List resource templates from the server
|
|
770
|
+
*
|
|
771
|
+
* @param options - Request options
|
|
772
|
+
* @returns List of available resource templates
|
|
773
|
+
*/
|
|
774
|
+
async listResourceTemplates(options) {
|
|
775
|
+
if (!this.client) {
|
|
776
|
+
throw new Error("MCP client is not connected");
|
|
777
|
+
}
|
|
778
|
+
logger.debug("Listing resource templates");
|
|
779
|
+
return await this.client.listResourceTemplates(void 0, options);
|
|
780
|
+
}
|
|
781
|
+
/** Read a resource by URI. */
|
|
782
|
+
async readResource(uri, options) {
|
|
783
|
+
if (!this.client) {
|
|
784
|
+
throw new Error("MCP client is not connected");
|
|
785
|
+
}
|
|
786
|
+
logger.debug(`Reading resource ${uri}`);
|
|
787
|
+
const res = await this.client.readResource({ uri }, options);
|
|
788
|
+
return res;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Subscribe to resource updates
|
|
792
|
+
*
|
|
793
|
+
* @param uri - URI of the resource to subscribe to
|
|
794
|
+
* @param options - Request options
|
|
795
|
+
*/
|
|
796
|
+
async subscribeToResource(uri, options) {
|
|
797
|
+
if (!this.client) {
|
|
798
|
+
throw new Error("MCP client is not connected");
|
|
799
|
+
}
|
|
800
|
+
logger.debug(`Subscribing to resource: ${uri}`);
|
|
801
|
+
return await this.client.subscribeResource({ uri }, options);
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Unsubscribe from resource updates
|
|
805
|
+
*
|
|
806
|
+
* @param uri - URI of the resource to unsubscribe from
|
|
807
|
+
* @param options - Request options
|
|
808
|
+
*/
|
|
809
|
+
async unsubscribeFromResource(uri, options) {
|
|
810
|
+
if (!this.client) {
|
|
811
|
+
throw new Error("MCP client is not connected");
|
|
812
|
+
}
|
|
813
|
+
logger.debug(`Unsubscribing from resource: ${uri}`);
|
|
814
|
+
return await this.client.unsubscribeResource({ uri }, options);
|
|
815
|
+
}
|
|
816
|
+
async listPrompts() {
|
|
817
|
+
if (!this.client) {
|
|
818
|
+
throw new Error("MCP client is not connected");
|
|
819
|
+
}
|
|
820
|
+
if (!this.capabilitiesCache?.prompts) {
|
|
821
|
+
logger.debug("Server does not advertise prompts capability, skipping");
|
|
822
|
+
return { prompts: [] };
|
|
823
|
+
}
|
|
824
|
+
try {
|
|
825
|
+
logger.debug("Listing prompts");
|
|
826
|
+
return await this.client.listPrompts();
|
|
827
|
+
} catch (err) {
|
|
828
|
+
const error = err;
|
|
829
|
+
if (error.code === -32601) {
|
|
830
|
+
logger.debug("Server advertised prompts but method not found");
|
|
831
|
+
return { prompts: [] };
|
|
832
|
+
}
|
|
833
|
+
throw err;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
async getPrompt(name, args) {
|
|
837
|
+
if (!this.client) {
|
|
838
|
+
throw new Error("MCP client is not connected");
|
|
839
|
+
}
|
|
840
|
+
logger.debug(`Getting prompt ${name}`);
|
|
841
|
+
return await this.client.getPrompt({ name, arguments: args });
|
|
842
|
+
}
|
|
843
|
+
/** Send a raw request through the client. */
|
|
844
|
+
async request(method, params = null, options) {
|
|
845
|
+
if (!this.client) {
|
|
846
|
+
throw new Error("MCP client is not connected");
|
|
847
|
+
}
|
|
848
|
+
logger.debug(`Sending raw request '${method}' with params`, params);
|
|
849
|
+
return await this.client.request(
|
|
850
|
+
{ method, params: params ?? {} },
|
|
851
|
+
void 0,
|
|
852
|
+
options
|
|
853
|
+
);
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Helper to tear down the client & connection manager safely.
|
|
857
|
+
*/
|
|
858
|
+
async cleanupResources() {
|
|
859
|
+
const issues = [];
|
|
860
|
+
if (this.client) {
|
|
861
|
+
try {
|
|
862
|
+
if (typeof this.client.close === "function") {
|
|
863
|
+
await this.client.close();
|
|
864
|
+
}
|
|
865
|
+
} catch (e) {
|
|
866
|
+
const msg = `Error closing client: ${e}`;
|
|
867
|
+
logger.warn(msg);
|
|
868
|
+
issues.push(msg);
|
|
869
|
+
} finally {
|
|
870
|
+
this.client = null;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
if (this.connectionManager) {
|
|
874
|
+
try {
|
|
875
|
+
await this.connectionManager.stop();
|
|
876
|
+
} catch (e) {
|
|
877
|
+
const msg = `Error stopping connection manager: ${e}`;
|
|
878
|
+
logger.warn(msg);
|
|
879
|
+
issues.push(msg);
|
|
880
|
+
} finally {
|
|
881
|
+
this.connectionManager = null;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
this.toolsCache = null;
|
|
885
|
+
if (issues.length) {
|
|
886
|
+
logger.warn(`Resource cleanup finished with ${issues.length} issue(s)`);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
|
|
415
891
|
// src/connectors/http.ts
|
|
416
892
|
import { Client } from "@mcp-use/modelcontextprotocol-sdk/client/index.js";
|
|
417
893
|
import {
|
|
@@ -635,6 +1111,7 @@ var HttpConnector = class extends BaseConnector {
|
|
|
635
1111
|
sseReadTimeout;
|
|
636
1112
|
clientInfo;
|
|
637
1113
|
preferSse;
|
|
1114
|
+
disableSseFallback;
|
|
638
1115
|
transportType = null;
|
|
639
1116
|
streamableTransport = null;
|
|
640
1117
|
constructor(baseUrl, opts = {}) {
|
|
@@ -651,6 +1128,7 @@ var HttpConnector = class extends BaseConnector {
|
|
|
651
1128
|
version: "1.0.0"
|
|
652
1129
|
};
|
|
653
1130
|
this.preferSse = opts.preferSse ?? false;
|
|
1131
|
+
this.disableSseFallback = opts.disableSseFallback ?? false;
|
|
654
1132
|
}
|
|
655
1133
|
/** Establish connection to the MCP implementation via HTTP (streamable or SSE). */
|
|
656
1134
|
async connect() {
|
|
@@ -707,6 +1185,13 @@ var HttpConnector = class extends BaseConnector {
|
|
|
707
1185
|
authError.code = 401;
|
|
708
1186
|
throw authError;
|
|
709
1187
|
}
|
|
1188
|
+
if (this.disableSseFallback) {
|
|
1189
|
+
logger.info("SSE fallback disabled - failing connection");
|
|
1190
|
+
await this.cleanupResources();
|
|
1191
|
+
throw new Error(
|
|
1192
|
+
`Streamable HTTP connection failed: ${fallbackReason}. SSE fallback is disabled.`
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
710
1195
|
logger.info("\u{1F504} Falling back to SSE transport...");
|
|
711
1196
|
try {
|
|
712
1197
|
await this.connectWithSse(baseUrl);
|
|
@@ -900,6 +1385,7 @@ var HttpConnector = class extends BaseConnector {
|
|
|
900
1385
|
export {
|
|
901
1386
|
MCPSession,
|
|
902
1387
|
BaseMCPClient,
|
|
1388
|
+
BaseConnector,
|
|
903
1389
|
ConnectionManager,
|
|
904
1390
|
HttpConnector
|
|
905
1391
|
};
|