agents 0.0.0-6de101a → 0.0.0-6e0420c
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 +255 -27
- package/dist/ai-chat-agent.d.ts +59 -11
- package/dist/ai-chat-agent.js +548 -163
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-gdyLiTd8.js +155 -0
- package/dist/ai-chat-v5-migration-gdyLiTd8.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +155 -0
- package/dist/ai-chat-v5-migration.js +3 -0
- package/dist/ai-react.d.ts +73 -72
- package/dist/ai-react.js +260 -183
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-B0GBFDwi.js +20 -0
- package/dist/ai-types-B0GBFDwi.js.map +1 -0
- package/dist/ai-types-BWW4umHY.d.ts +95 -0
- package/dist/ai-types.d.ts +6 -69
- package/dist/ai-types.js +3 -1
- package/dist/client-C-u-lCFT.d.ts +5311 -0
- package/dist/client-CmMi85Sj.d.ts +104 -0
- package/dist/client-WbaRgKYN.js +788 -0
- package/dist/client-WbaRgKYN.js.map +1 -0
- package/dist/client-zS-OCVJA.js +117 -0
- package/dist/client-zS-OCVJA.js.map +1 -0
- package/dist/client.d.ts +12 -79
- package/dist/client.js +4 -131
- package/dist/codemode/ai.d.ts +27 -0
- package/dist/codemode/ai.js +151 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/do-oauth-client-provider-B2jr6UNq.js +93 -0
- package/dist/do-oauth-client-provider-B2jr6UNq.js.map +1 -0
- package/dist/do-oauth-client-provider-CCwGwnrA.d.ts +55 -0
- package/dist/index-DWcUTPtX.d.ts +558 -0
- package/dist/index-W4JUkafc.d.ts +54 -0
- package/dist/index.d.ts +63 -301
- package/dist/index.js +7 -21
- package/dist/mcp/client.d.ts +4 -675
- package/dist/mcp/client.js +2 -785
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -0
- package/dist/mcp/do-oauth-client-provider.js +3 -0
- package/dist/mcp/index.d.ts +73 -39
- package/dist/mcp/index.js +841 -129
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +34 -0
- package/dist/mcp/x402.js +194 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-BEwaCsxO.d.ts +61 -0
- package/dist/observability/index.d.ts +3 -0
- package/dist/observability/index.js +7 -0
- package/dist/react-B8BT6PYZ.d.ts +113 -0
- package/dist/react.d.ts +10 -39
- package/dist/react.js +185 -91
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +89 -12
- package/dist/schedule.js +46 -21
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-gtr9YMhp.d.ts +34 -0
- package/dist/serializable.d.ts +7 -0
- package/dist/serializable.js +1 -0
- package/dist/src-C9xZ0CrH.js +1179 -0
- package/dist/src-C9xZ0CrH.js.map +1 -0
- package/package.json +114 -52
- package/src/index.ts +1268 -156
- package/dist/ai-types.js.map +0 -1
- package/dist/chunk-EZ76ZGDB.js +0 -1721
- package/dist/chunk-EZ76ZGDB.js.map +0 -1
- package/dist/chunk-SZEXGW6W.js +0 -580
- package/dist/chunk-SZEXGW6W.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mcp/client.js.map +0 -1
package/dist/mcp/client.js
CHANGED
|
@@ -1,786 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CallToolResultSchema,
|
|
3
|
-
CancelledNotificationSchema,
|
|
4
|
-
CompleteResultSchema,
|
|
5
|
-
EmptyResultSchema,
|
|
6
|
-
ErrorCode,
|
|
7
|
-
GetPromptResultSchema,
|
|
8
|
-
InitializeResultSchema,
|
|
9
|
-
LATEST_PROTOCOL_VERSION,
|
|
10
|
-
ListPromptsResultSchema,
|
|
11
|
-
ListResourceTemplatesResultSchema,
|
|
12
|
-
ListResourcesResultSchema,
|
|
13
|
-
ListToolsResultSchema,
|
|
14
|
-
McpError,
|
|
15
|
-
PingRequestSchema,
|
|
16
|
-
ProgressNotificationSchema,
|
|
17
|
-
PromptListChangedNotificationSchema,
|
|
18
|
-
ReadResourceResultSchema,
|
|
19
|
-
ResourceListChangedNotificationSchema,
|
|
20
|
-
SSEEdgeClientTransport,
|
|
21
|
-
SUPPORTED_PROTOCOL_VERSIONS,
|
|
22
|
-
ToolListChangedNotificationSchema
|
|
23
|
-
} from "../chunk-EZ76ZGDB.js";
|
|
1
|
+
import { MCPClientManager, getNamespacedData } from "../client-WbaRgKYN.js";
|
|
24
2
|
|
|
25
|
-
|
|
26
|
-
var DEFAULT_REQUEST_TIMEOUT_MSEC = 6e4;
|
|
27
|
-
var Protocol = class {
|
|
28
|
-
constructor(_options) {
|
|
29
|
-
this._options = _options;
|
|
30
|
-
this._requestMessageId = 0;
|
|
31
|
-
this._requestHandlers = /* @__PURE__ */ new Map();
|
|
32
|
-
this._requestHandlerAbortControllers = /* @__PURE__ */ new Map();
|
|
33
|
-
this._notificationHandlers = /* @__PURE__ */ new Map();
|
|
34
|
-
this._responseHandlers = /* @__PURE__ */ new Map();
|
|
35
|
-
this._progressHandlers = /* @__PURE__ */ new Map();
|
|
36
|
-
this._timeoutInfo = /* @__PURE__ */ new Map();
|
|
37
|
-
this.setNotificationHandler(CancelledNotificationSchema, (notification) => {
|
|
38
|
-
const controller = this._requestHandlerAbortControllers.get(notification.params.requestId);
|
|
39
|
-
controller === null || controller === void 0 ? void 0 : controller.abort(notification.params.reason);
|
|
40
|
-
});
|
|
41
|
-
this.setNotificationHandler(ProgressNotificationSchema, (notification) => {
|
|
42
|
-
this._onprogress(notification);
|
|
43
|
-
});
|
|
44
|
-
this.setRequestHandler(
|
|
45
|
-
PingRequestSchema,
|
|
46
|
-
// Automatic pong by default.
|
|
47
|
-
(_request) => ({})
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
_setupTimeout(messageId, timeout, maxTotalTimeout, onTimeout) {
|
|
51
|
-
this._timeoutInfo.set(messageId, {
|
|
52
|
-
timeoutId: setTimeout(onTimeout, timeout),
|
|
53
|
-
startTime: Date.now(),
|
|
54
|
-
timeout,
|
|
55
|
-
maxTotalTimeout,
|
|
56
|
-
onTimeout
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
_resetTimeout(messageId) {
|
|
60
|
-
const info = this._timeoutInfo.get(messageId);
|
|
61
|
-
if (!info)
|
|
62
|
-
return false;
|
|
63
|
-
const totalElapsed = Date.now() - info.startTime;
|
|
64
|
-
if (info.maxTotalTimeout && totalElapsed >= info.maxTotalTimeout) {
|
|
65
|
-
this._timeoutInfo.delete(messageId);
|
|
66
|
-
throw new McpError(ErrorCode.RequestTimeout, "Maximum total timeout exceeded", { maxTotalTimeout: info.maxTotalTimeout, totalElapsed });
|
|
67
|
-
}
|
|
68
|
-
clearTimeout(info.timeoutId);
|
|
69
|
-
info.timeoutId = setTimeout(info.onTimeout, info.timeout);
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
_cleanupTimeout(messageId) {
|
|
73
|
-
const info = this._timeoutInfo.get(messageId);
|
|
74
|
-
if (info) {
|
|
75
|
-
clearTimeout(info.timeoutId);
|
|
76
|
-
this._timeoutInfo.delete(messageId);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Attaches to the given transport, starts it, and starts listening for messages.
|
|
81
|
-
*
|
|
82
|
-
* The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
|
|
83
|
-
*/
|
|
84
|
-
async connect(transport) {
|
|
85
|
-
this._transport = transport;
|
|
86
|
-
this._transport.onclose = () => {
|
|
87
|
-
this._onclose();
|
|
88
|
-
};
|
|
89
|
-
this._transport.onerror = (error) => {
|
|
90
|
-
this._onerror(error);
|
|
91
|
-
};
|
|
92
|
-
this._transport.onmessage = (message) => {
|
|
93
|
-
if (!("method" in message)) {
|
|
94
|
-
this._onresponse(message);
|
|
95
|
-
} else if ("id" in message) {
|
|
96
|
-
this._onrequest(message);
|
|
97
|
-
} else {
|
|
98
|
-
this._onnotification(message);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
await this._transport.start();
|
|
102
|
-
}
|
|
103
|
-
_onclose() {
|
|
104
|
-
var _a;
|
|
105
|
-
const responseHandlers = this._responseHandlers;
|
|
106
|
-
this._responseHandlers = /* @__PURE__ */ new Map();
|
|
107
|
-
this._progressHandlers.clear();
|
|
108
|
-
this._transport = void 0;
|
|
109
|
-
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
110
|
-
const error = new McpError(ErrorCode.ConnectionClosed, "Connection closed");
|
|
111
|
-
for (const handler of responseHandlers.values()) {
|
|
112
|
-
handler(error);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
_onerror(error) {
|
|
116
|
-
var _a;
|
|
117
|
-
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
118
|
-
}
|
|
119
|
-
_onnotification(notification) {
|
|
120
|
-
var _a;
|
|
121
|
-
const handler = (_a = this._notificationHandlers.get(notification.method)) !== null && _a !== void 0 ? _a : this.fallbackNotificationHandler;
|
|
122
|
-
if (handler === void 0) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
Promise.resolve().then(() => handler(notification)).catch((error) => this._onerror(new Error(`Uncaught error in notification handler: ${error}`)));
|
|
126
|
-
}
|
|
127
|
-
_onrequest(request) {
|
|
128
|
-
var _a, _b, _c;
|
|
129
|
-
const handler = (_a = this._requestHandlers.get(request.method)) !== null && _a !== void 0 ? _a : this.fallbackRequestHandler;
|
|
130
|
-
if (handler === void 0) {
|
|
131
|
-
(_b = this._transport) === null || _b === void 0 ? void 0 : _b.send({
|
|
132
|
-
jsonrpc: "2.0",
|
|
133
|
-
id: request.id,
|
|
134
|
-
error: {
|
|
135
|
-
code: ErrorCode.MethodNotFound,
|
|
136
|
-
message: "Method not found"
|
|
137
|
-
}
|
|
138
|
-
}).catch((error) => this._onerror(new Error(`Failed to send an error response: ${error}`)));
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const abortController = new AbortController();
|
|
142
|
-
this._requestHandlerAbortControllers.set(request.id, abortController);
|
|
143
|
-
const extra = {
|
|
144
|
-
signal: abortController.signal,
|
|
145
|
-
sessionId: (_c = this._transport) === null || _c === void 0 ? void 0 : _c.sessionId
|
|
146
|
-
};
|
|
147
|
-
Promise.resolve().then(() => handler(request, extra)).then((result) => {
|
|
148
|
-
var _a2;
|
|
149
|
-
if (abortController.signal.aborted) {
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
return (_a2 = this._transport) === null || _a2 === void 0 ? void 0 : _a2.send({
|
|
153
|
-
result,
|
|
154
|
-
jsonrpc: "2.0",
|
|
155
|
-
id: request.id
|
|
156
|
-
});
|
|
157
|
-
}, (error) => {
|
|
158
|
-
var _a2, _b2;
|
|
159
|
-
if (abortController.signal.aborted) {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
return (_a2 = this._transport) === null || _a2 === void 0 ? void 0 : _a2.send({
|
|
163
|
-
jsonrpc: "2.0",
|
|
164
|
-
id: request.id,
|
|
165
|
-
error: {
|
|
166
|
-
code: Number.isSafeInteger(error["code"]) ? error["code"] : ErrorCode.InternalError,
|
|
167
|
-
message: (_b2 = error.message) !== null && _b2 !== void 0 ? _b2 : "Internal error"
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}).catch((error) => this._onerror(new Error(`Failed to send response: ${error}`))).finally(() => {
|
|
171
|
-
this._requestHandlerAbortControllers.delete(request.id);
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
_onprogress(notification) {
|
|
175
|
-
const { progressToken, ...params } = notification.params;
|
|
176
|
-
const messageId = Number(progressToken);
|
|
177
|
-
const handler = this._progressHandlers.get(messageId);
|
|
178
|
-
if (!handler) {
|
|
179
|
-
this._onerror(new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
const responseHandler = this._responseHandlers.get(messageId);
|
|
183
|
-
if (this._timeoutInfo.has(messageId) && responseHandler) {
|
|
184
|
-
try {
|
|
185
|
-
this._resetTimeout(messageId);
|
|
186
|
-
} catch (error) {
|
|
187
|
-
responseHandler(error);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
handler(params);
|
|
192
|
-
}
|
|
193
|
-
_onresponse(response) {
|
|
194
|
-
const messageId = Number(response.id);
|
|
195
|
-
const handler = this._responseHandlers.get(messageId);
|
|
196
|
-
if (handler === void 0) {
|
|
197
|
-
this._onerror(new Error(`Received a response for an unknown message ID: ${JSON.stringify(response)}`));
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
this._responseHandlers.delete(messageId);
|
|
201
|
-
this._progressHandlers.delete(messageId);
|
|
202
|
-
this._cleanupTimeout(messageId);
|
|
203
|
-
if ("result" in response) {
|
|
204
|
-
handler(response);
|
|
205
|
-
} else {
|
|
206
|
-
const error = new McpError(response.error.code, response.error.message, response.error.data);
|
|
207
|
-
handler(error);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
get transport() {
|
|
211
|
-
return this._transport;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Closes the connection.
|
|
215
|
-
*/
|
|
216
|
-
async close() {
|
|
217
|
-
var _a;
|
|
218
|
-
await ((_a = this._transport) === null || _a === void 0 ? void 0 : _a.close());
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Sends a request and wait for a response.
|
|
222
|
-
*
|
|
223
|
-
* Do not use this method to emit notifications! Use notification() instead.
|
|
224
|
-
*/
|
|
225
|
-
request(request, resultSchema, options) {
|
|
226
|
-
return new Promise((resolve, reject) => {
|
|
227
|
-
var _a, _b, _c, _d;
|
|
228
|
-
if (!this._transport) {
|
|
229
|
-
reject(new Error("Not connected"));
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.enforceStrictCapabilities) === true) {
|
|
233
|
-
this.assertCapabilityForMethod(request.method);
|
|
234
|
-
}
|
|
235
|
-
(_b = options === null || options === void 0 ? void 0 : options.signal) === null || _b === void 0 ? void 0 : _b.throwIfAborted();
|
|
236
|
-
const messageId = this._requestMessageId++;
|
|
237
|
-
const jsonrpcRequest = {
|
|
238
|
-
...request,
|
|
239
|
-
jsonrpc: "2.0",
|
|
240
|
-
id: messageId
|
|
241
|
-
};
|
|
242
|
-
if (options === null || options === void 0 ? void 0 : options.onprogress) {
|
|
243
|
-
this._progressHandlers.set(messageId, options.onprogress);
|
|
244
|
-
jsonrpcRequest.params = {
|
|
245
|
-
...request.params,
|
|
246
|
-
_meta: { progressToken: messageId }
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
const cancel = (reason) => {
|
|
250
|
-
var _a2;
|
|
251
|
-
this._responseHandlers.delete(messageId);
|
|
252
|
-
this._progressHandlers.delete(messageId);
|
|
253
|
-
this._cleanupTimeout(messageId);
|
|
254
|
-
(_a2 = this._transport) === null || _a2 === void 0 ? void 0 : _a2.send({
|
|
255
|
-
jsonrpc: "2.0",
|
|
256
|
-
method: "notifications/cancelled",
|
|
257
|
-
params: {
|
|
258
|
-
requestId: messageId,
|
|
259
|
-
reason: String(reason)
|
|
260
|
-
}
|
|
261
|
-
}).catch((error) => this._onerror(new Error(`Failed to send cancellation: ${error}`)));
|
|
262
|
-
reject(reason);
|
|
263
|
-
};
|
|
264
|
-
this._responseHandlers.set(messageId, (response) => {
|
|
265
|
-
var _a2;
|
|
266
|
-
if ((_a2 = options === null || options === void 0 ? void 0 : options.signal) === null || _a2 === void 0 ? void 0 : _a2.aborted) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
if (response instanceof Error) {
|
|
270
|
-
return reject(response);
|
|
271
|
-
}
|
|
272
|
-
try {
|
|
273
|
-
const result = resultSchema.parse(response.result);
|
|
274
|
-
resolve(result);
|
|
275
|
-
} catch (error) {
|
|
276
|
-
reject(error);
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
(_c = options === null || options === void 0 ? void 0 : options.signal) === null || _c === void 0 ? void 0 : _c.addEventListener("abort", () => {
|
|
280
|
-
var _a2;
|
|
281
|
-
cancel((_a2 = options === null || options === void 0 ? void 0 : options.signal) === null || _a2 === void 0 ? void 0 : _a2.reason);
|
|
282
|
-
});
|
|
283
|
-
const timeout = (_d = options === null || options === void 0 ? void 0 : options.timeout) !== null && _d !== void 0 ? _d : DEFAULT_REQUEST_TIMEOUT_MSEC;
|
|
284
|
-
const timeoutHandler = () => cancel(new McpError(ErrorCode.RequestTimeout, "Request timed out", { timeout }));
|
|
285
|
-
this._setupTimeout(messageId, timeout, options === null || options === void 0 ? void 0 : options.maxTotalTimeout, timeoutHandler);
|
|
286
|
-
this._transport.send(jsonrpcRequest).catch((error) => {
|
|
287
|
-
this._cleanupTimeout(messageId);
|
|
288
|
-
reject(error);
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Emits a notification, which is a one-way message that does not expect a response.
|
|
294
|
-
*/
|
|
295
|
-
async notification(notification) {
|
|
296
|
-
if (!this._transport) {
|
|
297
|
-
throw new Error("Not connected");
|
|
298
|
-
}
|
|
299
|
-
this.assertNotificationCapability(notification.method);
|
|
300
|
-
const jsonrpcNotification = {
|
|
301
|
-
...notification,
|
|
302
|
-
jsonrpc: "2.0"
|
|
303
|
-
};
|
|
304
|
-
await this._transport.send(jsonrpcNotification);
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
308
|
-
*
|
|
309
|
-
* Note that this will replace any previous request handler for the same method.
|
|
310
|
-
*/
|
|
311
|
-
setRequestHandler(requestSchema, handler) {
|
|
312
|
-
const method = requestSchema.shape.method.value;
|
|
313
|
-
this.assertRequestHandlerCapability(method);
|
|
314
|
-
this._requestHandlers.set(method, (request, extra) => Promise.resolve(handler(requestSchema.parse(request), extra)));
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Removes the request handler for the given method.
|
|
318
|
-
*/
|
|
319
|
-
removeRequestHandler(method) {
|
|
320
|
-
this._requestHandlers.delete(method);
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.
|
|
324
|
-
*/
|
|
325
|
-
assertCanSetRequestHandler(method) {
|
|
326
|
-
if (this._requestHandlers.has(method)) {
|
|
327
|
-
throw new Error(`A request handler for ${method} already exists, which would be overridden`);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
332
|
-
*
|
|
333
|
-
* Note that this will replace any previous notification handler for the same method.
|
|
334
|
-
*/
|
|
335
|
-
setNotificationHandler(notificationSchema, handler) {
|
|
336
|
-
this._notificationHandlers.set(notificationSchema.shape.method.value, (notification) => Promise.resolve(handler(notificationSchema.parse(notification))));
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Removes the notification handler for the given method.
|
|
340
|
-
*/
|
|
341
|
-
removeNotificationHandler(method) {
|
|
342
|
-
this._notificationHandlers.delete(method);
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
function mergeCapabilities(base, additional) {
|
|
346
|
-
return Object.entries(additional).reduce((acc, [key, value]) => {
|
|
347
|
-
if (value && typeof value === "object") {
|
|
348
|
-
acc[key] = acc[key] ? { ...acc[key], ...value } : value;
|
|
349
|
-
} else {
|
|
350
|
-
acc[key] = value;
|
|
351
|
-
}
|
|
352
|
-
return acc;
|
|
353
|
-
}, { ...base });
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// ../../node_modules/@modelcontextprotocol/sdk/dist/esm/client/index.js
|
|
357
|
-
var Client = class extends Protocol {
|
|
358
|
-
/**
|
|
359
|
-
* Initializes this client with the given name and version information.
|
|
360
|
-
*/
|
|
361
|
-
constructor(_clientInfo, options) {
|
|
362
|
-
var _a;
|
|
363
|
-
super(options);
|
|
364
|
-
this._clientInfo = _clientInfo;
|
|
365
|
-
this._capabilities = (_a = options === null || options === void 0 ? void 0 : options.capabilities) !== null && _a !== void 0 ? _a : {};
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Registers new capabilities. This can only be called before connecting to a transport.
|
|
369
|
-
*
|
|
370
|
-
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
|
|
371
|
-
*/
|
|
372
|
-
registerCapabilities(capabilities) {
|
|
373
|
-
if (this.transport) {
|
|
374
|
-
throw new Error("Cannot register capabilities after connecting to transport");
|
|
375
|
-
}
|
|
376
|
-
this._capabilities = mergeCapabilities(this._capabilities, capabilities);
|
|
377
|
-
}
|
|
378
|
-
assertCapability(capability, method) {
|
|
379
|
-
var _a;
|
|
380
|
-
if (!((_a = this._serverCapabilities) === null || _a === void 0 ? void 0 : _a[capability])) {
|
|
381
|
-
throw new Error(`Server does not support ${capability} (required for ${method})`);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
async connect(transport) {
|
|
385
|
-
await super.connect(transport);
|
|
386
|
-
try {
|
|
387
|
-
const result = await this.request({
|
|
388
|
-
method: "initialize",
|
|
389
|
-
params: {
|
|
390
|
-
protocolVersion: LATEST_PROTOCOL_VERSION,
|
|
391
|
-
capabilities: this._capabilities,
|
|
392
|
-
clientInfo: this._clientInfo
|
|
393
|
-
}
|
|
394
|
-
}, InitializeResultSchema);
|
|
395
|
-
if (result === void 0) {
|
|
396
|
-
throw new Error(`Server sent invalid initialize result: ${result}`);
|
|
397
|
-
}
|
|
398
|
-
if (!SUPPORTED_PROTOCOL_VERSIONS.includes(result.protocolVersion)) {
|
|
399
|
-
throw new Error(`Server's protocol version is not supported: ${result.protocolVersion}`);
|
|
400
|
-
}
|
|
401
|
-
this._serverCapabilities = result.capabilities;
|
|
402
|
-
this._serverVersion = result.serverInfo;
|
|
403
|
-
this._instructions = result.instructions;
|
|
404
|
-
await this.notification({
|
|
405
|
-
method: "notifications/initialized"
|
|
406
|
-
});
|
|
407
|
-
} catch (error) {
|
|
408
|
-
void this.close();
|
|
409
|
-
throw error;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* After initialization has completed, this will be populated with the server's reported capabilities.
|
|
414
|
-
*/
|
|
415
|
-
getServerCapabilities() {
|
|
416
|
-
return this._serverCapabilities;
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* After initialization has completed, this will be populated with information about the server's name and version.
|
|
420
|
-
*/
|
|
421
|
-
getServerVersion() {
|
|
422
|
-
return this._serverVersion;
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* After initialization has completed, this may be populated with information about the server's instructions.
|
|
426
|
-
*/
|
|
427
|
-
getInstructions() {
|
|
428
|
-
return this._instructions;
|
|
429
|
-
}
|
|
430
|
-
assertCapabilityForMethod(method) {
|
|
431
|
-
var _a, _b, _c, _d, _e;
|
|
432
|
-
switch (method) {
|
|
433
|
-
case "logging/setLevel":
|
|
434
|
-
if (!((_a = this._serverCapabilities) === null || _a === void 0 ? void 0 : _a.logging)) {
|
|
435
|
-
throw new Error(`Server does not support logging (required for ${method})`);
|
|
436
|
-
}
|
|
437
|
-
break;
|
|
438
|
-
case "prompts/get":
|
|
439
|
-
case "prompts/list":
|
|
440
|
-
if (!((_b = this._serverCapabilities) === null || _b === void 0 ? void 0 : _b.prompts)) {
|
|
441
|
-
throw new Error(`Server does not support prompts (required for ${method})`);
|
|
442
|
-
}
|
|
443
|
-
break;
|
|
444
|
-
case "resources/list":
|
|
445
|
-
case "resources/templates/list":
|
|
446
|
-
case "resources/read":
|
|
447
|
-
case "resources/subscribe":
|
|
448
|
-
case "resources/unsubscribe":
|
|
449
|
-
if (!((_c = this._serverCapabilities) === null || _c === void 0 ? void 0 : _c.resources)) {
|
|
450
|
-
throw new Error(`Server does not support resources (required for ${method})`);
|
|
451
|
-
}
|
|
452
|
-
if (method === "resources/subscribe" && !this._serverCapabilities.resources.subscribe) {
|
|
453
|
-
throw new Error(`Server does not support resource subscriptions (required for ${method})`);
|
|
454
|
-
}
|
|
455
|
-
break;
|
|
456
|
-
case "tools/call":
|
|
457
|
-
case "tools/list":
|
|
458
|
-
if (!((_d = this._serverCapabilities) === null || _d === void 0 ? void 0 : _d.tools)) {
|
|
459
|
-
throw new Error(`Server does not support tools (required for ${method})`);
|
|
460
|
-
}
|
|
461
|
-
break;
|
|
462
|
-
case "completion/complete":
|
|
463
|
-
if (!((_e = this._serverCapabilities) === null || _e === void 0 ? void 0 : _e.prompts)) {
|
|
464
|
-
throw new Error(`Server does not support prompts (required for ${method})`);
|
|
465
|
-
}
|
|
466
|
-
break;
|
|
467
|
-
case "initialize":
|
|
468
|
-
break;
|
|
469
|
-
case "ping":
|
|
470
|
-
break;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
assertNotificationCapability(method) {
|
|
474
|
-
var _a;
|
|
475
|
-
switch (method) {
|
|
476
|
-
case "notifications/roots/list_changed":
|
|
477
|
-
if (!((_a = this._capabilities.roots) === null || _a === void 0 ? void 0 : _a.listChanged)) {
|
|
478
|
-
throw new Error(`Client does not support roots list changed notifications (required for ${method})`);
|
|
479
|
-
}
|
|
480
|
-
break;
|
|
481
|
-
case "notifications/initialized":
|
|
482
|
-
break;
|
|
483
|
-
case "notifications/cancelled":
|
|
484
|
-
break;
|
|
485
|
-
case "notifications/progress":
|
|
486
|
-
break;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
assertRequestHandlerCapability(method) {
|
|
490
|
-
switch (method) {
|
|
491
|
-
case "sampling/createMessage":
|
|
492
|
-
if (!this._capabilities.sampling) {
|
|
493
|
-
throw new Error(`Client does not support sampling capability (required for ${method})`);
|
|
494
|
-
}
|
|
495
|
-
break;
|
|
496
|
-
case "roots/list":
|
|
497
|
-
if (!this._capabilities.roots) {
|
|
498
|
-
throw new Error(`Client does not support roots capability (required for ${method})`);
|
|
499
|
-
}
|
|
500
|
-
break;
|
|
501
|
-
case "ping":
|
|
502
|
-
break;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
async ping(options) {
|
|
506
|
-
return this.request({ method: "ping" }, EmptyResultSchema, options);
|
|
507
|
-
}
|
|
508
|
-
async complete(params, options) {
|
|
509
|
-
return this.request({ method: "completion/complete", params }, CompleteResultSchema, options);
|
|
510
|
-
}
|
|
511
|
-
async setLoggingLevel(level, options) {
|
|
512
|
-
return this.request({ method: "logging/setLevel", params: { level } }, EmptyResultSchema, options);
|
|
513
|
-
}
|
|
514
|
-
async getPrompt(params, options) {
|
|
515
|
-
return this.request({ method: "prompts/get", params }, GetPromptResultSchema, options);
|
|
516
|
-
}
|
|
517
|
-
async listPrompts(params, options) {
|
|
518
|
-
return this.request({ method: "prompts/list", params }, ListPromptsResultSchema, options);
|
|
519
|
-
}
|
|
520
|
-
async listResources(params, options) {
|
|
521
|
-
return this.request({ method: "resources/list", params }, ListResourcesResultSchema, options);
|
|
522
|
-
}
|
|
523
|
-
async listResourceTemplates(params, options) {
|
|
524
|
-
return this.request({ method: "resources/templates/list", params }, ListResourceTemplatesResultSchema, options);
|
|
525
|
-
}
|
|
526
|
-
async readResource(params, options) {
|
|
527
|
-
return this.request({ method: "resources/read", params }, ReadResourceResultSchema, options);
|
|
528
|
-
}
|
|
529
|
-
async subscribeResource(params, options) {
|
|
530
|
-
return this.request({ method: "resources/subscribe", params }, EmptyResultSchema, options);
|
|
531
|
-
}
|
|
532
|
-
async unsubscribeResource(params, options) {
|
|
533
|
-
return this.request({ method: "resources/unsubscribe", params }, EmptyResultSchema, options);
|
|
534
|
-
}
|
|
535
|
-
async callTool(params, resultSchema = CallToolResultSchema, options) {
|
|
536
|
-
return this.request({ method: "tools/call", params }, resultSchema, options);
|
|
537
|
-
}
|
|
538
|
-
async listTools(params, options) {
|
|
539
|
-
return this.request({ method: "tools/list", params }, ListToolsResultSchema, options);
|
|
540
|
-
}
|
|
541
|
-
async sendRootsListChanged() {
|
|
542
|
-
return this.notification({ method: "notifications/roots/list_changed" });
|
|
543
|
-
}
|
|
544
|
-
};
|
|
545
|
-
|
|
546
|
-
// src/mcp/client-connection.ts
|
|
547
|
-
var MCPClientConnection = class {
|
|
548
|
-
constructor(url, info, opts = { transport: {}, client: {}, capabilities: {} }) {
|
|
549
|
-
this.info = info;
|
|
550
|
-
this.transport = new SSEEdgeClientTransport(url, opts.transport);
|
|
551
|
-
this.client = new Client(info, opts.client);
|
|
552
|
-
this.client.registerCapabilities(opts.capabilities);
|
|
553
|
-
this.connected = false;
|
|
554
|
-
this.tools = [];
|
|
555
|
-
this.prompts = [];
|
|
556
|
-
this.resources = [];
|
|
557
|
-
this.resourceTemplates = [];
|
|
558
|
-
}
|
|
559
|
-
client;
|
|
560
|
-
transport;
|
|
561
|
-
connected;
|
|
562
|
-
instructions;
|
|
563
|
-
tools;
|
|
564
|
-
prompts;
|
|
565
|
-
resources;
|
|
566
|
-
resourceTemplates;
|
|
567
|
-
serverCapabilities;
|
|
568
|
-
async init() {
|
|
569
|
-
await this.client.connect(this.transport);
|
|
570
|
-
this.serverCapabilities = await this.client.getServerCapabilities();
|
|
571
|
-
if (!this.serverCapabilities) {
|
|
572
|
-
throw new Error(
|
|
573
|
-
`The MCP Server ${this.info.name} failed to return server capabilities`
|
|
574
|
-
);
|
|
575
|
-
}
|
|
576
|
-
const [instructions, tools, resources, prompts, resourceTemplates] = await Promise.all([
|
|
577
|
-
this.client.getInstructions(),
|
|
578
|
-
this.registerTools(),
|
|
579
|
-
this.registerResources(),
|
|
580
|
-
this.registerPrompts(),
|
|
581
|
-
this.registerResourceTemplates()
|
|
582
|
-
]);
|
|
583
|
-
this.instructions = instructions;
|
|
584
|
-
this.tools = tools;
|
|
585
|
-
this.resources = resources;
|
|
586
|
-
this.prompts = prompts;
|
|
587
|
-
this.resourceTemplates = resourceTemplates;
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* Notification handler registration
|
|
591
|
-
*/
|
|
592
|
-
async registerTools() {
|
|
593
|
-
if (!this.serverCapabilities || !this.serverCapabilities.tools) {
|
|
594
|
-
return [];
|
|
595
|
-
}
|
|
596
|
-
if (this.serverCapabilities.tools.listChanged) {
|
|
597
|
-
this.client.setNotificationHandler(
|
|
598
|
-
ToolListChangedNotificationSchema,
|
|
599
|
-
async (_notification) => {
|
|
600
|
-
this.tools = await this.fetchTools();
|
|
601
|
-
}
|
|
602
|
-
);
|
|
603
|
-
}
|
|
604
|
-
return this.fetchTools();
|
|
605
|
-
}
|
|
606
|
-
async registerResources() {
|
|
607
|
-
if (!this.serverCapabilities || !this.serverCapabilities.resources) {
|
|
608
|
-
return [];
|
|
609
|
-
}
|
|
610
|
-
if (this.serverCapabilities.resources.listChanged) {
|
|
611
|
-
this.client.setNotificationHandler(
|
|
612
|
-
ResourceListChangedNotificationSchema,
|
|
613
|
-
async (_notification) => {
|
|
614
|
-
this.resources = await this.fetchResources();
|
|
615
|
-
}
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
return this.fetchResources();
|
|
619
|
-
}
|
|
620
|
-
async registerPrompts() {
|
|
621
|
-
if (!this.serverCapabilities || !this.serverCapabilities.prompts) {
|
|
622
|
-
return [];
|
|
623
|
-
}
|
|
624
|
-
if (this.serverCapabilities.prompts.listChanged) {
|
|
625
|
-
this.client.setNotificationHandler(
|
|
626
|
-
PromptListChangedNotificationSchema,
|
|
627
|
-
async (_notification) => {
|
|
628
|
-
this.prompts = await this.fetchPrompts();
|
|
629
|
-
}
|
|
630
|
-
);
|
|
631
|
-
}
|
|
632
|
-
return this.fetchPrompts();
|
|
633
|
-
}
|
|
634
|
-
async registerResourceTemplates() {
|
|
635
|
-
if (!this.serverCapabilities || !this.serverCapabilities.resources) {
|
|
636
|
-
return [];
|
|
637
|
-
}
|
|
638
|
-
return this.fetchResourceTemplates();
|
|
639
|
-
}
|
|
640
|
-
async fetchTools() {
|
|
641
|
-
let toolsAgg = [];
|
|
642
|
-
let toolsResult = { tools: [] };
|
|
643
|
-
do {
|
|
644
|
-
toolsResult = await this.client.listTools({
|
|
645
|
-
cursor: toolsResult.nextCursor
|
|
646
|
-
});
|
|
647
|
-
toolsAgg = toolsAgg.concat(toolsResult.tools);
|
|
648
|
-
} while (toolsResult.nextCursor);
|
|
649
|
-
return toolsAgg;
|
|
650
|
-
}
|
|
651
|
-
async fetchResources() {
|
|
652
|
-
let resourcesAgg = [];
|
|
653
|
-
let resourcesResult = { resources: [] };
|
|
654
|
-
do {
|
|
655
|
-
resourcesResult = await this.client.listResources({
|
|
656
|
-
cursor: resourcesResult.nextCursor
|
|
657
|
-
});
|
|
658
|
-
resourcesAgg = resourcesAgg.concat(resourcesResult.resources);
|
|
659
|
-
} while (resourcesResult.nextCursor);
|
|
660
|
-
return resourcesAgg;
|
|
661
|
-
}
|
|
662
|
-
async fetchPrompts() {
|
|
663
|
-
let promptsAgg = [];
|
|
664
|
-
let promptsResult = { prompts: [] };
|
|
665
|
-
do {
|
|
666
|
-
promptsResult = await this.client.listPrompts({
|
|
667
|
-
cursor: promptsResult.nextCursor
|
|
668
|
-
});
|
|
669
|
-
promptsAgg = promptsAgg.concat(promptsResult.prompts);
|
|
670
|
-
} while (promptsResult.nextCursor);
|
|
671
|
-
return promptsAgg;
|
|
672
|
-
}
|
|
673
|
-
async fetchResourceTemplates() {
|
|
674
|
-
let templatesAgg = [];
|
|
675
|
-
let templatesResult = {
|
|
676
|
-
resourceTemplates: []
|
|
677
|
-
};
|
|
678
|
-
do {
|
|
679
|
-
templatesResult = await this.client.listResourceTemplates({
|
|
680
|
-
cursor: templatesResult.nextCursor
|
|
681
|
-
});
|
|
682
|
-
templatesAgg = templatesAgg.concat(templatesResult.resourceTemplates);
|
|
683
|
-
} while (templatesResult.nextCursor);
|
|
684
|
-
return templatesAgg;
|
|
685
|
-
}
|
|
686
|
-
};
|
|
687
|
-
|
|
688
|
-
// src/mcp/client.ts
|
|
689
|
-
var MCPClientManager = class {
|
|
690
|
-
mcpConnections = {};
|
|
691
|
-
/**
|
|
692
|
-
* Connect to and register an MCP server
|
|
693
|
-
*
|
|
694
|
-
* @param transportConfig Transport config
|
|
695
|
-
* @param clientConfig Client config
|
|
696
|
-
* @param capabilities Client capabilities (i.e. if the client supports roots/sampling)
|
|
697
|
-
*/
|
|
698
|
-
async connectToServer(url, info, opts = { transport: {}, client: {}, capabilities: {} }) {
|
|
699
|
-
if (info.name in this.mcpConnections) {
|
|
700
|
-
throw new Error(
|
|
701
|
-
`An existing MCP client has already been registered under the name "${info.name}". The MCP client name must be unique.`
|
|
702
|
-
);
|
|
703
|
-
}
|
|
704
|
-
this.mcpConnections[info.name] = new MCPClientConnection(url, info, opts);
|
|
705
|
-
await this.mcpConnections[info.name].init();
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* @returns namespaced list of tools
|
|
709
|
-
*/
|
|
710
|
-
listTools() {
|
|
711
|
-
return getNamespacedData(this.mcpConnections, "tools");
|
|
712
|
-
}
|
|
713
|
-
/**
|
|
714
|
-
* @returns namespaced list of prompts
|
|
715
|
-
*/
|
|
716
|
-
listPrompts() {
|
|
717
|
-
return getNamespacedData(this.mcpConnections, "prompts");
|
|
718
|
-
}
|
|
719
|
-
/**
|
|
720
|
-
* @returns namespaced list of tools
|
|
721
|
-
*/
|
|
722
|
-
listResources() {
|
|
723
|
-
return getNamespacedData(this.mcpConnections, "resources");
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* @returns namespaced list of resource templates
|
|
727
|
-
*/
|
|
728
|
-
listResourceTemplates() {
|
|
729
|
-
return getNamespacedData(this.mcpConnections, "resourceTemplates");
|
|
730
|
-
}
|
|
731
|
-
/**
|
|
732
|
-
* Namespaced version of callTool
|
|
733
|
-
*/
|
|
734
|
-
callTool(params, resultSchema, options) {
|
|
735
|
-
const unqualifiedName = params.name.replace(`${params.serverName}.`, "");
|
|
736
|
-
return this.mcpConnections[params.serverName].client.callTool(
|
|
737
|
-
{
|
|
738
|
-
...params,
|
|
739
|
-
name: unqualifiedName
|
|
740
|
-
},
|
|
741
|
-
resultSchema,
|
|
742
|
-
options
|
|
743
|
-
);
|
|
744
|
-
}
|
|
745
|
-
/**
|
|
746
|
-
* Namespaced version of readResource
|
|
747
|
-
*/
|
|
748
|
-
readResource(params, options) {
|
|
749
|
-
return this.mcpConnections[params.serverName].client.readResource(
|
|
750
|
-
params,
|
|
751
|
-
options
|
|
752
|
-
);
|
|
753
|
-
}
|
|
754
|
-
/**
|
|
755
|
-
* Namespaced version of getPrompt
|
|
756
|
-
*/
|
|
757
|
-
getPrompt(params, options) {
|
|
758
|
-
return this.mcpConnections[params.serverName].client.getPrompt(
|
|
759
|
-
params,
|
|
760
|
-
options
|
|
761
|
-
);
|
|
762
|
-
}
|
|
763
|
-
};
|
|
764
|
-
function getNamespacedData(mcpClients, type) {
|
|
765
|
-
const sets = Object.entries(mcpClients).map(([name, conn]) => {
|
|
766
|
-
return { name, data: conn[type] };
|
|
767
|
-
});
|
|
768
|
-
const namespacedData = sets.flatMap(({ name: serverName, data }) => {
|
|
769
|
-
return data.map((item) => {
|
|
770
|
-
return {
|
|
771
|
-
...item,
|
|
772
|
-
// we add a servername so we can easily pull it out and convert between qualified<->unqualified name
|
|
773
|
-
// just in case the server name or item name includes a "."
|
|
774
|
-
serverName: `${serverName}`,
|
|
775
|
-
// qualified name
|
|
776
|
-
name: `${serverName}.${item.name}`
|
|
777
|
-
};
|
|
778
|
-
});
|
|
779
|
-
});
|
|
780
|
-
return namespacedData;
|
|
781
|
-
}
|
|
782
|
-
export {
|
|
783
|
-
MCPClientManager,
|
|
784
|
-
getNamespacedData
|
|
785
|
-
};
|
|
786
|
-
//# sourceMappingURL=client.js.map
|
|
3
|
+
export { MCPClientManager, getNamespacedData };
|