@mcpjam/sdk 0.1.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/README.md +27 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/index.cjs +485 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +470 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp-client-manager/index.cjs +746 -0
- package/dist/mcp-client-manager/index.cjs.map +1 -0
- package/dist/mcp-client-manager/index.d.cts +1629 -0
- package/dist/mcp-client-manager/index.d.ts +1629 -0
- package/dist/mcp-client-manager/index.js +738 -0
- package/dist/mcp-client-manager/index.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// mcp-client-manager/index.ts
|
|
21
|
+
var mcp_client_manager_exports = {};
|
|
22
|
+
__export(mcp_client_manager_exports, {
|
|
23
|
+
MCPClientManager: () => MCPClientManager
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(mcp_client_manager_exports);
|
|
26
|
+
var import_client = require("@modelcontextprotocol/sdk/client/index.js");
|
|
27
|
+
var import_sse = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
28
|
+
var import_stdio = require("@modelcontextprotocol/sdk/client/stdio.js");
|
|
29
|
+
var import_streamableHttp = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
30
|
+
var import_protocol = require("@modelcontextprotocol/sdk/shared/protocol.js");
|
|
31
|
+
var import_types2 = require("@modelcontextprotocol/sdk/types.js");
|
|
32
|
+
|
|
33
|
+
// mcp-client-manager/tool-converters.ts
|
|
34
|
+
var import_types = require("@modelcontextprotocol/sdk/types.js");
|
|
35
|
+
var import_ai = require("ai");
|
|
36
|
+
var ensureJsonSchemaObject = (schema) => {
|
|
37
|
+
var _a;
|
|
38
|
+
if (schema && typeof schema === "object") {
|
|
39
|
+
const record = schema;
|
|
40
|
+
const base = record.jsonSchema ? ensureJsonSchemaObject(record.jsonSchema) : record;
|
|
41
|
+
if (!("type" in base) || base.type === void 0) {
|
|
42
|
+
base.type = "object";
|
|
43
|
+
}
|
|
44
|
+
if (base.type === "object") {
|
|
45
|
+
base.properties = (_a = base.properties) != null ? _a : {};
|
|
46
|
+
if (base.additionalProperties === void 0) {
|
|
47
|
+
base.additionalProperties = false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return base;
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
type: "object",
|
|
54
|
+
properties: {},
|
|
55
|
+
additionalProperties: false
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
async function convertMCPToolsToVercelTools(listToolsResult, {
|
|
59
|
+
schemas = "automatic",
|
|
60
|
+
callTool
|
|
61
|
+
}) {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
const tools = {};
|
|
64
|
+
for (const toolDescription of listToolsResult.tools) {
|
|
65
|
+
const { name, description, inputSchema } = toolDescription;
|
|
66
|
+
const execute = async (args, options) => {
|
|
67
|
+
var _a2, _b2;
|
|
68
|
+
(_b2 = (_a2 = options == null ? void 0 : options.abortSignal) == null ? void 0 : _a2.throwIfAborted) == null ? void 0 : _b2.call(_a2);
|
|
69
|
+
const result = await callTool({ name, args, options });
|
|
70
|
+
return import_types.CallToolResultSchema.parse(result);
|
|
71
|
+
};
|
|
72
|
+
let vercelTool;
|
|
73
|
+
if (schemas === "automatic") {
|
|
74
|
+
const normalizedInputSchema = ensureJsonSchemaObject(inputSchema);
|
|
75
|
+
vercelTool = (0, import_ai.dynamicTool)({
|
|
76
|
+
description,
|
|
77
|
+
inputSchema: (0, import_ai.jsonSchema)({
|
|
78
|
+
type: "object",
|
|
79
|
+
properties: (_a = normalizedInputSchema.properties) != null ? _a : {},
|
|
80
|
+
additionalProperties: (_b = normalizedInputSchema.additionalProperties) != null ? _b : false
|
|
81
|
+
}),
|
|
82
|
+
execute
|
|
83
|
+
});
|
|
84
|
+
} else {
|
|
85
|
+
const overrides = schemas;
|
|
86
|
+
if (!(name in overrides)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
vercelTool = (0, import_ai.tool)({
|
|
90
|
+
description,
|
|
91
|
+
inputSchema: overrides[name].inputSchema,
|
|
92
|
+
execute
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
tools[name] = vercelTool;
|
|
96
|
+
}
|
|
97
|
+
return tools;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// mcp-client-manager/index.ts
|
|
101
|
+
var MCPClientManager = class {
|
|
102
|
+
constructor(servers = {}, options = {}) {
|
|
103
|
+
this.clientStates = /* @__PURE__ */ new Map();
|
|
104
|
+
this.notificationHandlers = /* @__PURE__ */ new Map();
|
|
105
|
+
this.elicitationHandlers = /* @__PURE__ */ new Map();
|
|
106
|
+
this.toolsMetadataCache = /* @__PURE__ */ new Map();
|
|
107
|
+
this.pendingElicitations = /* @__PURE__ */ new Map();
|
|
108
|
+
var _a, _b, _c;
|
|
109
|
+
this.defaultClientVersion = (_a = options.defaultClientVersion) != null ? _a : "1.0.0";
|
|
110
|
+
this.defaultCapabilities = { ...(_b = options.defaultCapabilities) != null ? _b : {} };
|
|
111
|
+
this.defaultTimeout = (_c = options.defaultTimeout) != null ? _c : import_protocol.DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
112
|
+
for (const [id, config] of Object.entries(servers)) {
|
|
113
|
+
void this.connectToServer(id, config);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
listServers() {
|
|
117
|
+
return Array.from(this.clientStates.keys());
|
|
118
|
+
}
|
|
119
|
+
hasServer(serverId) {
|
|
120
|
+
return this.clientStates.has(serverId);
|
|
121
|
+
}
|
|
122
|
+
getServerSummaries() {
|
|
123
|
+
return Array.from(this.clientStates.entries()).map(([serverId, state]) => ({
|
|
124
|
+
id: serverId,
|
|
125
|
+
status: this.resolveConnectionStatus(state),
|
|
126
|
+
config: state.config
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
getConnectionStatus(serverId) {
|
|
130
|
+
return this.resolveConnectionStatus(this.clientStates.get(serverId));
|
|
131
|
+
}
|
|
132
|
+
getServerConfig(serverId) {
|
|
133
|
+
var _a;
|
|
134
|
+
return (_a = this.clientStates.get(serverId)) == null ? void 0 : _a.config;
|
|
135
|
+
}
|
|
136
|
+
async connectToServer(serverId, config) {
|
|
137
|
+
var _a;
|
|
138
|
+
if (this.clientStates.has(serverId)) {
|
|
139
|
+
throw new Error(`MCP server "${serverId}" is already connected.`);
|
|
140
|
+
}
|
|
141
|
+
const timeout = this.getTimeout(config);
|
|
142
|
+
const state = (_a = this.clientStates.get(serverId)) != null ? _a : {
|
|
143
|
+
config,
|
|
144
|
+
timeout
|
|
145
|
+
};
|
|
146
|
+
state.config = config;
|
|
147
|
+
state.timeout = timeout;
|
|
148
|
+
if (state.client) {
|
|
149
|
+
this.clientStates.set(serverId, state);
|
|
150
|
+
return state.client;
|
|
151
|
+
}
|
|
152
|
+
if (state.promise) {
|
|
153
|
+
this.clientStates.set(serverId, state);
|
|
154
|
+
return state.promise;
|
|
155
|
+
}
|
|
156
|
+
const connectionPromise = (async () => {
|
|
157
|
+
var _a2;
|
|
158
|
+
const client = new import_client.Client(
|
|
159
|
+
{
|
|
160
|
+
name: serverId,
|
|
161
|
+
version: (_a2 = config.version) != null ? _a2 : this.defaultClientVersion
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
capabilities: this.buildCapabilities(config)
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
this.applyNotificationHandlers(serverId, client);
|
|
168
|
+
this.applyElicitationHandler(serverId, client);
|
|
169
|
+
if (config.onError) {
|
|
170
|
+
client.onerror = (error) => {
|
|
171
|
+
var _a3;
|
|
172
|
+
(_a3 = config.onError) == null ? void 0 : _a3.call(config, error);
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
client.onclose = () => {
|
|
176
|
+
this.resetState(serverId);
|
|
177
|
+
};
|
|
178
|
+
let transport;
|
|
179
|
+
if (this.isStdioConfig(config)) {
|
|
180
|
+
transport = await this.connectViaStdio(client, config, timeout);
|
|
181
|
+
} else {
|
|
182
|
+
transport = await this.connectViaHttp(
|
|
183
|
+
serverId,
|
|
184
|
+
client,
|
|
185
|
+
config,
|
|
186
|
+
timeout
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
state.client = client;
|
|
190
|
+
state.transport = transport;
|
|
191
|
+
state.promise = void 0;
|
|
192
|
+
this.clientStates.set(serverId, state);
|
|
193
|
+
return client;
|
|
194
|
+
})().catch((error) => {
|
|
195
|
+
state.promise = void 0;
|
|
196
|
+
state.client = void 0;
|
|
197
|
+
state.transport = void 0;
|
|
198
|
+
this.clientStates.set(serverId, state);
|
|
199
|
+
throw error;
|
|
200
|
+
});
|
|
201
|
+
state.promise = connectionPromise;
|
|
202
|
+
this.clientStates.set(serverId, state);
|
|
203
|
+
return connectionPromise;
|
|
204
|
+
}
|
|
205
|
+
async disconnectServer(serverId) {
|
|
206
|
+
const client = this.getClientById(serverId);
|
|
207
|
+
try {
|
|
208
|
+
await client.close();
|
|
209
|
+
} finally {
|
|
210
|
+
if (client.transport) {
|
|
211
|
+
await this.safeCloseTransport(client.transport);
|
|
212
|
+
}
|
|
213
|
+
this.resetState(serverId);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
removeServer(serverId) {
|
|
217
|
+
this.resetState(serverId);
|
|
218
|
+
this.notificationHandlers.delete(serverId);
|
|
219
|
+
this.elicitationHandlers.delete(serverId);
|
|
220
|
+
}
|
|
221
|
+
async disconnectAllServers() {
|
|
222
|
+
const serverIds = this.listServers();
|
|
223
|
+
await Promise.all(
|
|
224
|
+
serverIds.map((serverId) => this.disconnectServer(serverId))
|
|
225
|
+
);
|
|
226
|
+
for (const serverId of serverIds) {
|
|
227
|
+
this.resetState(serverId);
|
|
228
|
+
this.notificationHandlers.delete(serverId);
|
|
229
|
+
this.elicitationHandlers.delete(serverId);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
async listTools(serverId, params, options) {
|
|
233
|
+
await this.ensureConnected(serverId);
|
|
234
|
+
const client = this.getClientById(serverId);
|
|
235
|
+
try {
|
|
236
|
+
const result = await client.listTools(
|
|
237
|
+
params,
|
|
238
|
+
this.withTimeout(serverId, options)
|
|
239
|
+
);
|
|
240
|
+
const metadataMap = /* @__PURE__ */ new Map();
|
|
241
|
+
for (const tool of result.tools) {
|
|
242
|
+
if (tool._meta) {
|
|
243
|
+
metadataMap.set(tool.name, tool._meta);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
this.toolsMetadataCache.set(serverId, metadataMap);
|
|
247
|
+
return result;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
if (this.isMethodUnavailableError(error, "tools/list")) {
|
|
250
|
+
this.toolsMetadataCache.set(serverId, /* @__PURE__ */ new Map());
|
|
251
|
+
return { tools: [] };
|
|
252
|
+
}
|
|
253
|
+
throw error;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async getTools(serverIds) {
|
|
257
|
+
const targetServerIds = serverIds && serverIds.length > 0 ? serverIds : this.listServers();
|
|
258
|
+
const toolLists = await Promise.all(
|
|
259
|
+
targetServerIds.map(async (serverId) => {
|
|
260
|
+
await this.ensureConnected(serverId);
|
|
261
|
+
const client = this.getClientById(serverId);
|
|
262
|
+
const result = await client.listTools(
|
|
263
|
+
void 0,
|
|
264
|
+
this.withTimeout(serverId)
|
|
265
|
+
);
|
|
266
|
+
const metadataMap = /* @__PURE__ */ new Map();
|
|
267
|
+
for (const tool of result.tools) {
|
|
268
|
+
if (tool._meta) {
|
|
269
|
+
metadataMap.set(tool.name, tool._meta);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
this.toolsMetadataCache.set(serverId, metadataMap);
|
|
273
|
+
return result.tools;
|
|
274
|
+
})
|
|
275
|
+
);
|
|
276
|
+
return { tools: toolLists.flat() };
|
|
277
|
+
}
|
|
278
|
+
getAllToolsMetadata(serverId) {
|
|
279
|
+
const metadataMap = this.toolsMetadataCache.get(serverId);
|
|
280
|
+
return metadataMap ? Object.fromEntries(metadataMap) : {};
|
|
281
|
+
}
|
|
282
|
+
pingServer(serverId, options) {
|
|
283
|
+
const client = this.getClientById(serverId);
|
|
284
|
+
try {
|
|
285
|
+
client.ping(options);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`Failed to ping MCP server "${serverId}": ${error instanceof Error ? error.message : "Unknown error"}`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async getToolsForAiSdk(serverIds, options = {}) {
|
|
293
|
+
const ids = Array.isArray(serverIds) ? serverIds : serverIds ? [serverIds] : this.listServers();
|
|
294
|
+
const loadForServer = async (id) => {
|
|
295
|
+
await this.ensureConnected(id);
|
|
296
|
+
const listToolsResult = await this.listTools(id);
|
|
297
|
+
return convertMCPToolsToVercelTools(listToolsResult, {
|
|
298
|
+
schemas: options.schemas,
|
|
299
|
+
callTool: async ({ name, args, options: callOptions }) => {
|
|
300
|
+
const requestOptions = (callOptions == null ? void 0 : callOptions.abortSignal) ? { signal: callOptions.abortSignal } : void 0;
|
|
301
|
+
const result = await this.executeTool(
|
|
302
|
+
id,
|
|
303
|
+
name,
|
|
304
|
+
args != null ? args : {},
|
|
305
|
+
requestOptions
|
|
306
|
+
);
|
|
307
|
+
return import_types2.CallToolResultSchema.parse(result);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
const perServerTools = await Promise.all(
|
|
312
|
+
ids.map(async (id) => {
|
|
313
|
+
try {
|
|
314
|
+
const tools = await loadForServer(id);
|
|
315
|
+
for (const [name, tool] of Object.entries(tools)) {
|
|
316
|
+
tool._serverId = id;
|
|
317
|
+
}
|
|
318
|
+
return tools;
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (this.isMethodUnavailableError(error, "tools/list")) {
|
|
321
|
+
return {};
|
|
322
|
+
}
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
const flattened = {};
|
|
328
|
+
for (const toolset of perServerTools) {
|
|
329
|
+
for (const [name, tool] of Object.entries(toolset)) {
|
|
330
|
+
flattened[name] = tool;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return flattened;
|
|
334
|
+
}
|
|
335
|
+
async executeTool(serverId, toolName, args = {}, options) {
|
|
336
|
+
await this.ensureConnected(serverId);
|
|
337
|
+
const client = this.getClientById(serverId);
|
|
338
|
+
return client.callTool(
|
|
339
|
+
{
|
|
340
|
+
name: toolName,
|
|
341
|
+
arguments: args
|
|
342
|
+
},
|
|
343
|
+
import_types2.CallToolResultSchema,
|
|
344
|
+
this.withTimeout(serverId, options)
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
async listResources(serverId, params, options) {
|
|
348
|
+
await this.ensureConnected(serverId);
|
|
349
|
+
const client = this.getClientById(serverId);
|
|
350
|
+
try {
|
|
351
|
+
return await client.listResources(
|
|
352
|
+
params,
|
|
353
|
+
this.withTimeout(serverId, options)
|
|
354
|
+
);
|
|
355
|
+
} catch (error) {
|
|
356
|
+
if (this.isMethodUnavailableError(error, "resources/list")) {
|
|
357
|
+
return {
|
|
358
|
+
resources: []
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async readResource(serverId, params, options) {
|
|
365
|
+
await this.ensureConnected(serverId);
|
|
366
|
+
const client = this.getClientById(serverId);
|
|
367
|
+
return client.readResource(params, this.withTimeout(serverId, options));
|
|
368
|
+
}
|
|
369
|
+
async subscribeResource(serverId, params, options) {
|
|
370
|
+
await this.ensureConnected(serverId);
|
|
371
|
+
const client = this.getClientById(serverId);
|
|
372
|
+
return client.subscribeResource(
|
|
373
|
+
params,
|
|
374
|
+
this.withTimeout(serverId, options)
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
async unsubscribeResource(serverId, params, options) {
|
|
378
|
+
await this.ensureConnected(serverId);
|
|
379
|
+
const client = this.getClientById(serverId);
|
|
380
|
+
return client.unsubscribeResource(
|
|
381
|
+
params,
|
|
382
|
+
this.withTimeout(serverId, options)
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
async listResourceTemplates(serverId, params, options) {
|
|
386
|
+
await this.ensureConnected(serverId);
|
|
387
|
+
const client = this.getClientById(serverId);
|
|
388
|
+
return client.listResourceTemplates(
|
|
389
|
+
params,
|
|
390
|
+
this.withTimeout(serverId, options)
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
async listPrompts(serverId, params, options) {
|
|
394
|
+
await this.ensureConnected(serverId);
|
|
395
|
+
const client = this.getClientById(serverId);
|
|
396
|
+
try {
|
|
397
|
+
return await client.listPrompts(
|
|
398
|
+
params,
|
|
399
|
+
this.withTimeout(serverId, options)
|
|
400
|
+
);
|
|
401
|
+
} catch (error) {
|
|
402
|
+
if (this.isMethodUnavailableError(error, "prompts/list")) {
|
|
403
|
+
return {
|
|
404
|
+
prompts: []
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async getPrompt(serverId, params, options) {
|
|
411
|
+
await this.ensureConnected(serverId);
|
|
412
|
+
const client = this.getClientById(serverId);
|
|
413
|
+
return client.getPrompt(params, this.withTimeout(serverId, options));
|
|
414
|
+
}
|
|
415
|
+
getSessionIdByServer(serverId) {
|
|
416
|
+
const state = this.clientStates.get(serverId);
|
|
417
|
+
if (!(state == null ? void 0 : state.transport)) {
|
|
418
|
+
throw new Error(`Unknown MCP server "${serverId}".`);
|
|
419
|
+
}
|
|
420
|
+
if (state.transport instanceof import_streamableHttp.StreamableHTTPClientTransport) {
|
|
421
|
+
return state.transport.sessionId;
|
|
422
|
+
}
|
|
423
|
+
throw new Error(
|
|
424
|
+
`Server "${serverId}" must be Streamable HTTP to get the session ID.`
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
addNotificationHandler(serverId, schema, handler) {
|
|
428
|
+
var _a, _b, _c;
|
|
429
|
+
const serverHandlers = (_a = this.notificationHandlers.get(serverId)) != null ? _a : /* @__PURE__ */ new Map();
|
|
430
|
+
const handlersForSchema = (_b = serverHandlers.get(schema)) != null ? _b : /* @__PURE__ */ new Set();
|
|
431
|
+
handlersForSchema.add(handler);
|
|
432
|
+
serverHandlers.set(schema, handlersForSchema);
|
|
433
|
+
this.notificationHandlers.set(serverId, serverHandlers);
|
|
434
|
+
const client = (_c = this.clientStates.get(serverId)) == null ? void 0 : _c.client;
|
|
435
|
+
if (client) {
|
|
436
|
+
client.setNotificationHandler(
|
|
437
|
+
schema,
|
|
438
|
+
this.createNotificationDispatcher(serverId, schema)
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
onResourceListChanged(serverId, handler) {
|
|
443
|
+
this.addNotificationHandler(
|
|
444
|
+
serverId,
|
|
445
|
+
import_types2.ResourceListChangedNotificationSchema,
|
|
446
|
+
handler
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
onResourceUpdated(serverId, handler) {
|
|
450
|
+
this.addNotificationHandler(
|
|
451
|
+
serverId,
|
|
452
|
+
import_types2.ResourceUpdatedNotificationSchema,
|
|
453
|
+
handler
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
onPromptListChanged(serverId, handler) {
|
|
457
|
+
this.addNotificationHandler(
|
|
458
|
+
serverId,
|
|
459
|
+
import_types2.PromptListChangedNotificationSchema,
|
|
460
|
+
handler
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
getClient(serverId) {
|
|
464
|
+
var _a;
|
|
465
|
+
return (_a = this.clientStates.get(serverId)) == null ? void 0 : _a.client;
|
|
466
|
+
}
|
|
467
|
+
setElicitationHandler(serverId, handler) {
|
|
468
|
+
var _a;
|
|
469
|
+
if (!this.clientStates.has(serverId)) {
|
|
470
|
+
throw new Error(`Unknown MCP server "${serverId}".`);
|
|
471
|
+
}
|
|
472
|
+
this.elicitationHandlers.set(serverId, handler);
|
|
473
|
+
const client = (_a = this.clientStates.get(serverId)) == null ? void 0 : _a.client;
|
|
474
|
+
if (client) {
|
|
475
|
+
this.applyElicitationHandler(serverId, client);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
clearElicitationHandler(serverId) {
|
|
479
|
+
var _a;
|
|
480
|
+
this.elicitationHandlers.delete(serverId);
|
|
481
|
+
const client = (_a = this.clientStates.get(serverId)) == null ? void 0 : _a.client;
|
|
482
|
+
if (client) {
|
|
483
|
+
client.removeRequestHandler("elicitation/create");
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
// Global elicitation callback API (no serverId required)
|
|
487
|
+
setElicitationCallback(callback) {
|
|
488
|
+
this.elicitationCallback = callback;
|
|
489
|
+
for (const [serverId, state] of this.clientStates.entries()) {
|
|
490
|
+
const client = state.client;
|
|
491
|
+
if (!client) continue;
|
|
492
|
+
if (this.elicitationHandlers.has(serverId)) {
|
|
493
|
+
this.applyElicitationHandler(serverId, client);
|
|
494
|
+
} else {
|
|
495
|
+
this.applyElicitationHandler(serverId, client);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
clearElicitationCallback() {
|
|
500
|
+
this.elicitationCallback = void 0;
|
|
501
|
+
for (const [serverId, state] of this.clientStates.entries()) {
|
|
502
|
+
const client = state.client;
|
|
503
|
+
if (!client) continue;
|
|
504
|
+
if (this.elicitationHandlers.has(serverId)) {
|
|
505
|
+
this.applyElicitationHandler(serverId, client);
|
|
506
|
+
} else {
|
|
507
|
+
client.removeRequestHandler("elicitation/create");
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
// Expose the pending elicitation map so callers can add resolvers
|
|
512
|
+
getPendingElicitations() {
|
|
513
|
+
return this.pendingElicitations;
|
|
514
|
+
}
|
|
515
|
+
// Helper to resolve a pending elicitation from outside
|
|
516
|
+
respondToElicitation(requestId, response) {
|
|
517
|
+
const pending = this.pendingElicitations.get(requestId);
|
|
518
|
+
if (!pending) return false;
|
|
519
|
+
try {
|
|
520
|
+
pending.resolve(response);
|
|
521
|
+
return true;
|
|
522
|
+
} finally {
|
|
523
|
+
this.pendingElicitations.delete(requestId);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
async connectViaStdio(client, config, timeout) {
|
|
527
|
+
var _a;
|
|
528
|
+
const transport = new import_stdio.StdioClientTransport({
|
|
529
|
+
command: config.command,
|
|
530
|
+
args: config.args,
|
|
531
|
+
env: { ...(0, import_stdio.getDefaultEnvironment)(), ...(_a = config.env) != null ? _a : {} }
|
|
532
|
+
});
|
|
533
|
+
await client.connect(transport, { timeout });
|
|
534
|
+
return transport;
|
|
535
|
+
}
|
|
536
|
+
async connectViaHttp(serverId, client, config, timeout) {
|
|
537
|
+
var _a;
|
|
538
|
+
const preferSSE = (_a = config.preferSSE) != null ? _a : config.url.pathname.endsWith("/sse");
|
|
539
|
+
let streamableError;
|
|
540
|
+
if (!preferSSE) {
|
|
541
|
+
const streamableTransport = new import_streamableHttp.StreamableHTTPClientTransport(
|
|
542
|
+
config.url,
|
|
543
|
+
{
|
|
544
|
+
requestInit: config.requestInit,
|
|
545
|
+
reconnectionOptions: config.reconnectionOptions,
|
|
546
|
+
authProvider: config.authProvider,
|
|
547
|
+
sessionId: config.sessionId
|
|
548
|
+
}
|
|
549
|
+
);
|
|
550
|
+
try {
|
|
551
|
+
await client.connect(streamableTransport, {
|
|
552
|
+
timeout: Math.min(timeout, 3e3)
|
|
553
|
+
});
|
|
554
|
+
return streamableTransport;
|
|
555
|
+
} catch (error) {
|
|
556
|
+
streamableError = error;
|
|
557
|
+
await this.safeCloseTransport(streamableTransport);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
const sseTransport = new import_sse.SSEClientTransport(config.url, {
|
|
561
|
+
requestInit: config.requestInit,
|
|
562
|
+
eventSourceInit: config.eventSourceInit,
|
|
563
|
+
authProvider: config.authProvider
|
|
564
|
+
});
|
|
565
|
+
try {
|
|
566
|
+
await client.connect(sseTransport, { timeout });
|
|
567
|
+
return sseTransport;
|
|
568
|
+
} catch (error) {
|
|
569
|
+
await this.safeCloseTransport(sseTransport);
|
|
570
|
+
const streamableMessage = streamableError ? ` Streamable HTTP error: ${this.formatError(streamableError)}.` : "";
|
|
571
|
+
throw new Error(
|
|
572
|
+
`Failed to connect to MCP server "${serverId}" using HTTP transports.${streamableMessage} SSE error: ${this.formatError(error)}.`
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
async safeCloseTransport(transport) {
|
|
577
|
+
try {
|
|
578
|
+
await transport.close();
|
|
579
|
+
} catch {
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
applyNotificationHandlers(serverId, client) {
|
|
583
|
+
const serverHandlers = this.notificationHandlers.get(serverId);
|
|
584
|
+
if (!serverHandlers) {
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
for (const [schema] of serverHandlers) {
|
|
588
|
+
client.setNotificationHandler(
|
|
589
|
+
schema,
|
|
590
|
+
this.createNotificationDispatcher(serverId, schema)
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
createNotificationDispatcher(serverId, schema) {
|
|
595
|
+
return (notification) => {
|
|
596
|
+
const serverHandlers = this.notificationHandlers.get(serverId);
|
|
597
|
+
const handlersForSchema = serverHandlers == null ? void 0 : serverHandlers.get(schema);
|
|
598
|
+
if (!handlersForSchema || handlersForSchema.size === 0) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
for (const handler of handlersForSchema) {
|
|
602
|
+
try {
|
|
603
|
+
handler(notification);
|
|
604
|
+
} catch {
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
applyElicitationHandler(serverId, client) {
|
|
610
|
+
const serverSpecific = this.elicitationHandlers.get(serverId);
|
|
611
|
+
if (serverSpecific) {
|
|
612
|
+
client.setRequestHandler(
|
|
613
|
+
import_types2.ElicitRequestSchema,
|
|
614
|
+
async (request) => serverSpecific(request.params)
|
|
615
|
+
);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (this.elicitationCallback) {
|
|
619
|
+
client.setRequestHandler(import_types2.ElicitRequestSchema, async (request) => {
|
|
620
|
+
var _a, _b, _c, _d;
|
|
621
|
+
const reqId = `elicit_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
622
|
+
return await this.elicitationCallback({
|
|
623
|
+
requestId: reqId,
|
|
624
|
+
message: (_a = request.params) == null ? void 0 : _a.message,
|
|
625
|
+
schema: (_d = (_b = request.params) == null ? void 0 : _b.requestedSchema) != null ? _d : (_c = request.params) == null ? void 0 : _c.schema
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
async ensureConnected(serverId) {
|
|
632
|
+
const state = this.clientStates.get(serverId);
|
|
633
|
+
if (state == null ? void 0 : state.client) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (!state) {
|
|
637
|
+
throw new Error(`Unknown MCP server "${serverId}".`);
|
|
638
|
+
}
|
|
639
|
+
if (state.promise) {
|
|
640
|
+
await state.promise;
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
await this.connectToServer(serverId, state.config);
|
|
644
|
+
}
|
|
645
|
+
resetState(serverId) {
|
|
646
|
+
this.clientStates.delete(serverId);
|
|
647
|
+
this.toolsMetadataCache.delete(serverId);
|
|
648
|
+
}
|
|
649
|
+
resolveConnectionStatus(state) {
|
|
650
|
+
if (!state) {
|
|
651
|
+
return "disconnected";
|
|
652
|
+
}
|
|
653
|
+
if (state.client) {
|
|
654
|
+
return "connected";
|
|
655
|
+
}
|
|
656
|
+
if (state.promise) {
|
|
657
|
+
return "connecting";
|
|
658
|
+
}
|
|
659
|
+
return "disconnected";
|
|
660
|
+
}
|
|
661
|
+
withTimeout(serverId, options) {
|
|
662
|
+
var _a;
|
|
663
|
+
const state = this.clientStates.get(serverId);
|
|
664
|
+
const timeout = (_a = state == null ? void 0 : state.timeout) != null ? _a : state ? this.getTimeout(state.config) : this.defaultTimeout;
|
|
665
|
+
if (!options) {
|
|
666
|
+
return { timeout };
|
|
667
|
+
}
|
|
668
|
+
if (options.timeout === void 0) {
|
|
669
|
+
return { ...options, timeout };
|
|
670
|
+
}
|
|
671
|
+
return options;
|
|
672
|
+
}
|
|
673
|
+
buildCapabilities(config) {
|
|
674
|
+
var _a;
|
|
675
|
+
const capabilities = {
|
|
676
|
+
...this.defaultCapabilities,
|
|
677
|
+
...(_a = config.capabilities) != null ? _a : {}
|
|
678
|
+
};
|
|
679
|
+
if (!capabilities.elicitation) {
|
|
680
|
+
capabilities.elicitation = {};
|
|
681
|
+
}
|
|
682
|
+
return capabilities;
|
|
683
|
+
}
|
|
684
|
+
formatError(error) {
|
|
685
|
+
if (error instanceof Error) {
|
|
686
|
+
return error.message;
|
|
687
|
+
}
|
|
688
|
+
try {
|
|
689
|
+
return JSON.stringify(error);
|
|
690
|
+
} catch {
|
|
691
|
+
return String(error);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
isMethodUnavailableError(error, method) {
|
|
695
|
+
if (!(error instanceof Error)) {
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
const message = error.message.toLowerCase();
|
|
699
|
+
const methodTokens = /* @__PURE__ */ new Set();
|
|
700
|
+
const pushToken = (token) => {
|
|
701
|
+
if (token) {
|
|
702
|
+
methodTokens.add(token.toLowerCase());
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
pushToken(method);
|
|
706
|
+
for (const part of method.split(/[\/:._-]/)) {
|
|
707
|
+
pushToken(part);
|
|
708
|
+
}
|
|
709
|
+
const indicators = [
|
|
710
|
+
"method not found",
|
|
711
|
+
"not implemented",
|
|
712
|
+
"unsupported",
|
|
713
|
+
"does not support",
|
|
714
|
+
"unimplemented"
|
|
715
|
+
];
|
|
716
|
+
const indicatorMatch = indicators.some(
|
|
717
|
+
(indicator) => message.includes(indicator)
|
|
718
|
+
);
|
|
719
|
+
if (!indicatorMatch) {
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
if (Array.from(methodTokens).some((token) => message.includes(token))) {
|
|
723
|
+
return true;
|
|
724
|
+
}
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
getTimeout(config) {
|
|
728
|
+
var _a;
|
|
729
|
+
return (_a = config.timeout) != null ? _a : this.defaultTimeout;
|
|
730
|
+
}
|
|
731
|
+
isStdioConfig(config) {
|
|
732
|
+
return "command" in config;
|
|
733
|
+
}
|
|
734
|
+
getClientById(serverId) {
|
|
735
|
+
const state = this.clientStates.get(serverId);
|
|
736
|
+
if (!(state == null ? void 0 : state.client)) {
|
|
737
|
+
throw new Error(`MCP server "${serverId}" is not connected.`);
|
|
738
|
+
}
|
|
739
|
+
return state.client;
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
743
|
+
0 && (module.exports = {
|
|
744
|
+
MCPClientManager
|
|
745
|
+
});
|
|
746
|
+
//# sourceMappingURL=index.cjs.map
|