@ai-sdk/mcp 2.0.0-beta.8 → 2.0.0-canary.38
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/CHANGELOG.md +252 -8
- package/dist/index.d.ts +27 -1
- package/dist/index.js +295 -280
- package/dist/index.js.map +1 -1
- package/dist/mcp-stdio/index.js +149 -170
- package/dist/mcp-stdio/index.js.map +1 -1
- package/package.json +14 -15
- package/src/index.ts +1 -0
- package/src/tool/json-rpc-message.ts +7 -0
- package/src/tool/mcp-client.ts +70 -32
- package/src/tool/mcp-http-transport.ts +19 -8
- package/src/tool/mcp-sse-transport.ts +17 -10
- package/src/tool/mcp-stdio/create-child-process.ts +2 -2
- package/src/tool/mcp-stdio/mcp-stdio-transport.ts +17 -14
- package/src/tool/mcp-transport.ts +10 -2
- package/src/tool/mock-mcp-transport.ts +6 -8
- package/src/tool/oauth.ts +17 -12
- package/src/tool/types.ts +4 -2
- package/src/util/oauth-util.ts +13 -0
- package/dist/index.d.mts +0 -516
- package/dist/index.mjs +0 -2137
- package/dist/index.mjs.map +0 -1
- package/dist/mcp-stdio/index.d.mts +0 -89
- package/dist/mcp-stdio/index.mjs +0 -426
- package/dist/mcp-stdio/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,54 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name3 in all)
|
|
10
|
-
__defProp(target, name3, { get: all[name3], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
ElicitResultSchema: () => ElicitResultSchema,
|
|
34
|
-
ElicitationRequestSchema: () => ElicitationRequestSchema,
|
|
35
|
-
UnauthorizedError: () => UnauthorizedError,
|
|
36
|
-
auth: () => auth,
|
|
37
|
-
createMCPClient: () => createMCPClient,
|
|
38
|
-
experimental_createMCPClient: () => createMCPClient
|
|
39
|
-
});
|
|
40
|
-
module.exports = __toCommonJS(index_exports);
|
|
41
|
-
|
|
42
1
|
// src/tool/mcp-client.ts
|
|
43
|
-
|
|
2
|
+
import {
|
|
3
|
+
asSchema,
|
|
4
|
+
dynamicTool,
|
|
5
|
+
jsonSchema,
|
|
6
|
+
safeParseJSON,
|
|
7
|
+
safeValidateTypes,
|
|
8
|
+
tool
|
|
9
|
+
} from "@ai-sdk/provider-utils";
|
|
44
10
|
|
|
45
11
|
// src/error/mcp-client-error.ts
|
|
46
|
-
|
|
12
|
+
import { AISDKError } from "@ai-sdk/provider";
|
|
47
13
|
var name = "AI_MCPClientError";
|
|
48
14
|
var marker = `vercel.ai.error.${name}`;
|
|
49
15
|
var symbol = Symbol.for(marker);
|
|
50
16
|
var _a, _b;
|
|
51
|
-
var MCPClientError = class extends (_b =
|
|
17
|
+
var MCPClientError = class extends (_b = AISDKError, _a = symbol, _b) {
|
|
52
18
|
constructor({
|
|
53
19
|
name: name3 = "MCPClientError",
|
|
54
20
|
message,
|
|
@@ -62,18 +28,23 @@ var MCPClientError = class extends (_b = import_provider.AISDKError, _a = symbol
|
|
|
62
28
|
this.code = code;
|
|
63
29
|
}
|
|
64
30
|
static isInstance(error) {
|
|
65
|
-
return
|
|
31
|
+
return AISDKError.hasMarker(error, marker);
|
|
66
32
|
}
|
|
67
33
|
};
|
|
68
34
|
|
|
69
35
|
// src/tool/mcp-sse-transport.ts
|
|
70
|
-
|
|
36
|
+
import {
|
|
37
|
+
EventSourceParserStream,
|
|
38
|
+
withUserAgentSuffix,
|
|
39
|
+
getRuntimeEnvironmentUserAgent
|
|
40
|
+
} from "@ai-sdk/provider-utils";
|
|
71
41
|
|
|
72
42
|
// src/tool/json-rpc-message.ts
|
|
73
|
-
|
|
43
|
+
import { parseJSON } from "@ai-sdk/provider-utils";
|
|
44
|
+
import { z as z2 } from "zod/v4";
|
|
74
45
|
|
|
75
46
|
// src/tool/types.ts
|
|
76
|
-
|
|
47
|
+
import { z } from "zod/v4";
|
|
77
48
|
var LATEST_PROTOCOL_VERSION = "2025-11-25";
|
|
78
49
|
var SUPPORTED_PROTOCOL_VERSIONS = [
|
|
79
50
|
LATEST_PROTOCOL_VERSION,
|
|
@@ -81,260 +52,264 @@ var SUPPORTED_PROTOCOL_VERSIONS = [
|
|
|
81
52
|
"2025-03-26",
|
|
82
53
|
"2024-11-05"
|
|
83
54
|
];
|
|
84
|
-
var ToolMetaSchema =
|
|
85
|
-
var ClientOrServerImplementationSchema =
|
|
86
|
-
name:
|
|
87
|
-
version:
|
|
55
|
+
var ToolMetaSchema = z.optional(z.record(z.string(), z.unknown()));
|
|
56
|
+
var ClientOrServerImplementationSchema = z.looseObject({
|
|
57
|
+
name: z.string(),
|
|
58
|
+
version: z.string(),
|
|
59
|
+
title: z.optional(z.string())
|
|
88
60
|
});
|
|
89
|
-
var BaseParamsSchema =
|
|
90
|
-
_meta:
|
|
61
|
+
var BaseParamsSchema = z.looseObject({
|
|
62
|
+
_meta: z.optional(z.object({}).loose())
|
|
91
63
|
});
|
|
92
64
|
var ResultSchema = BaseParamsSchema;
|
|
93
|
-
var RequestSchema =
|
|
94
|
-
method:
|
|
95
|
-
params:
|
|
65
|
+
var RequestSchema = z.object({
|
|
66
|
+
method: z.string(),
|
|
67
|
+
params: z.optional(BaseParamsSchema)
|
|
96
68
|
});
|
|
97
|
-
var ElicitationCapabilitySchema =
|
|
98
|
-
applyDefaults:
|
|
69
|
+
var ElicitationCapabilitySchema = z.object({
|
|
70
|
+
applyDefaults: z.optional(z.boolean())
|
|
99
71
|
}).loose();
|
|
100
|
-
var ServerCapabilitiesSchema =
|
|
101
|
-
experimental:
|
|
102
|
-
logging:
|
|
103
|
-
prompts:
|
|
104
|
-
|
|
105
|
-
listChanged:
|
|
72
|
+
var ServerCapabilitiesSchema = z.looseObject({
|
|
73
|
+
experimental: z.optional(z.object({}).loose()),
|
|
74
|
+
logging: z.optional(z.object({}).loose()),
|
|
75
|
+
prompts: z.optional(
|
|
76
|
+
z.looseObject({
|
|
77
|
+
listChanged: z.optional(z.boolean())
|
|
106
78
|
})
|
|
107
79
|
),
|
|
108
|
-
resources:
|
|
109
|
-
|
|
110
|
-
subscribe:
|
|
111
|
-
listChanged:
|
|
80
|
+
resources: z.optional(
|
|
81
|
+
z.looseObject({
|
|
82
|
+
subscribe: z.optional(z.boolean()),
|
|
83
|
+
listChanged: z.optional(z.boolean())
|
|
112
84
|
})
|
|
113
85
|
),
|
|
114
|
-
tools:
|
|
115
|
-
|
|
116
|
-
listChanged:
|
|
86
|
+
tools: z.optional(
|
|
87
|
+
z.looseObject({
|
|
88
|
+
listChanged: z.optional(z.boolean())
|
|
117
89
|
})
|
|
118
90
|
),
|
|
119
|
-
elicitation:
|
|
91
|
+
elicitation: z.optional(ElicitationCapabilitySchema)
|
|
120
92
|
});
|
|
121
|
-
var ClientCapabilitiesSchema =
|
|
122
|
-
elicitation:
|
|
93
|
+
var ClientCapabilitiesSchema = z.object({
|
|
94
|
+
elicitation: z.optional(ElicitationCapabilitySchema)
|
|
123
95
|
}).loose();
|
|
124
96
|
var InitializeResultSchema = ResultSchema.extend({
|
|
125
|
-
protocolVersion:
|
|
97
|
+
protocolVersion: z.string(),
|
|
126
98
|
capabilities: ServerCapabilitiesSchema,
|
|
127
99
|
serverInfo: ClientOrServerImplementationSchema,
|
|
128
|
-
instructions:
|
|
100
|
+
instructions: z.optional(z.string())
|
|
129
101
|
});
|
|
130
102
|
var PaginatedResultSchema = ResultSchema.extend({
|
|
131
|
-
nextCursor:
|
|
103
|
+
nextCursor: z.optional(z.string())
|
|
132
104
|
});
|
|
133
|
-
var ToolSchema =
|
|
134
|
-
name:
|
|
105
|
+
var ToolSchema = z.object({
|
|
106
|
+
name: z.string(),
|
|
135
107
|
/**
|
|
136
108
|
* @see https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool
|
|
137
109
|
*/
|
|
138
|
-
title:
|
|
139
|
-
description:
|
|
140
|
-
inputSchema:
|
|
141
|
-
type:
|
|
142
|
-
properties:
|
|
110
|
+
title: z.optional(z.string()),
|
|
111
|
+
description: z.optional(z.string()),
|
|
112
|
+
inputSchema: z.object({
|
|
113
|
+
type: z.literal("object"),
|
|
114
|
+
properties: z.optional(z.object({}).loose())
|
|
143
115
|
}).loose(),
|
|
144
116
|
/**
|
|
145
117
|
* @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema
|
|
146
118
|
*/
|
|
147
|
-
outputSchema:
|
|
148
|
-
annotations:
|
|
149
|
-
|
|
150
|
-
title:
|
|
119
|
+
outputSchema: z.optional(z.object({}).loose()),
|
|
120
|
+
annotations: z.optional(
|
|
121
|
+
z.object({
|
|
122
|
+
title: z.optional(z.string())
|
|
151
123
|
}).loose()
|
|
152
124
|
),
|
|
153
125
|
_meta: ToolMetaSchema
|
|
154
126
|
}).loose();
|
|
155
127
|
var ListToolsResultSchema = PaginatedResultSchema.extend({
|
|
156
|
-
tools:
|
|
128
|
+
tools: z.array(ToolSchema)
|
|
157
129
|
});
|
|
158
|
-
var TextContentSchema =
|
|
159
|
-
type:
|
|
160
|
-
text:
|
|
130
|
+
var TextContentSchema = z.object({
|
|
131
|
+
type: z.literal("text"),
|
|
132
|
+
text: z.string()
|
|
161
133
|
}).loose();
|
|
162
|
-
var ImageContentSchema =
|
|
163
|
-
type:
|
|
164
|
-
data:
|
|
165
|
-
mimeType:
|
|
134
|
+
var ImageContentSchema = z.object({
|
|
135
|
+
type: z.literal("image"),
|
|
136
|
+
data: z.base64(),
|
|
137
|
+
mimeType: z.string()
|
|
166
138
|
}).loose();
|
|
167
|
-
var ResourceSchema =
|
|
168
|
-
uri:
|
|
169
|
-
name:
|
|
170
|
-
title:
|
|
171
|
-
description:
|
|
172
|
-
mimeType:
|
|
173
|
-
size:
|
|
139
|
+
var ResourceSchema = z.object({
|
|
140
|
+
uri: z.string(),
|
|
141
|
+
name: z.string(),
|
|
142
|
+
title: z.optional(z.string()),
|
|
143
|
+
description: z.optional(z.string()),
|
|
144
|
+
mimeType: z.optional(z.string()),
|
|
145
|
+
size: z.optional(z.number())
|
|
174
146
|
}).loose();
|
|
175
147
|
var ListResourcesResultSchema = PaginatedResultSchema.extend({
|
|
176
|
-
resources:
|
|
148
|
+
resources: z.array(ResourceSchema)
|
|
177
149
|
});
|
|
178
|
-
var ResourceContentsSchema =
|
|
150
|
+
var ResourceContentsSchema = z.object({
|
|
179
151
|
/**
|
|
180
152
|
* The URI of this resource.
|
|
181
153
|
*/
|
|
182
|
-
uri:
|
|
154
|
+
uri: z.string(),
|
|
183
155
|
/**
|
|
184
156
|
* Optional display name of the resource content.
|
|
185
157
|
*/
|
|
186
|
-
name:
|
|
158
|
+
name: z.optional(z.string()),
|
|
187
159
|
/**
|
|
188
160
|
* Optional human readable title.
|
|
189
161
|
*/
|
|
190
|
-
title:
|
|
162
|
+
title: z.optional(z.string()),
|
|
191
163
|
/**
|
|
192
164
|
* The MIME type of this resource, if known.
|
|
193
165
|
*/
|
|
194
|
-
mimeType:
|
|
166
|
+
mimeType: z.optional(z.string())
|
|
195
167
|
}).loose();
|
|
196
168
|
var TextResourceContentsSchema = ResourceContentsSchema.extend({
|
|
197
|
-
text:
|
|
169
|
+
text: z.string()
|
|
198
170
|
});
|
|
199
171
|
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
|
|
200
|
-
blob:
|
|
172
|
+
blob: z.base64()
|
|
201
173
|
});
|
|
202
|
-
var EmbeddedResourceSchema =
|
|
203
|
-
type:
|
|
204
|
-
resource:
|
|
174
|
+
var EmbeddedResourceSchema = z.object({
|
|
175
|
+
type: z.literal("resource"),
|
|
176
|
+
resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
|
205
177
|
}).loose();
|
|
206
178
|
var CallToolResultSchema = ResultSchema.extend({
|
|
207
|
-
content:
|
|
208
|
-
|
|
179
|
+
content: z.array(
|
|
180
|
+
z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
|
|
209
181
|
),
|
|
210
182
|
/**
|
|
211
183
|
* @see https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content
|
|
212
184
|
*/
|
|
213
|
-
structuredContent:
|
|
214
|
-
isError:
|
|
185
|
+
structuredContent: z.optional(z.unknown()),
|
|
186
|
+
isError: z.boolean().default(false).optional()
|
|
215
187
|
}).or(
|
|
216
188
|
ResultSchema.extend({
|
|
217
|
-
toolResult:
|
|
189
|
+
toolResult: z.unknown()
|
|
218
190
|
})
|
|
219
191
|
);
|
|
220
|
-
var ResourceTemplateSchema =
|
|
221
|
-
uriTemplate:
|
|
222
|
-
name:
|
|
223
|
-
title:
|
|
224
|
-
description:
|
|
225
|
-
mimeType:
|
|
192
|
+
var ResourceTemplateSchema = z.object({
|
|
193
|
+
uriTemplate: z.string(),
|
|
194
|
+
name: z.string(),
|
|
195
|
+
title: z.optional(z.string()),
|
|
196
|
+
description: z.optional(z.string()),
|
|
197
|
+
mimeType: z.optional(z.string())
|
|
226
198
|
}).loose();
|
|
227
199
|
var ListResourceTemplatesResultSchema = ResultSchema.extend({
|
|
228
|
-
resourceTemplates:
|
|
200
|
+
resourceTemplates: z.array(ResourceTemplateSchema)
|
|
229
201
|
});
|
|
230
202
|
var ReadResourceResultSchema = ResultSchema.extend({
|
|
231
|
-
contents:
|
|
232
|
-
|
|
203
|
+
contents: z.array(
|
|
204
|
+
z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
|
233
205
|
)
|
|
234
206
|
});
|
|
235
|
-
var PromptArgumentSchema =
|
|
236
|
-
name:
|
|
237
|
-
description:
|
|
238
|
-
required:
|
|
207
|
+
var PromptArgumentSchema = z.object({
|
|
208
|
+
name: z.string(),
|
|
209
|
+
description: z.optional(z.string()),
|
|
210
|
+
required: z.optional(z.boolean())
|
|
239
211
|
}).loose();
|
|
240
|
-
var PromptSchema =
|
|
241
|
-
name:
|
|
242
|
-
title:
|
|
243
|
-
description:
|
|
244
|
-
arguments:
|
|
212
|
+
var PromptSchema = z.object({
|
|
213
|
+
name: z.string(),
|
|
214
|
+
title: z.optional(z.string()),
|
|
215
|
+
description: z.optional(z.string()),
|
|
216
|
+
arguments: z.optional(z.array(PromptArgumentSchema))
|
|
245
217
|
}).loose();
|
|
246
218
|
var ListPromptsResultSchema = PaginatedResultSchema.extend({
|
|
247
|
-
prompts:
|
|
219
|
+
prompts: z.array(PromptSchema)
|
|
248
220
|
});
|
|
249
|
-
var PromptMessageSchema =
|
|
250
|
-
role:
|
|
251
|
-
content:
|
|
221
|
+
var PromptMessageSchema = z.object({
|
|
222
|
+
role: z.union([z.literal("user"), z.literal("assistant")]),
|
|
223
|
+
content: z.union([
|
|
252
224
|
TextContentSchema,
|
|
253
225
|
ImageContentSchema,
|
|
254
226
|
EmbeddedResourceSchema
|
|
255
227
|
])
|
|
256
228
|
}).loose();
|
|
257
229
|
var GetPromptResultSchema = ResultSchema.extend({
|
|
258
|
-
description:
|
|
259
|
-
messages:
|
|
230
|
+
description: z.optional(z.string()),
|
|
231
|
+
messages: z.array(PromptMessageSchema)
|
|
260
232
|
});
|
|
261
233
|
var ElicitationRequestParamsSchema = BaseParamsSchema.extend({
|
|
262
|
-
message:
|
|
263
|
-
requestedSchema:
|
|
234
|
+
message: z.string(),
|
|
235
|
+
requestedSchema: z.unknown()
|
|
264
236
|
});
|
|
265
237
|
var ElicitationRequestSchema = RequestSchema.extend({
|
|
266
|
-
method:
|
|
238
|
+
method: z.literal("elicitation/create"),
|
|
267
239
|
params: ElicitationRequestParamsSchema
|
|
268
240
|
});
|
|
269
241
|
var ElicitResultSchema = ResultSchema.extend({
|
|
270
|
-
action:
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
242
|
+
action: z.union([
|
|
243
|
+
z.literal("accept"),
|
|
244
|
+
z.literal("decline"),
|
|
245
|
+
z.literal("cancel")
|
|
274
246
|
]),
|
|
275
|
-
content:
|
|
247
|
+
content: z.optional(z.record(z.string(), z.unknown()))
|
|
276
248
|
});
|
|
277
249
|
|
|
278
250
|
// src/tool/json-rpc-message.ts
|
|
279
251
|
var JSONRPC_VERSION = "2.0";
|
|
280
|
-
var JSONRPCRequestSchema =
|
|
281
|
-
jsonrpc:
|
|
282
|
-
id:
|
|
252
|
+
var JSONRPCRequestSchema = z2.object({
|
|
253
|
+
jsonrpc: z2.literal(JSONRPC_VERSION),
|
|
254
|
+
id: z2.union([z2.string(), z2.number().int()])
|
|
283
255
|
}).merge(RequestSchema).strict();
|
|
284
|
-
var JSONRPCResponseSchema =
|
|
285
|
-
jsonrpc:
|
|
286
|
-
id:
|
|
256
|
+
var JSONRPCResponseSchema = z2.object({
|
|
257
|
+
jsonrpc: z2.literal(JSONRPC_VERSION),
|
|
258
|
+
id: z2.union([z2.string(), z2.number().int()]),
|
|
287
259
|
result: ResultSchema
|
|
288
260
|
}).strict();
|
|
289
|
-
var JSONRPCErrorSchema =
|
|
290
|
-
jsonrpc:
|
|
291
|
-
id:
|
|
292
|
-
error:
|
|
293
|
-
code:
|
|
294
|
-
message:
|
|
295
|
-
data:
|
|
261
|
+
var JSONRPCErrorSchema = z2.object({
|
|
262
|
+
jsonrpc: z2.literal(JSONRPC_VERSION),
|
|
263
|
+
id: z2.union([z2.string(), z2.number().int()]),
|
|
264
|
+
error: z2.object({
|
|
265
|
+
code: z2.number().int(),
|
|
266
|
+
message: z2.string(),
|
|
267
|
+
data: z2.optional(z2.unknown())
|
|
296
268
|
})
|
|
297
269
|
}).strict();
|
|
298
|
-
var JSONRPCNotificationSchema =
|
|
299
|
-
jsonrpc:
|
|
270
|
+
var JSONRPCNotificationSchema = z2.object({
|
|
271
|
+
jsonrpc: z2.literal(JSONRPC_VERSION)
|
|
300
272
|
}).merge(
|
|
301
|
-
|
|
302
|
-
method:
|
|
303
|
-
params:
|
|
273
|
+
z2.object({
|
|
274
|
+
method: z2.string(),
|
|
275
|
+
params: z2.optional(BaseParamsSchema)
|
|
304
276
|
})
|
|
305
277
|
).strict();
|
|
306
|
-
var JSONRPCMessageSchema =
|
|
278
|
+
var JSONRPCMessageSchema = z2.union([
|
|
307
279
|
JSONRPCRequestSchema,
|
|
308
280
|
JSONRPCNotificationSchema,
|
|
309
281
|
JSONRPCResponseSchema,
|
|
310
282
|
JSONRPCErrorSchema
|
|
311
283
|
]);
|
|
284
|
+
async function parseJSONRPCMessage(text) {
|
|
285
|
+
return JSONRPCMessageSchema.parse(await parseJSON({ text }));
|
|
286
|
+
}
|
|
312
287
|
|
|
313
288
|
// src/version.ts
|
|
314
289
|
var VERSION = typeof __PACKAGE_VERSION__ !== "undefined" ? __PACKAGE_VERSION__ : "0.0.0-test";
|
|
315
290
|
|
|
316
291
|
// src/tool/oauth.ts
|
|
317
|
-
|
|
292
|
+
import pkceChallenge from "pkce-challenge";
|
|
318
293
|
|
|
319
294
|
// src/tool/oauth-types.ts
|
|
320
|
-
|
|
321
|
-
var OAuthTokensSchema =
|
|
322
|
-
access_token:
|
|
323
|
-
id_token:
|
|
295
|
+
import { z as z3 } from "zod/v4";
|
|
296
|
+
var OAuthTokensSchema = z3.object({
|
|
297
|
+
access_token: z3.string(),
|
|
298
|
+
id_token: z3.string().optional(),
|
|
324
299
|
// Optional for OAuth 2.1, but necessary in OpenID Connect
|
|
325
|
-
token_type:
|
|
326
|
-
expires_in:
|
|
327
|
-
scope:
|
|
328
|
-
refresh_token:
|
|
300
|
+
token_type: z3.string(),
|
|
301
|
+
expires_in: z3.number().optional(),
|
|
302
|
+
scope: z3.string().optional(),
|
|
303
|
+
refresh_token: z3.string().optional()
|
|
329
304
|
}).strip();
|
|
330
|
-
var SafeUrlSchema =
|
|
305
|
+
var SafeUrlSchema = z3.string().url().superRefine((val, ctx) => {
|
|
331
306
|
if (!URL.canParse(val)) {
|
|
332
307
|
ctx.addIssue({
|
|
333
|
-
code:
|
|
308
|
+
code: z3.ZodIssueCode.custom,
|
|
334
309
|
message: "URL must be parseable",
|
|
335
310
|
fatal: true
|
|
336
311
|
});
|
|
337
|
-
return
|
|
312
|
+
return z3.NEVER;
|
|
338
313
|
}
|
|
339
314
|
}).refine(
|
|
340
315
|
(url) => {
|
|
@@ -343,94 +318,94 @@ var SafeUrlSchema = import_v43.z.string().url().superRefine((val, ctx) => {
|
|
|
343
318
|
},
|
|
344
319
|
{ message: "URL cannot use javascript:, data:, or vbscript: scheme" }
|
|
345
320
|
);
|
|
346
|
-
var OAuthProtectedResourceMetadataSchema =
|
|
347
|
-
resource:
|
|
348
|
-
authorization_servers:
|
|
349
|
-
jwks_uri:
|
|
350
|
-
scopes_supported:
|
|
351
|
-
bearer_methods_supported:
|
|
352
|
-
resource_signing_alg_values_supported:
|
|
353
|
-
resource_name:
|
|
354
|
-
resource_documentation:
|
|
355
|
-
resource_policy_uri:
|
|
356
|
-
resource_tos_uri:
|
|
357
|
-
tls_client_certificate_bound_access_tokens:
|
|
358
|
-
authorization_details_types_supported:
|
|
359
|
-
dpop_signing_alg_values_supported:
|
|
360
|
-
dpop_bound_access_tokens_required:
|
|
321
|
+
var OAuthProtectedResourceMetadataSchema = z3.object({
|
|
322
|
+
resource: z3.string().url(),
|
|
323
|
+
authorization_servers: z3.array(SafeUrlSchema).optional(),
|
|
324
|
+
jwks_uri: z3.string().url().optional(),
|
|
325
|
+
scopes_supported: z3.array(z3.string()).optional(),
|
|
326
|
+
bearer_methods_supported: z3.array(z3.string()).optional(),
|
|
327
|
+
resource_signing_alg_values_supported: z3.array(z3.string()).optional(),
|
|
328
|
+
resource_name: z3.string().optional(),
|
|
329
|
+
resource_documentation: z3.string().optional(),
|
|
330
|
+
resource_policy_uri: z3.string().url().optional(),
|
|
331
|
+
resource_tos_uri: z3.string().url().optional(),
|
|
332
|
+
tls_client_certificate_bound_access_tokens: z3.boolean().optional(),
|
|
333
|
+
authorization_details_types_supported: z3.array(z3.string()).optional(),
|
|
334
|
+
dpop_signing_alg_values_supported: z3.array(z3.string()).optional(),
|
|
335
|
+
dpop_bound_access_tokens_required: z3.boolean().optional()
|
|
361
336
|
}).passthrough();
|
|
362
|
-
var OAuthMetadataSchema =
|
|
363
|
-
issuer:
|
|
337
|
+
var OAuthMetadataSchema = z3.object({
|
|
338
|
+
issuer: z3.string(),
|
|
364
339
|
authorization_endpoint: SafeUrlSchema,
|
|
365
340
|
token_endpoint: SafeUrlSchema,
|
|
366
341
|
registration_endpoint: SafeUrlSchema.optional(),
|
|
367
|
-
scopes_supported:
|
|
368
|
-
response_types_supported:
|
|
369
|
-
grant_types_supported:
|
|
370
|
-
code_challenge_methods_supported:
|
|
371
|
-
token_endpoint_auth_methods_supported:
|
|
372
|
-
token_endpoint_auth_signing_alg_values_supported:
|
|
342
|
+
scopes_supported: z3.array(z3.string()).optional(),
|
|
343
|
+
response_types_supported: z3.array(z3.string()),
|
|
344
|
+
grant_types_supported: z3.array(z3.string()).optional(),
|
|
345
|
+
code_challenge_methods_supported: z3.array(z3.string()),
|
|
346
|
+
token_endpoint_auth_methods_supported: z3.array(z3.string()).optional(),
|
|
347
|
+
token_endpoint_auth_signing_alg_values_supported: z3.array(z3.string()).optional()
|
|
373
348
|
}).passthrough();
|
|
374
|
-
var OpenIdProviderMetadataSchema =
|
|
375
|
-
issuer:
|
|
349
|
+
var OpenIdProviderMetadataSchema = z3.object({
|
|
350
|
+
issuer: z3.string(),
|
|
376
351
|
authorization_endpoint: SafeUrlSchema,
|
|
377
352
|
token_endpoint: SafeUrlSchema,
|
|
378
353
|
userinfo_endpoint: SafeUrlSchema.optional(),
|
|
379
354
|
jwks_uri: SafeUrlSchema,
|
|
380
355
|
registration_endpoint: SafeUrlSchema.optional(),
|
|
381
|
-
scopes_supported:
|
|
382
|
-
response_types_supported:
|
|
383
|
-
grant_types_supported:
|
|
384
|
-
subject_types_supported:
|
|
385
|
-
id_token_signing_alg_values_supported:
|
|
386
|
-
claims_supported:
|
|
387
|
-
token_endpoint_auth_methods_supported:
|
|
356
|
+
scopes_supported: z3.array(z3.string()).optional(),
|
|
357
|
+
response_types_supported: z3.array(z3.string()),
|
|
358
|
+
grant_types_supported: z3.array(z3.string()).optional(),
|
|
359
|
+
subject_types_supported: z3.array(z3.string()),
|
|
360
|
+
id_token_signing_alg_values_supported: z3.array(z3.string()),
|
|
361
|
+
claims_supported: z3.array(z3.string()).optional(),
|
|
362
|
+
token_endpoint_auth_methods_supported: z3.array(z3.string()).optional()
|
|
388
363
|
}).passthrough();
|
|
389
364
|
var OpenIdProviderDiscoveryMetadataSchema = OpenIdProviderMetadataSchema.merge(
|
|
390
365
|
OAuthMetadataSchema.pick({
|
|
391
366
|
code_challenge_methods_supported: true
|
|
392
367
|
})
|
|
393
368
|
);
|
|
394
|
-
var OAuthClientInformationSchema =
|
|
395
|
-
client_id:
|
|
396
|
-
client_secret:
|
|
397
|
-
client_id_issued_at:
|
|
398
|
-
client_secret_expires_at:
|
|
369
|
+
var OAuthClientInformationSchema = z3.object({
|
|
370
|
+
client_id: z3.string(),
|
|
371
|
+
client_secret: z3.string().optional(),
|
|
372
|
+
client_id_issued_at: z3.number().optional(),
|
|
373
|
+
client_secret_expires_at: z3.number().optional()
|
|
399
374
|
}).strip();
|
|
400
|
-
var OAuthClientMetadataSchema =
|
|
401
|
-
redirect_uris:
|
|
402
|
-
token_endpoint_auth_method:
|
|
403
|
-
grant_types:
|
|
404
|
-
response_types:
|
|
405
|
-
client_name:
|
|
375
|
+
var OAuthClientMetadataSchema = z3.object({
|
|
376
|
+
redirect_uris: z3.array(SafeUrlSchema),
|
|
377
|
+
token_endpoint_auth_method: z3.string().optional(),
|
|
378
|
+
grant_types: z3.array(z3.string()).optional(),
|
|
379
|
+
response_types: z3.array(z3.string()).optional(),
|
|
380
|
+
client_name: z3.string().optional(),
|
|
406
381
|
client_uri: SafeUrlSchema.optional(),
|
|
407
382
|
logo_uri: SafeUrlSchema.optional(),
|
|
408
|
-
scope:
|
|
409
|
-
contacts:
|
|
383
|
+
scope: z3.string().optional(),
|
|
384
|
+
contacts: z3.array(z3.string()).optional(),
|
|
410
385
|
tos_uri: SafeUrlSchema.optional(),
|
|
411
|
-
policy_uri:
|
|
386
|
+
policy_uri: z3.string().optional(),
|
|
412
387
|
jwks_uri: SafeUrlSchema.optional(),
|
|
413
|
-
jwks:
|
|
414
|
-
software_id:
|
|
415
|
-
software_version:
|
|
416
|
-
software_statement:
|
|
388
|
+
jwks: z3.any().optional(),
|
|
389
|
+
software_id: z3.string().optional(),
|
|
390
|
+
software_version: z3.string().optional(),
|
|
391
|
+
software_statement: z3.string().optional()
|
|
417
392
|
}).strip();
|
|
418
|
-
var OAuthErrorResponseSchema =
|
|
419
|
-
error:
|
|
420
|
-
error_description:
|
|
421
|
-
error_uri:
|
|
393
|
+
var OAuthErrorResponseSchema = z3.object({
|
|
394
|
+
error: z3.string(),
|
|
395
|
+
error_description: z3.string().optional(),
|
|
396
|
+
error_uri: z3.string().optional()
|
|
422
397
|
});
|
|
423
398
|
var OAuthClientInformationFullSchema = OAuthClientMetadataSchema.merge(
|
|
424
399
|
OAuthClientInformationSchema
|
|
425
400
|
);
|
|
426
401
|
|
|
427
402
|
// src/error/oauth-error.ts
|
|
428
|
-
|
|
403
|
+
import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
|
|
429
404
|
var name2 = "AI_MCPClientOAuthError";
|
|
430
405
|
var marker2 = `vercel.ai.error.${name2}`;
|
|
431
406
|
var symbol2 = Symbol.for(marker2);
|
|
432
407
|
var _a2, _b2;
|
|
433
|
-
var MCPClientOAuthError = class extends (_b2 =
|
|
408
|
+
var MCPClientOAuthError = class extends (_b2 = AISDKError2, _a2 = symbol2, _b2) {
|
|
434
409
|
constructor({
|
|
435
410
|
name: name3 = "MCPClientOAuthError",
|
|
436
411
|
message,
|
|
@@ -440,7 +415,7 @@ var MCPClientOAuthError = class extends (_b2 = import_provider2.AISDKError, _a2
|
|
|
440
415
|
this[_a2] = true;
|
|
441
416
|
}
|
|
442
417
|
static isInstance(error) {
|
|
443
|
-
return
|
|
418
|
+
return AISDKError2.hasMarker(error, marker2);
|
|
444
419
|
}
|
|
445
420
|
};
|
|
446
421
|
var ServerError = class extends MCPClientOAuthError {
|
|
@@ -468,6 +443,13 @@ function resourceUrlFromServerUrl(url) {
|
|
|
468
443
|
resourceURL.hash = "";
|
|
469
444
|
return resourceURL;
|
|
470
445
|
}
|
|
446
|
+
function resourceUrlStripSlash(resource) {
|
|
447
|
+
const href = resource.href;
|
|
448
|
+
if (resource.pathname === "/" && href.endsWith("/")) {
|
|
449
|
+
return href.slice(0, -1);
|
|
450
|
+
}
|
|
451
|
+
return href;
|
|
452
|
+
}
|
|
471
453
|
function checkResourceAllowed({
|
|
472
454
|
requestedResource,
|
|
473
455
|
configuredResource
|
|
@@ -486,6 +468,7 @@ function checkResourceAllowed({
|
|
|
486
468
|
}
|
|
487
469
|
|
|
488
470
|
// src/tool/oauth.ts
|
|
471
|
+
import { parseJSON as parseJSON2 } from "@ai-sdk/provider-utils";
|
|
489
472
|
var UnauthorizedError = class extends Error {
|
|
490
473
|
constructor(message = "Unauthorized") {
|
|
491
474
|
super(message);
|
|
@@ -685,7 +668,7 @@ async function startAuthorization(authorizationServerUrl, {
|
|
|
685
668
|
} else {
|
|
686
669
|
authorizationUrl = new URL("/authorize", authorizationServerUrl);
|
|
687
670
|
}
|
|
688
|
-
const challenge = await (
|
|
671
|
+
const challenge = await pkceChallenge();
|
|
689
672
|
const codeVerifier = challenge.code_verifier;
|
|
690
673
|
const codeChallenge = challenge.code_challenge;
|
|
691
674
|
authorizationUrl.searchParams.set("response_type", responseType);
|
|
@@ -706,7 +689,10 @@ async function startAuthorization(authorizationServerUrl, {
|
|
|
706
689
|
authorizationUrl.searchParams.append("prompt", "consent");
|
|
707
690
|
}
|
|
708
691
|
if (resource) {
|
|
709
|
-
authorizationUrl.searchParams.set(
|
|
692
|
+
authorizationUrl.searchParams.set(
|
|
693
|
+
"resource",
|
|
694
|
+
resourceUrlStripSlash(resource)
|
|
695
|
+
);
|
|
710
696
|
}
|
|
711
697
|
return { authorizationUrl, codeVerifier };
|
|
712
698
|
}
|
|
@@ -764,7 +750,9 @@ async function parseErrorResponse(input) {
|
|
|
764
750
|
const statusCode = input instanceof Response ? input.status : void 0;
|
|
765
751
|
const body = input instanceof Response ? await input.text() : input;
|
|
766
752
|
try {
|
|
767
|
-
const result = OAuthErrorResponseSchema.parse(
|
|
753
|
+
const result = OAuthErrorResponseSchema.parse(
|
|
754
|
+
await parseJSON2({ text: body })
|
|
755
|
+
);
|
|
768
756
|
const { error, error_description, error_uri } = result;
|
|
769
757
|
const errorClass = OAUTH_ERRORS[error] || ServerError;
|
|
770
758
|
return new errorClass({
|
|
@@ -815,7 +803,7 @@ async function exchangeAuthorization(authorizationServerUrl, {
|
|
|
815
803
|
applyClientAuthentication(authMethod, clientInformation, headers, params);
|
|
816
804
|
}
|
|
817
805
|
if (resource) {
|
|
818
|
-
params.set("resource", resource
|
|
806
|
+
params.set("resource", resourceUrlStripSlash(resource));
|
|
819
807
|
}
|
|
820
808
|
const response = await (fetchFn != null ? fetchFn : fetch)(tokenUrl, {
|
|
821
809
|
method: "POST",
|
|
@@ -867,7 +855,7 @@ async function refreshAuthorization(authorizationServerUrl, {
|
|
|
867
855
|
applyClientAuthentication(authMethod, clientInformation, headers, params);
|
|
868
856
|
}
|
|
869
857
|
if (resource) {
|
|
870
|
-
params.set("resource", resource
|
|
858
|
+
params.set("resource", resourceUrlStripSlash(resource));
|
|
871
859
|
}
|
|
872
860
|
const response = await (fetchFn != null ? fetchFn : fetch)(tokenUrl, {
|
|
873
861
|
method: "POST",
|
|
@@ -1073,13 +1061,15 @@ var SseMCPTransport = class {
|
|
|
1073
1061
|
url,
|
|
1074
1062
|
headers,
|
|
1075
1063
|
authProvider,
|
|
1076
|
-
redirect = "error"
|
|
1064
|
+
redirect = "error",
|
|
1065
|
+
fetch: fetchFn
|
|
1077
1066
|
}) {
|
|
1078
1067
|
this.connected = false;
|
|
1079
1068
|
this.url = new URL(url);
|
|
1080
1069
|
this.headers = headers;
|
|
1081
1070
|
this.authProvider = authProvider;
|
|
1082
1071
|
this.redirectMode = redirect;
|
|
1072
|
+
this.fetchFn = fetchFn != null ? fetchFn : globalThis.fetch;
|
|
1083
1073
|
}
|
|
1084
1074
|
async commonHeaders(base) {
|
|
1085
1075
|
const headers = {
|
|
@@ -1093,10 +1083,10 @@ var SseMCPTransport = class {
|
|
|
1093
1083
|
headers["Authorization"] = `Bearer ${tokens.access_token}`;
|
|
1094
1084
|
}
|
|
1095
1085
|
}
|
|
1096
|
-
return
|
|
1086
|
+
return withUserAgentSuffix(
|
|
1097
1087
|
headers,
|
|
1098
1088
|
`ai-sdk/${VERSION}`,
|
|
1099
|
-
|
|
1089
|
+
getRuntimeEnvironmentUserAgent()
|
|
1100
1090
|
);
|
|
1101
1091
|
}
|
|
1102
1092
|
async start() {
|
|
@@ -1111,7 +1101,7 @@ var SseMCPTransport = class {
|
|
|
1111
1101
|
const headers = await this.commonHeaders({
|
|
1112
1102
|
Accept: "text/event-stream"
|
|
1113
1103
|
});
|
|
1114
|
-
const response = await
|
|
1104
|
+
const response = await this.fetchFn(this.url.href, {
|
|
1115
1105
|
headers,
|
|
1116
1106
|
signal: (_a3 = this.abortController) == null ? void 0 : _a3.signal,
|
|
1117
1107
|
redirect: this.redirectMode
|
|
@@ -1121,7 +1111,8 @@ var SseMCPTransport = class {
|
|
|
1121
1111
|
try {
|
|
1122
1112
|
const result = await auth(this.authProvider, {
|
|
1123
1113
|
serverUrl: this.url,
|
|
1124
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1114
|
+
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
1115
|
+
fetchFn: this.fetchFn
|
|
1125
1116
|
});
|
|
1126
1117
|
if (result !== "AUTHORIZED") {
|
|
1127
1118
|
const error = new UnauthorizedError();
|
|
@@ -1145,7 +1136,7 @@ var SseMCPTransport = class {
|
|
|
1145
1136
|
(_d = this.onerror) == null ? void 0 : _d.call(this, error);
|
|
1146
1137
|
return reject(error);
|
|
1147
1138
|
}
|
|
1148
|
-
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new
|
|
1139
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream());
|
|
1149
1140
|
const reader = stream.getReader();
|
|
1150
1141
|
const processEvents = async () => {
|
|
1151
1142
|
var _a4, _b4, _c2;
|
|
@@ -1173,9 +1164,7 @@ var SseMCPTransport = class {
|
|
|
1173
1164
|
resolve();
|
|
1174
1165
|
} else if (event === "message") {
|
|
1175
1166
|
try {
|
|
1176
|
-
const message =
|
|
1177
|
-
JSON.parse(data)
|
|
1178
|
-
);
|
|
1167
|
+
const message = await parseJSONRPCMessage(data);
|
|
1179
1168
|
(_a4 = this.onmessage) == null ? void 0 : _a4.call(this, message);
|
|
1180
1169
|
} catch (error) {
|
|
1181
1170
|
const e = new MCPClientError({
|
|
@@ -1236,13 +1225,14 @@ var SseMCPTransport = class {
|
|
|
1236
1225
|
signal: (_a3 = this.abortController) == null ? void 0 : _a3.signal,
|
|
1237
1226
|
redirect: this.redirectMode
|
|
1238
1227
|
};
|
|
1239
|
-
const response = await
|
|
1228
|
+
const response = await this.fetchFn(endpoint.href, init);
|
|
1240
1229
|
if (response.status === 401 && this.authProvider && !triedAuth) {
|
|
1241
1230
|
this.resourceMetadataUrl = extractResourceMetadataUrl(response);
|
|
1242
1231
|
try {
|
|
1243
1232
|
const result = await auth(this.authProvider, {
|
|
1244
1233
|
serverUrl: this.url,
|
|
1245
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1234
|
+
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
1235
|
+
fetchFn: this.fetchFn
|
|
1246
1236
|
});
|
|
1247
1237
|
if (result !== "AUTHORIZED") {
|
|
1248
1238
|
const error = new UnauthorizedError();
|
|
@@ -1273,13 +1263,18 @@ var SseMCPTransport = class {
|
|
|
1273
1263
|
};
|
|
1274
1264
|
|
|
1275
1265
|
// src/tool/mcp-http-transport.ts
|
|
1276
|
-
|
|
1266
|
+
import {
|
|
1267
|
+
EventSourceParserStream as EventSourceParserStream2,
|
|
1268
|
+
withUserAgentSuffix as withUserAgentSuffix2,
|
|
1269
|
+
getRuntimeEnvironmentUserAgent as getRuntimeEnvironmentUserAgent2
|
|
1270
|
+
} from "@ai-sdk/provider-utils";
|
|
1277
1271
|
var HttpMCPTransport = class {
|
|
1278
1272
|
constructor({
|
|
1279
1273
|
url,
|
|
1280
1274
|
headers,
|
|
1281
1275
|
authProvider,
|
|
1282
|
-
redirect = "error"
|
|
1276
|
+
redirect = "error",
|
|
1277
|
+
fetch: fetchFn
|
|
1283
1278
|
}) {
|
|
1284
1279
|
this.inboundReconnectAttempts = 0;
|
|
1285
1280
|
this.reconnectionOptions = {
|
|
@@ -1292,6 +1287,7 @@ var HttpMCPTransport = class {
|
|
|
1292
1287
|
this.headers = headers;
|
|
1293
1288
|
this.authProvider = authProvider;
|
|
1294
1289
|
this.redirectMode = redirect;
|
|
1290
|
+
this.fetchFn = fetchFn != null ? fetchFn : globalThis.fetch;
|
|
1295
1291
|
}
|
|
1296
1292
|
async commonHeaders(base) {
|
|
1297
1293
|
const headers = {
|
|
@@ -1308,10 +1304,10 @@ var HttpMCPTransport = class {
|
|
|
1308
1304
|
headers["Authorization"] = `Bearer ${tokens.access_token}`;
|
|
1309
1305
|
}
|
|
1310
1306
|
}
|
|
1311
|
-
return (
|
|
1307
|
+
return withUserAgentSuffix2(
|
|
1312
1308
|
headers,
|
|
1313
1309
|
`ai-sdk/${VERSION}`,
|
|
1314
|
-
(
|
|
1310
|
+
getRuntimeEnvironmentUserAgent2()
|
|
1315
1311
|
);
|
|
1316
1312
|
}
|
|
1317
1313
|
async start() {
|
|
@@ -1329,7 +1325,7 @@ var HttpMCPTransport = class {
|
|
|
1329
1325
|
try {
|
|
1330
1326
|
if (this.sessionId && this.abortController && !this.abortController.signal.aborted) {
|
|
1331
1327
|
const headers = await this.commonHeaders({});
|
|
1332
|
-
await
|
|
1328
|
+
await this.fetchFn(this.url.href, {
|
|
1333
1329
|
method: "DELETE",
|
|
1334
1330
|
headers,
|
|
1335
1331
|
signal: this.abortController.signal,
|
|
@@ -1356,7 +1352,7 @@ var HttpMCPTransport = class {
|
|
|
1356
1352
|
signal: (_a3 = this.abortController) == null ? void 0 : _a3.signal,
|
|
1357
1353
|
redirect: this.redirectMode
|
|
1358
1354
|
};
|
|
1359
|
-
const response = await
|
|
1355
|
+
const response = await this.fetchFn(this.url.href, init);
|
|
1360
1356
|
const sessionId = response.headers.get("mcp-session-id");
|
|
1361
1357
|
if (sessionId) {
|
|
1362
1358
|
this.sessionId = sessionId;
|
|
@@ -1366,7 +1362,8 @@ var HttpMCPTransport = class {
|
|
|
1366
1362
|
try {
|
|
1367
1363
|
const result = await auth(this.authProvider, {
|
|
1368
1364
|
serverUrl: this.url,
|
|
1369
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1365
|
+
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
1366
|
+
fetchFn: this.fetchFn
|
|
1370
1367
|
});
|
|
1371
1368
|
if (result !== "AUTHORIZED") {
|
|
1372
1369
|
const error2 = new UnauthorizedError();
|
|
@@ -1415,7 +1412,7 @@ var HttpMCPTransport = class {
|
|
|
1415
1412
|
(_e = this.onerror) == null ? void 0 : _e.call(this, error2);
|
|
1416
1413
|
throw error2;
|
|
1417
1414
|
}
|
|
1418
|
-
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new
|
|
1415
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream2());
|
|
1419
1416
|
const reader = stream.getReader();
|
|
1420
1417
|
const processEvents = async () => {
|
|
1421
1418
|
var _a4, _b4, _c2;
|
|
@@ -1426,7 +1423,7 @@ var HttpMCPTransport = class {
|
|
|
1426
1423
|
const { event, data } = value;
|
|
1427
1424
|
if (event === "message") {
|
|
1428
1425
|
try {
|
|
1429
|
-
const msg =
|
|
1426
|
+
const msg = await parseJSONRPCMessage(data);
|
|
1430
1427
|
(_a4 = this.onmessage) == null ? void 0 : _a4.call(this, msg);
|
|
1431
1428
|
} catch (error2) {
|
|
1432
1429
|
const e = new MCPClientError({
|
|
@@ -1500,7 +1497,7 @@ var HttpMCPTransport = class {
|
|
|
1500
1497
|
if (resumeToken) {
|
|
1501
1498
|
headers["last-event-id"] = resumeToken;
|
|
1502
1499
|
}
|
|
1503
|
-
const response = await
|
|
1500
|
+
const response = await this.fetchFn(this.url.href, {
|
|
1504
1501
|
method: "GET",
|
|
1505
1502
|
headers,
|
|
1506
1503
|
signal: (_a3 = this.abortController) == null ? void 0 : _a3.signal,
|
|
@@ -1515,7 +1512,8 @@ var HttpMCPTransport = class {
|
|
|
1515
1512
|
try {
|
|
1516
1513
|
const result = await auth(this.authProvider, {
|
|
1517
1514
|
serverUrl: this.url,
|
|
1518
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1515
|
+
resourceMetadataUrl: this.resourceMetadataUrl,
|
|
1516
|
+
fetchFn: this.fetchFn
|
|
1519
1517
|
});
|
|
1520
1518
|
if (result !== "AUTHORIZED") {
|
|
1521
1519
|
const error = new UnauthorizedError();
|
|
@@ -1538,7 +1536,7 @@ var HttpMCPTransport = class {
|
|
|
1538
1536
|
(_d = this.onerror) == null ? void 0 : _d.call(this, error);
|
|
1539
1537
|
return;
|
|
1540
1538
|
}
|
|
1541
|
-
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new
|
|
1539
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream2());
|
|
1542
1540
|
const reader = stream.getReader();
|
|
1543
1541
|
const processEvents = async () => {
|
|
1544
1542
|
var _a4, _b4, _c2, _d2;
|
|
@@ -1552,7 +1550,7 @@ var HttpMCPTransport = class {
|
|
|
1552
1550
|
}
|
|
1553
1551
|
if (event === "message") {
|
|
1554
1552
|
try {
|
|
1555
|
-
const msg =
|
|
1553
|
+
const msg = await parseJSONRPCMessage(data);
|
|
1556
1554
|
(_a4 = this.onmessage) == null ? void 0 : _a4.call(this, msg);
|
|
1557
1555
|
} catch (error) {
|
|
1558
1556
|
const e = new MCPClientError({
|
|
@@ -1623,7 +1621,7 @@ function mcpToModelOutput({
|
|
|
1623
1621
|
}
|
|
1624
1622
|
if (part.type === "image" && "data" in part && "mimeType" in part) {
|
|
1625
1623
|
return {
|
|
1626
|
-
type: "
|
|
1624
|
+
type: "file-data",
|
|
1627
1625
|
data: part.data,
|
|
1628
1626
|
mediaType: part.mimeType
|
|
1629
1627
|
};
|
|
@@ -1649,6 +1647,7 @@ var DefaultMCPClient = class {
|
|
|
1649
1647
|
this.requestMessageId = 0;
|
|
1650
1648
|
this.responseHandlers = /* @__PURE__ */ new Map();
|
|
1651
1649
|
this.serverCapabilities = {};
|
|
1650
|
+
this._serverInfo = { name: "", version: "" };
|
|
1652
1651
|
this.isClosed = true;
|
|
1653
1652
|
this.onUncaughtError = onUncaughtError;
|
|
1654
1653
|
this.clientCapabilities = capabilities != null ? capabilities : {};
|
|
@@ -1679,6 +1678,12 @@ var DefaultMCPClient = class {
|
|
|
1679
1678
|
version
|
|
1680
1679
|
};
|
|
1681
1680
|
}
|
|
1681
|
+
get serverInfo() {
|
|
1682
|
+
return this._serverInfo;
|
|
1683
|
+
}
|
|
1684
|
+
get instructions() {
|
|
1685
|
+
return this._serverInstructions;
|
|
1686
|
+
}
|
|
1682
1687
|
async init() {
|
|
1683
1688
|
try {
|
|
1684
1689
|
await this.transport.start();
|
|
@@ -1705,6 +1710,8 @@ var DefaultMCPClient = class {
|
|
|
1705
1710
|
});
|
|
1706
1711
|
}
|
|
1707
1712
|
this.serverCapabilities = result.capabilities;
|
|
1713
|
+
this._serverInfo = result.serverInfo;
|
|
1714
|
+
this._serverInstructions = result.instructions;
|
|
1708
1715
|
await this.notification({
|
|
1709
1716
|
method: "notifications/initialized"
|
|
1710
1717
|
});
|
|
@@ -1950,24 +1957,33 @@ var DefaultMCPClient = class {
|
|
|
1950
1957
|
var _a4;
|
|
1951
1958
|
(_a4 = options == null ? void 0 : options.abortSignal) == null ? void 0 : _a4.throwIfAborted();
|
|
1952
1959
|
const result = await self.callTool({ name: name3, args, options });
|
|
1960
|
+
if (result.isError) {
|
|
1961
|
+
return result;
|
|
1962
|
+
}
|
|
1953
1963
|
if (outputSchema != null) {
|
|
1954
1964
|
return self.extractStructuredContent(result, outputSchema, name3);
|
|
1955
1965
|
}
|
|
1956
1966
|
return result;
|
|
1957
1967
|
};
|
|
1958
|
-
const toolWithExecute = schemas === "automatic" ?
|
|
1968
|
+
const toolWithExecute = schemas === "automatic" ? dynamicTool({
|
|
1959
1969
|
description,
|
|
1960
1970
|
title: resolvedTitle,
|
|
1961
|
-
|
|
1971
|
+
providerMetadata: {
|
|
1972
|
+
mcp: { name: this.clientInfo.name }
|
|
1973
|
+
},
|
|
1974
|
+
inputSchema: jsonSchema({
|
|
1962
1975
|
...inputSchema,
|
|
1963
1976
|
properties: (_b3 = inputSchema.properties) != null ? _b3 : {},
|
|
1964
1977
|
additionalProperties: false
|
|
1965
1978
|
}),
|
|
1966
1979
|
execute,
|
|
1967
1980
|
toModelOutput: mcpToModelOutput
|
|
1968
|
-
}) :
|
|
1981
|
+
}) : tool({
|
|
1969
1982
|
description,
|
|
1970
1983
|
title: resolvedTitle,
|
|
1984
|
+
providerMetadata: {
|
|
1985
|
+
mcp: { name: this.clientInfo.name }
|
|
1986
|
+
},
|
|
1971
1987
|
inputSchema: schemas[name3].inputSchema,
|
|
1972
1988
|
...outputSchema != null ? { outputSchema } : {},
|
|
1973
1989
|
execute,
|
|
@@ -1982,9 +1998,9 @@ var DefaultMCPClient = class {
|
|
|
1982
1998
|
*/
|
|
1983
1999
|
async extractStructuredContent(result, outputSchema, toolName) {
|
|
1984
2000
|
if ("structuredContent" in result && result.structuredContent != null) {
|
|
1985
|
-
const validationResult = await
|
|
2001
|
+
const validationResult = await safeValidateTypes({
|
|
1986
2002
|
value: result.structuredContent,
|
|
1987
|
-
schema:
|
|
2003
|
+
schema: asSchema(outputSchema)
|
|
1988
2004
|
});
|
|
1989
2005
|
if (!validationResult.success) {
|
|
1990
2006
|
throw new MCPClientError({
|
|
@@ -1997,7 +2013,7 @@ var DefaultMCPClient = class {
|
|
|
1997
2013
|
if ("content" in result && Array.isArray(result.content)) {
|
|
1998
2014
|
const textContent = result.content.find((c) => c.type === "text");
|
|
1999
2015
|
if (textContent && "text" in textContent) {
|
|
2000
|
-
const parseResult = await
|
|
2016
|
+
const parseResult = await safeParseJSON({
|
|
2001
2017
|
text: textContent.text,
|
|
2002
2018
|
schema: outputSchema
|
|
2003
2019
|
});
|
|
@@ -2152,13 +2168,12 @@ var DefaultMCPClient = class {
|
|
|
2152
2168
|
);
|
|
2153
2169
|
}
|
|
2154
2170
|
};
|
|
2155
|
-
|
|
2156
|
-
0 && (module.exports = {
|
|
2171
|
+
export {
|
|
2157
2172
|
ElicitResultSchema,
|
|
2158
2173
|
ElicitationRequestSchema,
|
|
2159
2174
|
UnauthorizedError,
|
|
2160
2175
|
auth,
|
|
2161
2176
|
createMCPClient,
|
|
2162
|
-
experimental_createMCPClient
|
|
2163
|
-
}
|
|
2177
|
+
createMCPClient as experimental_createMCPClient
|
|
2178
|
+
};
|
|
2164
2179
|
//# sourceMappingURL=index.js.map
|