@mcpjam/inspector 1.0.7 → 1.0.8
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/server/index.js
CHANGED
|
@@ -47,7 +47,831 @@ import { serveStatic } from "@hono/node-server/serve-static";
|
|
|
47
47
|
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
48
48
|
import { join as join2, dirname, resolve } from "path";
|
|
49
49
|
import { fileURLToPath } from "url";
|
|
50
|
-
|
|
50
|
+
|
|
51
|
+
// ../sdk/dist/chunk-6XEFXCUG.js
|
|
52
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
53
|
+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
54
|
+
import {
|
|
55
|
+
getDefaultEnvironment,
|
|
56
|
+
StdioClientTransport
|
|
57
|
+
} from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
58
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
59
|
+
import { DEFAULT_REQUEST_TIMEOUT_MSEC } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
60
|
+
import {
|
|
61
|
+
CallToolResultSchema as CallToolResultSchema2,
|
|
62
|
+
ElicitRequestSchema,
|
|
63
|
+
ResourceListChangedNotificationSchema,
|
|
64
|
+
ResourceUpdatedNotificationSchema,
|
|
65
|
+
PromptListChangedNotificationSchema
|
|
66
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
67
|
+
import {
|
|
68
|
+
CallToolResultSchema
|
|
69
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
70
|
+
import {
|
|
71
|
+
dynamicTool,
|
|
72
|
+
jsonSchema,
|
|
73
|
+
tool as defineTool
|
|
74
|
+
} from "ai";
|
|
75
|
+
var __defProp = Object.defineProperty;
|
|
76
|
+
var __export = (target, all) => {
|
|
77
|
+
for (var name in all)
|
|
78
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
79
|
+
};
|
|
80
|
+
var mcp_client_manager_exports = {};
|
|
81
|
+
__export(mcp_client_manager_exports, {
|
|
82
|
+
MCPClientManager: () => MCPClientManager
|
|
83
|
+
});
|
|
84
|
+
var ensureJsonSchemaObject = (schema) => {
|
|
85
|
+
var _a2;
|
|
86
|
+
if (schema && typeof schema === "object") {
|
|
87
|
+
const record = schema;
|
|
88
|
+
const base = record.jsonSchema ? ensureJsonSchemaObject(record.jsonSchema) : record;
|
|
89
|
+
if (!("type" in base) || base.type === void 0) {
|
|
90
|
+
base.type = "object";
|
|
91
|
+
}
|
|
92
|
+
if (base.type === "object") {
|
|
93
|
+
base.properties = (_a2 = base.properties) != null ? _a2 : {};
|
|
94
|
+
if (base.additionalProperties === void 0) {
|
|
95
|
+
base.additionalProperties = false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return base;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
type: "object",
|
|
102
|
+
properties: {},
|
|
103
|
+
additionalProperties: false
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
async function convertMCPToolsToVercelTools(listToolsResult, {
|
|
107
|
+
schemas = "automatic",
|
|
108
|
+
callTool
|
|
109
|
+
}) {
|
|
110
|
+
var _a2, _b2;
|
|
111
|
+
const tools2 = {};
|
|
112
|
+
for (const toolDescription of listToolsResult.tools) {
|
|
113
|
+
const { name, description, inputSchema } = toolDescription;
|
|
114
|
+
const execute = async (args, options) => {
|
|
115
|
+
var _a22, _b22;
|
|
116
|
+
(_b22 = (_a22 = options == null ? void 0 : options.abortSignal) == null ? void 0 : _a22.throwIfAborted) == null ? void 0 : _b22.call(_a22);
|
|
117
|
+
const result = await callTool({ name, args, options });
|
|
118
|
+
return CallToolResultSchema.parse(result);
|
|
119
|
+
};
|
|
120
|
+
let vercelTool;
|
|
121
|
+
if (schemas === "automatic") {
|
|
122
|
+
const normalizedInputSchema = ensureJsonSchemaObject(inputSchema);
|
|
123
|
+
vercelTool = dynamicTool({
|
|
124
|
+
description,
|
|
125
|
+
inputSchema: jsonSchema({
|
|
126
|
+
type: "object",
|
|
127
|
+
properties: (_a2 = normalizedInputSchema.properties) != null ? _a2 : {},
|
|
128
|
+
additionalProperties: (_b2 = normalizedInputSchema.additionalProperties) != null ? _b2 : false
|
|
129
|
+
}),
|
|
130
|
+
execute
|
|
131
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
const overrides = schemas;
|
|
134
|
+
if (!(name in overrides)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
vercelTool = defineTool({
|
|
138
|
+
description,
|
|
139
|
+
inputSchema: overrides[name].inputSchema,
|
|
140
|
+
execute
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
tools2[name] = vercelTool;
|
|
144
|
+
}
|
|
145
|
+
return tools2;
|
|
146
|
+
}
|
|
147
|
+
var MCPClientManager = class {
|
|
148
|
+
constructor(servers2 = {}, options = {}) {
|
|
149
|
+
this.clientStates = /* @__PURE__ */ new Map();
|
|
150
|
+
this.notificationHandlers = /* @__PURE__ */ new Map();
|
|
151
|
+
this.elicitationHandlers = /* @__PURE__ */ new Map();
|
|
152
|
+
this.toolsMetadataCache = /* @__PURE__ */ new Map();
|
|
153
|
+
this.defaultLogJsonRpc = false;
|
|
154
|
+
this.pendingElicitations = /* @__PURE__ */ new Map();
|
|
155
|
+
var _a2, _b2, _c, _d;
|
|
156
|
+
this.defaultClientVersion = (_a2 = options.defaultClientVersion) != null ? _a2 : "1.0.0";
|
|
157
|
+
this.defaultCapabilities = { ...(_b2 = options.defaultCapabilities) != null ? _b2 : {} };
|
|
158
|
+
this.defaultTimeout = (_c = options.defaultTimeout) != null ? _c : DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
159
|
+
this.defaultLogJsonRpc = (_d = options.defaultLogJsonRpc) != null ? _d : false;
|
|
160
|
+
this.defaultRpcLogger = options.rpcLogger;
|
|
161
|
+
for (const [id, config] of Object.entries(servers2)) {
|
|
162
|
+
void this.connectToServer(id, config);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
listServers() {
|
|
166
|
+
return Array.from(this.clientStates.keys());
|
|
167
|
+
}
|
|
168
|
+
hasServer(serverId) {
|
|
169
|
+
return this.clientStates.has(serverId);
|
|
170
|
+
}
|
|
171
|
+
getServerSummaries() {
|
|
172
|
+
return Array.from(this.clientStates.entries()).map(([serverId, state]) => ({
|
|
173
|
+
id: serverId,
|
|
174
|
+
status: this.resolveConnectionStatus(state),
|
|
175
|
+
config: state.config
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
getConnectionStatus(serverId) {
|
|
179
|
+
return this.resolveConnectionStatus(this.clientStates.get(serverId));
|
|
180
|
+
}
|
|
181
|
+
getServerConfig(serverId) {
|
|
182
|
+
var _a2;
|
|
183
|
+
return (_a2 = this.clientStates.get(serverId)) == null ? void 0 : _a2.config;
|
|
184
|
+
}
|
|
185
|
+
async connectToServer(serverId, config) {
|
|
186
|
+
var _a2;
|
|
187
|
+
if (this.clientStates.has(serverId)) {
|
|
188
|
+
throw new Error(`MCP server "${serverId}" is already connected.`);
|
|
189
|
+
}
|
|
190
|
+
const timeout = this.getTimeout(config);
|
|
191
|
+
const state = (_a2 = this.clientStates.get(serverId)) != null ? _a2 : {
|
|
192
|
+
config,
|
|
193
|
+
timeout
|
|
194
|
+
};
|
|
195
|
+
state.config = config;
|
|
196
|
+
state.timeout = timeout;
|
|
197
|
+
if (state.client) {
|
|
198
|
+
this.clientStates.set(serverId, state);
|
|
199
|
+
return state.client;
|
|
200
|
+
}
|
|
201
|
+
if (state.promise) {
|
|
202
|
+
this.clientStates.set(serverId, state);
|
|
203
|
+
return state.promise;
|
|
204
|
+
}
|
|
205
|
+
const connectionPromise = (async () => {
|
|
206
|
+
var _a22;
|
|
207
|
+
const client = new Client(
|
|
208
|
+
{
|
|
209
|
+
name: serverId,
|
|
210
|
+
version: (_a22 = config.version) != null ? _a22 : this.defaultClientVersion
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
capabilities: this.buildCapabilities(config)
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
this.applyNotificationHandlers(serverId, client);
|
|
217
|
+
this.applyElicitationHandler(serverId, client);
|
|
218
|
+
if (config.onError) {
|
|
219
|
+
client.onerror = (error) => {
|
|
220
|
+
var _a3;
|
|
221
|
+
(_a3 = config.onError) == null ? void 0 : _a3.call(config, error);
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
client.onclose = () => {
|
|
225
|
+
this.resetState(serverId);
|
|
226
|
+
};
|
|
227
|
+
let transport;
|
|
228
|
+
if (this.isStdioConfig(config)) {
|
|
229
|
+
transport = await this.connectViaStdio(
|
|
230
|
+
serverId,
|
|
231
|
+
client,
|
|
232
|
+
config,
|
|
233
|
+
timeout
|
|
234
|
+
);
|
|
235
|
+
} else {
|
|
236
|
+
transport = await this.connectViaHttp(
|
|
237
|
+
serverId,
|
|
238
|
+
client,
|
|
239
|
+
config,
|
|
240
|
+
timeout
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
state.client = client;
|
|
244
|
+
state.transport = transport;
|
|
245
|
+
state.promise = void 0;
|
|
246
|
+
this.clientStates.set(serverId, state);
|
|
247
|
+
return client;
|
|
248
|
+
})().catch((error) => {
|
|
249
|
+
state.promise = void 0;
|
|
250
|
+
state.client = void 0;
|
|
251
|
+
state.transport = void 0;
|
|
252
|
+
this.clientStates.set(serverId, state);
|
|
253
|
+
throw error;
|
|
254
|
+
});
|
|
255
|
+
state.promise = connectionPromise;
|
|
256
|
+
this.clientStates.set(serverId, state);
|
|
257
|
+
return connectionPromise;
|
|
258
|
+
}
|
|
259
|
+
async disconnectServer(serverId) {
|
|
260
|
+
const client = this.getClientById(serverId);
|
|
261
|
+
try {
|
|
262
|
+
await client.close();
|
|
263
|
+
} finally {
|
|
264
|
+
if (client.transport) {
|
|
265
|
+
await this.safeCloseTransport(client.transport);
|
|
266
|
+
}
|
|
267
|
+
this.resetState(serverId);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
removeServer(serverId) {
|
|
271
|
+
this.resetState(serverId);
|
|
272
|
+
this.notificationHandlers.delete(serverId);
|
|
273
|
+
this.elicitationHandlers.delete(serverId);
|
|
274
|
+
}
|
|
275
|
+
async disconnectAllServers() {
|
|
276
|
+
const serverIds = this.listServers();
|
|
277
|
+
await Promise.all(
|
|
278
|
+
serverIds.map((serverId) => this.disconnectServer(serverId))
|
|
279
|
+
);
|
|
280
|
+
for (const serverId of serverIds) {
|
|
281
|
+
this.resetState(serverId);
|
|
282
|
+
this.notificationHandlers.delete(serverId);
|
|
283
|
+
this.elicitationHandlers.delete(serverId);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
async listTools(serverId, params, options) {
|
|
287
|
+
await this.ensureConnected(serverId);
|
|
288
|
+
const client = this.getClientById(serverId);
|
|
289
|
+
try {
|
|
290
|
+
const result = await client.listTools(
|
|
291
|
+
params,
|
|
292
|
+
this.withTimeout(serverId, options)
|
|
293
|
+
);
|
|
294
|
+
const metadataMap = /* @__PURE__ */ new Map();
|
|
295
|
+
for (const tool2 of result.tools) {
|
|
296
|
+
if (tool2._meta) {
|
|
297
|
+
metadataMap.set(tool2.name, tool2._meta);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
this.toolsMetadataCache.set(serverId, metadataMap);
|
|
301
|
+
return result;
|
|
302
|
+
} catch (error) {
|
|
303
|
+
if (this.isMethodUnavailableError(error, "tools/list")) {
|
|
304
|
+
this.toolsMetadataCache.set(serverId, /* @__PURE__ */ new Map());
|
|
305
|
+
return { tools: [] };
|
|
306
|
+
}
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
async getTools(serverIds) {
|
|
311
|
+
const targetServerIds = serverIds && serverIds.length > 0 ? serverIds : this.listServers();
|
|
312
|
+
const toolLists = await Promise.all(
|
|
313
|
+
targetServerIds.map(async (serverId) => {
|
|
314
|
+
await this.ensureConnected(serverId);
|
|
315
|
+
const client = this.getClientById(serverId);
|
|
316
|
+
const result = await client.listTools(
|
|
317
|
+
void 0,
|
|
318
|
+
this.withTimeout(serverId)
|
|
319
|
+
);
|
|
320
|
+
const metadataMap = /* @__PURE__ */ new Map();
|
|
321
|
+
for (const tool2 of result.tools) {
|
|
322
|
+
if (tool2._meta) {
|
|
323
|
+
metadataMap.set(tool2.name, tool2._meta);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
this.toolsMetadataCache.set(serverId, metadataMap);
|
|
327
|
+
return result.tools;
|
|
328
|
+
})
|
|
329
|
+
);
|
|
330
|
+
return { tools: toolLists.flat() };
|
|
331
|
+
}
|
|
332
|
+
getAllToolsMetadata(serverId) {
|
|
333
|
+
const metadataMap = this.toolsMetadataCache.get(serverId);
|
|
334
|
+
return metadataMap ? Object.fromEntries(metadataMap) : {};
|
|
335
|
+
}
|
|
336
|
+
pingServer(serverId, options) {
|
|
337
|
+
const client = this.getClientById(serverId);
|
|
338
|
+
try {
|
|
339
|
+
client.ping(options);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
throw new Error(
|
|
342
|
+
`Failed to ping MCP server "${serverId}": ${error instanceof Error ? error.message : "Unknown error"}`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async getToolsForAiSdk(serverIds, options = {}) {
|
|
347
|
+
const ids = Array.isArray(serverIds) ? serverIds : serverIds ? [serverIds] : this.listServers();
|
|
348
|
+
const loadForServer = async (id) => {
|
|
349
|
+
await this.ensureConnected(id);
|
|
350
|
+
const listToolsResult = await this.listTools(id);
|
|
351
|
+
return convertMCPToolsToVercelTools(listToolsResult, {
|
|
352
|
+
schemas: options.schemas,
|
|
353
|
+
callTool: async ({ name, args, options: callOptions }) => {
|
|
354
|
+
const requestOptions = (callOptions == null ? void 0 : callOptions.abortSignal) ? { signal: callOptions.abortSignal } : void 0;
|
|
355
|
+
const result = await this.executeTool(
|
|
356
|
+
id,
|
|
357
|
+
name,
|
|
358
|
+
args != null ? args : {},
|
|
359
|
+
requestOptions
|
|
360
|
+
);
|
|
361
|
+
return CallToolResultSchema2.parse(result);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
const perServerTools = await Promise.all(
|
|
366
|
+
ids.map(async (id) => {
|
|
367
|
+
try {
|
|
368
|
+
const tools2 = await loadForServer(id);
|
|
369
|
+
for (const [name, tool2] of Object.entries(tools2)) {
|
|
370
|
+
tool2._serverId = id;
|
|
371
|
+
}
|
|
372
|
+
return tools2;
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (this.isMethodUnavailableError(error, "tools/list")) {
|
|
375
|
+
return {};
|
|
376
|
+
}
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
})
|
|
380
|
+
);
|
|
381
|
+
const flattened = {};
|
|
382
|
+
for (const toolset of perServerTools) {
|
|
383
|
+
for (const [name, tool2] of Object.entries(toolset)) {
|
|
384
|
+
flattened[name] = tool2;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return flattened;
|
|
388
|
+
}
|
|
389
|
+
async executeTool(serverId, toolName, args = {}, options) {
|
|
390
|
+
await this.ensureConnected(serverId);
|
|
391
|
+
const client = this.getClientById(serverId);
|
|
392
|
+
return client.callTool(
|
|
393
|
+
{
|
|
394
|
+
name: toolName,
|
|
395
|
+
arguments: args
|
|
396
|
+
},
|
|
397
|
+
CallToolResultSchema2,
|
|
398
|
+
this.withTimeout(serverId, options)
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
async listResources(serverId, params, options) {
|
|
402
|
+
await this.ensureConnected(serverId);
|
|
403
|
+
const client = this.getClientById(serverId);
|
|
404
|
+
try {
|
|
405
|
+
return await client.listResources(
|
|
406
|
+
params,
|
|
407
|
+
this.withTimeout(serverId, options)
|
|
408
|
+
);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
if (this.isMethodUnavailableError(error, "resources/list")) {
|
|
411
|
+
return {
|
|
412
|
+
resources: []
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
throw error;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
async readResource(serverId, params, options) {
|
|
419
|
+
await this.ensureConnected(serverId);
|
|
420
|
+
const client = this.getClientById(serverId);
|
|
421
|
+
return client.readResource(params, this.withTimeout(serverId, options));
|
|
422
|
+
}
|
|
423
|
+
async subscribeResource(serverId, params, options) {
|
|
424
|
+
await this.ensureConnected(serverId);
|
|
425
|
+
const client = this.getClientById(serverId);
|
|
426
|
+
return client.subscribeResource(
|
|
427
|
+
params,
|
|
428
|
+
this.withTimeout(serverId, options)
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
async unsubscribeResource(serverId, params, options) {
|
|
432
|
+
await this.ensureConnected(serverId);
|
|
433
|
+
const client = this.getClientById(serverId);
|
|
434
|
+
return client.unsubscribeResource(
|
|
435
|
+
params,
|
|
436
|
+
this.withTimeout(serverId, options)
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
async listResourceTemplates(serverId, params, options) {
|
|
440
|
+
await this.ensureConnected(serverId);
|
|
441
|
+
const client = this.getClientById(serverId);
|
|
442
|
+
return client.listResourceTemplates(
|
|
443
|
+
params,
|
|
444
|
+
this.withTimeout(serverId, options)
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
async listPrompts(serverId, params, options) {
|
|
448
|
+
await this.ensureConnected(serverId);
|
|
449
|
+
const client = this.getClientById(serverId);
|
|
450
|
+
try {
|
|
451
|
+
return await client.listPrompts(
|
|
452
|
+
params,
|
|
453
|
+
this.withTimeout(serverId, options)
|
|
454
|
+
);
|
|
455
|
+
} catch (error) {
|
|
456
|
+
if (this.isMethodUnavailableError(error, "prompts/list")) {
|
|
457
|
+
return {
|
|
458
|
+
prompts: []
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
throw error;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async getPrompt(serverId, params, options) {
|
|
465
|
+
await this.ensureConnected(serverId);
|
|
466
|
+
const client = this.getClientById(serverId);
|
|
467
|
+
return client.getPrompt(params, this.withTimeout(serverId, options));
|
|
468
|
+
}
|
|
469
|
+
getSessionIdByServer(serverId) {
|
|
470
|
+
const state = this.clientStates.get(serverId);
|
|
471
|
+
if (!(state == null ? void 0 : state.transport)) {
|
|
472
|
+
throw new Error(`Unknown MCP server "${serverId}".`);
|
|
473
|
+
}
|
|
474
|
+
if (state.transport instanceof StreamableHTTPClientTransport) {
|
|
475
|
+
return state.transport.sessionId;
|
|
476
|
+
}
|
|
477
|
+
throw new Error(
|
|
478
|
+
`Server "${serverId}" must be Streamable HTTP to get the session ID.`
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
addNotificationHandler(serverId, schema, handler) {
|
|
482
|
+
var _a2, _b2, _c;
|
|
483
|
+
const serverHandlers = (_a2 = this.notificationHandlers.get(serverId)) != null ? _a2 : /* @__PURE__ */ new Map();
|
|
484
|
+
const handlersForSchema = (_b2 = serverHandlers.get(schema)) != null ? _b2 : /* @__PURE__ */ new Set();
|
|
485
|
+
handlersForSchema.add(handler);
|
|
486
|
+
serverHandlers.set(schema, handlersForSchema);
|
|
487
|
+
this.notificationHandlers.set(serverId, serverHandlers);
|
|
488
|
+
const client = (_c = this.clientStates.get(serverId)) == null ? void 0 : _c.client;
|
|
489
|
+
if (client) {
|
|
490
|
+
client.setNotificationHandler(
|
|
491
|
+
schema,
|
|
492
|
+
this.createNotificationDispatcher(serverId, schema)
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
onResourceListChanged(serverId, handler) {
|
|
497
|
+
this.addNotificationHandler(
|
|
498
|
+
serverId,
|
|
499
|
+
ResourceListChangedNotificationSchema,
|
|
500
|
+
handler
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
onResourceUpdated(serverId, handler) {
|
|
504
|
+
this.addNotificationHandler(
|
|
505
|
+
serverId,
|
|
506
|
+
ResourceUpdatedNotificationSchema,
|
|
507
|
+
handler
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
onPromptListChanged(serverId, handler) {
|
|
511
|
+
this.addNotificationHandler(
|
|
512
|
+
serverId,
|
|
513
|
+
PromptListChangedNotificationSchema,
|
|
514
|
+
handler
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
getClient(serverId) {
|
|
518
|
+
var _a2;
|
|
519
|
+
return (_a2 = this.clientStates.get(serverId)) == null ? void 0 : _a2.client;
|
|
520
|
+
}
|
|
521
|
+
setElicitationHandler(serverId, handler) {
|
|
522
|
+
var _a2;
|
|
523
|
+
if (!this.clientStates.has(serverId)) {
|
|
524
|
+
throw new Error(`Unknown MCP server "${serverId}".`);
|
|
525
|
+
}
|
|
526
|
+
this.elicitationHandlers.set(serverId, handler);
|
|
527
|
+
const client = (_a2 = this.clientStates.get(serverId)) == null ? void 0 : _a2.client;
|
|
528
|
+
if (client) {
|
|
529
|
+
this.applyElicitationHandler(serverId, client);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
clearElicitationHandler(serverId) {
|
|
533
|
+
var _a2;
|
|
534
|
+
this.elicitationHandlers.delete(serverId);
|
|
535
|
+
const client = (_a2 = this.clientStates.get(serverId)) == null ? void 0 : _a2.client;
|
|
536
|
+
if (client) {
|
|
537
|
+
client.removeRequestHandler("elicitation/create");
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
// Global elicitation callback API (no serverId required)
|
|
541
|
+
setElicitationCallback(callback) {
|
|
542
|
+
this.elicitationCallback = callback;
|
|
543
|
+
for (const [serverId, state] of this.clientStates.entries()) {
|
|
544
|
+
const client = state.client;
|
|
545
|
+
if (!client) continue;
|
|
546
|
+
if (this.elicitationHandlers.has(serverId)) {
|
|
547
|
+
this.applyElicitationHandler(serverId, client);
|
|
548
|
+
} else {
|
|
549
|
+
this.applyElicitationHandler(serverId, client);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
clearElicitationCallback() {
|
|
554
|
+
this.elicitationCallback = void 0;
|
|
555
|
+
for (const [serverId, state] of this.clientStates.entries()) {
|
|
556
|
+
const client = state.client;
|
|
557
|
+
if (!client) continue;
|
|
558
|
+
if (this.elicitationHandlers.has(serverId)) {
|
|
559
|
+
this.applyElicitationHandler(serverId, client);
|
|
560
|
+
} else {
|
|
561
|
+
client.removeRequestHandler("elicitation/create");
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
// Expose the pending elicitation map so callers can add resolvers
|
|
566
|
+
getPendingElicitations() {
|
|
567
|
+
return this.pendingElicitations;
|
|
568
|
+
}
|
|
569
|
+
// Helper to resolve a pending elicitation from outside
|
|
570
|
+
respondToElicitation(requestId, response) {
|
|
571
|
+
const pending = this.pendingElicitations.get(requestId);
|
|
572
|
+
if (!pending) return false;
|
|
573
|
+
try {
|
|
574
|
+
pending.resolve(response);
|
|
575
|
+
return true;
|
|
576
|
+
} finally {
|
|
577
|
+
this.pendingElicitations.delete(requestId);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
async connectViaStdio(serverId, client, config, timeout) {
|
|
581
|
+
var _a2;
|
|
582
|
+
const underlying = new StdioClientTransport({
|
|
583
|
+
command: config.command,
|
|
584
|
+
args: config.args,
|
|
585
|
+
env: { ...getDefaultEnvironment(), ...(_a2 = config.env) != null ? _a2 : {} }
|
|
586
|
+
});
|
|
587
|
+
const wrapped = this.wrapTransportForLogging(serverId, config, underlying);
|
|
588
|
+
await client.connect(wrapped, { timeout });
|
|
589
|
+
return underlying;
|
|
590
|
+
}
|
|
591
|
+
async connectViaHttp(serverId, client, config, timeout) {
|
|
592
|
+
var _a2;
|
|
593
|
+
const preferSSE = (_a2 = config.preferSSE) != null ? _a2 : config.url.pathname.endsWith("/sse");
|
|
594
|
+
let streamableError;
|
|
595
|
+
if (!preferSSE) {
|
|
596
|
+
const streamableTransport = new StreamableHTTPClientTransport(
|
|
597
|
+
config.url,
|
|
598
|
+
{
|
|
599
|
+
requestInit: config.requestInit,
|
|
600
|
+
reconnectionOptions: config.reconnectionOptions,
|
|
601
|
+
authProvider: config.authProvider,
|
|
602
|
+
sessionId: config.sessionId
|
|
603
|
+
}
|
|
604
|
+
);
|
|
605
|
+
try {
|
|
606
|
+
const wrapped = this.wrapTransportForLogging(
|
|
607
|
+
serverId,
|
|
608
|
+
config,
|
|
609
|
+
streamableTransport
|
|
610
|
+
);
|
|
611
|
+
await client.connect(wrapped, {
|
|
612
|
+
timeout: Math.min(timeout, 3e3)
|
|
613
|
+
});
|
|
614
|
+
return streamableTransport;
|
|
615
|
+
} catch (error) {
|
|
616
|
+
streamableError = error;
|
|
617
|
+
await this.safeCloseTransport(streamableTransport);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
const sseTransport = new SSEClientTransport(config.url, {
|
|
621
|
+
requestInit: config.requestInit,
|
|
622
|
+
eventSourceInit: config.eventSourceInit,
|
|
623
|
+
authProvider: config.authProvider
|
|
624
|
+
});
|
|
625
|
+
try {
|
|
626
|
+
const wrapped = this.wrapTransportForLogging(
|
|
627
|
+
serverId,
|
|
628
|
+
config,
|
|
629
|
+
sseTransport
|
|
630
|
+
);
|
|
631
|
+
await client.connect(wrapped, { timeout });
|
|
632
|
+
return sseTransport;
|
|
633
|
+
} catch (error) {
|
|
634
|
+
await this.safeCloseTransport(sseTransport);
|
|
635
|
+
const streamableMessage = streamableError ? ` Streamable HTTP error: ${this.formatError(streamableError)}.` : "";
|
|
636
|
+
throw new Error(
|
|
637
|
+
`Failed to connect to MCP server "${serverId}" using HTTP transports.${streamableMessage} SSE error: ${this.formatError(error)}.`
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
async safeCloseTransport(transport) {
|
|
642
|
+
try {
|
|
643
|
+
await transport.close();
|
|
644
|
+
} catch {
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
applyNotificationHandlers(serverId, client) {
|
|
648
|
+
const serverHandlers = this.notificationHandlers.get(serverId);
|
|
649
|
+
if (!serverHandlers) {
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
for (const [schema] of serverHandlers) {
|
|
653
|
+
client.setNotificationHandler(
|
|
654
|
+
schema,
|
|
655
|
+
this.createNotificationDispatcher(serverId, schema)
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
createNotificationDispatcher(serverId, schema) {
|
|
660
|
+
return (notification) => {
|
|
661
|
+
const serverHandlers = this.notificationHandlers.get(serverId);
|
|
662
|
+
const handlersForSchema = serverHandlers == null ? void 0 : serverHandlers.get(schema);
|
|
663
|
+
if (!handlersForSchema || handlersForSchema.size === 0) {
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
for (const handler of handlersForSchema) {
|
|
667
|
+
try {
|
|
668
|
+
handler(notification);
|
|
669
|
+
} catch {
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
applyElicitationHandler(serverId, client) {
|
|
675
|
+
const serverSpecific = this.elicitationHandlers.get(serverId);
|
|
676
|
+
if (serverSpecific) {
|
|
677
|
+
client.setRequestHandler(
|
|
678
|
+
ElicitRequestSchema,
|
|
679
|
+
async (request) => serverSpecific(request.params)
|
|
680
|
+
);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
if (this.elicitationCallback) {
|
|
684
|
+
client.setRequestHandler(ElicitRequestSchema, async (request) => {
|
|
685
|
+
var _a2, _b2, _c, _d;
|
|
686
|
+
const reqId = `elicit_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
687
|
+
return await this.elicitationCallback({
|
|
688
|
+
requestId: reqId,
|
|
689
|
+
message: (_a2 = request.params) == null ? void 0 : _a2.message,
|
|
690
|
+
schema: (_d = (_b2 = request.params) == null ? void 0 : _b2.requestedSchema) != null ? _d : (_c = request.params) == null ? void 0 : _c.schema
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
async ensureConnected(serverId) {
|
|
697
|
+
const state = this.clientStates.get(serverId);
|
|
698
|
+
if (state == null ? void 0 : state.client) {
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
if (!state) {
|
|
702
|
+
throw new Error(`Unknown MCP server "${serverId}".`);
|
|
703
|
+
}
|
|
704
|
+
if (state.promise) {
|
|
705
|
+
await state.promise;
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
await this.connectToServer(serverId, state.config);
|
|
709
|
+
}
|
|
710
|
+
resetState(serverId) {
|
|
711
|
+
this.clientStates.delete(serverId);
|
|
712
|
+
this.toolsMetadataCache.delete(serverId);
|
|
713
|
+
}
|
|
714
|
+
resolveConnectionStatus(state) {
|
|
715
|
+
if (!state) {
|
|
716
|
+
return "disconnected";
|
|
717
|
+
}
|
|
718
|
+
if (state.client) {
|
|
719
|
+
return "connected";
|
|
720
|
+
}
|
|
721
|
+
if (state.promise) {
|
|
722
|
+
return "connecting";
|
|
723
|
+
}
|
|
724
|
+
return "disconnected";
|
|
725
|
+
}
|
|
726
|
+
withTimeout(serverId, options) {
|
|
727
|
+
var _a2;
|
|
728
|
+
const state = this.clientStates.get(serverId);
|
|
729
|
+
const timeout = (_a2 = state == null ? void 0 : state.timeout) != null ? _a2 : state ? this.getTimeout(state.config) : this.defaultTimeout;
|
|
730
|
+
if (!options) {
|
|
731
|
+
return { timeout };
|
|
732
|
+
}
|
|
733
|
+
if (options.timeout === void 0) {
|
|
734
|
+
return { ...options, timeout };
|
|
735
|
+
}
|
|
736
|
+
return options;
|
|
737
|
+
}
|
|
738
|
+
buildCapabilities(config) {
|
|
739
|
+
var _a2;
|
|
740
|
+
const capabilities = {
|
|
741
|
+
...this.defaultCapabilities,
|
|
742
|
+
...(_a2 = config.capabilities) != null ? _a2 : {}
|
|
743
|
+
};
|
|
744
|
+
if (!capabilities.elicitation) {
|
|
745
|
+
capabilities.elicitation = {};
|
|
746
|
+
}
|
|
747
|
+
return capabilities;
|
|
748
|
+
}
|
|
749
|
+
formatError(error) {
|
|
750
|
+
if (error instanceof Error) {
|
|
751
|
+
return error.message;
|
|
752
|
+
}
|
|
753
|
+
try {
|
|
754
|
+
return JSON.stringify(error);
|
|
755
|
+
} catch {
|
|
756
|
+
return String(error);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
// Returns a transport that logs JSON-RPC traffic if enabled for this server
|
|
760
|
+
wrapTransportForLogging(serverId, config, transport) {
|
|
761
|
+
const logger2 = this.resolveRpcLogger(serverId, config);
|
|
762
|
+
if (!logger2) return transport;
|
|
763
|
+
const log = logger2;
|
|
764
|
+
const self = this;
|
|
765
|
+
class LoggingTransport {
|
|
766
|
+
constructor(inner) {
|
|
767
|
+
this.inner = inner;
|
|
768
|
+
this.inner.onmessage = (message, extra) => {
|
|
769
|
+
var _a2;
|
|
770
|
+
try {
|
|
771
|
+
log({ direction: "receive", message, serverId });
|
|
772
|
+
} catch {
|
|
773
|
+
}
|
|
774
|
+
(_a2 = this.onmessage) == null ? void 0 : _a2.call(this, message, extra);
|
|
775
|
+
};
|
|
776
|
+
this.inner.onclose = () => {
|
|
777
|
+
var _a2;
|
|
778
|
+
(_a2 = this.onclose) == null ? void 0 : _a2.call(this);
|
|
779
|
+
};
|
|
780
|
+
this.inner.onerror = (error) => {
|
|
781
|
+
var _a2;
|
|
782
|
+
(_a2 = this.onerror) == null ? void 0 : _a2.call(this, error);
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
async start() {
|
|
786
|
+
if (typeof this.inner.start === "function") {
|
|
787
|
+
await this.inner.start();
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
async send(message, options) {
|
|
791
|
+
try {
|
|
792
|
+
log({ direction: "send", message, serverId });
|
|
793
|
+
} catch {
|
|
794
|
+
}
|
|
795
|
+
await this.inner.send(message, options);
|
|
796
|
+
}
|
|
797
|
+
async close() {
|
|
798
|
+
await this.inner.close();
|
|
799
|
+
}
|
|
800
|
+
get sessionId() {
|
|
801
|
+
return this.inner.sessionId;
|
|
802
|
+
}
|
|
803
|
+
setProtocolVersion(version2) {
|
|
804
|
+
if (typeof this.inner.setProtocolVersion === "function") {
|
|
805
|
+
this.inner.setProtocolVersion(version2);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
return new LoggingTransport(transport);
|
|
810
|
+
}
|
|
811
|
+
resolveRpcLogger(serverId, config) {
|
|
812
|
+
if (config.rpcLogger) return config.rpcLogger;
|
|
813
|
+
if (config.logJsonRpc || this.defaultLogJsonRpc) {
|
|
814
|
+
return ({ direction, message, serverId: id }) => {
|
|
815
|
+
let printable;
|
|
816
|
+
try {
|
|
817
|
+
printable = typeof message === "string" ? message : JSON.stringify(message);
|
|
818
|
+
} catch {
|
|
819
|
+
printable = String(message);
|
|
820
|
+
}
|
|
821
|
+
console.debug(`[MCP:${id}] ${direction.toUpperCase()} ${printable}`);
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
if (this.defaultRpcLogger) return this.defaultRpcLogger;
|
|
825
|
+
return void 0;
|
|
826
|
+
}
|
|
827
|
+
isMethodUnavailableError(error, method) {
|
|
828
|
+
if (!(error instanceof Error)) {
|
|
829
|
+
return false;
|
|
830
|
+
}
|
|
831
|
+
const message = error.message.toLowerCase();
|
|
832
|
+
const methodTokens = /* @__PURE__ */ new Set();
|
|
833
|
+
const pushToken = (token) => {
|
|
834
|
+
if (token) {
|
|
835
|
+
methodTokens.add(token.toLowerCase());
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
pushToken(method);
|
|
839
|
+
for (const part of method.split(/[\/:._-]/)) {
|
|
840
|
+
pushToken(part);
|
|
841
|
+
}
|
|
842
|
+
const indicators = [
|
|
843
|
+
"method not found",
|
|
844
|
+
"not implemented",
|
|
845
|
+
"unsupported",
|
|
846
|
+
"does not support",
|
|
847
|
+
"unimplemented"
|
|
848
|
+
];
|
|
849
|
+
const indicatorMatch = indicators.some(
|
|
850
|
+
(indicator) => message.includes(indicator)
|
|
851
|
+
);
|
|
852
|
+
if (!indicatorMatch) {
|
|
853
|
+
return false;
|
|
854
|
+
}
|
|
855
|
+
if (Array.from(methodTokens).some((token) => message.includes(token))) {
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
getTimeout(config) {
|
|
861
|
+
var _a2;
|
|
862
|
+
return (_a2 = config.timeout) != null ? _a2 : this.defaultTimeout;
|
|
863
|
+
}
|
|
864
|
+
isStdioConfig(config) {
|
|
865
|
+
return "command" in config;
|
|
866
|
+
}
|
|
867
|
+
getClientById(serverId) {
|
|
868
|
+
const state = this.clientStates.get(serverId);
|
|
869
|
+
if (!(state == null ? void 0 : state.client)) {
|
|
870
|
+
throw new Error(`MCP server "${serverId}" is not connected.`);
|
|
871
|
+
}
|
|
872
|
+
return state.client;
|
|
873
|
+
}
|
|
874
|
+
};
|
|
51
875
|
|
|
52
876
|
// routes/mcp/index.ts
|
|
53
877
|
import { Hono as Hono12 } from "hono";
|
|
@@ -2418,8 +3242,8 @@ function convexToJson(value) {
|
|
|
2418
3242
|
}
|
|
2419
3243
|
|
|
2420
3244
|
// ../node_modules/convex/dist/esm/values/errors.js
|
|
2421
|
-
var
|
|
2422
|
-
var __defNormalProp = (obj, key, value) => key in obj ?
|
|
3245
|
+
var __defProp2 = Object.defineProperty;
|
|
3246
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2423
3247
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2424
3248
|
var _a;
|
|
2425
3249
|
var _b;
|
|
@@ -2440,8 +3264,8 @@ var aBytes = arr();
|
|
|
2440
3264
|
var bBytes = arr();
|
|
2441
3265
|
|
|
2442
3266
|
// ../node_modules/convex/dist/esm/browser/logging.js
|
|
2443
|
-
var
|
|
2444
|
-
var __defNormalProp2 = (obj, key, value) => key in obj ?
|
|
3267
|
+
var __defProp3 = Object.defineProperty;
|
|
3268
|
+
var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2445
3269
|
var __publicField2 = (obj, key, value) => __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2446
3270
|
var INFO_COLOR = "color:rgb(0, 145, 255)";
|
|
2447
3271
|
function prefix_for_source(source) {
|
|
@@ -2634,8 +3458,8 @@ function createApi(pathParts = []) {
|
|
|
2634
3458
|
var anyApi = createApi();
|
|
2635
3459
|
|
|
2636
3460
|
// ../node_modules/convex/dist/esm/vendor/long.js
|
|
2637
|
-
var
|
|
2638
|
-
var __defNormalProp3 = (obj, key, value) => key in obj ?
|
|
3461
|
+
var __defProp4 = Object.defineProperty;
|
|
3462
|
+
var __defNormalProp3 = (obj, key, value) => key in obj ? __defProp4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2639
3463
|
var __publicField3 = (obj, key, value) => __defNormalProp3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2640
3464
|
var Long = class _Long {
|
|
2641
3465
|
constructor(low, high) {
|
|
@@ -2723,8 +3547,8 @@ import { createRequire } from "module";
|
|
|
2723
3547
|
import { resolve as nodePathResolve } from "path";
|
|
2724
3548
|
|
|
2725
3549
|
// ../node_modules/convex/dist/esm/browser/http_client.js
|
|
2726
|
-
var
|
|
2727
|
-
var __defNormalProp4 = (obj, key, value) => key in obj ?
|
|
3550
|
+
var __defProp5 = Object.defineProperty;
|
|
3551
|
+
var __defNormalProp4 = (obj, key, value) => key in obj ? __defProp5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2728
3552
|
var __publicField4 = (obj, key, value) => __defNormalProp4(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2729
3553
|
var STATUS_CODE_UDF_FAILED = 560;
|
|
2730
3554
|
var specifiedFetch = void 0;
|
|
@@ -3171,7 +3995,7 @@ function setDefaultWebSocketConstructor(ws) {
|
|
|
3171
3995
|
// ../node_modules/convex/dist/esm/browser/simple_client-node.js
|
|
3172
3996
|
var require2 = createRequire(nodePathResolve("."));
|
|
3173
3997
|
var __create = Object.create;
|
|
3174
|
-
var
|
|
3998
|
+
var __defProp6 = Object.defineProperty;
|
|
3175
3999
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3176
4000
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3177
4001
|
var __getProtoOf = Object.getPrototypeOf;
|
|
@@ -3189,7 +4013,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
3189
4013
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
3190
4014
|
for (let key of __getOwnPropNames(from))
|
|
3191
4015
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
3192
|
-
|
|
4016
|
+
__defProp6(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
3193
4017
|
}
|
|
3194
4018
|
return to;
|
|
3195
4019
|
};
|
|
@@ -3198,7 +4022,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
3198
4022
|
// file that has been converted to a CommonJS file using a Babel-
|
|
3199
4023
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
3200
4024
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
3201
|
-
isNodeMode || !mod || !mod.__esModule ?
|
|
4025
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp6(target, "default", { value: mod, enumerable: true }) : target,
|
|
3202
4026
|
mod
|
|
3203
4027
|
));
|
|
3204
4028
|
var require_stream = __commonJS({
|