mcp-use 1.5.0-canary.4 → 1.5.0-canary.5
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-WERYJ6PF.js → chunk-2AOGMX4T.js} +1 -1
- package/dist/{chunk-DSBKVAWD.js → chunk-2JBWOW4S.js} +152 -0
- package/dist/{chunk-UT7O4SIJ.js → chunk-BWOTID2D.js} +209 -75
- package/dist/{chunk-GPAOZN2F.js → chunk-QRABML5H.js} +15 -3
- package/dist/index.cjs +395 -84
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -10
- package/dist/src/agents/index.cjs +1 -0
- package/dist/src/agents/index.js +2 -2
- package/dist/src/browser.cjs +351 -72
- package/dist/src/browser.d.ts +2 -0
- package/dist/src/browser.d.ts.map +1 -1
- package/dist/src/browser.js +2 -2
- package/dist/src/client/browser.d.ts.map +1 -1
- package/dist/src/client/prompts.cjs +3 -0
- package/dist/src/client/prompts.js +2 -2
- package/dist/src/client.d.ts +8 -0
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/config.d.ts +2 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/connectors/base.d.ts +79 -1
- package/dist/src/connectors/base.d.ts.map +1 -1
- package/dist/src/connectors/http.d.ts +1 -0
- package/dist/src/connectors/http.d.ts.map +1 -1
- package/dist/src/connectors/stdio.d.ts.map +1 -1
- package/dist/src/connectors/websocket.d.ts +6 -0
- package/dist/src/connectors/websocket.d.ts.map +1 -1
- package/dist/src/react/index.cjs +365 -73
- package/dist/src/react/index.js +3 -3
- package/dist/src/react/types.d.ts +9 -1
- package/dist/src/react/types.d.ts.map +1 -1
- package/dist/src/react/useMcp.d.ts.map +1 -1
- package/dist/src/server/adapters/mcp-ui-adapter.d.ts +1 -0
- package/dist/src/server/adapters/mcp-ui-adapter.d.ts.map +1 -1
- package/dist/src/server/index.cjs +432 -27
- package/dist/src/server/index.js +432 -27
- package/dist/src/server/mcp-server.d.ts +179 -2
- package/dist/src/server/mcp-server.d.ts.map +1 -1
- package/dist/src/server/types/common.d.ts +2 -0
- package/dist/src/server/types/common.d.ts.map +1 -1
- package/dist/src/session.d.ts +40 -1
- package/dist/src/session.d.ts.map +1 -1
- package/package.json +10 -4
package/dist/index.cjs
CHANGED
|
@@ -3899,6 +3899,51 @@ var MCPSession = class {
|
|
|
3899
3899
|
get isConnected() {
|
|
3900
3900
|
return this.connector && this.connector.isClientConnected;
|
|
3901
3901
|
}
|
|
3902
|
+
/**
|
|
3903
|
+
* Register an event handler for session events
|
|
3904
|
+
*
|
|
3905
|
+
* @param event - The event type to listen for
|
|
3906
|
+
* @param handler - The handler function to call when the event occurs
|
|
3907
|
+
*
|
|
3908
|
+
* @example
|
|
3909
|
+
* ```typescript
|
|
3910
|
+
* session.on("notification", async (notification) => {
|
|
3911
|
+
* console.log(`Received: ${notification.method}`, notification.params);
|
|
3912
|
+
*
|
|
3913
|
+
* if (notification.method === "notifications/tools/list_changed") {
|
|
3914
|
+
* // Refresh tools list
|
|
3915
|
+
* }
|
|
3916
|
+
* });
|
|
3917
|
+
* ```
|
|
3918
|
+
*/
|
|
3919
|
+
on(event, handler) {
|
|
3920
|
+
if (event === "notification") {
|
|
3921
|
+
this.connector.onNotification(handler);
|
|
3922
|
+
}
|
|
3923
|
+
}
|
|
3924
|
+
/**
|
|
3925
|
+
* Set roots and notify the server.
|
|
3926
|
+
* Roots represent directories or files that the client has access to.
|
|
3927
|
+
*
|
|
3928
|
+
* @param roots - Array of Root objects with `uri` (must start with "file://") and optional `name`
|
|
3929
|
+
*
|
|
3930
|
+
* @example
|
|
3931
|
+
* ```typescript
|
|
3932
|
+
* await session.setRoots([
|
|
3933
|
+
* { uri: "file:///home/user/project", name: "My Project" },
|
|
3934
|
+
* { uri: "file:///home/user/data" }
|
|
3935
|
+
* ]);
|
|
3936
|
+
* ```
|
|
3937
|
+
*/
|
|
3938
|
+
async setRoots(roots) {
|
|
3939
|
+
return this.connector.setRoots(roots);
|
|
3940
|
+
}
|
|
3941
|
+
/**
|
|
3942
|
+
* Get the current roots.
|
|
3943
|
+
*/
|
|
3944
|
+
getRoots() {
|
|
3945
|
+
return this.connector.getRoots();
|
|
3946
|
+
}
|
|
3902
3947
|
};
|
|
3903
3948
|
|
|
3904
3949
|
// src/client/base.ts
|
|
@@ -4655,6 +4700,7 @@ var VMCodeExecutor = class extends BaseCodeExecutor {
|
|
|
4655
4700
|
};
|
|
4656
4701
|
|
|
4657
4702
|
// src/connectors/base.ts
|
|
4703
|
+
var import_types = require("@modelcontextprotocol/sdk/types.js");
|
|
4658
4704
|
init_logging();
|
|
4659
4705
|
var BaseConnector = class {
|
|
4660
4706
|
static {
|
|
@@ -4667,8 +4713,156 @@ var BaseConnector = class {
|
|
|
4667
4713
|
serverInfoCache = null;
|
|
4668
4714
|
connected = false;
|
|
4669
4715
|
opts;
|
|
4716
|
+
notificationHandlers = [];
|
|
4717
|
+
rootsCache = [];
|
|
4670
4718
|
constructor(opts = {}) {
|
|
4671
4719
|
this.opts = opts;
|
|
4720
|
+
if (opts.roots) {
|
|
4721
|
+
this.rootsCache = [...opts.roots];
|
|
4722
|
+
}
|
|
4723
|
+
}
|
|
4724
|
+
/**
|
|
4725
|
+
* Register a handler for server notifications
|
|
4726
|
+
*
|
|
4727
|
+
* @param handler - Function to call when a notification is received
|
|
4728
|
+
*
|
|
4729
|
+
* @example
|
|
4730
|
+
* ```typescript
|
|
4731
|
+
* connector.onNotification((notification) => {
|
|
4732
|
+
* console.log(`Received: ${notification.method}`, notification.params);
|
|
4733
|
+
* });
|
|
4734
|
+
* ```
|
|
4735
|
+
*/
|
|
4736
|
+
onNotification(handler) {
|
|
4737
|
+
this.notificationHandlers.push(handler);
|
|
4738
|
+
if (this.client) {
|
|
4739
|
+
this.setupNotificationHandler();
|
|
4740
|
+
}
|
|
4741
|
+
}
|
|
4742
|
+
/**
|
|
4743
|
+
* Internal: wire notification handlers to the SDK client
|
|
4744
|
+
* Includes automatic handling for list_changed notifications per MCP spec
|
|
4745
|
+
*/
|
|
4746
|
+
setupNotificationHandler() {
|
|
4747
|
+
if (!this.client) return;
|
|
4748
|
+
this.client.fallbackNotificationHandler = async (notification) => {
|
|
4749
|
+
switch (notification.method) {
|
|
4750
|
+
case "notifications/tools/list_changed":
|
|
4751
|
+
await this.refreshToolsCache();
|
|
4752
|
+
break;
|
|
4753
|
+
case "notifications/resources/list_changed":
|
|
4754
|
+
await this.onResourcesListChanged();
|
|
4755
|
+
break;
|
|
4756
|
+
case "notifications/prompts/list_changed":
|
|
4757
|
+
await this.onPromptsListChanged();
|
|
4758
|
+
break;
|
|
4759
|
+
default:
|
|
4760
|
+
break;
|
|
4761
|
+
}
|
|
4762
|
+
for (const handler of this.notificationHandlers) {
|
|
4763
|
+
try {
|
|
4764
|
+
await handler(notification);
|
|
4765
|
+
} catch (err) {
|
|
4766
|
+
logger.error("Error in notification handler:", err);
|
|
4767
|
+
}
|
|
4768
|
+
}
|
|
4769
|
+
};
|
|
4770
|
+
}
|
|
4771
|
+
/**
|
|
4772
|
+
* Auto-refresh tools cache when server sends tools/list_changed notification
|
|
4773
|
+
*/
|
|
4774
|
+
async refreshToolsCache() {
|
|
4775
|
+
if (!this.client) return;
|
|
4776
|
+
try {
|
|
4777
|
+
logger.debug(
|
|
4778
|
+
"[Auto] Refreshing tools cache due to list_changed notification"
|
|
4779
|
+
);
|
|
4780
|
+
const result = await this.client.listTools();
|
|
4781
|
+
this.toolsCache = result.tools ?? [];
|
|
4782
|
+
logger.debug(
|
|
4783
|
+
`[Auto] Refreshed tools cache: ${this.toolsCache.length} tools`
|
|
4784
|
+
);
|
|
4785
|
+
} catch (err) {
|
|
4786
|
+
logger.warn("[Auto] Failed to refresh tools cache:", err);
|
|
4787
|
+
}
|
|
4788
|
+
}
|
|
4789
|
+
/**
|
|
4790
|
+
* Called when server sends resources/list_changed notification
|
|
4791
|
+
* Resources aren't cached by default, but we log for user awareness
|
|
4792
|
+
*/
|
|
4793
|
+
async onResourcesListChanged() {
|
|
4794
|
+
logger.debug(
|
|
4795
|
+
"[Auto] Resources list changed - clients should re-fetch if needed"
|
|
4796
|
+
);
|
|
4797
|
+
}
|
|
4798
|
+
/**
|
|
4799
|
+
* Called when server sends prompts/list_changed notification
|
|
4800
|
+
* Prompts aren't cached by default, but we log for user awareness
|
|
4801
|
+
*/
|
|
4802
|
+
async onPromptsListChanged() {
|
|
4803
|
+
logger.debug(
|
|
4804
|
+
"[Auto] Prompts list changed - clients should re-fetch if needed"
|
|
4805
|
+
);
|
|
4806
|
+
}
|
|
4807
|
+
/**
|
|
4808
|
+
* Set roots and notify the server.
|
|
4809
|
+
* Roots represent directories or files that the client has access to.
|
|
4810
|
+
*
|
|
4811
|
+
* @param roots - Array of Root objects with `uri` (must start with "file://") and optional `name`
|
|
4812
|
+
*
|
|
4813
|
+
* @example
|
|
4814
|
+
* ```typescript
|
|
4815
|
+
* await connector.setRoots([
|
|
4816
|
+
* { uri: "file:///home/user/project", name: "My Project" },
|
|
4817
|
+
* { uri: "file:///home/user/data" }
|
|
4818
|
+
* ]);
|
|
4819
|
+
* ```
|
|
4820
|
+
*/
|
|
4821
|
+
async setRoots(roots) {
|
|
4822
|
+
this.rootsCache = [...roots];
|
|
4823
|
+
if (this.client) {
|
|
4824
|
+
logger.debug(
|
|
4825
|
+
`Sending roots/list_changed notification with ${roots.length} root(s)`
|
|
4826
|
+
);
|
|
4827
|
+
await this.client.sendRootsListChanged();
|
|
4828
|
+
}
|
|
4829
|
+
}
|
|
4830
|
+
/**
|
|
4831
|
+
* Get the current roots.
|
|
4832
|
+
*/
|
|
4833
|
+
getRoots() {
|
|
4834
|
+
return [...this.rootsCache];
|
|
4835
|
+
}
|
|
4836
|
+
/**
|
|
4837
|
+
* Internal: set up roots/list request handler.
|
|
4838
|
+
* This is called after the client connects to register the handler for server requests.
|
|
4839
|
+
*/
|
|
4840
|
+
setupRootsHandler() {
|
|
4841
|
+
if (!this.client) return;
|
|
4842
|
+
this.client.setRequestHandler(
|
|
4843
|
+
import_types.ListRootsRequestSchema,
|
|
4844
|
+
async (_request, _extra) => {
|
|
4845
|
+
logger.debug(
|
|
4846
|
+
`Server requested roots list, returning ${this.rootsCache.length} root(s)`
|
|
4847
|
+
);
|
|
4848
|
+
return { roots: this.rootsCache };
|
|
4849
|
+
}
|
|
4850
|
+
);
|
|
4851
|
+
}
|
|
4852
|
+
/**
|
|
4853
|
+
* Internal: set up sampling/createMessage request handler.
|
|
4854
|
+
* This is called after the client connects to register the handler for sampling requests.
|
|
4855
|
+
*/
|
|
4856
|
+
setupSamplingHandler() {
|
|
4857
|
+
if (!this.client) return;
|
|
4858
|
+
if (!this.opts.samplingCallback) return;
|
|
4859
|
+
this.client.setRequestHandler(
|
|
4860
|
+
import_types.CreateMessageRequestSchema,
|
|
4861
|
+
async (request, _extra) => {
|
|
4862
|
+
logger.debug("Server requested sampling, forwarding to callback");
|
|
4863
|
+
return await this.opts.samplingCallback(request.params);
|
|
4864
|
+
}
|
|
4865
|
+
);
|
|
4672
4866
|
}
|
|
4673
4867
|
/** Disconnect and release resources. */
|
|
4674
4868
|
async disconnect() {
|
|
@@ -5106,7 +5300,7 @@ var import_node_fs = require("fs");
|
|
|
5106
5300
|
|
|
5107
5301
|
// src/connectors/http.ts
|
|
5108
5302
|
var import_client = require("@modelcontextprotocol/sdk/client/index.js");
|
|
5109
|
-
var
|
|
5303
|
+
var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
5110
5304
|
init_logging();
|
|
5111
5305
|
|
|
5112
5306
|
// src/task_managers/sse.ts
|
|
@@ -5274,58 +5468,6 @@ var SseConnectionManager = class extends ConnectionManager {
|
|
|
5274
5468
|
}
|
|
5275
5469
|
};
|
|
5276
5470
|
|
|
5277
|
-
// src/task_managers/streamable_http.ts
|
|
5278
|
-
var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
5279
|
-
init_logging();
|
|
5280
|
-
var StreamableHttpConnectionManager = class extends ConnectionManager {
|
|
5281
|
-
static {
|
|
5282
|
-
__name(this, "StreamableHttpConnectionManager");
|
|
5283
|
-
}
|
|
5284
|
-
url;
|
|
5285
|
-
opts;
|
|
5286
|
-
_transport = null;
|
|
5287
|
-
/**
|
|
5288
|
-
* Create a Streamable HTTP connection manager.
|
|
5289
|
-
*
|
|
5290
|
-
* @param url The HTTP endpoint URL.
|
|
5291
|
-
* @param opts Optional transport options (auth, headers, etc.).
|
|
5292
|
-
*/
|
|
5293
|
-
constructor(url, opts) {
|
|
5294
|
-
super();
|
|
5295
|
-
this.url = typeof url === "string" ? new URL(url) : url;
|
|
5296
|
-
this.opts = opts;
|
|
5297
|
-
}
|
|
5298
|
-
/**
|
|
5299
|
-
* Spawn a new `StreamableHTTPClientTransport` and return it.
|
|
5300
|
-
* The Client.connect() method will handle starting the transport.
|
|
5301
|
-
*/
|
|
5302
|
-
async establishConnection() {
|
|
5303
|
-
this._transport = new import_streamableHttp.StreamableHTTPClientTransport(this.url, this.opts);
|
|
5304
|
-
logger.debug(`${this.constructor.name} created successfully`);
|
|
5305
|
-
return this._transport;
|
|
5306
|
-
}
|
|
5307
|
-
/**
|
|
5308
|
-
* Close the underlying transport and clean up resources.
|
|
5309
|
-
*/
|
|
5310
|
-
async closeConnection(_connection) {
|
|
5311
|
-
if (this._transport) {
|
|
5312
|
-
try {
|
|
5313
|
-
await this._transport.close();
|
|
5314
|
-
} catch (e) {
|
|
5315
|
-
logger.warn(`Error closing Streamable HTTP transport: ${e}`);
|
|
5316
|
-
} finally {
|
|
5317
|
-
this._transport = null;
|
|
5318
|
-
}
|
|
5319
|
-
}
|
|
5320
|
-
}
|
|
5321
|
-
/**
|
|
5322
|
-
* Get the session ID from the transport if available.
|
|
5323
|
-
*/
|
|
5324
|
-
get sessionId() {
|
|
5325
|
-
return this._transport?.sessionId;
|
|
5326
|
-
}
|
|
5327
|
-
};
|
|
5328
|
-
|
|
5329
5471
|
// src/connectors/http.ts
|
|
5330
5472
|
var HttpConnector = class extends BaseConnector {
|
|
5331
5473
|
static {
|
|
@@ -5338,6 +5480,7 @@ var HttpConnector = class extends BaseConnector {
|
|
|
5338
5480
|
clientInfo;
|
|
5339
5481
|
preferSse;
|
|
5340
5482
|
transportType = null;
|
|
5483
|
+
streamableTransport = null;
|
|
5341
5484
|
constructor(baseUrl, opts = {}) {
|
|
5342
5485
|
super(opts);
|
|
5343
5486
|
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
@@ -5373,7 +5516,7 @@ var HttpConnector = class extends BaseConnector {
|
|
|
5373
5516
|
} catch (err) {
|
|
5374
5517
|
let fallbackReason = "Unknown error";
|
|
5375
5518
|
let is401Error = false;
|
|
5376
|
-
if (err instanceof
|
|
5519
|
+
if (err instanceof import_streamableHttp.StreamableHTTPError) {
|
|
5377
5520
|
is401Error = err.code === 401;
|
|
5378
5521
|
if (err.code === 400 && err.message.includes("Missing session ID")) {
|
|
5379
5522
|
fallbackReason = "Server requires session ID (FastMCP compatibility) - using SSE transport";
|
|
@@ -5429,34 +5572,67 @@ var HttpConnector = class extends BaseConnector {
|
|
|
5429
5572
|
}
|
|
5430
5573
|
async connectWithStreamableHttp(baseUrl) {
|
|
5431
5574
|
try {
|
|
5432
|
-
|
|
5433
|
-
|
|
5434
|
-
|
|
5435
|
-
|
|
5436
|
-
|
|
5437
|
-
|
|
5438
|
-
|
|
5439
|
-
|
|
5440
|
-
|
|
5441
|
-
|
|
5442
|
-
|
|
5443
|
-
|
|
5575
|
+
const streamableTransport = new import_streamableHttp.StreamableHTTPClientTransport(
|
|
5576
|
+
new URL(baseUrl),
|
|
5577
|
+
{
|
|
5578
|
+
authProvider: this.opts.authProvider,
|
|
5579
|
+
// ← Pass OAuth provider to SDK
|
|
5580
|
+
requestInit: {
|
|
5581
|
+
headers: this.headers
|
|
5582
|
+
},
|
|
5583
|
+
// Pass through reconnection options
|
|
5584
|
+
reconnectionOptions: {
|
|
5585
|
+
maxReconnectionDelay: 3e4,
|
|
5586
|
+
initialReconnectionDelay: 1e3,
|
|
5587
|
+
reconnectionDelayGrowFactor: 1.5,
|
|
5588
|
+
maxRetries: 2
|
|
5589
|
+
}
|
|
5590
|
+
// Don't pass sessionId - let the SDK generate it automatically during connect()
|
|
5444
5591
|
}
|
|
5445
|
-
|
|
5446
|
-
let transport =
|
|
5592
|
+
);
|
|
5593
|
+
let transport = streamableTransport;
|
|
5447
5594
|
if (this.opts.wrapTransport) {
|
|
5448
5595
|
const serverId = this.baseUrl;
|
|
5449
|
-
transport = this.opts.wrapTransport(
|
|
5596
|
+
transport = this.opts.wrapTransport(
|
|
5597
|
+
transport,
|
|
5598
|
+
serverId
|
|
5599
|
+
);
|
|
5450
5600
|
}
|
|
5451
|
-
|
|
5601
|
+
const clientOptions = {
|
|
5602
|
+
...this.opts.clientOptions || {},
|
|
5603
|
+
capabilities: {
|
|
5604
|
+
...this.opts.clientOptions?.capabilities || {},
|
|
5605
|
+
roots: { listChanged: true },
|
|
5606
|
+
// Always advertise roots capability
|
|
5607
|
+
// Add sampling capability if callback is provided
|
|
5608
|
+
...this.opts.samplingCallback ? { sampling: {} } : {}
|
|
5609
|
+
}
|
|
5610
|
+
};
|
|
5611
|
+
logger.debug(
|
|
5612
|
+
`Creating Client with capabilities:`,
|
|
5613
|
+
JSON.stringify(clientOptions.capabilities, null, 2)
|
|
5614
|
+
);
|
|
5615
|
+
this.client = new import_client.Client(this.clientInfo, clientOptions);
|
|
5616
|
+
this.setupRootsHandler();
|
|
5617
|
+
logger.debug("Roots handler registered before connect");
|
|
5452
5618
|
try {
|
|
5453
|
-
await this.client.connect(transport
|
|
5619
|
+
await this.client.connect(transport, {
|
|
5620
|
+
timeout: Math.min(this.timeout, 3e3)
|
|
5621
|
+
});
|
|
5622
|
+
const sessionId = streamableTransport.sessionId;
|
|
5623
|
+
if (sessionId) {
|
|
5624
|
+
logger.debug(`Session ID obtained: ${sessionId}`);
|
|
5625
|
+
} else {
|
|
5626
|
+
logger.warn(
|
|
5627
|
+
"Session ID not available after connect - this may cause issues with SSE stream"
|
|
5628
|
+
);
|
|
5629
|
+
}
|
|
5454
5630
|
} catch (connectErr) {
|
|
5455
5631
|
if (connectErr instanceof Error) {
|
|
5456
5632
|
const errMsg = connectErr.message || connectErr.toString();
|
|
5457
|
-
if (errMsg.includes("Missing session ID") || errMsg.includes("Bad Request: Missing session ID")) {
|
|
5633
|
+
if (errMsg.includes("Missing session ID") || errMsg.includes("Bad Request: Missing session ID") || errMsg.includes("Mcp-Session-Id header is required")) {
|
|
5458
5634
|
const wrappedError = new Error(
|
|
5459
|
-
`
|
|
5635
|
+
`Session ID error: ${errMsg}. The SDK should automatically extract session ID from initialize response.`
|
|
5460
5636
|
);
|
|
5461
5637
|
wrappedError.cause = connectErr;
|
|
5462
5638
|
throw wrappedError;
|
|
@@ -5464,8 +5640,24 @@ var HttpConnector = class extends BaseConnector {
|
|
|
5464
5640
|
}
|
|
5465
5641
|
throw connectErr;
|
|
5466
5642
|
}
|
|
5643
|
+
this.streamableTransport = streamableTransport;
|
|
5644
|
+
this.connectionManager = {
|
|
5645
|
+
stop: /* @__PURE__ */ __name(async () => {
|
|
5646
|
+
if (this.streamableTransport) {
|
|
5647
|
+
try {
|
|
5648
|
+
await this.streamableTransport.close();
|
|
5649
|
+
} catch (e) {
|
|
5650
|
+
logger.warn(`Error closing Streamable HTTP transport: ${e}`);
|
|
5651
|
+
} finally {
|
|
5652
|
+
this.streamableTransport = null;
|
|
5653
|
+
}
|
|
5654
|
+
}
|
|
5655
|
+
}, "stop")
|
|
5656
|
+
};
|
|
5467
5657
|
this.connected = true;
|
|
5468
5658
|
this.transportType = "streamable-http";
|
|
5659
|
+
this.setupNotificationHandler();
|
|
5660
|
+
this.setupSamplingHandler();
|
|
5469
5661
|
logger.debug(
|
|
5470
5662
|
`Successfully connected to MCP implementation via streamable HTTP: ${baseUrl}`
|
|
5471
5663
|
);
|
|
@@ -5486,10 +5678,28 @@ var HttpConnector = class extends BaseConnector {
|
|
|
5486
5678
|
const serverId = this.baseUrl;
|
|
5487
5679
|
transport = this.opts.wrapTransport(transport, serverId);
|
|
5488
5680
|
}
|
|
5489
|
-
|
|
5681
|
+
const clientOptions = {
|
|
5682
|
+
...this.opts.clientOptions || {},
|
|
5683
|
+
capabilities: {
|
|
5684
|
+
...this.opts.clientOptions?.capabilities || {},
|
|
5685
|
+
roots: { listChanged: true },
|
|
5686
|
+
// Always advertise roots capability
|
|
5687
|
+
// Add sampling capability if callback is provided
|
|
5688
|
+
...this.opts.samplingCallback ? { sampling: {} } : {}
|
|
5689
|
+
}
|
|
5690
|
+
};
|
|
5691
|
+
logger.debug(
|
|
5692
|
+
`Creating Client with capabilities (SSE):`,
|
|
5693
|
+
JSON.stringify(clientOptions.capabilities, null, 2)
|
|
5694
|
+
);
|
|
5695
|
+
this.client = new import_client.Client(this.clientInfo, clientOptions);
|
|
5696
|
+
this.setupRootsHandler();
|
|
5697
|
+
logger.debug("Roots handler registered before connect (SSE)");
|
|
5490
5698
|
await this.client.connect(transport);
|
|
5491
5699
|
this.connected = true;
|
|
5492
5700
|
this.transportType = "sse";
|
|
5701
|
+
this.setupNotificationHandler();
|
|
5702
|
+
this.setupSamplingHandler();
|
|
5493
5703
|
logger.debug(
|
|
5494
5704
|
`Successfully connected to MCP implementation via HTTP/SSE: ${baseUrl}`
|
|
5495
5705
|
);
|
|
@@ -5626,9 +5836,22 @@ var StdioConnector = class extends BaseConnector {
|
|
|
5626
5836
|
this.errlog
|
|
5627
5837
|
);
|
|
5628
5838
|
const transport = await this.connectionManager.start();
|
|
5629
|
-
|
|
5839
|
+
const clientOptions = {
|
|
5840
|
+
...this.opts.clientOptions || {},
|
|
5841
|
+
capabilities: {
|
|
5842
|
+
...this.opts.clientOptions?.capabilities || {},
|
|
5843
|
+
roots: { listChanged: true },
|
|
5844
|
+
// Always advertise roots capability
|
|
5845
|
+
// Add sampling capability if callback is provided
|
|
5846
|
+
...this.opts.samplingCallback ? { sampling: {} } : {}
|
|
5847
|
+
}
|
|
5848
|
+
};
|
|
5849
|
+
this.client = new import_client2.Client(this.clientInfo, clientOptions);
|
|
5630
5850
|
await this.client.connect(transport);
|
|
5631
5851
|
this.connected = true;
|
|
5852
|
+
this.setupNotificationHandler();
|
|
5853
|
+
this.setupRootsHandler();
|
|
5854
|
+
this.setupSamplingHandler();
|
|
5632
5855
|
logger.debug(
|
|
5633
5856
|
`Successfully connected to MCP implementation: ${this.command}`
|
|
5634
5857
|
);
|
|
@@ -5798,6 +6021,9 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
5798
6021
|
this.pending.delete(id);
|
|
5799
6022
|
if ("result" in data) resolve(data.result);
|
|
5800
6023
|
else if ("error" in data) reject(data.error);
|
|
6024
|
+
} else if (data.method && !data.id) {
|
|
6025
|
+
logger.debug("Received notification", data.method, data.params);
|
|
6026
|
+
this.handleNotification(data);
|
|
5801
6027
|
} else {
|
|
5802
6028
|
logger.debug("Received unsolicited message", data);
|
|
5803
6029
|
}
|
|
@@ -5828,6 +6054,49 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
5828
6054
|
for (const { reject } of this.pending.values()) reject(err);
|
|
5829
6055
|
this.pending.clear();
|
|
5830
6056
|
}
|
|
6057
|
+
async handleNotification(data) {
|
|
6058
|
+
switch (data.method) {
|
|
6059
|
+
case "notifications/tools/list_changed":
|
|
6060
|
+
await this.refreshToolsCache();
|
|
6061
|
+
break;
|
|
6062
|
+
case "notifications/resources/list_changed":
|
|
6063
|
+
await this.onResourcesListChanged();
|
|
6064
|
+
break;
|
|
6065
|
+
case "notifications/prompts/list_changed":
|
|
6066
|
+
await this.onPromptsListChanged();
|
|
6067
|
+
break;
|
|
6068
|
+
default:
|
|
6069
|
+
break;
|
|
6070
|
+
}
|
|
6071
|
+
for (const handler of this.notificationHandlers) {
|
|
6072
|
+
try {
|
|
6073
|
+
await handler({
|
|
6074
|
+
method: data.method,
|
|
6075
|
+
params: data.params
|
|
6076
|
+
});
|
|
6077
|
+
} catch (err) {
|
|
6078
|
+
logger.error("Error in notification handler:", err);
|
|
6079
|
+
}
|
|
6080
|
+
}
|
|
6081
|
+
}
|
|
6082
|
+
/**
|
|
6083
|
+
* Auto-refresh tools cache when server sends tools/list_changed notification
|
|
6084
|
+
* Override to use WebSocket-specific listTools method
|
|
6085
|
+
*/
|
|
6086
|
+
async refreshToolsCache() {
|
|
6087
|
+
try {
|
|
6088
|
+
logger.debug(
|
|
6089
|
+
"[Auto] Refreshing tools cache due to list_changed notification"
|
|
6090
|
+
);
|
|
6091
|
+
const tools = await this.listTools();
|
|
6092
|
+
this.toolsCache = tools.map((t) => t);
|
|
6093
|
+
logger.debug(
|
|
6094
|
+
`[Auto] Refreshed tools cache: ${this.toolsCache.length} tools`
|
|
6095
|
+
);
|
|
6096
|
+
} catch (err) {
|
|
6097
|
+
logger.warn("[Auto] Failed to refresh tools cache:", err);
|
|
6098
|
+
}
|
|
6099
|
+
}
|
|
5831
6100
|
async initialize() {
|
|
5832
6101
|
logger.debug("Initializing MCP session over WebSocket");
|
|
5833
6102
|
const result = await this.sendRequest("initialize");
|
|
@@ -5884,12 +6153,13 @@ function loadConfigFile(filepath) {
|
|
|
5884
6153
|
return JSON.parse(raw);
|
|
5885
6154
|
}
|
|
5886
6155
|
__name(loadConfigFile, "loadConfigFile");
|
|
5887
|
-
function createConnectorFromConfig(serverConfig) {
|
|
6156
|
+
function createConnectorFromConfig(serverConfig, connectorOptions) {
|
|
5888
6157
|
if ("command" in serverConfig && "args" in serverConfig) {
|
|
5889
6158
|
return new StdioConnector({
|
|
5890
6159
|
command: serverConfig.command,
|
|
5891
6160
|
args: serverConfig.args,
|
|
5892
|
-
env: serverConfig.env
|
|
6161
|
+
env: serverConfig.env,
|
|
6162
|
+
...connectorOptions
|
|
5893
6163
|
});
|
|
5894
6164
|
}
|
|
5895
6165
|
if ("url" in serverConfig) {
|
|
@@ -5898,13 +6168,15 @@ function createConnectorFromConfig(serverConfig) {
|
|
|
5898
6168
|
headers: serverConfig.headers,
|
|
5899
6169
|
authToken: serverConfig.auth_token || serverConfig.authToken,
|
|
5900
6170
|
// Only force SSE if explicitly requested
|
|
5901
|
-
preferSse: serverConfig.preferSse || transport === "sse"
|
|
6171
|
+
preferSse: serverConfig.preferSse || transport === "sse",
|
|
6172
|
+
...connectorOptions
|
|
5902
6173
|
});
|
|
5903
6174
|
}
|
|
5904
6175
|
if ("ws_url" in serverConfig) {
|
|
5905
6176
|
return new WebSocketConnector(serverConfig.ws_url, {
|
|
5906
6177
|
headers: serverConfig.headers,
|
|
5907
|
-
authToken: serverConfig.auth_token
|
|
6178
|
+
authToken: serverConfig.auth_token,
|
|
6179
|
+
...connectorOptions
|
|
5908
6180
|
});
|
|
5909
6181
|
}
|
|
5910
6182
|
throw new Error("Cannot determine connector type from config");
|
|
@@ -5922,6 +6194,7 @@ var MCPClient = class _MCPClient extends BaseMCPClient {
|
|
|
5922
6194
|
_customCodeExecutor = null;
|
|
5923
6195
|
_codeExecutorConfig = "vm";
|
|
5924
6196
|
_executorOptions;
|
|
6197
|
+
_samplingCallback;
|
|
5925
6198
|
constructor(config2, options) {
|
|
5926
6199
|
if (config2) {
|
|
5927
6200
|
if (typeof config2 === "string") {
|
|
@@ -5947,6 +6220,7 @@ var MCPClient = class _MCPClient extends BaseMCPClient {
|
|
|
5947
6220
|
this.codeMode = codeModeEnabled;
|
|
5948
6221
|
this._codeExecutorConfig = executorConfig;
|
|
5949
6222
|
this._executorOptions = executorOptions;
|
|
6223
|
+
this._samplingCallback = options?.samplingCallback;
|
|
5950
6224
|
if (this.codeMode) {
|
|
5951
6225
|
this._setupCodeModeConnector();
|
|
5952
6226
|
}
|
|
@@ -5972,7 +6246,9 @@ var MCPClient = class _MCPClient extends BaseMCPClient {
|
|
|
5972
6246
|
* Supports all connector types including StdioConnector
|
|
5973
6247
|
*/
|
|
5974
6248
|
createConnectorFromConfig(serverConfig) {
|
|
5975
|
-
return createConnectorFromConfig(serverConfig
|
|
6249
|
+
return createConnectorFromConfig(serverConfig, {
|
|
6250
|
+
samplingCallback: this._samplingCallback
|
|
6251
|
+
});
|
|
5976
6252
|
}
|
|
5977
6253
|
_setupCodeModeConnector() {
|
|
5978
6254
|
logger.debug("Code mode connector initialized as internal meta server");
|
|
@@ -6908,7 +7184,16 @@ var BrowserMCPClient = class _BrowserMCPClient extends BaseMCPClient {
|
|
|
6908
7184
|
* Supports HTTP and WebSocket connectors only
|
|
6909
7185
|
*/
|
|
6910
7186
|
createConnectorFromConfig(serverConfig) {
|
|
6911
|
-
const {
|
|
7187
|
+
const {
|
|
7188
|
+
url,
|
|
7189
|
+
transport,
|
|
7190
|
+
headers,
|
|
7191
|
+
authToken,
|
|
7192
|
+
authProvider,
|
|
7193
|
+
wrapTransport,
|
|
7194
|
+
clientOptions,
|
|
7195
|
+
samplingCallback
|
|
7196
|
+
} = serverConfig;
|
|
6912
7197
|
if (!url) {
|
|
6913
7198
|
throw new Error("Server URL is required");
|
|
6914
7199
|
}
|
|
@@ -6917,9 +7202,23 @@ var BrowserMCPClient = class _BrowserMCPClient extends BaseMCPClient {
|
|
|
6917
7202
|
authToken,
|
|
6918
7203
|
authProvider,
|
|
6919
7204
|
// ← Pass OAuth provider to connector
|
|
6920
|
-
wrapTransport
|
|
7205
|
+
wrapTransport,
|
|
6921
7206
|
// ← Pass transport wrapper if provided
|
|
7207
|
+
clientOptions,
|
|
7208
|
+
// ← Pass client options (capabilities, etc.) to connector
|
|
7209
|
+
samplingCallback
|
|
7210
|
+
// ← Pass sampling callback to connector
|
|
6922
7211
|
};
|
|
7212
|
+
if (clientOptions) {
|
|
7213
|
+
console.log(
|
|
7214
|
+
"[BrowserMCPClient] Passing clientOptions to connector:",
|
|
7215
|
+
JSON.stringify(clientOptions, null, 2)
|
|
7216
|
+
);
|
|
7217
|
+
} else {
|
|
7218
|
+
console.warn(
|
|
7219
|
+
"[BrowserMCPClient] No clientOptions provided to connector!"
|
|
7220
|
+
);
|
|
7221
|
+
}
|
|
6923
7222
|
if (transport === "websocket" || url.startsWith("ws://") || url.startsWith("wss://")) {
|
|
6924
7223
|
return new WebSocketConnector(url, connectorOptions);
|
|
6925
7224
|
} else if (transport === "http" || url.startsWith("http://") || url.startsWith("https://")) {
|
|
@@ -6964,7 +7263,9 @@ function useMcp(options) {
|
|
|
6964
7263
|
// 30 seconds default for connection timeout
|
|
6965
7264
|
sseReadTimeout = 3e5,
|
|
6966
7265
|
// 5 minutes default for SSE read timeout
|
|
6967
|
-
wrapTransport
|
|
7266
|
+
wrapTransport,
|
|
7267
|
+
onNotification,
|
|
7268
|
+
samplingCallback
|
|
6968
7269
|
} = options;
|
|
6969
7270
|
const [state, setState] = (0, import_react.useState)("discovering");
|
|
6970
7271
|
const [tools, setTools] = (0, import_react.useState)([]);
|
|
@@ -7117,6 +7418,10 @@ function useMcp(options) {
|
|
|
7117
7418
|
...serverConfig,
|
|
7118
7419
|
authProvider: authProviderRef.current,
|
|
7119
7420
|
// ← SDK handles OAuth automatically!
|
|
7421
|
+
clientOptions: clientConfig,
|
|
7422
|
+
// ← Pass client config to connector
|
|
7423
|
+
samplingCallback,
|
|
7424
|
+
// ← Pass sampling callback to connector
|
|
7120
7425
|
wrapTransport: wrapTransport ? (transport) => {
|
|
7121
7426
|
console.log(
|
|
7122
7427
|
"[useMcp] Applying transport wrapper for server:",
|
|
@@ -7127,7 +7432,13 @@ function useMcp(options) {
|
|
|
7127
7432
|
return wrapTransport(transport, url);
|
|
7128
7433
|
} : void 0
|
|
7129
7434
|
});
|
|
7130
|
-
const session = await clientRef.current.createSession(
|
|
7435
|
+
const session = await clientRef.current.createSession(
|
|
7436
|
+
serverName,
|
|
7437
|
+
false
|
|
7438
|
+
);
|
|
7439
|
+
if (onNotification) {
|
|
7440
|
+
session.on("notification", onNotification);
|
|
7441
|
+
}
|
|
7131
7442
|
await session.initialize();
|
|
7132
7443
|
addLog("info", "\u2705 Successfully connected to MCP server");
|
|
7133
7444
|
addLog("info", "Server info:", session.connector.serverInfo);
|
package/dist/index.d.ts
CHANGED
|
@@ -13,11 +13,12 @@ import { RemoteAgent } from "./src/agents/remote.js";
|
|
|
13
13
|
import { MCPClient } from "./src/client.js";
|
|
14
14
|
import { loadConfigFile } from "./src/config.js";
|
|
15
15
|
import { BaseConnector } from "./src/connectors/base.js";
|
|
16
|
+
import type { NotificationHandler } from "./src/connectors/base.js";
|
|
16
17
|
import { HttpConnector } from "./src/connectors/http.js";
|
|
17
18
|
import { StdioConnector } from "./src/connectors/stdio.js";
|
|
18
19
|
import { WebSocketConnector } from "./src/connectors/websocket.js";
|
|
19
20
|
import { Logger, logger } from "./src/logging.js";
|
|
20
|
-
import { MCPSession } from "./src/session.js";
|
|
21
|
+
import { MCPSession, Notification, Root } from "./src/session.js";
|
|
21
22
|
export { BaseAdapter, LangChainAdapter } from "./src/adapters/index.js";
|
|
22
23
|
export * from "./src/agents/utils/index.js";
|
|
23
24
|
export { ServerManager } from "./src/managers/server_manager.js";
|
|
@@ -30,7 +31,8 @@ export { BrowserOAuthClientProvider, onMcpAuthorization, } from "./src/auth/inde
|
|
|
30
31
|
export type { StoredState } from "./src/auth/types.js";
|
|
31
32
|
export * from "./src/react/index.js";
|
|
32
33
|
export { PROMPTS } from "./src/agents/index.js";
|
|
33
|
-
export { BaseConnector, HttpConnector, loadConfigFile, Logger, logger, MCPAgent, MCPClient, MCPSession, RemoteAgent, StdioConnector, WebSocketConnector, };
|
|
34
|
+
export { BaseConnector, HttpConnector, loadConfigFile, Logger, logger, MCPAgent, MCPClient, MCPSession, Notification, RemoteAgent, Root, StdioConnector, WebSocketConnector, };
|
|
35
|
+
export type { NotificationHandler };
|
|
34
36
|
export type { CodeModeConfig, E2BExecutorOptions, ExecutorOptions, MCPClientOptions, VMExecutorOptions, } from "./src/client.js";
|
|
35
37
|
export { BaseCodeExecutor, E2BCodeExecutor, VMCodeExecutor, isVMAvailable, } from "./src/client.js";
|
|
36
38
|
export type { ExecutionResult, SearchToolsFunction, ToolNamespaceInfo, ToolSearchResult, } from "./src/client/codeExecutor.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAEnE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAEnE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAElE,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAExE,cAAc,6BAA6B,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAEjE,cAAc,+BAA+B,CAAC;AAG9C,OAAO,EACL,oBAAoB,EACpB,KAAK,mBAAmB,GACzB,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAGzE,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,WAAW,GACZ,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EACV,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,WAAW,EACX,UAAU,GACX,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,0BAA0B,EAC1B,kBAAkB,GACnB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGvD,cAAc,sBAAsB,CAAC;AAGrC,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAQhD,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,EACd,MAAM,EACN,MAAM,EACN,QAAQ,EACR,SAAS,EACT,UAAU,EACV,YAAY,EACZ,WAAW,EACX,IAAI,EACJ,cAAc,EACd,kBAAkB,GACnB,CAAC;AAGF,YAAY,EAAE,mBAAmB,EAAE,CAAC;AAGpC,YAAY,EACV,cAAc,EACd,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,aAAa,GACd,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,8BAA8B,CAAC"}
|