agents 0.7.3 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-chat-agent.js +3 -4
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.js +3 -4
- package/dist/ai-chat-v5-migration.js.map +1 -1
- package/dist/ai-react.js +3 -4
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.js +1 -2
- package/dist/ai-types.js.map +1 -1
- package/dist/cli/index.js +2 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/client-CgXIwdcc.js +1442 -0
- package/dist/client-CgXIwdcc.js.map +1 -0
- package/dist/client.js +1 -2
- package/dist/client.js.map +1 -1
- package/dist/codemode/ai.js +2 -2
- package/dist/do-oauth-client-provider-C2jurFjW.d.ts +78 -0
- package/dist/email-U_MG7UET.d.ts +157 -0
- package/dist/email.d.ts +16 -146
- package/dist/email.js +2 -2
- package/dist/email.js.map +1 -1
- package/dist/experimental/forever.d.ts +26 -71
- package/dist/experimental/forever.js +1 -2
- package/dist/experimental/forever.js.map +1 -1
- package/dist/experimental/memory/session/index.js +3 -12
- package/dist/experimental/memory/session/index.js.map +1 -1
- package/dist/experimental/sub-agent.js +1 -2
- package/dist/experimental/sub-agent.js.map +1 -1
- package/dist/experimental/workspace.d.ts +273 -0
- package/dist/experimental/workspace.js +1263 -0
- package/dist/experimental/workspace.js.map +1 -0
- package/dist/index-BS_jL8MI.d.ts +492 -0
- package/dist/index-p1XLNvwQ.d.ts +2719 -0
- package/dist/index.d.ts +45 -1320
- package/dist/index.js +40 -6
- package/dist/index.js.map +1 -1
- package/dist/internal_context-DgcmHqS1.d.ts +37 -0
- package/dist/internal_context.d.ts +5 -32
- package/dist/internal_context.js +1 -2
- package/dist/internal_context.js.map +1 -1
- package/dist/mcp/client.d.ts +2 -575
- package/dist/mcp/client.js +1 -847
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -61
- package/dist/mcp/do-oauth-client-provider.js +1 -2
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +2 -95
- package/dist/mcp/index.js +2 -9
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.js +1 -2
- package/dist/mcp/x402.js.map +1 -1
- package/dist/observability/index.d.ts +2 -93
- package/dist/observability/index.js +4 -3
- package/dist/observability/index.js.map +1 -1
- package/dist/react.d.ts +1 -2
- package/dist/react.js +1 -2
- package/dist/react.js.map +1 -1
- package/dist/retries-DXMQGhG3.d.ts +79 -0
- package/dist/retries.d.ts +7 -72
- package/dist/retries.js +1 -1
- package/dist/retries.js.map +1 -1
- package/dist/schedule.js +1 -2
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.js +1 -1
- package/dist/types-BB1plA51.d.ts +15 -0
- package/dist/types.d.ts +1 -14
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/workflow-types-CZNXKj_D.d.ts +260 -0
- package/dist/workflow-types.d.ts +23 -235
- package/dist/workflow-types.js +1 -1
- package/dist/workflow-types.js.map +1 -1
- package/dist/workflows.d.ts +22 -23
- package/dist/workflows.js +5 -6
- package/dist/workflows.js.map +1 -1
- package/package.json +15 -5
- package/dist/agent-eZnMHidZ.d.ts +0 -273
- package/dist/client-connection-D3Wcd6Q6.js +0 -603
- package/dist/client-connection-D3Wcd6Q6.js.map +0 -1
- package/dist/client-storage-yDVwzgfF.d.ts +0 -604
- package/dist/mcp/client.js.map +0 -1
|
@@ -1,603 +0,0 @@
|
|
|
1
|
-
import { getServerByName } from "partyserver";
|
|
2
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
3
|
-
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
4
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
5
|
-
import { ElicitRequestSchema, JSONRPCMessageSchema, PromptListChangedNotificationSchema, ResourceListChangedNotificationSchema, ToolListChangedNotificationSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
6
|
-
|
|
7
|
-
//#region src/core/events.ts
|
|
8
|
-
function toDisposable(fn) {
|
|
9
|
-
return { dispose: fn };
|
|
10
|
-
}
|
|
11
|
-
var DisposableStore = class {
|
|
12
|
-
constructor() {
|
|
13
|
-
this._items = [];
|
|
14
|
-
}
|
|
15
|
-
add(d) {
|
|
16
|
-
this._items.push(d);
|
|
17
|
-
return d;
|
|
18
|
-
}
|
|
19
|
-
dispose() {
|
|
20
|
-
while (this._items.length) try {
|
|
21
|
-
this._items.pop().dispose();
|
|
22
|
-
} catch {}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
var Emitter = class {
|
|
26
|
-
constructor() {
|
|
27
|
-
this._listeners = /* @__PURE__ */ new Set();
|
|
28
|
-
this.event = (listener) => {
|
|
29
|
-
this._listeners.add(listener);
|
|
30
|
-
return toDisposable(() => this._listeners.delete(listener));
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
fire(data) {
|
|
34
|
-
for (const listener of [...this._listeners]) try {
|
|
35
|
-
listener(data);
|
|
36
|
-
} catch (err) {
|
|
37
|
-
console.error("Emitter listener error:", err);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
dispose() {
|
|
41
|
-
this._listeners.clear();
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
//#endregion
|
|
46
|
-
//#region src/mcp/errors.ts
|
|
47
|
-
function toErrorMessage(error) {
|
|
48
|
-
return error instanceof Error ? error.message : String(error);
|
|
49
|
-
}
|
|
50
|
-
function getErrorCode(error) {
|
|
51
|
-
if (error && typeof error === "object" && "code" in error && typeof error.code === "number") return error.code;
|
|
52
|
-
}
|
|
53
|
-
function isUnauthorized(error) {
|
|
54
|
-
if (getErrorCode(error) === 401) return true;
|
|
55
|
-
const msg = toErrorMessage(error);
|
|
56
|
-
return msg.includes("Unauthorized") || msg.includes("401");
|
|
57
|
-
}
|
|
58
|
-
function isTransportNotImplemented(error) {
|
|
59
|
-
const code = getErrorCode(error);
|
|
60
|
-
if (code === 404 || code === 405) return true;
|
|
61
|
-
const msg = toErrorMessage(error);
|
|
62
|
-
return msg.includes("404") || msg.includes("405") || msg.includes("Not Implemented") || msg.includes("not implemented");
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/mcp/rpc.ts
|
|
67
|
-
const RPC_DO_PREFIX = "rpc:";
|
|
68
|
-
function makeInvalidRequestError(id) {
|
|
69
|
-
return {
|
|
70
|
-
jsonrpc: "2.0",
|
|
71
|
-
id: id ?? null,
|
|
72
|
-
error: {
|
|
73
|
-
code: -32600,
|
|
74
|
-
message: "Invalid Request"
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
function validateBatch(batch) {
|
|
79
|
-
if (batch.length === 0) throw new Error("Invalid JSON-RPC batch: array must not be empty");
|
|
80
|
-
}
|
|
81
|
-
var RPCClientTransport = class {
|
|
82
|
-
constructor(options) {
|
|
83
|
-
this._started = false;
|
|
84
|
-
this._namespace = options.namespace;
|
|
85
|
-
this._name = options.name;
|
|
86
|
-
this._props = options.props;
|
|
87
|
-
}
|
|
88
|
-
setProtocolVersion(version) {
|
|
89
|
-
this._protocolVersion = version;
|
|
90
|
-
}
|
|
91
|
-
getProtocolVersion() {
|
|
92
|
-
return this._protocolVersion;
|
|
93
|
-
}
|
|
94
|
-
async start() {
|
|
95
|
-
if (this._started) throw new Error("Transport already started");
|
|
96
|
-
const doName = `${RPC_DO_PREFIX}${this._name}`;
|
|
97
|
-
this._stub = await getServerByName(this._namespace, doName, { props: this._props });
|
|
98
|
-
this._started = true;
|
|
99
|
-
}
|
|
100
|
-
async close() {
|
|
101
|
-
this._started = false;
|
|
102
|
-
this._stub = void 0;
|
|
103
|
-
this.onclose?.();
|
|
104
|
-
}
|
|
105
|
-
async send(message, options) {
|
|
106
|
-
if (!this._started || !this._stub) throw new Error("Transport not started");
|
|
107
|
-
try {
|
|
108
|
-
const result = await this._stub.handleMcpMessage(message);
|
|
109
|
-
if (!result) return;
|
|
110
|
-
const extra = options?.relatedRequestId ? { requestInfo: { headers: {} } } : void 0;
|
|
111
|
-
const messages = Array.isArray(result) ? result : [result];
|
|
112
|
-
for (const msg of messages) this.onmessage?.(msg, extra);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
this.onerror?.(error);
|
|
115
|
-
throw error;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
var RPCServerTransport = class {
|
|
120
|
-
constructor(options) {
|
|
121
|
-
this._started = false;
|
|
122
|
-
this._pendingResponse = null;
|
|
123
|
-
this._responseResolver = null;
|
|
124
|
-
this._timeout = options?.timeout ?? 6e4;
|
|
125
|
-
}
|
|
126
|
-
setProtocolVersion(version) {
|
|
127
|
-
this._protocolVersion = version;
|
|
128
|
-
}
|
|
129
|
-
getProtocolVersion() {
|
|
130
|
-
return this._protocolVersion;
|
|
131
|
-
}
|
|
132
|
-
async start() {
|
|
133
|
-
if (this._started) throw new Error("Transport already started");
|
|
134
|
-
this._started = true;
|
|
135
|
-
}
|
|
136
|
-
async close() {
|
|
137
|
-
this._started = false;
|
|
138
|
-
this.onclose?.();
|
|
139
|
-
if (this._responseResolver) {
|
|
140
|
-
this._responseResolver();
|
|
141
|
-
this._responseResolver = null;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
async send(message, _options) {
|
|
145
|
-
if (!this._started) throw new Error("Transport not started");
|
|
146
|
-
if (!this._pendingResponse) this._pendingResponse = message;
|
|
147
|
-
else if (Array.isArray(this._pendingResponse)) this._pendingResponse.push(message);
|
|
148
|
-
else this._pendingResponse = [this._pendingResponse, message];
|
|
149
|
-
if (this._responseResolver) {
|
|
150
|
-
const resolver = this._responseResolver;
|
|
151
|
-
queueMicrotask(() => resolver());
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
async handle(message) {
|
|
155
|
-
if (!this._started) throw new Error("Transport not started");
|
|
156
|
-
if (Array.isArray(message)) {
|
|
157
|
-
validateBatch(message);
|
|
158
|
-
const responses = [];
|
|
159
|
-
for (const msg of message) {
|
|
160
|
-
const response = await this.handle(msg);
|
|
161
|
-
if (response !== void 0) if (Array.isArray(response)) responses.push(...response);
|
|
162
|
-
else responses.push(response);
|
|
163
|
-
}
|
|
164
|
-
return responses.length === 0 ? void 0 : responses;
|
|
165
|
-
}
|
|
166
|
-
try {
|
|
167
|
-
JSONRPCMessageSchema.parse(message);
|
|
168
|
-
} catch {
|
|
169
|
-
return makeInvalidRequestError(typeof message === "object" && message !== null && "id" in message ? message.id : null);
|
|
170
|
-
}
|
|
171
|
-
this._pendingResponse = null;
|
|
172
|
-
if (!("id" in message)) {
|
|
173
|
-
this.onmessage?.(message);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
let timeoutId = null;
|
|
177
|
-
const responsePromise = new Promise((resolve, reject) => {
|
|
178
|
-
timeoutId = setTimeout(() => {
|
|
179
|
-
this._responseResolver = null;
|
|
180
|
-
reject(/* @__PURE__ */ new Error(`Request timeout: No response received within ${this._timeout}ms`));
|
|
181
|
-
}, this._timeout);
|
|
182
|
-
this._responseResolver = () => {
|
|
183
|
-
if (timeoutId) {
|
|
184
|
-
clearTimeout(timeoutId);
|
|
185
|
-
timeoutId = null;
|
|
186
|
-
}
|
|
187
|
-
this._responseResolver = null;
|
|
188
|
-
resolve();
|
|
189
|
-
};
|
|
190
|
-
});
|
|
191
|
-
this.onmessage?.(message);
|
|
192
|
-
try {
|
|
193
|
-
await responsePromise;
|
|
194
|
-
} catch (error) {
|
|
195
|
-
this._pendingResponse = null;
|
|
196
|
-
this._responseResolver = null;
|
|
197
|
-
throw error;
|
|
198
|
-
}
|
|
199
|
-
const response = this._pendingResponse;
|
|
200
|
-
this._pendingResponse = null;
|
|
201
|
-
return response ?? void 0;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
//#endregion
|
|
206
|
-
//#region src/mcp/client-connection.ts
|
|
207
|
-
/**
|
|
208
|
-
* Connection state machine for MCP client connections.
|
|
209
|
-
*
|
|
210
|
-
* State transitions:
|
|
211
|
-
* - Non-OAuth: init() → CONNECTING → DISCOVERING → READY
|
|
212
|
-
* - OAuth: init() → AUTHENTICATING → (callback) → CONNECTING → DISCOVERING → READY
|
|
213
|
-
* - Any state can transition to FAILED on error
|
|
214
|
-
*/
|
|
215
|
-
const MCPConnectionState = {
|
|
216
|
-
AUTHENTICATING: "authenticating",
|
|
217
|
-
CONNECTING: "connecting",
|
|
218
|
-
CONNECTED: "connected",
|
|
219
|
-
DISCOVERING: "discovering",
|
|
220
|
-
READY: "ready",
|
|
221
|
-
FAILED: "failed"
|
|
222
|
-
};
|
|
223
|
-
var MCPClientConnection = class {
|
|
224
|
-
constructor(url, info, options = {
|
|
225
|
-
client: {},
|
|
226
|
-
transport: {}
|
|
227
|
-
}) {
|
|
228
|
-
this.url = url;
|
|
229
|
-
this.options = options;
|
|
230
|
-
this.connectionState = MCPConnectionState.CONNECTING;
|
|
231
|
-
this.connectionError = null;
|
|
232
|
-
this.tools = [];
|
|
233
|
-
this.prompts = [];
|
|
234
|
-
this.resources = [];
|
|
235
|
-
this.resourceTemplates = [];
|
|
236
|
-
this._onObservabilityEvent = new Emitter();
|
|
237
|
-
this.onObservabilityEvent = this._onObservabilityEvent.event;
|
|
238
|
-
this.client = new Client(info, {
|
|
239
|
-
...options.client,
|
|
240
|
-
capabilities: {
|
|
241
|
-
...options.client?.capabilities,
|
|
242
|
-
elicitation: {}
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Initialize a client connection, if authentication is required, the connection will be in the AUTHENTICATING state
|
|
248
|
-
* Sets connection state based on the result and emits observability events
|
|
249
|
-
*
|
|
250
|
-
* @returns Error message if connection failed, undefined otherwise
|
|
251
|
-
*/
|
|
252
|
-
async init() {
|
|
253
|
-
const transportType = this.options.transport.type;
|
|
254
|
-
if (!transportType) throw new Error("Transport type must be specified");
|
|
255
|
-
const res = await this.tryConnect(transportType);
|
|
256
|
-
this.connectionState = res.state;
|
|
257
|
-
if (res.state === MCPConnectionState.CONNECTED && res.transport) {
|
|
258
|
-
this.client.setRequestHandler(ElicitRequestSchema, async (request) => {
|
|
259
|
-
return await this.handleElicitationRequest(request);
|
|
260
|
-
});
|
|
261
|
-
this.lastConnectedTransport = res.transport;
|
|
262
|
-
this._onObservabilityEvent.fire({
|
|
263
|
-
type: "mcp:client:connect",
|
|
264
|
-
payload: {
|
|
265
|
-
url: this.url.toString(),
|
|
266
|
-
transport: res.transport,
|
|
267
|
-
state: this.connectionState
|
|
268
|
-
},
|
|
269
|
-
timestamp: Date.now()
|
|
270
|
-
});
|
|
271
|
-
return;
|
|
272
|
-
} else if (res.state === MCPConnectionState.FAILED && res.error) {
|
|
273
|
-
const errorMessage = toErrorMessage(res.error);
|
|
274
|
-
this._onObservabilityEvent.fire({
|
|
275
|
-
type: "mcp:client:connect",
|
|
276
|
-
payload: {
|
|
277
|
-
url: this.url.toString(),
|
|
278
|
-
transport: transportType,
|
|
279
|
-
state: this.connectionState,
|
|
280
|
-
error: errorMessage
|
|
281
|
-
},
|
|
282
|
-
timestamp: Date.now()
|
|
283
|
-
});
|
|
284
|
-
return errorMessage;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Finish OAuth by probing transports based on configured type.
|
|
289
|
-
* - Explicit: finish on that transport
|
|
290
|
-
* - Auto: try streamable-http, then sse on 404/405/Not Implemented
|
|
291
|
-
*/
|
|
292
|
-
async finishAuthProbe(code) {
|
|
293
|
-
if (!this.options.transport.authProvider) throw new Error("No auth provider configured");
|
|
294
|
-
const configuredType = this.options.transport.type;
|
|
295
|
-
if (!configuredType) throw new Error("Transport type must be specified");
|
|
296
|
-
const finishAuth = async (base) => {
|
|
297
|
-
const transport = this.getTransport(base);
|
|
298
|
-
if ("finishAuth" in transport && typeof transport.finishAuth === "function") await transport.finishAuth(code);
|
|
299
|
-
};
|
|
300
|
-
if (configuredType === "rpc") throw new Error("RPC transport does not support authentication");
|
|
301
|
-
if (configuredType === "sse" || configuredType === "streamable-http") {
|
|
302
|
-
await finishAuth(configuredType);
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
try {
|
|
306
|
-
await finishAuth("streamable-http");
|
|
307
|
-
} catch (e) {
|
|
308
|
-
if (isTransportNotImplemented(e)) {
|
|
309
|
-
await finishAuth("sse");
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
throw e;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Complete OAuth authorization
|
|
317
|
-
*/
|
|
318
|
-
async completeAuthorization(code) {
|
|
319
|
-
if (this.connectionState !== MCPConnectionState.AUTHENTICATING) throw new Error("Connection must be in authenticating state to complete authorization");
|
|
320
|
-
try {
|
|
321
|
-
await this.finishAuthProbe(code);
|
|
322
|
-
this.connectionState = MCPConnectionState.CONNECTING;
|
|
323
|
-
} catch (error) {
|
|
324
|
-
this.connectionState = MCPConnectionState.FAILED;
|
|
325
|
-
throw error;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Discover server capabilities and register tools, resources, prompts, and templates.
|
|
330
|
-
* This method does the work but does not manage connection state - that's handled by discover().
|
|
331
|
-
*/
|
|
332
|
-
async discoverAndRegister() {
|
|
333
|
-
this.serverCapabilities = this.client.getServerCapabilities();
|
|
334
|
-
if (!this.serverCapabilities) throw new Error("The MCP Server failed to return server capabilities");
|
|
335
|
-
const operations = [];
|
|
336
|
-
const operationNames = [];
|
|
337
|
-
operations.push(Promise.resolve(this.client.getInstructions()));
|
|
338
|
-
operationNames.push("instructions");
|
|
339
|
-
if (this.serverCapabilities.tools) {
|
|
340
|
-
operations.push(this.registerTools());
|
|
341
|
-
operationNames.push("tools");
|
|
342
|
-
}
|
|
343
|
-
if (this.serverCapabilities.resources) {
|
|
344
|
-
operations.push(this.registerResources());
|
|
345
|
-
operationNames.push("resources");
|
|
346
|
-
}
|
|
347
|
-
if (this.serverCapabilities.prompts) {
|
|
348
|
-
operations.push(this.registerPrompts());
|
|
349
|
-
operationNames.push("prompts");
|
|
350
|
-
}
|
|
351
|
-
if (this.serverCapabilities.resources) {
|
|
352
|
-
operations.push(this.registerResourceTemplates());
|
|
353
|
-
operationNames.push("resource templates");
|
|
354
|
-
}
|
|
355
|
-
try {
|
|
356
|
-
const results = await Promise.all(operations);
|
|
357
|
-
for (let i = 0; i < results.length; i++) {
|
|
358
|
-
const result = results[i];
|
|
359
|
-
switch (operationNames[i]) {
|
|
360
|
-
case "instructions":
|
|
361
|
-
this.instructions = result;
|
|
362
|
-
break;
|
|
363
|
-
case "tools":
|
|
364
|
-
this.tools = result;
|
|
365
|
-
break;
|
|
366
|
-
case "resources":
|
|
367
|
-
this.resources = result;
|
|
368
|
-
break;
|
|
369
|
-
case "prompts":
|
|
370
|
-
this.prompts = result;
|
|
371
|
-
break;
|
|
372
|
-
case "resource templates":
|
|
373
|
-
this.resourceTemplates = result;
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
} catch (error) {
|
|
378
|
-
this._onObservabilityEvent.fire({
|
|
379
|
-
type: "mcp:client:discover",
|
|
380
|
-
payload: {
|
|
381
|
-
url: this.url.toString(),
|
|
382
|
-
error: toErrorMessage(error)
|
|
383
|
-
},
|
|
384
|
-
timestamp: Date.now()
|
|
385
|
-
});
|
|
386
|
-
throw error;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Discover server capabilities with timeout and cancellation support.
|
|
391
|
-
* If called while a previous discovery is in-flight, the previous discovery will be aborted.
|
|
392
|
-
*
|
|
393
|
-
* @param options Optional configuration
|
|
394
|
-
* @param options.timeoutMs Timeout in milliseconds (default: 15000)
|
|
395
|
-
* @returns Result indicating success/failure with optional error message
|
|
396
|
-
*/
|
|
397
|
-
async discover(options = {}) {
|
|
398
|
-
const { timeoutMs = 15e3 } = options;
|
|
399
|
-
if (this.connectionState !== MCPConnectionState.CONNECTED && this.connectionState !== MCPConnectionState.READY) {
|
|
400
|
-
this._onObservabilityEvent.fire({
|
|
401
|
-
type: "mcp:client:discover",
|
|
402
|
-
payload: {
|
|
403
|
-
url: this.url.toString(),
|
|
404
|
-
state: this.connectionState
|
|
405
|
-
},
|
|
406
|
-
timestamp: Date.now()
|
|
407
|
-
});
|
|
408
|
-
return {
|
|
409
|
-
success: false,
|
|
410
|
-
error: `Discovery skipped - connection in ${this.connectionState} state`
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
if (this._discoveryAbortController) {
|
|
414
|
-
this._discoveryAbortController.abort();
|
|
415
|
-
this._discoveryAbortController = void 0;
|
|
416
|
-
}
|
|
417
|
-
const abortController = new AbortController();
|
|
418
|
-
this._discoveryAbortController = abortController;
|
|
419
|
-
this.connectionState = MCPConnectionState.DISCOVERING;
|
|
420
|
-
let timeoutId;
|
|
421
|
-
try {
|
|
422
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
423
|
-
timeoutId = setTimeout(() => reject(/* @__PURE__ */ new Error(`Discovery timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
424
|
-
});
|
|
425
|
-
if (abortController.signal.aborted) throw new Error("Discovery was cancelled");
|
|
426
|
-
const abortPromise = new Promise((_, reject) => {
|
|
427
|
-
abortController.signal.addEventListener("abort", () => {
|
|
428
|
-
reject(/* @__PURE__ */ new Error("Discovery was cancelled"));
|
|
429
|
-
});
|
|
430
|
-
});
|
|
431
|
-
await Promise.race([
|
|
432
|
-
this.discoverAndRegister(),
|
|
433
|
-
timeoutPromise,
|
|
434
|
-
abortPromise
|
|
435
|
-
]);
|
|
436
|
-
if (timeoutId !== void 0) clearTimeout(timeoutId);
|
|
437
|
-
this.connectionState = MCPConnectionState.READY;
|
|
438
|
-
this._onObservabilityEvent.fire({
|
|
439
|
-
type: "mcp:client:discover",
|
|
440
|
-
payload: { url: this.url.toString() },
|
|
441
|
-
timestamp: Date.now()
|
|
442
|
-
});
|
|
443
|
-
return { success: true };
|
|
444
|
-
} catch (e) {
|
|
445
|
-
if (timeoutId !== void 0) clearTimeout(timeoutId);
|
|
446
|
-
this.connectionState = MCPConnectionState.CONNECTED;
|
|
447
|
-
return {
|
|
448
|
-
success: false,
|
|
449
|
-
error: e instanceof Error ? e.message : String(e)
|
|
450
|
-
};
|
|
451
|
-
} finally {
|
|
452
|
-
this._discoveryAbortController = void 0;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Cancel any in-flight discovery operation.
|
|
457
|
-
* Called when closing the connection.
|
|
458
|
-
*/
|
|
459
|
-
cancelDiscovery() {
|
|
460
|
-
if (this._discoveryAbortController) {
|
|
461
|
-
this._discoveryAbortController.abort();
|
|
462
|
-
this._discoveryAbortController = void 0;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Notification handler registration for tools
|
|
467
|
-
* Should only be called if serverCapabilities.tools exists
|
|
468
|
-
*/
|
|
469
|
-
async registerTools() {
|
|
470
|
-
if (this.serverCapabilities?.tools?.listChanged) this.client.setNotificationHandler(ToolListChangedNotificationSchema, async (_notification) => {
|
|
471
|
-
this.tools = await this.fetchTools();
|
|
472
|
-
});
|
|
473
|
-
return this.fetchTools();
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Notification handler registration for resources
|
|
477
|
-
* Should only be called if serverCapabilities.resources exists
|
|
478
|
-
*/
|
|
479
|
-
async registerResources() {
|
|
480
|
-
if (this.serverCapabilities?.resources?.listChanged) this.client.setNotificationHandler(ResourceListChangedNotificationSchema, async (_notification) => {
|
|
481
|
-
this.resources = await this.fetchResources();
|
|
482
|
-
});
|
|
483
|
-
return this.fetchResources();
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Notification handler registration for prompts
|
|
487
|
-
* Should only be called if serverCapabilities.prompts exists
|
|
488
|
-
*/
|
|
489
|
-
async registerPrompts() {
|
|
490
|
-
if (this.serverCapabilities?.prompts?.listChanged) this.client.setNotificationHandler(PromptListChangedNotificationSchema, async (_notification) => {
|
|
491
|
-
this.prompts = await this.fetchPrompts();
|
|
492
|
-
});
|
|
493
|
-
return this.fetchPrompts();
|
|
494
|
-
}
|
|
495
|
-
async registerResourceTemplates() {
|
|
496
|
-
return this.fetchResourceTemplates();
|
|
497
|
-
}
|
|
498
|
-
async fetchTools() {
|
|
499
|
-
let toolsAgg = [];
|
|
500
|
-
let toolsResult = { tools: [] };
|
|
501
|
-
do {
|
|
502
|
-
toolsResult = await this.client.listTools({ cursor: toolsResult.nextCursor }).catch(this._capabilityErrorHandler({ tools: [] }, "tools/list"));
|
|
503
|
-
toolsAgg = toolsAgg.concat(toolsResult.tools);
|
|
504
|
-
} while (toolsResult.nextCursor);
|
|
505
|
-
return toolsAgg;
|
|
506
|
-
}
|
|
507
|
-
async fetchResources() {
|
|
508
|
-
let resourcesAgg = [];
|
|
509
|
-
let resourcesResult = { resources: [] };
|
|
510
|
-
do {
|
|
511
|
-
resourcesResult = await this.client.listResources({ cursor: resourcesResult.nextCursor }).catch(this._capabilityErrorHandler({ resources: [] }, "resources/list"));
|
|
512
|
-
resourcesAgg = resourcesAgg.concat(resourcesResult.resources);
|
|
513
|
-
} while (resourcesResult.nextCursor);
|
|
514
|
-
return resourcesAgg;
|
|
515
|
-
}
|
|
516
|
-
async fetchPrompts() {
|
|
517
|
-
let promptsAgg = [];
|
|
518
|
-
let promptsResult = { prompts: [] };
|
|
519
|
-
do {
|
|
520
|
-
promptsResult = await this.client.listPrompts({ cursor: promptsResult.nextCursor }).catch(this._capabilityErrorHandler({ prompts: [] }, "prompts/list"));
|
|
521
|
-
promptsAgg = promptsAgg.concat(promptsResult.prompts);
|
|
522
|
-
} while (promptsResult.nextCursor);
|
|
523
|
-
return promptsAgg;
|
|
524
|
-
}
|
|
525
|
-
async fetchResourceTemplates() {
|
|
526
|
-
let templatesAgg = [];
|
|
527
|
-
let templatesResult = { resourceTemplates: [] };
|
|
528
|
-
do {
|
|
529
|
-
templatesResult = await this.client.listResourceTemplates({ cursor: templatesResult.nextCursor }).catch(this._capabilityErrorHandler({ resourceTemplates: [] }, "resources/templates/list"));
|
|
530
|
-
templatesAgg = templatesAgg.concat(templatesResult.resourceTemplates);
|
|
531
|
-
} while (templatesResult.nextCursor);
|
|
532
|
-
return templatesAgg;
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Handle elicitation request from server
|
|
536
|
-
* Automatically uses the Agent's built-in elicitation handling if available
|
|
537
|
-
*/
|
|
538
|
-
async handleElicitationRequest(_request) {
|
|
539
|
-
throw new Error("Elicitation handler must be implemented for your platform. Override handleElicitationRequest method.");
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Get the transport for the client
|
|
543
|
-
* @param transportType - The transport type to get
|
|
544
|
-
* @returns The transport for the client
|
|
545
|
-
*/
|
|
546
|
-
getTransport(transportType) {
|
|
547
|
-
switch (transportType) {
|
|
548
|
-
case "streamable-http": return new StreamableHTTPClientTransport(this.url, this.options.transport);
|
|
549
|
-
case "sse": return new SSEClientTransport(this.url, this.options.transport);
|
|
550
|
-
case "rpc": return new RPCClientTransport(this.options.transport);
|
|
551
|
-
default: throw new Error(`Unsupported transport type: ${transportType}`);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
async tryConnect(transportType) {
|
|
555
|
-
const transports = transportType === "auto" ? ["streamable-http", "sse"] : [transportType];
|
|
556
|
-
for (const currentTransportType of transports) {
|
|
557
|
-
const isLastTransport = currentTransportType === transports[transports.length - 1];
|
|
558
|
-
const hasFallback = transportType === "auto" && currentTransportType === "streamable-http" && !isLastTransport;
|
|
559
|
-
const transport = this.getTransport(currentTransportType);
|
|
560
|
-
try {
|
|
561
|
-
await this.client.connect(transport);
|
|
562
|
-
return {
|
|
563
|
-
state: MCPConnectionState.CONNECTED,
|
|
564
|
-
transport: currentTransportType
|
|
565
|
-
};
|
|
566
|
-
} catch (e) {
|
|
567
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
568
|
-
if (isUnauthorized(error)) return { state: MCPConnectionState.AUTHENTICATING };
|
|
569
|
-
if (isTransportNotImplemented(error) && hasFallback) continue;
|
|
570
|
-
return {
|
|
571
|
-
state: MCPConnectionState.FAILED,
|
|
572
|
-
error
|
|
573
|
-
};
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
return {
|
|
577
|
-
state: MCPConnectionState.FAILED,
|
|
578
|
-
error: /* @__PURE__ */ new Error("No transports available")
|
|
579
|
-
};
|
|
580
|
-
}
|
|
581
|
-
_capabilityErrorHandler(empty, method) {
|
|
582
|
-
return (e) => {
|
|
583
|
-
if (e.code === -32601) {
|
|
584
|
-
const url = this.url.toString();
|
|
585
|
-
this._onObservabilityEvent.fire({
|
|
586
|
-
type: "mcp:client:discover",
|
|
587
|
-
payload: {
|
|
588
|
-
url,
|
|
589
|
-
capability: method.split("/")[0],
|
|
590
|
-
error: toErrorMessage(e)
|
|
591
|
-
},
|
|
592
|
-
timestamp: Date.now()
|
|
593
|
-
});
|
|
594
|
-
return empty;
|
|
595
|
-
}
|
|
596
|
-
throw e;
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
};
|
|
600
|
-
|
|
601
|
-
//#endregion
|
|
602
|
-
export { RPC_DO_PREFIX as a, Emitter as c, RPCServerTransport as i, MCPConnectionState as n, toErrorMessage as o, RPCClientTransport as r, DisposableStore as s, MCPClientConnection as t };
|
|
603
|
-
//# sourceMappingURL=client-connection-D3Wcd6Q6.js.map
|