agents 0.0.0-e777fdd → 0.0.0-e7d0d4d
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.d.ts +16 -17
- package/dist/ai-chat-agent.js +532 -352
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-DBHGW4Hv.js +155 -0
- package/dist/ai-chat-v5-migration-DBHGW4Hv.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +7 -4
- package/dist/ai-chat-v5-migration.js +3 -20
- package/dist/ai-react.d.ts +14 -23
- package/dist/ai-react.js +258 -349
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-B3aQaFv3.js +20 -0
- package/dist/ai-types-B3aQaFv3.js.map +1 -0
- package/dist/ai-types-D5YoPrBZ.d.ts +95 -0
- package/dist/ai-types.d.ts +6 -92
- package/dist/ai-types.js +3 -8
- package/dist/client-9Ld2_lnt.js +786 -0
- package/dist/client-9Ld2_lnt.js.map +1 -0
- package/dist/client-BfiZ3HQd.js +117 -0
- package/dist/client-BfiZ3HQd.js.map +1 -0
- package/dist/client-CbWe9FBd.d.ts +104 -0
- package/dist/client-Csp_m13H.d.ts +5315 -0
- package/dist/client.d.ts +11 -92
- package/dist/client.js +4 -13
- 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-CswoD5Lu.js +93 -0
- package/dist/do-oauth-client-provider-CswoD5Lu.js.map +1 -0
- package/dist/do-oauth-client-provider-DGc5pP0l.d.ts +55 -0
- package/dist/index-DFqsR7mb.d.ts +560 -0
- package/dist/index-DhJCaDWd.d.ts +58 -0
- package/dist/index.d.ts +55 -556
- package/dist/index.js +7 -32
- package/dist/mcp/client.d.ts +4 -11
- package/dist/mcp/client.js +3 -10
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -50
- package/dist/mcp/do-oauth-client-provider.js +3 -8
- package/dist/mcp/index.d.ts +151 -75
- package/dist/mcp/index.js +1355 -1081
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +22 -27
- package/dist/mcp/x402.js +186 -3187
- package/dist/mcp/x402.js.map +1 -1
- package/dist/mcp-Dw5vDrY8.d.ts +61 -0
- package/dist/observability/index.d.ts +3 -46
- package/dist/observability/index.js +7 -12
- package/dist/react-NCPvtyCY.d.ts +115 -0
- package/dist/react.d.ts +10 -123
- package/dist/react.js +183 -113
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +13 -10
- package/dist/schedule.js +43 -33
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-CymX8ovI.d.ts +39 -0
- package/dist/serializable.d.ts +7 -32
- package/dist/serializable.js +1 -1
- package/dist/src-Dz0H9hSU.js +1200 -0
- package/dist/src-Dz0H9hSU.js.map +1 -0
- package/package.json +30 -12
- package/dist/_esm-LV5FJ3HK.js +0 -3922
- package/dist/_esm-LV5FJ3HK.js.map +0 -1
- package/dist/ai-chat-v5-migration.js.map +0 -1
- package/dist/ai-types.js.map +0 -1
- package/dist/ccip-CMBYN64O.js +0 -15
- package/dist/ccip-CMBYN64O.js.map +0 -1
- package/dist/chunk-5Y6BEZDY.js +0 -276
- package/dist/chunk-5Y6BEZDY.js.map +0 -1
- package/dist/chunk-BER7KXUJ.js +0 -18
- package/dist/chunk-BER7KXUJ.js.map +0 -1
- package/dist/chunk-JJBFIGUC.js +0 -5202
- package/dist/chunk-JJBFIGUC.js.map +0 -1
- package/dist/chunk-PR4QN5HX.js +0 -43
- package/dist/chunk-PR4QN5HX.js.map +0 -1
- package/dist/chunk-QEPGNUG6.js +0 -650
- package/dist/chunk-QEPGNUG6.js.map +0 -1
- package/dist/chunk-QEVM4BVL.js +0 -116
- package/dist/chunk-QEVM4BVL.js.map +0 -1
- package/dist/chunk-RS5OCNEQ.js +0 -1323
- package/dist/chunk-RS5OCNEQ.js.map +0 -1
- package/dist/chunk-TYAY6AU6.js +0 -159
- package/dist/chunk-TYAY6AU6.js.map +0 -1
- package/dist/chunk-UJVEAURM.js +0 -150
- package/dist/chunk-UJVEAURM.js.map +0 -1
- package/dist/chunk-XFS5ERG3.js +0 -127
- package/dist/chunk-XFS5ERG3.js.map +0 -1
- package/dist/client-BohGLma8.d.ts +0 -5041
- 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/do-oauth-client-provider.js.map +0 -1
- package/dist/observability/index.js.map +0 -1
- package/dist/secp256k1-M22GZP2U.js +0 -2193
- package/dist/secp256k1-M22GZP2U.js.map +0 -1
- package/dist/serializable.js.map +0 -1
- package/src/index.ts +0 -1987
package/dist/chunk-PR4QN5HX.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
-
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
-
});
|
|
13
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
-
};
|
|
16
|
-
var __export = (target, all) => {
|
|
17
|
-
for (var name in all)
|
|
18
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
-
};
|
|
20
|
-
var __copyProps = (to, from, except, desc) => {
|
|
21
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
-
for (let key of __getOwnPropNames(from))
|
|
23
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
-
}
|
|
26
|
-
return to;
|
|
27
|
-
};
|
|
28
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
-
mod
|
|
35
|
-
));
|
|
36
|
-
|
|
37
|
-
export {
|
|
38
|
-
__require,
|
|
39
|
-
__commonJS,
|
|
40
|
-
__export,
|
|
41
|
-
__toESM
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=chunk-PR4QN5HX.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/chunk-QEPGNUG6.js
DELETED
|
@@ -1,650 +0,0 @@
|
|
|
1
|
-
// src/mcp/client.ts
|
|
2
|
-
import { jsonSchema } from "ai";
|
|
3
|
-
import { nanoid } from "nanoid";
|
|
4
|
-
|
|
5
|
-
// src/mcp/client-connection.ts
|
|
6
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
|
-
import {
|
|
8
|
-
PromptListChangedNotificationSchema,
|
|
9
|
-
ResourceListChangedNotificationSchema,
|
|
10
|
-
ToolListChangedNotificationSchema,
|
|
11
|
-
ElicitRequestSchema
|
|
12
|
-
} from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
-
|
|
14
|
-
// src/mcp/sse-edge.ts
|
|
15
|
-
import {
|
|
16
|
-
SSEClientTransport
|
|
17
|
-
} from "@modelcontextprotocol/sdk/client/sse.js";
|
|
18
|
-
var SSEEdgeClientTransport = class extends SSEClientTransport {
|
|
19
|
-
/**
|
|
20
|
-
* Creates a new EdgeSSEClientTransport, which overrides fetch to be compatible with the CF workers environment
|
|
21
|
-
*/
|
|
22
|
-
constructor(url, options) {
|
|
23
|
-
const fetchOverride = async (fetchUrl, fetchInit = {}) => {
|
|
24
|
-
const headers = await this.authHeaders();
|
|
25
|
-
const workerOptions = {
|
|
26
|
-
...fetchInit,
|
|
27
|
-
headers: {
|
|
28
|
-
...options.requestInit?.headers,
|
|
29
|
-
...fetchInit?.headers,
|
|
30
|
-
...headers
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
delete workerOptions.mode;
|
|
34
|
-
return options.eventSourceInit?.fetch?.(
|
|
35
|
-
fetchUrl,
|
|
36
|
-
// @ts-expect-error Expects FetchLikeInit from EventSource but is compatible with RequestInit
|
|
37
|
-
workerOptions
|
|
38
|
-
) || fetch(fetchUrl, workerOptions);
|
|
39
|
-
};
|
|
40
|
-
super(url, {
|
|
41
|
-
...options,
|
|
42
|
-
eventSourceInit: {
|
|
43
|
-
...options.eventSourceInit,
|
|
44
|
-
fetch: fetchOverride
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
this.authProvider = options.authProvider;
|
|
48
|
-
}
|
|
49
|
-
async authHeaders() {
|
|
50
|
-
if (this.authProvider) {
|
|
51
|
-
const tokens = await this.authProvider.tokens();
|
|
52
|
-
if (tokens) {
|
|
53
|
-
return {
|
|
54
|
-
Authorization: `Bearer ${tokens.access_token}`
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// src/mcp/streamable-http-edge.ts
|
|
62
|
-
import {
|
|
63
|
-
StreamableHTTPClientTransport
|
|
64
|
-
} from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
65
|
-
var StreamableHTTPEdgeClientTransport = class extends StreamableHTTPClientTransport {
|
|
66
|
-
/**
|
|
67
|
-
* Creates a new StreamableHTTPEdgeClientTransport, which overrides fetch to be compatible with the CF workers environment
|
|
68
|
-
*/
|
|
69
|
-
constructor(url, options) {
|
|
70
|
-
const fetchOverride = async (fetchUrl, fetchInit = {}) => {
|
|
71
|
-
const headers = await this.authHeaders();
|
|
72
|
-
const workerOptions = {
|
|
73
|
-
...fetchInit,
|
|
74
|
-
headers: {
|
|
75
|
-
...options.requestInit?.headers,
|
|
76
|
-
...fetchInit?.headers,
|
|
77
|
-
...headers
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
delete workerOptions.mode;
|
|
81
|
-
return (
|
|
82
|
-
// @ts-expect-error Custom fetch function for Cloudflare Workers compatibility
|
|
83
|
-
options.requestInit?.fetch?.(
|
|
84
|
-
fetchUrl,
|
|
85
|
-
workerOptions
|
|
86
|
-
) || fetch(fetchUrl, workerOptions)
|
|
87
|
-
);
|
|
88
|
-
};
|
|
89
|
-
super(url, {
|
|
90
|
-
...options,
|
|
91
|
-
requestInit: {
|
|
92
|
-
...options.requestInit,
|
|
93
|
-
// @ts-expect-error Custom fetch override for Cloudflare Workers
|
|
94
|
-
fetch: fetchOverride
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
this.authProvider = options.authProvider;
|
|
98
|
-
}
|
|
99
|
-
async authHeaders() {
|
|
100
|
-
if (this.authProvider) {
|
|
101
|
-
const tokens = await this.authProvider.tokens();
|
|
102
|
-
if (tokens) {
|
|
103
|
-
return {
|
|
104
|
-
Authorization: `Bearer ${tokens.access_token}`
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
// src/mcp/client-connection.ts
|
|
112
|
-
var MCPClientConnection = class {
|
|
113
|
-
constructor(url, info, options = { client: {}, transport: {} }) {
|
|
114
|
-
this.url = url;
|
|
115
|
-
this.options = options;
|
|
116
|
-
this.connectionState = "connecting";
|
|
117
|
-
this.tools = [];
|
|
118
|
-
this.prompts = [];
|
|
119
|
-
this.resources = [];
|
|
120
|
-
this.resourceTemplates = [];
|
|
121
|
-
this.url = url;
|
|
122
|
-
const clientOptions = {
|
|
123
|
-
...options.client,
|
|
124
|
-
capabilities: {
|
|
125
|
-
...options.client?.capabilities,
|
|
126
|
-
elicitation: {}
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
this.client = new Client(info, clientOptions);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Initialize a client connection
|
|
133
|
-
*
|
|
134
|
-
* @param code Optional OAuth code to initialize the connection with if auth hasn't been initialized
|
|
135
|
-
* @returns
|
|
136
|
-
*/
|
|
137
|
-
async init(code) {
|
|
138
|
-
try {
|
|
139
|
-
const transportType = this.options.transport.type || "streamable-http";
|
|
140
|
-
await this.tryConnect(transportType, code);
|
|
141
|
-
} catch (e) {
|
|
142
|
-
if (e.toString().includes("Unauthorized")) {
|
|
143
|
-
this.connectionState = "authenticating";
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
this.connectionState = "failed";
|
|
147
|
-
throw e;
|
|
148
|
-
}
|
|
149
|
-
this.connectionState = "discovering";
|
|
150
|
-
this.serverCapabilities = await this.client.getServerCapabilities();
|
|
151
|
-
if (!this.serverCapabilities) {
|
|
152
|
-
throw new Error("The MCP Server failed to return server capabilities");
|
|
153
|
-
}
|
|
154
|
-
const [
|
|
155
|
-
instructionsResult,
|
|
156
|
-
toolsResult,
|
|
157
|
-
resourcesResult,
|
|
158
|
-
promptsResult,
|
|
159
|
-
resourceTemplatesResult
|
|
160
|
-
] = await Promise.allSettled([
|
|
161
|
-
this.client.getInstructions(),
|
|
162
|
-
this.registerTools(),
|
|
163
|
-
this.registerResources(),
|
|
164
|
-
this.registerPrompts(),
|
|
165
|
-
this.registerResourceTemplates()
|
|
166
|
-
]);
|
|
167
|
-
const operations = [
|
|
168
|
-
{ name: "instructions", result: instructionsResult },
|
|
169
|
-
{ name: "tools", result: toolsResult },
|
|
170
|
-
{ name: "resources", result: resourcesResult },
|
|
171
|
-
{ name: "prompts", result: promptsResult },
|
|
172
|
-
{ name: "resource templates", result: resourceTemplatesResult }
|
|
173
|
-
];
|
|
174
|
-
for (const { name, result } of operations) {
|
|
175
|
-
if (result.status === "rejected") {
|
|
176
|
-
console.error(`Failed to initialize ${name}:`, result.reason);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
this.instructions = instructionsResult.status === "fulfilled" ? instructionsResult.value : void 0;
|
|
180
|
-
this.tools = toolsResult.status === "fulfilled" ? toolsResult.value : [];
|
|
181
|
-
this.resources = resourcesResult.status === "fulfilled" ? resourcesResult.value : [];
|
|
182
|
-
this.prompts = promptsResult.status === "fulfilled" ? promptsResult.value : [];
|
|
183
|
-
this.resourceTemplates = resourceTemplatesResult.status === "fulfilled" ? resourceTemplatesResult.value : [];
|
|
184
|
-
this.connectionState = "ready";
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Notification handler registration
|
|
188
|
-
*/
|
|
189
|
-
async registerTools() {
|
|
190
|
-
if (!this.serverCapabilities || !this.serverCapabilities.tools) {
|
|
191
|
-
return [];
|
|
192
|
-
}
|
|
193
|
-
if (this.serverCapabilities.tools.listChanged) {
|
|
194
|
-
this.client.setNotificationHandler(
|
|
195
|
-
ToolListChangedNotificationSchema,
|
|
196
|
-
async (_notification) => {
|
|
197
|
-
this.tools = await this.fetchTools();
|
|
198
|
-
}
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
return this.fetchTools();
|
|
202
|
-
}
|
|
203
|
-
async registerResources() {
|
|
204
|
-
if (!this.serverCapabilities || !this.serverCapabilities.resources) {
|
|
205
|
-
return [];
|
|
206
|
-
}
|
|
207
|
-
if (this.serverCapabilities.resources.listChanged) {
|
|
208
|
-
this.client.setNotificationHandler(
|
|
209
|
-
ResourceListChangedNotificationSchema,
|
|
210
|
-
async (_notification) => {
|
|
211
|
-
this.resources = await this.fetchResources();
|
|
212
|
-
}
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
return this.fetchResources();
|
|
216
|
-
}
|
|
217
|
-
async registerPrompts() {
|
|
218
|
-
if (!this.serverCapabilities || !this.serverCapabilities.prompts) {
|
|
219
|
-
return [];
|
|
220
|
-
}
|
|
221
|
-
if (this.serverCapabilities.prompts.listChanged) {
|
|
222
|
-
this.client.setNotificationHandler(
|
|
223
|
-
PromptListChangedNotificationSchema,
|
|
224
|
-
async (_notification) => {
|
|
225
|
-
this.prompts = await this.fetchPrompts();
|
|
226
|
-
}
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
return this.fetchPrompts();
|
|
230
|
-
}
|
|
231
|
-
async registerResourceTemplates() {
|
|
232
|
-
if (!this.serverCapabilities || !this.serverCapabilities.resources) {
|
|
233
|
-
return [];
|
|
234
|
-
}
|
|
235
|
-
return this.fetchResourceTemplates();
|
|
236
|
-
}
|
|
237
|
-
async fetchTools() {
|
|
238
|
-
let toolsAgg = [];
|
|
239
|
-
let toolsResult = { tools: [] };
|
|
240
|
-
do {
|
|
241
|
-
toolsResult = await this.client.listTools({
|
|
242
|
-
cursor: toolsResult.nextCursor
|
|
243
|
-
}).catch(capabilityErrorHandler({ tools: [] }, "tools/list"));
|
|
244
|
-
toolsAgg = toolsAgg.concat(toolsResult.tools);
|
|
245
|
-
} while (toolsResult.nextCursor);
|
|
246
|
-
return toolsAgg;
|
|
247
|
-
}
|
|
248
|
-
async fetchResources() {
|
|
249
|
-
let resourcesAgg = [];
|
|
250
|
-
let resourcesResult = { resources: [] };
|
|
251
|
-
do {
|
|
252
|
-
resourcesResult = await this.client.listResources({
|
|
253
|
-
cursor: resourcesResult.nextCursor
|
|
254
|
-
}).catch(capabilityErrorHandler({ resources: [] }, "resources/list"));
|
|
255
|
-
resourcesAgg = resourcesAgg.concat(resourcesResult.resources);
|
|
256
|
-
} while (resourcesResult.nextCursor);
|
|
257
|
-
return resourcesAgg;
|
|
258
|
-
}
|
|
259
|
-
async fetchPrompts() {
|
|
260
|
-
let promptsAgg = [];
|
|
261
|
-
let promptsResult = { prompts: [] };
|
|
262
|
-
do {
|
|
263
|
-
promptsResult = await this.client.listPrompts({
|
|
264
|
-
cursor: promptsResult.nextCursor
|
|
265
|
-
}).catch(capabilityErrorHandler({ prompts: [] }, "prompts/list"));
|
|
266
|
-
promptsAgg = promptsAgg.concat(promptsResult.prompts);
|
|
267
|
-
} while (promptsResult.nextCursor);
|
|
268
|
-
return promptsAgg;
|
|
269
|
-
}
|
|
270
|
-
async fetchResourceTemplates() {
|
|
271
|
-
let templatesAgg = [];
|
|
272
|
-
let templatesResult = {
|
|
273
|
-
resourceTemplates: []
|
|
274
|
-
};
|
|
275
|
-
do {
|
|
276
|
-
templatesResult = await this.client.listResourceTemplates({
|
|
277
|
-
cursor: templatesResult.nextCursor
|
|
278
|
-
}).catch(
|
|
279
|
-
capabilityErrorHandler(
|
|
280
|
-
{ resourceTemplates: [] },
|
|
281
|
-
"resources/templates/list"
|
|
282
|
-
)
|
|
283
|
-
);
|
|
284
|
-
templatesAgg = templatesAgg.concat(templatesResult.resourceTemplates);
|
|
285
|
-
} while (templatesResult.nextCursor);
|
|
286
|
-
return templatesAgg;
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Handle elicitation request from server
|
|
290
|
-
* Automatically uses the Agent's built-in elicitation handling if available
|
|
291
|
-
*/
|
|
292
|
-
async handleElicitationRequest(_request) {
|
|
293
|
-
throw new Error(
|
|
294
|
-
"Elicitation handler must be implemented for your platform. Override handleElicitationRequest method."
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Get the transport for the client
|
|
299
|
-
* @param transportType - The transport type to get
|
|
300
|
-
* @returns The transport for the client
|
|
301
|
-
*/
|
|
302
|
-
getTransport(transportType) {
|
|
303
|
-
switch (transportType) {
|
|
304
|
-
case "streamable-http":
|
|
305
|
-
return new StreamableHTTPEdgeClientTransport(
|
|
306
|
-
this.url,
|
|
307
|
-
this.options.transport
|
|
308
|
-
);
|
|
309
|
-
case "sse":
|
|
310
|
-
return new SSEEdgeClientTransport(
|
|
311
|
-
this.url,
|
|
312
|
-
this.options.transport
|
|
313
|
-
);
|
|
314
|
-
default:
|
|
315
|
-
throw new Error(`Unsupported transport type: ${transportType}`);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
async tryConnect(transportType, code) {
|
|
319
|
-
let effectiveTransportType = transportType;
|
|
320
|
-
if (code && this.options.transport.authProvider) {
|
|
321
|
-
const savedTransport = await this.options.transport.authProvider.getOAuthTransport();
|
|
322
|
-
if (savedTransport) {
|
|
323
|
-
effectiveTransportType = savedTransport;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
const transports = effectiveTransportType === "auto" ? ["streamable-http", "sse"] : [effectiveTransportType];
|
|
327
|
-
for (const currentTransportType of transports) {
|
|
328
|
-
const isLastTransport = currentTransportType === transports[transports.length - 1];
|
|
329
|
-
const hasFallback = effectiveTransportType === "auto" && currentTransportType === "streamable-http" && !isLastTransport;
|
|
330
|
-
const transport = this.getTransport(currentTransportType);
|
|
331
|
-
if (code) {
|
|
332
|
-
await transport.finishAuth(code);
|
|
333
|
-
}
|
|
334
|
-
try {
|
|
335
|
-
await this.client.connect(transport);
|
|
336
|
-
if (code && this.options.transport.authProvider) {
|
|
337
|
-
await this.options.transport.authProvider.clearOAuthTransport();
|
|
338
|
-
}
|
|
339
|
-
break;
|
|
340
|
-
} catch (e) {
|
|
341
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
342
|
-
if (!code && error.message.includes("Unauthorized") && this.options.transport.authProvider && currentTransportType) {
|
|
343
|
-
await this.options.transport.authProvider.saveOAuthTransport(
|
|
344
|
-
currentTransportType
|
|
345
|
-
);
|
|
346
|
-
throw e;
|
|
347
|
-
}
|
|
348
|
-
if (hasFallback && (error.message.includes("404") || error.message.includes("405"))) {
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
|
-
throw e;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
this.client.setRequestHandler(
|
|
355
|
-
ElicitRequestSchema,
|
|
356
|
-
async (request) => {
|
|
357
|
-
return await this.handleElicitationRequest(request);
|
|
358
|
-
}
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
function capabilityErrorHandler(empty, method) {
|
|
363
|
-
return (e) => {
|
|
364
|
-
if (e.code === -32601) {
|
|
365
|
-
console.error(
|
|
366
|
-
`The server advertised support for the capability ${method.split("/")[0]}, but returned "Method not found" for '${method}'.`
|
|
367
|
-
);
|
|
368
|
-
return empty;
|
|
369
|
-
}
|
|
370
|
-
throw e;
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
// src/mcp/client.ts
|
|
375
|
-
var MCPClientManager = class {
|
|
376
|
-
/**
|
|
377
|
-
* @param _name Name of the MCP client
|
|
378
|
-
* @param _version Version of the MCP Client
|
|
379
|
-
* @param auth Auth paramters if being used to create a DurableObjectOAuthClientProvider
|
|
380
|
-
*/
|
|
381
|
-
constructor(_name, _version) {
|
|
382
|
-
this._name = _name;
|
|
383
|
-
this._version = _version;
|
|
384
|
-
this.mcpConnections = {};
|
|
385
|
-
this._callbackUrls = [];
|
|
386
|
-
this._didWarnAboutUnstableGetAITools = false;
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Connect to and register an MCP server
|
|
390
|
-
*
|
|
391
|
-
* @param transportConfig Transport config
|
|
392
|
-
* @param clientConfig Client config
|
|
393
|
-
* @param capabilities Client capabilities (i.e. if the client supports roots/sampling)
|
|
394
|
-
*/
|
|
395
|
-
async connect(url, options = {}) {
|
|
396
|
-
const id = options.reconnect?.id ?? nanoid(8);
|
|
397
|
-
if (!options.transport?.authProvider) {
|
|
398
|
-
console.warn(
|
|
399
|
-
"No authProvider provided in the transport options. This client will only support unauthenticated remote MCP Servers"
|
|
400
|
-
);
|
|
401
|
-
} else {
|
|
402
|
-
options.transport.authProvider.serverId = id;
|
|
403
|
-
if (options.reconnect?.oauthClientId) {
|
|
404
|
-
options.transport.authProvider.clientId = options.reconnect?.oauthClientId;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
if (!options.reconnect?.oauthCode || !this.mcpConnections[id]) {
|
|
408
|
-
this.mcpConnections[id] = new MCPClientConnection(
|
|
409
|
-
new URL(url),
|
|
410
|
-
{
|
|
411
|
-
name: this._name,
|
|
412
|
-
version: this._version
|
|
413
|
-
},
|
|
414
|
-
{
|
|
415
|
-
client: options.client ?? {},
|
|
416
|
-
transport: options.transport ?? {}
|
|
417
|
-
}
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
await this.mcpConnections[id].init(options.reconnect?.oauthCode);
|
|
421
|
-
const authUrl = options.transport?.authProvider?.authUrl;
|
|
422
|
-
if (authUrl && options.transport?.authProvider?.redirectUrl) {
|
|
423
|
-
this._callbackUrls.push(
|
|
424
|
-
options.transport.authProvider.redirectUrl.toString()
|
|
425
|
-
);
|
|
426
|
-
return {
|
|
427
|
-
authUrl,
|
|
428
|
-
clientId: options.transport?.authProvider?.clientId,
|
|
429
|
-
id
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
return {
|
|
433
|
-
id
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
isCallbackRequest(req) {
|
|
437
|
-
return req.method === "GET" && !!this._callbackUrls.find((url) => {
|
|
438
|
-
return req.url.startsWith(url);
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
async handleCallbackRequest(req) {
|
|
442
|
-
const url = new URL(req.url);
|
|
443
|
-
const urlMatch = this._callbackUrls.find((url2) => {
|
|
444
|
-
return req.url.startsWith(url2);
|
|
445
|
-
});
|
|
446
|
-
if (!urlMatch) {
|
|
447
|
-
throw new Error(
|
|
448
|
-
`No callback URI match found for the request url: ${req.url}. Was the request matched with \`isCallbackRequest()\`?`
|
|
449
|
-
);
|
|
450
|
-
}
|
|
451
|
-
const code = url.searchParams.get("code");
|
|
452
|
-
const clientId = url.searchParams.get("state");
|
|
453
|
-
const urlParams = urlMatch.split("/");
|
|
454
|
-
const serverId = urlParams[urlParams.length - 1];
|
|
455
|
-
if (!code) {
|
|
456
|
-
throw new Error("Unauthorized: no code provided");
|
|
457
|
-
}
|
|
458
|
-
if (!clientId) {
|
|
459
|
-
throw new Error("Unauthorized: no state provided");
|
|
460
|
-
}
|
|
461
|
-
if (this.mcpConnections[serverId] === void 0) {
|
|
462
|
-
throw new Error(`Could not find serverId: ${serverId}`);
|
|
463
|
-
}
|
|
464
|
-
if (this.mcpConnections[serverId].connectionState !== "authenticating") {
|
|
465
|
-
throw new Error(
|
|
466
|
-
"Failed to authenticate: the client isn't in the `authenticating` state"
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
const conn = this.mcpConnections[serverId];
|
|
470
|
-
if (!conn.options.transport.authProvider) {
|
|
471
|
-
throw new Error(
|
|
472
|
-
"Trying to finalize authentication for a server connection without an authProvider"
|
|
473
|
-
);
|
|
474
|
-
}
|
|
475
|
-
conn.options.transport.authProvider.clientId = clientId;
|
|
476
|
-
conn.options.transport.authProvider.serverId = serverId;
|
|
477
|
-
const serverUrl = conn.url.toString();
|
|
478
|
-
await this.connect(serverUrl, {
|
|
479
|
-
reconnect: {
|
|
480
|
-
id: serverId,
|
|
481
|
-
oauthClientId: clientId,
|
|
482
|
-
oauthCode: code
|
|
483
|
-
},
|
|
484
|
-
...conn.options
|
|
485
|
-
});
|
|
486
|
-
if (this.mcpConnections[serverId].connectionState === "authenticating") {
|
|
487
|
-
throw new Error("Failed to authenticate: client failed to initialize");
|
|
488
|
-
}
|
|
489
|
-
return { serverId };
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Register a callback URL for OAuth handling
|
|
493
|
-
* @param url The callback URL to register
|
|
494
|
-
*/
|
|
495
|
-
registerCallbackUrl(url) {
|
|
496
|
-
if (!this._callbackUrls.includes(url)) {
|
|
497
|
-
this._callbackUrls.push(url);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Unregister a callback URL
|
|
502
|
-
* @param serverId The server ID whose callback URL should be removed
|
|
503
|
-
*/
|
|
504
|
-
unregisterCallbackUrl(serverId) {
|
|
505
|
-
this._callbackUrls = this._callbackUrls.filter(
|
|
506
|
-
(url) => !url.endsWith(`/${serverId}`)
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* @returns namespaced list of tools
|
|
511
|
-
*/
|
|
512
|
-
listTools() {
|
|
513
|
-
return getNamespacedData(this.mcpConnections, "tools");
|
|
514
|
-
}
|
|
515
|
-
/**
|
|
516
|
-
* @returns a set of tools that you can use with the AI SDK
|
|
517
|
-
*/
|
|
518
|
-
getAITools() {
|
|
519
|
-
return Object.fromEntries(
|
|
520
|
-
getNamespacedData(this.mcpConnections, "tools").map((tool) => {
|
|
521
|
-
return [
|
|
522
|
-
`tool_${tool.serverId}_${tool.name}`,
|
|
523
|
-
{
|
|
524
|
-
description: tool.description,
|
|
525
|
-
execute: async (args) => {
|
|
526
|
-
const result = await this.callTool({
|
|
527
|
-
arguments: args,
|
|
528
|
-
name: tool.name,
|
|
529
|
-
serverId: tool.serverId
|
|
530
|
-
});
|
|
531
|
-
if (result.isError) {
|
|
532
|
-
throw new Error(result.content[0].text);
|
|
533
|
-
}
|
|
534
|
-
return result;
|
|
535
|
-
},
|
|
536
|
-
inputSchema: jsonSchema(tool.inputSchema),
|
|
537
|
-
outputSchema: tool.outputSchema ? jsonSchema(tool.outputSchema) : void 0
|
|
538
|
-
}
|
|
539
|
-
];
|
|
540
|
-
})
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* @deprecated this has been renamed to getAITools(), and unstable_getAITools will be removed in the next major version
|
|
545
|
-
* @returns a set of tools that you can use with the AI SDK
|
|
546
|
-
*/
|
|
547
|
-
unstable_getAITools() {
|
|
548
|
-
if (!this._didWarnAboutUnstableGetAITools) {
|
|
549
|
-
this._didWarnAboutUnstableGetAITools = true;
|
|
550
|
-
console.warn(
|
|
551
|
-
"unstable_getAITools is deprecated, use getAITools instead. unstable_getAITools will be removed in the next major version."
|
|
552
|
-
);
|
|
553
|
-
}
|
|
554
|
-
return this.getAITools();
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* Closes all connections to MCP servers
|
|
558
|
-
*/
|
|
559
|
-
async closeAllConnections() {
|
|
560
|
-
return Promise.all(
|
|
561
|
-
Object.values(this.mcpConnections).map(async (connection) => {
|
|
562
|
-
await connection.client.close();
|
|
563
|
-
})
|
|
564
|
-
);
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Closes a connection to an MCP server
|
|
568
|
-
* @param id The id of the connection to close
|
|
569
|
-
*/
|
|
570
|
-
async closeConnection(id) {
|
|
571
|
-
if (!this.mcpConnections[id]) {
|
|
572
|
-
throw new Error(`Connection with id "${id}" does not exist.`);
|
|
573
|
-
}
|
|
574
|
-
await this.mcpConnections[id].client.close();
|
|
575
|
-
delete this.mcpConnections[id];
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* @returns namespaced list of prompts
|
|
579
|
-
*/
|
|
580
|
-
listPrompts() {
|
|
581
|
-
return getNamespacedData(this.mcpConnections, "prompts");
|
|
582
|
-
}
|
|
583
|
-
/**
|
|
584
|
-
* @returns namespaced list of tools
|
|
585
|
-
*/
|
|
586
|
-
listResources() {
|
|
587
|
-
return getNamespacedData(this.mcpConnections, "resources");
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* @returns namespaced list of resource templates
|
|
591
|
-
*/
|
|
592
|
-
listResourceTemplates() {
|
|
593
|
-
return getNamespacedData(this.mcpConnections, "resourceTemplates");
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* Namespaced version of callTool
|
|
597
|
-
*/
|
|
598
|
-
async callTool(params, resultSchema, options) {
|
|
599
|
-
const unqualifiedName = params.name.replace(`${params.serverId}.`, "");
|
|
600
|
-
return this.mcpConnections[params.serverId].client.callTool(
|
|
601
|
-
{
|
|
602
|
-
...params,
|
|
603
|
-
name: unqualifiedName
|
|
604
|
-
},
|
|
605
|
-
resultSchema,
|
|
606
|
-
options
|
|
607
|
-
);
|
|
608
|
-
}
|
|
609
|
-
/**
|
|
610
|
-
* Namespaced version of readResource
|
|
611
|
-
*/
|
|
612
|
-
readResource(params, options) {
|
|
613
|
-
return this.mcpConnections[params.serverId].client.readResource(
|
|
614
|
-
params,
|
|
615
|
-
options
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
/**
|
|
619
|
-
* Namespaced version of getPrompt
|
|
620
|
-
*/
|
|
621
|
-
getPrompt(params, options) {
|
|
622
|
-
return this.mcpConnections[params.serverId].client.getPrompt(
|
|
623
|
-
params,
|
|
624
|
-
options
|
|
625
|
-
);
|
|
626
|
-
}
|
|
627
|
-
};
|
|
628
|
-
function getNamespacedData(mcpClients, type) {
|
|
629
|
-
const sets = Object.entries(mcpClients).map(([name, conn]) => {
|
|
630
|
-
return { data: conn[type], name };
|
|
631
|
-
});
|
|
632
|
-
const namespacedData = sets.flatMap(({ name: serverId, data }) => {
|
|
633
|
-
return data.map((item) => {
|
|
634
|
-
return {
|
|
635
|
-
...item,
|
|
636
|
-
// we add a serverId so we can easily pull it out and send the tool call to the right server
|
|
637
|
-
serverId
|
|
638
|
-
};
|
|
639
|
-
});
|
|
640
|
-
});
|
|
641
|
-
return namespacedData;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
export {
|
|
645
|
-
SSEEdgeClientTransport,
|
|
646
|
-
StreamableHTTPEdgeClientTransport,
|
|
647
|
-
MCPClientManager,
|
|
648
|
-
getNamespacedData
|
|
649
|
-
};
|
|
650
|
-
//# sourceMappingURL=chunk-QEPGNUG6.js.map
|