getpatter 0.5.4 → 0.6.1
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/LICENSE +1 -1
- package/README.md +5 -2
- package/dist/aec-PJJMUM5E.mjs +228 -0
- package/dist/{banner-3GNZ6VQK.mjs → banner-UYW6UM3J.mjs} +4 -1
- package/dist/barge-in-strategies-X6ARMGIQ.mjs +12 -0
- package/dist/{carrier-config-33HQ2W4V.mjs → carrier-config-4ZKVYAWV.mjs} +5 -2
- package/dist/{chunk-AFUYSNDH.mjs → chunk-6GR5MHHQ.mjs} +9 -0
- package/dist/chunk-CYLJVT5G.mjs +7031 -0
- package/dist/chunk-D4424JZR.mjs +71 -0
- package/dist/{chunk-VJVDG4V5.mjs → chunk-MVOQFAEO.mjs} +5 -0
- package/dist/chunk-N565J3CF.mjs +69 -0
- package/dist/chunk-RV7APPYE.mjs +397 -0
- package/dist/{chunk-FIFIWBL7.mjs → chunk-TEW3NAZJ.mjs} +6000 -3156
- package/dist/{chunk-SEMKNPCD.mjs → chunk-XS45BAQL.mjs} +5 -1
- package/dist/cli.js +304 -640
- package/dist/client-2GJVZT42.mjs +8935 -0
- package/dist/dashboard/ui.html +63 -0
- package/dist/{dist-YRCCJQ26.mjs → dist-RYMPCILF.mjs} +28 -2
- package/dist/index.d.mts +3548 -428
- package/dist/index.d.ts +3548 -428
- package/dist/index.js +34336 -9532
- package/dist/index.mjs +3642 -512
- package/dist/{node-cron-6PRPSBG5.mjs → node-cron-JFWQQRBU.mjs} +23 -2
- package/dist/persistence-LVIAHESK.mjs +7 -0
- package/dist/silero-vad-NSEXI4XS.mjs +7 -0
- package/dist/streamableHttp-WKNGHDVO.mjs +1496 -0
- package/dist/test-mode-WEKKNBLD.mjs +8 -0
- package/dist/tunnel-43CHWPVQ.mjs +8 -0
- package/package.json +7 -7
- package/src/dashboard/ui.html +63 -0
- package/dist/chunk-QHHBUCMT.mjs +0 -25
- package/dist/persistence-LQBYQPQQ.mjs +0 -7
- package/dist/test-mode-MVJ3SKG4.mjs +0 -8
- package/dist/tunnel-UVR3PPAU.mjs +0 -8
|
@@ -0,0 +1,1496 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JSONRPCMessageSchema,
|
|
3
|
+
LATEST_PROTOCOL_VERSION,
|
|
4
|
+
NEVER,
|
|
5
|
+
ZodIssueCode,
|
|
6
|
+
any,
|
|
7
|
+
array,
|
|
8
|
+
boolean,
|
|
9
|
+
coerce_exports,
|
|
10
|
+
isInitializedNotification,
|
|
11
|
+
isJSONRPCRequest,
|
|
12
|
+
isJSONRPCResultResponse,
|
|
13
|
+
literal,
|
|
14
|
+
looseObject,
|
|
15
|
+
number,
|
|
16
|
+
object,
|
|
17
|
+
string,
|
|
18
|
+
url
|
|
19
|
+
} from "./chunk-CYLJVT5G.mjs";
|
|
20
|
+
import {
|
|
21
|
+
init_esm_shims
|
|
22
|
+
} from "./chunk-N565J3CF.mjs";
|
|
23
|
+
|
|
24
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/client/streamableHttp.js
|
|
25
|
+
init_esm_shims();
|
|
26
|
+
|
|
27
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/shared/transport.js
|
|
28
|
+
init_esm_shims();
|
|
29
|
+
function normalizeHeaders(headers) {
|
|
30
|
+
if (!headers)
|
|
31
|
+
return {};
|
|
32
|
+
if (headers instanceof Headers) {
|
|
33
|
+
return Object.fromEntries(headers.entries());
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(headers)) {
|
|
36
|
+
return Object.fromEntries(headers);
|
|
37
|
+
}
|
|
38
|
+
return { ...headers };
|
|
39
|
+
}
|
|
40
|
+
function createFetchWithInit(baseFetch = fetch, baseInit) {
|
|
41
|
+
if (!baseInit) {
|
|
42
|
+
return baseFetch;
|
|
43
|
+
}
|
|
44
|
+
return async (url2, init) => {
|
|
45
|
+
const mergedInit = {
|
|
46
|
+
...baseInit,
|
|
47
|
+
...init,
|
|
48
|
+
// Headers need special handling - merge instead of replace
|
|
49
|
+
headers: init?.headers ? { ...normalizeHeaders(baseInit.headers), ...normalizeHeaders(init.headers) } : baseInit.headers
|
|
50
|
+
};
|
|
51
|
+
return baseFetch(url2, mergedInit);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/client/auth.js
|
|
56
|
+
init_esm_shims();
|
|
57
|
+
|
|
58
|
+
// node_modules/pkce-challenge/dist/index.node.js
|
|
59
|
+
init_esm_shims();
|
|
60
|
+
var crypto;
|
|
61
|
+
crypto = globalThis.crypto?.webcrypto ?? // Node.js [18-16] REPL
|
|
62
|
+
globalThis.crypto ?? // Node.js >18
|
|
63
|
+
import("crypto").then((m) => m.webcrypto);
|
|
64
|
+
async function getRandomValues(size) {
|
|
65
|
+
return (await crypto).getRandomValues(new Uint8Array(size));
|
|
66
|
+
}
|
|
67
|
+
async function random(size) {
|
|
68
|
+
const mask = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";
|
|
69
|
+
const evenDistCutoff = Math.pow(2, 8) - Math.pow(2, 8) % mask.length;
|
|
70
|
+
let result = "";
|
|
71
|
+
while (result.length < size) {
|
|
72
|
+
const randomBytes = await getRandomValues(size - result.length);
|
|
73
|
+
for (const randomByte of randomBytes) {
|
|
74
|
+
if (randomByte < evenDistCutoff) {
|
|
75
|
+
result += mask[randomByte % mask.length];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
async function generateVerifier(length) {
|
|
82
|
+
return await random(length);
|
|
83
|
+
}
|
|
84
|
+
async function generateChallenge(code_verifier) {
|
|
85
|
+
const buffer = await (await crypto).subtle.digest("SHA-256", new TextEncoder().encode(code_verifier));
|
|
86
|
+
return btoa(String.fromCharCode(...new Uint8Array(buffer))).replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");
|
|
87
|
+
}
|
|
88
|
+
async function pkceChallenge(length) {
|
|
89
|
+
if (!length)
|
|
90
|
+
length = 43;
|
|
91
|
+
if (length < 43 || length > 128) {
|
|
92
|
+
throw `Expected a length between 43 and 128. Received ${length}.`;
|
|
93
|
+
}
|
|
94
|
+
const verifier = await generateVerifier(length);
|
|
95
|
+
const challenge = await generateChallenge(verifier);
|
|
96
|
+
return {
|
|
97
|
+
code_verifier: verifier,
|
|
98
|
+
code_challenge: challenge
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/shared/auth.js
|
|
103
|
+
init_esm_shims();
|
|
104
|
+
var SafeUrlSchema = url().superRefine((val, ctx) => {
|
|
105
|
+
if (!URL.canParse(val)) {
|
|
106
|
+
ctx.addIssue({
|
|
107
|
+
code: ZodIssueCode.custom,
|
|
108
|
+
message: "URL must be parseable",
|
|
109
|
+
fatal: true
|
|
110
|
+
});
|
|
111
|
+
return NEVER;
|
|
112
|
+
}
|
|
113
|
+
}).refine((url2) => {
|
|
114
|
+
const u = new URL(url2);
|
|
115
|
+
return u.protocol !== "javascript:" && u.protocol !== "data:" && u.protocol !== "vbscript:";
|
|
116
|
+
}, { message: "URL cannot use javascript:, data:, or vbscript: scheme" });
|
|
117
|
+
var OAuthProtectedResourceMetadataSchema = looseObject({
|
|
118
|
+
resource: string().url(),
|
|
119
|
+
authorization_servers: array(SafeUrlSchema).optional(),
|
|
120
|
+
jwks_uri: string().url().optional(),
|
|
121
|
+
scopes_supported: array(string()).optional(),
|
|
122
|
+
bearer_methods_supported: array(string()).optional(),
|
|
123
|
+
resource_signing_alg_values_supported: array(string()).optional(),
|
|
124
|
+
resource_name: string().optional(),
|
|
125
|
+
resource_documentation: string().optional(),
|
|
126
|
+
resource_policy_uri: string().url().optional(),
|
|
127
|
+
resource_tos_uri: string().url().optional(),
|
|
128
|
+
tls_client_certificate_bound_access_tokens: boolean().optional(),
|
|
129
|
+
authorization_details_types_supported: array(string()).optional(),
|
|
130
|
+
dpop_signing_alg_values_supported: array(string()).optional(),
|
|
131
|
+
dpop_bound_access_tokens_required: boolean().optional()
|
|
132
|
+
});
|
|
133
|
+
var OAuthMetadataSchema = looseObject({
|
|
134
|
+
issuer: string(),
|
|
135
|
+
authorization_endpoint: SafeUrlSchema,
|
|
136
|
+
token_endpoint: SafeUrlSchema,
|
|
137
|
+
registration_endpoint: SafeUrlSchema.optional(),
|
|
138
|
+
scopes_supported: array(string()).optional(),
|
|
139
|
+
response_types_supported: array(string()),
|
|
140
|
+
response_modes_supported: array(string()).optional(),
|
|
141
|
+
grant_types_supported: array(string()).optional(),
|
|
142
|
+
token_endpoint_auth_methods_supported: array(string()).optional(),
|
|
143
|
+
token_endpoint_auth_signing_alg_values_supported: array(string()).optional(),
|
|
144
|
+
service_documentation: SafeUrlSchema.optional(),
|
|
145
|
+
revocation_endpoint: SafeUrlSchema.optional(),
|
|
146
|
+
revocation_endpoint_auth_methods_supported: array(string()).optional(),
|
|
147
|
+
revocation_endpoint_auth_signing_alg_values_supported: array(string()).optional(),
|
|
148
|
+
introspection_endpoint: string().optional(),
|
|
149
|
+
introspection_endpoint_auth_methods_supported: array(string()).optional(),
|
|
150
|
+
introspection_endpoint_auth_signing_alg_values_supported: array(string()).optional(),
|
|
151
|
+
code_challenge_methods_supported: array(string()).optional(),
|
|
152
|
+
client_id_metadata_document_supported: boolean().optional()
|
|
153
|
+
});
|
|
154
|
+
var OpenIdProviderMetadataSchema = looseObject({
|
|
155
|
+
issuer: string(),
|
|
156
|
+
authorization_endpoint: SafeUrlSchema,
|
|
157
|
+
token_endpoint: SafeUrlSchema,
|
|
158
|
+
userinfo_endpoint: SafeUrlSchema.optional(),
|
|
159
|
+
jwks_uri: SafeUrlSchema,
|
|
160
|
+
registration_endpoint: SafeUrlSchema.optional(),
|
|
161
|
+
scopes_supported: array(string()).optional(),
|
|
162
|
+
response_types_supported: array(string()),
|
|
163
|
+
response_modes_supported: array(string()).optional(),
|
|
164
|
+
grant_types_supported: array(string()).optional(),
|
|
165
|
+
acr_values_supported: array(string()).optional(),
|
|
166
|
+
subject_types_supported: array(string()),
|
|
167
|
+
id_token_signing_alg_values_supported: array(string()),
|
|
168
|
+
id_token_encryption_alg_values_supported: array(string()).optional(),
|
|
169
|
+
id_token_encryption_enc_values_supported: array(string()).optional(),
|
|
170
|
+
userinfo_signing_alg_values_supported: array(string()).optional(),
|
|
171
|
+
userinfo_encryption_alg_values_supported: array(string()).optional(),
|
|
172
|
+
userinfo_encryption_enc_values_supported: array(string()).optional(),
|
|
173
|
+
request_object_signing_alg_values_supported: array(string()).optional(),
|
|
174
|
+
request_object_encryption_alg_values_supported: array(string()).optional(),
|
|
175
|
+
request_object_encryption_enc_values_supported: array(string()).optional(),
|
|
176
|
+
token_endpoint_auth_methods_supported: array(string()).optional(),
|
|
177
|
+
token_endpoint_auth_signing_alg_values_supported: array(string()).optional(),
|
|
178
|
+
display_values_supported: array(string()).optional(),
|
|
179
|
+
claim_types_supported: array(string()).optional(),
|
|
180
|
+
claims_supported: array(string()).optional(),
|
|
181
|
+
service_documentation: string().optional(),
|
|
182
|
+
claims_locales_supported: array(string()).optional(),
|
|
183
|
+
ui_locales_supported: array(string()).optional(),
|
|
184
|
+
claims_parameter_supported: boolean().optional(),
|
|
185
|
+
request_parameter_supported: boolean().optional(),
|
|
186
|
+
request_uri_parameter_supported: boolean().optional(),
|
|
187
|
+
require_request_uri_registration: boolean().optional(),
|
|
188
|
+
op_policy_uri: SafeUrlSchema.optional(),
|
|
189
|
+
op_tos_uri: SafeUrlSchema.optional(),
|
|
190
|
+
client_id_metadata_document_supported: boolean().optional()
|
|
191
|
+
});
|
|
192
|
+
var OpenIdProviderDiscoveryMetadataSchema = object({
|
|
193
|
+
...OpenIdProviderMetadataSchema.shape,
|
|
194
|
+
...OAuthMetadataSchema.pick({
|
|
195
|
+
code_challenge_methods_supported: true
|
|
196
|
+
}).shape
|
|
197
|
+
});
|
|
198
|
+
var OAuthTokensSchema = object({
|
|
199
|
+
access_token: string(),
|
|
200
|
+
id_token: string().optional(),
|
|
201
|
+
// Optional for OAuth 2.1, but necessary in OpenID Connect
|
|
202
|
+
token_type: string(),
|
|
203
|
+
expires_in: coerce_exports.number().optional(),
|
|
204
|
+
scope: string().optional(),
|
|
205
|
+
refresh_token: string().optional()
|
|
206
|
+
}).strip();
|
|
207
|
+
var OAuthErrorResponseSchema = object({
|
|
208
|
+
error: string(),
|
|
209
|
+
error_description: string().optional(),
|
|
210
|
+
error_uri: string().optional()
|
|
211
|
+
});
|
|
212
|
+
var OptionalSafeUrlSchema = SafeUrlSchema.optional().or(literal("").transform(() => void 0));
|
|
213
|
+
var OAuthClientMetadataSchema = object({
|
|
214
|
+
redirect_uris: array(SafeUrlSchema),
|
|
215
|
+
token_endpoint_auth_method: string().optional(),
|
|
216
|
+
grant_types: array(string()).optional(),
|
|
217
|
+
response_types: array(string()).optional(),
|
|
218
|
+
client_name: string().optional(),
|
|
219
|
+
client_uri: SafeUrlSchema.optional(),
|
|
220
|
+
logo_uri: OptionalSafeUrlSchema,
|
|
221
|
+
scope: string().optional(),
|
|
222
|
+
contacts: array(string()).optional(),
|
|
223
|
+
tos_uri: OptionalSafeUrlSchema,
|
|
224
|
+
policy_uri: string().optional(),
|
|
225
|
+
jwks_uri: SafeUrlSchema.optional(),
|
|
226
|
+
jwks: any().optional(),
|
|
227
|
+
software_id: string().optional(),
|
|
228
|
+
software_version: string().optional(),
|
|
229
|
+
software_statement: string().optional()
|
|
230
|
+
}).strip();
|
|
231
|
+
var OAuthClientInformationSchema = object({
|
|
232
|
+
client_id: string(),
|
|
233
|
+
client_secret: string().optional(),
|
|
234
|
+
client_id_issued_at: number().optional(),
|
|
235
|
+
client_secret_expires_at: number().optional()
|
|
236
|
+
}).strip();
|
|
237
|
+
var OAuthClientInformationFullSchema = OAuthClientMetadataSchema.merge(OAuthClientInformationSchema);
|
|
238
|
+
var OAuthClientRegistrationErrorSchema = object({
|
|
239
|
+
error: string(),
|
|
240
|
+
error_description: string().optional()
|
|
241
|
+
}).strip();
|
|
242
|
+
var OAuthTokenRevocationRequestSchema = object({
|
|
243
|
+
token: string(),
|
|
244
|
+
token_type_hint: string().optional()
|
|
245
|
+
}).strip();
|
|
246
|
+
|
|
247
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/shared/auth-utils.js
|
|
248
|
+
init_esm_shims();
|
|
249
|
+
function resourceUrlFromServerUrl(url2) {
|
|
250
|
+
const resourceURL = typeof url2 === "string" ? new URL(url2) : new URL(url2.href);
|
|
251
|
+
resourceURL.hash = "";
|
|
252
|
+
return resourceURL;
|
|
253
|
+
}
|
|
254
|
+
function checkResourceAllowed({ requestedResource, configuredResource }) {
|
|
255
|
+
const requested = typeof requestedResource === "string" ? new URL(requestedResource) : new URL(requestedResource.href);
|
|
256
|
+
const configured = typeof configuredResource === "string" ? new URL(configuredResource) : new URL(configuredResource.href);
|
|
257
|
+
if (requested.origin !== configured.origin) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
if (requested.pathname.length < configured.pathname.length) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
const requestedPath = requested.pathname.endsWith("/") ? requested.pathname : requested.pathname + "/";
|
|
264
|
+
const configuredPath = configured.pathname.endsWith("/") ? configured.pathname : configured.pathname + "/";
|
|
265
|
+
return requestedPath.startsWith(configuredPath);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/server/auth/errors.js
|
|
269
|
+
init_esm_shims();
|
|
270
|
+
var OAuthError = class extends Error {
|
|
271
|
+
constructor(message, errorUri) {
|
|
272
|
+
super(message);
|
|
273
|
+
this.errorUri = errorUri;
|
|
274
|
+
this.name = this.constructor.name;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Converts the error to a standard OAuth error response object
|
|
278
|
+
*/
|
|
279
|
+
toResponseObject() {
|
|
280
|
+
const response = {
|
|
281
|
+
error: this.errorCode,
|
|
282
|
+
error_description: this.message
|
|
283
|
+
};
|
|
284
|
+
if (this.errorUri) {
|
|
285
|
+
response.error_uri = this.errorUri;
|
|
286
|
+
}
|
|
287
|
+
return response;
|
|
288
|
+
}
|
|
289
|
+
get errorCode() {
|
|
290
|
+
return this.constructor.errorCode;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
var InvalidRequestError = class extends OAuthError {
|
|
294
|
+
};
|
|
295
|
+
InvalidRequestError.errorCode = "invalid_request";
|
|
296
|
+
var InvalidClientError = class extends OAuthError {
|
|
297
|
+
};
|
|
298
|
+
InvalidClientError.errorCode = "invalid_client";
|
|
299
|
+
var InvalidGrantError = class extends OAuthError {
|
|
300
|
+
};
|
|
301
|
+
InvalidGrantError.errorCode = "invalid_grant";
|
|
302
|
+
var UnauthorizedClientError = class extends OAuthError {
|
|
303
|
+
};
|
|
304
|
+
UnauthorizedClientError.errorCode = "unauthorized_client";
|
|
305
|
+
var UnsupportedGrantTypeError = class extends OAuthError {
|
|
306
|
+
};
|
|
307
|
+
UnsupportedGrantTypeError.errorCode = "unsupported_grant_type";
|
|
308
|
+
var InvalidScopeError = class extends OAuthError {
|
|
309
|
+
};
|
|
310
|
+
InvalidScopeError.errorCode = "invalid_scope";
|
|
311
|
+
var AccessDeniedError = class extends OAuthError {
|
|
312
|
+
};
|
|
313
|
+
AccessDeniedError.errorCode = "access_denied";
|
|
314
|
+
var ServerError = class extends OAuthError {
|
|
315
|
+
};
|
|
316
|
+
ServerError.errorCode = "server_error";
|
|
317
|
+
var TemporarilyUnavailableError = class extends OAuthError {
|
|
318
|
+
};
|
|
319
|
+
TemporarilyUnavailableError.errorCode = "temporarily_unavailable";
|
|
320
|
+
var UnsupportedResponseTypeError = class extends OAuthError {
|
|
321
|
+
};
|
|
322
|
+
UnsupportedResponseTypeError.errorCode = "unsupported_response_type";
|
|
323
|
+
var UnsupportedTokenTypeError = class extends OAuthError {
|
|
324
|
+
};
|
|
325
|
+
UnsupportedTokenTypeError.errorCode = "unsupported_token_type";
|
|
326
|
+
var InvalidTokenError = class extends OAuthError {
|
|
327
|
+
};
|
|
328
|
+
InvalidTokenError.errorCode = "invalid_token";
|
|
329
|
+
var MethodNotAllowedError = class extends OAuthError {
|
|
330
|
+
};
|
|
331
|
+
MethodNotAllowedError.errorCode = "method_not_allowed";
|
|
332
|
+
var TooManyRequestsError = class extends OAuthError {
|
|
333
|
+
};
|
|
334
|
+
TooManyRequestsError.errorCode = "too_many_requests";
|
|
335
|
+
var InvalidClientMetadataError = class extends OAuthError {
|
|
336
|
+
};
|
|
337
|
+
InvalidClientMetadataError.errorCode = "invalid_client_metadata";
|
|
338
|
+
var InsufficientScopeError = class extends OAuthError {
|
|
339
|
+
};
|
|
340
|
+
InsufficientScopeError.errorCode = "insufficient_scope";
|
|
341
|
+
var InvalidTargetError = class extends OAuthError {
|
|
342
|
+
};
|
|
343
|
+
InvalidTargetError.errorCode = "invalid_target";
|
|
344
|
+
var OAUTH_ERRORS = {
|
|
345
|
+
[InvalidRequestError.errorCode]: InvalidRequestError,
|
|
346
|
+
[InvalidClientError.errorCode]: InvalidClientError,
|
|
347
|
+
[InvalidGrantError.errorCode]: InvalidGrantError,
|
|
348
|
+
[UnauthorizedClientError.errorCode]: UnauthorizedClientError,
|
|
349
|
+
[UnsupportedGrantTypeError.errorCode]: UnsupportedGrantTypeError,
|
|
350
|
+
[InvalidScopeError.errorCode]: InvalidScopeError,
|
|
351
|
+
[AccessDeniedError.errorCode]: AccessDeniedError,
|
|
352
|
+
[ServerError.errorCode]: ServerError,
|
|
353
|
+
[TemporarilyUnavailableError.errorCode]: TemporarilyUnavailableError,
|
|
354
|
+
[UnsupportedResponseTypeError.errorCode]: UnsupportedResponseTypeError,
|
|
355
|
+
[UnsupportedTokenTypeError.errorCode]: UnsupportedTokenTypeError,
|
|
356
|
+
[InvalidTokenError.errorCode]: InvalidTokenError,
|
|
357
|
+
[MethodNotAllowedError.errorCode]: MethodNotAllowedError,
|
|
358
|
+
[TooManyRequestsError.errorCode]: TooManyRequestsError,
|
|
359
|
+
[InvalidClientMetadataError.errorCode]: InvalidClientMetadataError,
|
|
360
|
+
[InsufficientScopeError.errorCode]: InsufficientScopeError,
|
|
361
|
+
[InvalidTargetError.errorCode]: InvalidTargetError
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/client/auth.js
|
|
365
|
+
var UnauthorizedError = class extends Error {
|
|
366
|
+
constructor(message) {
|
|
367
|
+
super(message ?? "Unauthorized");
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
function isClientAuthMethod(method) {
|
|
371
|
+
return ["client_secret_basic", "client_secret_post", "none"].includes(method);
|
|
372
|
+
}
|
|
373
|
+
var AUTHORIZATION_CODE_RESPONSE_TYPE = "code";
|
|
374
|
+
var AUTHORIZATION_CODE_CHALLENGE_METHOD = "S256";
|
|
375
|
+
function selectClientAuthMethod(clientInformation, supportedMethods) {
|
|
376
|
+
const hasClientSecret = clientInformation.client_secret !== void 0;
|
|
377
|
+
if ("token_endpoint_auth_method" in clientInformation && clientInformation.token_endpoint_auth_method && isClientAuthMethod(clientInformation.token_endpoint_auth_method) && (supportedMethods.length === 0 || supportedMethods.includes(clientInformation.token_endpoint_auth_method))) {
|
|
378
|
+
return clientInformation.token_endpoint_auth_method;
|
|
379
|
+
}
|
|
380
|
+
if (supportedMethods.length === 0) {
|
|
381
|
+
return hasClientSecret ? "client_secret_basic" : "none";
|
|
382
|
+
}
|
|
383
|
+
if (hasClientSecret && supportedMethods.includes("client_secret_basic")) {
|
|
384
|
+
return "client_secret_basic";
|
|
385
|
+
}
|
|
386
|
+
if (hasClientSecret && supportedMethods.includes("client_secret_post")) {
|
|
387
|
+
return "client_secret_post";
|
|
388
|
+
}
|
|
389
|
+
if (supportedMethods.includes("none")) {
|
|
390
|
+
return "none";
|
|
391
|
+
}
|
|
392
|
+
return hasClientSecret ? "client_secret_post" : "none";
|
|
393
|
+
}
|
|
394
|
+
function applyClientAuthentication(method, clientInformation, headers, params) {
|
|
395
|
+
const { client_id, client_secret } = clientInformation;
|
|
396
|
+
switch (method) {
|
|
397
|
+
case "client_secret_basic":
|
|
398
|
+
applyBasicAuth(client_id, client_secret, headers);
|
|
399
|
+
return;
|
|
400
|
+
case "client_secret_post":
|
|
401
|
+
applyPostAuth(client_id, client_secret, params);
|
|
402
|
+
return;
|
|
403
|
+
case "none":
|
|
404
|
+
applyPublicAuth(client_id, params);
|
|
405
|
+
return;
|
|
406
|
+
default:
|
|
407
|
+
throw new Error(`Unsupported client authentication method: ${method}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function applyBasicAuth(clientId, clientSecret, headers) {
|
|
411
|
+
if (!clientSecret) {
|
|
412
|
+
throw new Error("client_secret_basic authentication requires a client_secret");
|
|
413
|
+
}
|
|
414
|
+
const credentials = btoa(`${clientId}:${clientSecret}`);
|
|
415
|
+
headers.set("Authorization", `Basic ${credentials}`);
|
|
416
|
+
}
|
|
417
|
+
function applyPostAuth(clientId, clientSecret, params) {
|
|
418
|
+
params.set("client_id", clientId);
|
|
419
|
+
if (clientSecret) {
|
|
420
|
+
params.set("client_secret", clientSecret);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function applyPublicAuth(clientId, params) {
|
|
424
|
+
params.set("client_id", clientId);
|
|
425
|
+
}
|
|
426
|
+
async function parseErrorResponse(input) {
|
|
427
|
+
const statusCode = input instanceof Response ? input.status : void 0;
|
|
428
|
+
const body = input instanceof Response ? await input.text() : input;
|
|
429
|
+
try {
|
|
430
|
+
const result = OAuthErrorResponseSchema.parse(JSON.parse(body));
|
|
431
|
+
const { error, error_description, error_uri } = result;
|
|
432
|
+
const errorClass = OAUTH_ERRORS[error] || ServerError;
|
|
433
|
+
return new errorClass(error_description || "", error_uri);
|
|
434
|
+
} catch (error) {
|
|
435
|
+
const errorMessage = `${statusCode ? `HTTP ${statusCode}: ` : ""}Invalid OAuth error response: ${error}. Raw body: ${body}`;
|
|
436
|
+
return new ServerError(errorMessage);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
async function auth(provider, options) {
|
|
440
|
+
try {
|
|
441
|
+
return await authInternal(provider, options);
|
|
442
|
+
} catch (error) {
|
|
443
|
+
if (error instanceof InvalidClientError || error instanceof UnauthorizedClientError) {
|
|
444
|
+
await provider.invalidateCredentials?.("all");
|
|
445
|
+
return await authInternal(provider, options);
|
|
446
|
+
} else if (error instanceof InvalidGrantError) {
|
|
447
|
+
await provider.invalidateCredentials?.("tokens");
|
|
448
|
+
return await authInternal(provider, options);
|
|
449
|
+
}
|
|
450
|
+
throw error;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
async function authInternal(provider, { serverUrl, authorizationCode, scope, resourceMetadataUrl, fetchFn }) {
|
|
454
|
+
const cachedState = await provider.discoveryState?.();
|
|
455
|
+
let resourceMetadata;
|
|
456
|
+
let authorizationServerUrl;
|
|
457
|
+
let metadata;
|
|
458
|
+
let effectiveResourceMetadataUrl = resourceMetadataUrl;
|
|
459
|
+
if (!effectiveResourceMetadataUrl && cachedState?.resourceMetadataUrl) {
|
|
460
|
+
effectiveResourceMetadataUrl = new URL(cachedState.resourceMetadataUrl);
|
|
461
|
+
}
|
|
462
|
+
if (cachedState?.authorizationServerUrl) {
|
|
463
|
+
authorizationServerUrl = cachedState.authorizationServerUrl;
|
|
464
|
+
resourceMetadata = cachedState.resourceMetadata;
|
|
465
|
+
metadata = cachedState.authorizationServerMetadata ?? await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn });
|
|
466
|
+
if (!resourceMetadata) {
|
|
467
|
+
try {
|
|
468
|
+
resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: effectiveResourceMetadataUrl }, fetchFn);
|
|
469
|
+
} catch {
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (metadata !== cachedState.authorizationServerMetadata || resourceMetadata !== cachedState.resourceMetadata) {
|
|
473
|
+
await provider.saveDiscoveryState?.({
|
|
474
|
+
authorizationServerUrl: String(authorizationServerUrl),
|
|
475
|
+
resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
|
|
476
|
+
resourceMetadata,
|
|
477
|
+
authorizationServerMetadata: metadata
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
} else {
|
|
481
|
+
const serverInfo = await discoverOAuthServerInfo(serverUrl, { resourceMetadataUrl: effectiveResourceMetadataUrl, fetchFn });
|
|
482
|
+
authorizationServerUrl = serverInfo.authorizationServerUrl;
|
|
483
|
+
metadata = serverInfo.authorizationServerMetadata;
|
|
484
|
+
resourceMetadata = serverInfo.resourceMetadata;
|
|
485
|
+
await provider.saveDiscoveryState?.({
|
|
486
|
+
authorizationServerUrl: String(authorizationServerUrl),
|
|
487
|
+
resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
|
|
488
|
+
resourceMetadata,
|
|
489
|
+
authorizationServerMetadata: metadata
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
const resource = await selectResourceURL(serverUrl, provider, resourceMetadata);
|
|
493
|
+
const resolvedScope = scope || resourceMetadata?.scopes_supported?.join(" ") || provider.clientMetadata.scope;
|
|
494
|
+
let clientInformation = await Promise.resolve(provider.clientInformation());
|
|
495
|
+
if (!clientInformation) {
|
|
496
|
+
if (authorizationCode !== void 0) {
|
|
497
|
+
throw new Error("Existing OAuth client information is required when exchanging an authorization code");
|
|
498
|
+
}
|
|
499
|
+
const supportsUrlBasedClientId = metadata?.client_id_metadata_document_supported === true;
|
|
500
|
+
const clientMetadataUrl = provider.clientMetadataUrl;
|
|
501
|
+
if (clientMetadataUrl && !isHttpsUrl(clientMetadataUrl)) {
|
|
502
|
+
throw new InvalidClientMetadataError(`clientMetadataUrl must be a valid HTTPS URL with a non-root pathname, got: ${clientMetadataUrl}`);
|
|
503
|
+
}
|
|
504
|
+
const shouldUseUrlBasedClientId = supportsUrlBasedClientId && clientMetadataUrl;
|
|
505
|
+
if (shouldUseUrlBasedClientId) {
|
|
506
|
+
clientInformation = {
|
|
507
|
+
client_id: clientMetadataUrl
|
|
508
|
+
};
|
|
509
|
+
await provider.saveClientInformation?.(clientInformation);
|
|
510
|
+
} else {
|
|
511
|
+
if (!provider.saveClientInformation) {
|
|
512
|
+
throw new Error("OAuth client information must be saveable for dynamic registration");
|
|
513
|
+
}
|
|
514
|
+
const fullInformation = await registerClient(authorizationServerUrl, {
|
|
515
|
+
metadata,
|
|
516
|
+
clientMetadata: provider.clientMetadata,
|
|
517
|
+
scope: resolvedScope,
|
|
518
|
+
fetchFn
|
|
519
|
+
});
|
|
520
|
+
await provider.saveClientInformation(fullInformation);
|
|
521
|
+
clientInformation = fullInformation;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
const nonInteractiveFlow = !provider.redirectUrl;
|
|
525
|
+
if (authorizationCode !== void 0 || nonInteractiveFlow) {
|
|
526
|
+
const tokens2 = await fetchToken(provider, authorizationServerUrl, {
|
|
527
|
+
metadata,
|
|
528
|
+
resource,
|
|
529
|
+
authorizationCode,
|
|
530
|
+
fetchFn
|
|
531
|
+
});
|
|
532
|
+
await provider.saveTokens(tokens2);
|
|
533
|
+
return "AUTHORIZED";
|
|
534
|
+
}
|
|
535
|
+
const tokens = await provider.tokens();
|
|
536
|
+
if (tokens?.refresh_token) {
|
|
537
|
+
try {
|
|
538
|
+
const newTokens = await refreshAuthorization(authorizationServerUrl, {
|
|
539
|
+
metadata,
|
|
540
|
+
clientInformation,
|
|
541
|
+
refreshToken: tokens.refresh_token,
|
|
542
|
+
resource,
|
|
543
|
+
addClientAuthentication: provider.addClientAuthentication,
|
|
544
|
+
fetchFn
|
|
545
|
+
});
|
|
546
|
+
await provider.saveTokens(newTokens);
|
|
547
|
+
return "AUTHORIZED";
|
|
548
|
+
} catch (error) {
|
|
549
|
+
if (!(error instanceof OAuthError) || error instanceof ServerError) {
|
|
550
|
+
} else {
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const state = provider.state ? await provider.state() : void 0;
|
|
556
|
+
const { authorizationUrl, codeVerifier } = await startAuthorization(authorizationServerUrl, {
|
|
557
|
+
metadata,
|
|
558
|
+
clientInformation,
|
|
559
|
+
state,
|
|
560
|
+
redirectUrl: provider.redirectUrl,
|
|
561
|
+
scope: resolvedScope,
|
|
562
|
+
resource
|
|
563
|
+
});
|
|
564
|
+
await provider.saveCodeVerifier(codeVerifier);
|
|
565
|
+
await provider.redirectToAuthorization(authorizationUrl);
|
|
566
|
+
return "REDIRECT";
|
|
567
|
+
}
|
|
568
|
+
function isHttpsUrl(value) {
|
|
569
|
+
if (!value)
|
|
570
|
+
return false;
|
|
571
|
+
try {
|
|
572
|
+
const url2 = new URL(value);
|
|
573
|
+
return url2.protocol === "https:" && url2.pathname !== "/";
|
|
574
|
+
} catch {
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
async function selectResourceURL(serverUrl, provider, resourceMetadata) {
|
|
579
|
+
const defaultResource = resourceUrlFromServerUrl(serverUrl);
|
|
580
|
+
if (provider.validateResourceURL) {
|
|
581
|
+
return await provider.validateResourceURL(defaultResource, resourceMetadata?.resource);
|
|
582
|
+
}
|
|
583
|
+
if (!resourceMetadata) {
|
|
584
|
+
return void 0;
|
|
585
|
+
}
|
|
586
|
+
if (!checkResourceAllowed({ requestedResource: defaultResource, configuredResource: resourceMetadata.resource })) {
|
|
587
|
+
throw new Error(`Protected resource ${resourceMetadata.resource} does not match expected ${defaultResource} (or origin)`);
|
|
588
|
+
}
|
|
589
|
+
return new URL(resourceMetadata.resource);
|
|
590
|
+
}
|
|
591
|
+
function extractWWWAuthenticateParams(res) {
|
|
592
|
+
const authenticateHeader = res.headers.get("WWW-Authenticate");
|
|
593
|
+
if (!authenticateHeader) {
|
|
594
|
+
return {};
|
|
595
|
+
}
|
|
596
|
+
const [type, scheme] = authenticateHeader.split(" ");
|
|
597
|
+
if (type.toLowerCase() !== "bearer" || !scheme) {
|
|
598
|
+
return {};
|
|
599
|
+
}
|
|
600
|
+
const resourceMetadataMatch = extractFieldFromWwwAuth(res, "resource_metadata") || void 0;
|
|
601
|
+
let resourceMetadataUrl;
|
|
602
|
+
if (resourceMetadataMatch) {
|
|
603
|
+
try {
|
|
604
|
+
resourceMetadataUrl = new URL(resourceMetadataMatch);
|
|
605
|
+
} catch {
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const scope = extractFieldFromWwwAuth(res, "scope") || void 0;
|
|
609
|
+
const error = extractFieldFromWwwAuth(res, "error") || void 0;
|
|
610
|
+
return {
|
|
611
|
+
resourceMetadataUrl,
|
|
612
|
+
scope,
|
|
613
|
+
error
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
function extractFieldFromWwwAuth(response, fieldName) {
|
|
617
|
+
const wwwAuthHeader = response.headers.get("WWW-Authenticate");
|
|
618
|
+
if (!wwwAuthHeader) {
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
const pattern = new RegExp(`${fieldName}=(?:"([^"]+)"|([^\\s,]+))`);
|
|
622
|
+
const match = wwwAuthHeader.match(pattern);
|
|
623
|
+
if (match) {
|
|
624
|
+
return match[1] || match[2];
|
|
625
|
+
}
|
|
626
|
+
return null;
|
|
627
|
+
}
|
|
628
|
+
async function discoverOAuthProtectedResourceMetadata(serverUrl, opts, fetchFn = fetch) {
|
|
629
|
+
const response = await discoverMetadataWithFallback(serverUrl, "oauth-protected-resource", fetchFn, {
|
|
630
|
+
protocolVersion: opts?.protocolVersion,
|
|
631
|
+
metadataUrl: opts?.resourceMetadataUrl
|
|
632
|
+
});
|
|
633
|
+
if (!response || response.status === 404) {
|
|
634
|
+
await response?.body?.cancel();
|
|
635
|
+
throw new Error(`Resource server does not implement OAuth 2.0 Protected Resource Metadata.`);
|
|
636
|
+
}
|
|
637
|
+
if (!response.ok) {
|
|
638
|
+
await response.body?.cancel();
|
|
639
|
+
throw new Error(`HTTP ${response.status} trying to load well-known OAuth protected resource metadata.`);
|
|
640
|
+
}
|
|
641
|
+
return OAuthProtectedResourceMetadataSchema.parse(await response.json());
|
|
642
|
+
}
|
|
643
|
+
async function fetchWithCorsRetry(url2, headers, fetchFn = fetch) {
|
|
644
|
+
try {
|
|
645
|
+
return await fetchFn(url2, { headers });
|
|
646
|
+
} catch (error) {
|
|
647
|
+
if (error instanceof TypeError) {
|
|
648
|
+
if (headers) {
|
|
649
|
+
return fetchWithCorsRetry(url2, void 0, fetchFn);
|
|
650
|
+
} else {
|
|
651
|
+
return void 0;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
throw error;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
function buildWellKnownPath(wellKnownPrefix, pathname = "", options = {}) {
|
|
658
|
+
if (pathname.endsWith("/")) {
|
|
659
|
+
pathname = pathname.slice(0, -1);
|
|
660
|
+
}
|
|
661
|
+
return options.prependPathname ? `${pathname}/.well-known/${wellKnownPrefix}` : `/.well-known/${wellKnownPrefix}${pathname}`;
|
|
662
|
+
}
|
|
663
|
+
async function tryMetadataDiscovery(url2, protocolVersion, fetchFn = fetch) {
|
|
664
|
+
const headers = {
|
|
665
|
+
"MCP-Protocol-Version": protocolVersion
|
|
666
|
+
};
|
|
667
|
+
return await fetchWithCorsRetry(url2, headers, fetchFn);
|
|
668
|
+
}
|
|
669
|
+
function shouldAttemptFallback(response, pathname) {
|
|
670
|
+
return !response || response.status >= 400 && response.status < 500 && pathname !== "/";
|
|
671
|
+
}
|
|
672
|
+
async function discoverMetadataWithFallback(serverUrl, wellKnownType, fetchFn, opts) {
|
|
673
|
+
const issuer = new URL(serverUrl);
|
|
674
|
+
const protocolVersion = opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION;
|
|
675
|
+
let url2;
|
|
676
|
+
if (opts?.metadataUrl) {
|
|
677
|
+
url2 = new URL(opts.metadataUrl);
|
|
678
|
+
} else {
|
|
679
|
+
const wellKnownPath = buildWellKnownPath(wellKnownType, issuer.pathname);
|
|
680
|
+
url2 = new URL(wellKnownPath, opts?.metadataServerUrl ?? issuer);
|
|
681
|
+
url2.search = issuer.search;
|
|
682
|
+
}
|
|
683
|
+
let response = await tryMetadataDiscovery(url2, protocolVersion, fetchFn);
|
|
684
|
+
if (!opts?.metadataUrl && shouldAttemptFallback(response, issuer.pathname)) {
|
|
685
|
+
const rootUrl = new URL(`/.well-known/${wellKnownType}`, issuer);
|
|
686
|
+
response = await tryMetadataDiscovery(rootUrl, protocolVersion, fetchFn);
|
|
687
|
+
}
|
|
688
|
+
return response;
|
|
689
|
+
}
|
|
690
|
+
function buildDiscoveryUrls(authorizationServerUrl) {
|
|
691
|
+
const url2 = typeof authorizationServerUrl === "string" ? new URL(authorizationServerUrl) : authorizationServerUrl;
|
|
692
|
+
const hasPath = url2.pathname !== "/";
|
|
693
|
+
const urlsToTry = [];
|
|
694
|
+
if (!hasPath) {
|
|
695
|
+
urlsToTry.push({
|
|
696
|
+
url: new URL("/.well-known/oauth-authorization-server", url2.origin),
|
|
697
|
+
type: "oauth"
|
|
698
|
+
});
|
|
699
|
+
urlsToTry.push({
|
|
700
|
+
url: new URL(`/.well-known/openid-configuration`, url2.origin),
|
|
701
|
+
type: "oidc"
|
|
702
|
+
});
|
|
703
|
+
return urlsToTry;
|
|
704
|
+
}
|
|
705
|
+
let pathname = url2.pathname;
|
|
706
|
+
if (pathname.endsWith("/")) {
|
|
707
|
+
pathname = pathname.slice(0, -1);
|
|
708
|
+
}
|
|
709
|
+
urlsToTry.push({
|
|
710
|
+
url: new URL(`/.well-known/oauth-authorization-server${pathname}`, url2.origin),
|
|
711
|
+
type: "oauth"
|
|
712
|
+
});
|
|
713
|
+
urlsToTry.push({
|
|
714
|
+
url: new URL(`/.well-known/openid-configuration${pathname}`, url2.origin),
|
|
715
|
+
type: "oidc"
|
|
716
|
+
});
|
|
717
|
+
urlsToTry.push({
|
|
718
|
+
url: new URL(`${pathname}/.well-known/openid-configuration`, url2.origin),
|
|
719
|
+
type: "oidc"
|
|
720
|
+
});
|
|
721
|
+
return urlsToTry;
|
|
722
|
+
}
|
|
723
|
+
async function discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn = fetch, protocolVersion = LATEST_PROTOCOL_VERSION } = {}) {
|
|
724
|
+
const headers = {
|
|
725
|
+
"MCP-Protocol-Version": protocolVersion,
|
|
726
|
+
Accept: "application/json"
|
|
727
|
+
};
|
|
728
|
+
const urlsToTry = buildDiscoveryUrls(authorizationServerUrl);
|
|
729
|
+
for (const { url: endpointUrl, type } of urlsToTry) {
|
|
730
|
+
const response = await fetchWithCorsRetry(endpointUrl, headers, fetchFn);
|
|
731
|
+
if (!response) {
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
if (!response.ok) {
|
|
735
|
+
await response.body?.cancel();
|
|
736
|
+
if (response.status >= 400 && response.status < 500) {
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
throw new Error(`HTTP ${response.status} trying to load ${type === "oauth" ? "OAuth" : "OpenID provider"} metadata from ${endpointUrl}`);
|
|
740
|
+
}
|
|
741
|
+
if (type === "oauth") {
|
|
742
|
+
return OAuthMetadataSchema.parse(await response.json());
|
|
743
|
+
} else {
|
|
744
|
+
return OpenIdProviderDiscoveryMetadataSchema.parse(await response.json());
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return void 0;
|
|
748
|
+
}
|
|
749
|
+
async function discoverOAuthServerInfo(serverUrl, opts) {
|
|
750
|
+
let resourceMetadata;
|
|
751
|
+
let authorizationServerUrl;
|
|
752
|
+
try {
|
|
753
|
+
resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: opts?.resourceMetadataUrl }, opts?.fetchFn);
|
|
754
|
+
if (resourceMetadata.authorization_servers && resourceMetadata.authorization_servers.length > 0) {
|
|
755
|
+
authorizationServerUrl = resourceMetadata.authorization_servers[0];
|
|
756
|
+
}
|
|
757
|
+
} catch {
|
|
758
|
+
}
|
|
759
|
+
if (!authorizationServerUrl) {
|
|
760
|
+
authorizationServerUrl = String(new URL("/", serverUrl));
|
|
761
|
+
}
|
|
762
|
+
const authorizationServerMetadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn: opts?.fetchFn });
|
|
763
|
+
return {
|
|
764
|
+
authorizationServerUrl,
|
|
765
|
+
authorizationServerMetadata,
|
|
766
|
+
resourceMetadata
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
async function startAuthorization(authorizationServerUrl, { metadata, clientInformation, redirectUrl, scope, state, resource }) {
|
|
770
|
+
let authorizationUrl;
|
|
771
|
+
if (metadata) {
|
|
772
|
+
authorizationUrl = new URL(metadata.authorization_endpoint);
|
|
773
|
+
if (!metadata.response_types_supported.includes(AUTHORIZATION_CODE_RESPONSE_TYPE)) {
|
|
774
|
+
throw new Error(`Incompatible auth server: does not support response type ${AUTHORIZATION_CODE_RESPONSE_TYPE}`);
|
|
775
|
+
}
|
|
776
|
+
if (metadata.code_challenge_methods_supported && !metadata.code_challenge_methods_supported.includes(AUTHORIZATION_CODE_CHALLENGE_METHOD)) {
|
|
777
|
+
throw new Error(`Incompatible auth server: does not support code challenge method ${AUTHORIZATION_CODE_CHALLENGE_METHOD}`);
|
|
778
|
+
}
|
|
779
|
+
} else {
|
|
780
|
+
authorizationUrl = new URL("/authorize", authorizationServerUrl);
|
|
781
|
+
}
|
|
782
|
+
const challenge = await pkceChallenge();
|
|
783
|
+
const codeVerifier = challenge.code_verifier;
|
|
784
|
+
const codeChallenge = challenge.code_challenge;
|
|
785
|
+
authorizationUrl.searchParams.set("response_type", AUTHORIZATION_CODE_RESPONSE_TYPE);
|
|
786
|
+
authorizationUrl.searchParams.set("client_id", clientInformation.client_id);
|
|
787
|
+
authorizationUrl.searchParams.set("code_challenge", codeChallenge);
|
|
788
|
+
authorizationUrl.searchParams.set("code_challenge_method", AUTHORIZATION_CODE_CHALLENGE_METHOD);
|
|
789
|
+
authorizationUrl.searchParams.set("redirect_uri", String(redirectUrl));
|
|
790
|
+
if (state) {
|
|
791
|
+
authorizationUrl.searchParams.set("state", state);
|
|
792
|
+
}
|
|
793
|
+
if (scope) {
|
|
794
|
+
authorizationUrl.searchParams.set("scope", scope);
|
|
795
|
+
}
|
|
796
|
+
if (scope?.includes("offline_access")) {
|
|
797
|
+
authorizationUrl.searchParams.append("prompt", "consent");
|
|
798
|
+
}
|
|
799
|
+
if (resource) {
|
|
800
|
+
authorizationUrl.searchParams.set("resource", resource.href);
|
|
801
|
+
}
|
|
802
|
+
return { authorizationUrl, codeVerifier };
|
|
803
|
+
}
|
|
804
|
+
function prepareAuthorizationCodeRequest(authorizationCode, codeVerifier, redirectUri) {
|
|
805
|
+
return new URLSearchParams({
|
|
806
|
+
grant_type: "authorization_code",
|
|
807
|
+
code: authorizationCode,
|
|
808
|
+
code_verifier: codeVerifier,
|
|
809
|
+
redirect_uri: String(redirectUri)
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
async function executeTokenRequest(authorizationServerUrl, { metadata, tokenRequestParams, clientInformation, addClientAuthentication, resource, fetchFn }) {
|
|
813
|
+
const tokenUrl = metadata?.token_endpoint ? new URL(metadata.token_endpoint) : new URL("/token", authorizationServerUrl);
|
|
814
|
+
const headers = new Headers({
|
|
815
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
816
|
+
Accept: "application/json"
|
|
817
|
+
});
|
|
818
|
+
if (resource) {
|
|
819
|
+
tokenRequestParams.set("resource", resource.href);
|
|
820
|
+
}
|
|
821
|
+
if (addClientAuthentication) {
|
|
822
|
+
await addClientAuthentication(headers, tokenRequestParams, tokenUrl, metadata);
|
|
823
|
+
} else if (clientInformation) {
|
|
824
|
+
const supportedMethods = metadata?.token_endpoint_auth_methods_supported ?? [];
|
|
825
|
+
const authMethod = selectClientAuthMethod(clientInformation, supportedMethods);
|
|
826
|
+
applyClientAuthentication(authMethod, clientInformation, headers, tokenRequestParams);
|
|
827
|
+
}
|
|
828
|
+
const response = await (fetchFn ?? fetch)(tokenUrl, {
|
|
829
|
+
method: "POST",
|
|
830
|
+
headers,
|
|
831
|
+
body: tokenRequestParams
|
|
832
|
+
});
|
|
833
|
+
if (!response.ok) {
|
|
834
|
+
throw await parseErrorResponse(response);
|
|
835
|
+
}
|
|
836
|
+
return OAuthTokensSchema.parse(await response.json());
|
|
837
|
+
}
|
|
838
|
+
async function refreshAuthorization(authorizationServerUrl, { metadata, clientInformation, refreshToken, resource, addClientAuthentication, fetchFn }) {
|
|
839
|
+
const tokenRequestParams = new URLSearchParams({
|
|
840
|
+
grant_type: "refresh_token",
|
|
841
|
+
refresh_token: refreshToken
|
|
842
|
+
});
|
|
843
|
+
const tokens = await executeTokenRequest(authorizationServerUrl, {
|
|
844
|
+
metadata,
|
|
845
|
+
tokenRequestParams,
|
|
846
|
+
clientInformation,
|
|
847
|
+
addClientAuthentication,
|
|
848
|
+
resource,
|
|
849
|
+
fetchFn
|
|
850
|
+
});
|
|
851
|
+
return { refresh_token: refreshToken, ...tokens };
|
|
852
|
+
}
|
|
853
|
+
async function fetchToken(provider, authorizationServerUrl, { metadata, resource, authorizationCode, fetchFn } = {}) {
|
|
854
|
+
const scope = provider.clientMetadata.scope;
|
|
855
|
+
let tokenRequestParams;
|
|
856
|
+
if (provider.prepareTokenRequest) {
|
|
857
|
+
tokenRequestParams = await provider.prepareTokenRequest(scope);
|
|
858
|
+
}
|
|
859
|
+
if (!tokenRequestParams) {
|
|
860
|
+
if (!authorizationCode) {
|
|
861
|
+
throw new Error("Either provider.prepareTokenRequest() or authorizationCode is required");
|
|
862
|
+
}
|
|
863
|
+
if (!provider.redirectUrl) {
|
|
864
|
+
throw new Error("redirectUrl is required for authorization_code flow");
|
|
865
|
+
}
|
|
866
|
+
const codeVerifier = await provider.codeVerifier();
|
|
867
|
+
tokenRequestParams = prepareAuthorizationCodeRequest(authorizationCode, codeVerifier, provider.redirectUrl);
|
|
868
|
+
}
|
|
869
|
+
const clientInformation = await provider.clientInformation();
|
|
870
|
+
return executeTokenRequest(authorizationServerUrl, {
|
|
871
|
+
metadata,
|
|
872
|
+
tokenRequestParams,
|
|
873
|
+
clientInformation: clientInformation ?? void 0,
|
|
874
|
+
addClientAuthentication: provider.addClientAuthentication,
|
|
875
|
+
resource,
|
|
876
|
+
fetchFn
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
async function registerClient(authorizationServerUrl, { metadata, clientMetadata, scope, fetchFn }) {
|
|
880
|
+
let registrationUrl;
|
|
881
|
+
if (metadata) {
|
|
882
|
+
if (!metadata.registration_endpoint) {
|
|
883
|
+
throw new Error("Incompatible auth server: does not support dynamic client registration");
|
|
884
|
+
}
|
|
885
|
+
registrationUrl = new URL(metadata.registration_endpoint);
|
|
886
|
+
} else {
|
|
887
|
+
registrationUrl = new URL("/register", authorizationServerUrl);
|
|
888
|
+
}
|
|
889
|
+
const response = await (fetchFn ?? fetch)(registrationUrl, {
|
|
890
|
+
method: "POST",
|
|
891
|
+
headers: {
|
|
892
|
+
"Content-Type": "application/json"
|
|
893
|
+
},
|
|
894
|
+
body: JSON.stringify({
|
|
895
|
+
...clientMetadata,
|
|
896
|
+
...scope !== void 0 ? { scope } : {}
|
|
897
|
+
})
|
|
898
|
+
});
|
|
899
|
+
if (!response.ok) {
|
|
900
|
+
throw await parseErrorResponse(response);
|
|
901
|
+
}
|
|
902
|
+
return OAuthClientInformationFullSchema.parse(await response.json());
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// node_modules/eventsource-parser/dist/stream.js
|
|
906
|
+
init_esm_shims();
|
|
907
|
+
|
|
908
|
+
// node_modules/eventsource-parser/dist/index.js
|
|
909
|
+
init_esm_shims();
|
|
910
|
+
var ParseError = class extends Error {
|
|
911
|
+
constructor(message, options) {
|
|
912
|
+
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
var LF = 10;
|
|
916
|
+
var CR = 13;
|
|
917
|
+
var SPACE = 32;
|
|
918
|
+
function noop(_arg) {
|
|
919
|
+
}
|
|
920
|
+
function createParser(callbacks) {
|
|
921
|
+
if (typeof callbacks == "function")
|
|
922
|
+
throw new TypeError(
|
|
923
|
+
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
924
|
+
);
|
|
925
|
+
const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks, pendingFragments = [];
|
|
926
|
+
let isFirstChunk = true, id, data = "", dataLines = 0, eventType;
|
|
927
|
+
function feed(chunk) {
|
|
928
|
+
if (isFirstChunk && (isFirstChunk = false, chunk.charCodeAt(0) === 239 && chunk.charCodeAt(1) === 187 && chunk.charCodeAt(2) === 191 && (chunk = chunk.slice(3))), pendingFragments.length === 0) {
|
|
929
|
+
const trailing2 = processLines(chunk);
|
|
930
|
+
trailing2 !== "" && pendingFragments.push(trailing2);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
if (chunk.indexOf(`
|
|
934
|
+
`) === -1 && chunk.indexOf("\r") === -1) {
|
|
935
|
+
pendingFragments.push(chunk);
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
pendingFragments.push(chunk);
|
|
939
|
+
const input = pendingFragments.join("");
|
|
940
|
+
pendingFragments.length = 0;
|
|
941
|
+
const trailing = processLines(input);
|
|
942
|
+
trailing !== "" && pendingFragments.push(trailing);
|
|
943
|
+
}
|
|
944
|
+
function processLines(chunk) {
|
|
945
|
+
let searchIndex = 0;
|
|
946
|
+
if (chunk.indexOf("\r") === -1) {
|
|
947
|
+
let lfIndex = chunk.indexOf(`
|
|
948
|
+
`, searchIndex);
|
|
949
|
+
for (; lfIndex !== -1; ) {
|
|
950
|
+
if (searchIndex === lfIndex) {
|
|
951
|
+
dataLines > 0 && onEvent({ id, event: eventType, data }), id = void 0, data = "", dataLines = 0, eventType = void 0, searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
952
|
+
`, searchIndex);
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
const firstCharCode = chunk.charCodeAt(searchIndex);
|
|
956
|
+
if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
|
|
957
|
+
const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
|
|
958
|
+
if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
|
|
959
|
+
onEvent({ id, event: eventType, data: value }), id = void 0, data = "", eventType = void 0, searchIndex = lfIndex + 2, lfIndex = chunk.indexOf(`
|
|
960
|
+
`, searchIndex);
|
|
961
|
+
continue;
|
|
962
|
+
}
|
|
963
|
+
data = dataLines === 0 ? value : `${data}
|
|
964
|
+
${value}`, dataLines++;
|
|
965
|
+
} else isEventPrefix(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(
|
|
966
|
+
chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
|
|
967
|
+
lfIndex
|
|
968
|
+
) || void 0 : parseLine(chunk, searchIndex, lfIndex);
|
|
969
|
+
searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
970
|
+
`, searchIndex);
|
|
971
|
+
}
|
|
972
|
+
return chunk.slice(searchIndex);
|
|
973
|
+
}
|
|
974
|
+
for (; searchIndex < chunk.length; ) {
|
|
975
|
+
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
976
|
+
`, searchIndex);
|
|
977
|
+
let lineEnd = -1;
|
|
978
|
+
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = crIndex < lfIndex ? crIndex : lfIndex : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1)
|
|
979
|
+
break;
|
|
980
|
+
parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF && searchIndex++;
|
|
981
|
+
}
|
|
982
|
+
return chunk.slice(searchIndex);
|
|
983
|
+
}
|
|
984
|
+
function parseLine(chunk, start, end) {
|
|
985
|
+
if (start === end) {
|
|
986
|
+
dispatchEvent();
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
const firstCharCode = chunk.charCodeAt(start);
|
|
990
|
+
if (isDataPrefix(chunk, start, firstCharCode)) {
|
|
991
|
+
const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
|
|
992
|
+
data = dataLines === 0 ? value2 : `${data}
|
|
993
|
+
${value2}`, dataLines++;
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
if (isEventPrefix(chunk, start, firstCharCode)) {
|
|
997
|
+
eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
|
|
1001
|
+
const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
|
|
1002
|
+
id = value2.includes("\0") ? void 0 : value2;
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
if (firstCharCode === 58) {
|
|
1006
|
+
if (onComment) {
|
|
1007
|
+
const line2 = chunk.slice(start, end);
|
|
1008
|
+
onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
|
|
1009
|
+
}
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
const line = chunk.slice(start, end), fieldSeparatorIndex = line.indexOf(":");
|
|
1013
|
+
if (fieldSeparatorIndex === -1) {
|
|
1014
|
+
processField(line, "", line);
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
|
|
1018
|
+
processField(field, value, line);
|
|
1019
|
+
}
|
|
1020
|
+
function processField(field, value, line) {
|
|
1021
|
+
switch (field) {
|
|
1022
|
+
case "event":
|
|
1023
|
+
eventType = value || void 0;
|
|
1024
|
+
break;
|
|
1025
|
+
case "data":
|
|
1026
|
+
data = dataLines === 0 ? value : `${data}
|
|
1027
|
+
${value}`, dataLines++;
|
|
1028
|
+
break;
|
|
1029
|
+
case "id":
|
|
1030
|
+
id = value.includes("\0") ? void 0 : value;
|
|
1031
|
+
break;
|
|
1032
|
+
case "retry":
|
|
1033
|
+
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
|
|
1034
|
+
new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
1035
|
+
type: "invalid-retry",
|
|
1036
|
+
value,
|
|
1037
|
+
line
|
|
1038
|
+
})
|
|
1039
|
+
);
|
|
1040
|
+
break;
|
|
1041
|
+
default:
|
|
1042
|
+
onError(
|
|
1043
|
+
new ParseError(
|
|
1044
|
+
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
|
|
1045
|
+
{ type: "unknown-field", field, value, line }
|
|
1046
|
+
)
|
|
1047
|
+
);
|
|
1048
|
+
break;
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
function dispatchEvent() {
|
|
1052
|
+
dataLines > 0 && onEvent({
|
|
1053
|
+
id,
|
|
1054
|
+
event: eventType,
|
|
1055
|
+
data
|
|
1056
|
+
}), id = void 0, data = "", dataLines = 0, eventType = void 0;
|
|
1057
|
+
}
|
|
1058
|
+
function reset(options = {}) {
|
|
1059
|
+
if (options.consume && pendingFragments.length > 0) {
|
|
1060
|
+
const incompleteLine = pendingFragments.join("");
|
|
1061
|
+
parseLine(incompleteLine, 0, incompleteLine.length);
|
|
1062
|
+
}
|
|
1063
|
+
isFirstChunk = true, id = void 0, data = "", dataLines = 0, eventType = void 0, pendingFragments.length = 0;
|
|
1064
|
+
}
|
|
1065
|
+
return { feed, reset };
|
|
1066
|
+
}
|
|
1067
|
+
function isDataPrefix(chunk, i, firstCharCode) {
|
|
1068
|
+
return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
|
|
1069
|
+
}
|
|
1070
|
+
function isEventPrefix(chunk, i, firstCharCode) {
|
|
1071
|
+
return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// node_modules/eventsource-parser/dist/stream.js
|
|
1075
|
+
var EventSourceParserStream = class extends TransformStream {
|
|
1076
|
+
constructor({ onError, onRetry, onComment } = {}) {
|
|
1077
|
+
let parser;
|
|
1078
|
+
super({
|
|
1079
|
+
start(controller) {
|
|
1080
|
+
parser = createParser({
|
|
1081
|
+
onEvent: (event) => {
|
|
1082
|
+
controller.enqueue(event);
|
|
1083
|
+
},
|
|
1084
|
+
onError(error) {
|
|
1085
|
+
onError === "terminate" ? controller.error(error) : typeof onError == "function" && onError(error);
|
|
1086
|
+
},
|
|
1087
|
+
onRetry,
|
|
1088
|
+
onComment
|
|
1089
|
+
});
|
|
1090
|
+
},
|
|
1091
|
+
transform(chunk) {
|
|
1092
|
+
parser.feed(chunk);
|
|
1093
|
+
}
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
// node_modules/@modelcontextprotocol/sdk/dist/esm/client/streamableHttp.js
|
|
1099
|
+
var DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS = {
|
|
1100
|
+
initialReconnectionDelay: 1e3,
|
|
1101
|
+
maxReconnectionDelay: 3e4,
|
|
1102
|
+
reconnectionDelayGrowFactor: 1.5,
|
|
1103
|
+
maxRetries: 2
|
|
1104
|
+
};
|
|
1105
|
+
var StreamableHTTPError = class extends Error {
|
|
1106
|
+
constructor(code, message) {
|
|
1107
|
+
super(`Streamable HTTP error: ${message}`);
|
|
1108
|
+
this.code = code;
|
|
1109
|
+
}
|
|
1110
|
+
};
|
|
1111
|
+
var StreamableHTTPClientTransport = class {
|
|
1112
|
+
constructor(url2, opts) {
|
|
1113
|
+
this._hasCompletedAuthFlow = false;
|
|
1114
|
+
this._url = url2;
|
|
1115
|
+
this._resourceMetadataUrl = void 0;
|
|
1116
|
+
this._scope = void 0;
|
|
1117
|
+
this._requestInit = opts?.requestInit;
|
|
1118
|
+
this._authProvider = opts?.authProvider;
|
|
1119
|
+
this._fetch = opts?.fetch;
|
|
1120
|
+
this._fetchWithInit = createFetchWithInit(opts?.fetch, opts?.requestInit);
|
|
1121
|
+
this._sessionId = opts?.sessionId;
|
|
1122
|
+
this._reconnectionOptions = opts?.reconnectionOptions ?? DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS;
|
|
1123
|
+
}
|
|
1124
|
+
async _authThenStart() {
|
|
1125
|
+
if (!this._authProvider) {
|
|
1126
|
+
throw new UnauthorizedError("No auth provider");
|
|
1127
|
+
}
|
|
1128
|
+
let result;
|
|
1129
|
+
try {
|
|
1130
|
+
result = await auth(this._authProvider, {
|
|
1131
|
+
serverUrl: this._url,
|
|
1132
|
+
resourceMetadataUrl: this._resourceMetadataUrl,
|
|
1133
|
+
scope: this._scope,
|
|
1134
|
+
fetchFn: this._fetchWithInit
|
|
1135
|
+
});
|
|
1136
|
+
} catch (error) {
|
|
1137
|
+
this.onerror?.(error);
|
|
1138
|
+
throw error;
|
|
1139
|
+
}
|
|
1140
|
+
if (result !== "AUTHORIZED") {
|
|
1141
|
+
throw new UnauthorizedError();
|
|
1142
|
+
}
|
|
1143
|
+
return await this._startOrAuthSse({ resumptionToken: void 0 });
|
|
1144
|
+
}
|
|
1145
|
+
async _commonHeaders() {
|
|
1146
|
+
const headers = {};
|
|
1147
|
+
if (this._authProvider) {
|
|
1148
|
+
const tokens = await this._authProvider.tokens();
|
|
1149
|
+
if (tokens) {
|
|
1150
|
+
headers["Authorization"] = `Bearer ${tokens.access_token}`;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
if (this._sessionId) {
|
|
1154
|
+
headers["mcp-session-id"] = this._sessionId;
|
|
1155
|
+
}
|
|
1156
|
+
if (this._protocolVersion) {
|
|
1157
|
+
headers["mcp-protocol-version"] = this._protocolVersion;
|
|
1158
|
+
}
|
|
1159
|
+
const extraHeaders = normalizeHeaders(this._requestInit?.headers);
|
|
1160
|
+
return new Headers({
|
|
1161
|
+
...headers,
|
|
1162
|
+
...extraHeaders
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
async _startOrAuthSse(options) {
|
|
1166
|
+
const { resumptionToken } = options;
|
|
1167
|
+
try {
|
|
1168
|
+
const headers = await this._commonHeaders();
|
|
1169
|
+
headers.set("Accept", "text/event-stream");
|
|
1170
|
+
if (resumptionToken) {
|
|
1171
|
+
headers.set("last-event-id", resumptionToken);
|
|
1172
|
+
}
|
|
1173
|
+
const response = await (this._fetch ?? fetch)(this._url, {
|
|
1174
|
+
method: "GET",
|
|
1175
|
+
headers,
|
|
1176
|
+
signal: this._abortController?.signal
|
|
1177
|
+
});
|
|
1178
|
+
if (!response.ok) {
|
|
1179
|
+
await response.body?.cancel();
|
|
1180
|
+
if (response.status === 401 && this._authProvider) {
|
|
1181
|
+
return await this._authThenStart();
|
|
1182
|
+
}
|
|
1183
|
+
if (response.status === 405) {
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
throw new StreamableHTTPError(response.status, `Failed to open SSE stream: ${response.statusText}`);
|
|
1187
|
+
}
|
|
1188
|
+
this._handleSseStream(response.body, options, true);
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
this.onerror?.(error);
|
|
1191
|
+
throw error;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Calculates the next reconnection delay using backoff algorithm
|
|
1196
|
+
*
|
|
1197
|
+
* @param attempt Current reconnection attempt count for the specific stream
|
|
1198
|
+
* @returns Time to wait in milliseconds before next reconnection attempt
|
|
1199
|
+
*/
|
|
1200
|
+
_getNextReconnectionDelay(attempt) {
|
|
1201
|
+
if (this._serverRetryMs !== void 0) {
|
|
1202
|
+
return this._serverRetryMs;
|
|
1203
|
+
}
|
|
1204
|
+
const initialDelay = this._reconnectionOptions.initialReconnectionDelay;
|
|
1205
|
+
const growFactor = this._reconnectionOptions.reconnectionDelayGrowFactor;
|
|
1206
|
+
const maxDelay = this._reconnectionOptions.maxReconnectionDelay;
|
|
1207
|
+
return Math.min(initialDelay * Math.pow(growFactor, attempt), maxDelay);
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Schedule a reconnection attempt using server-provided retry interval or backoff
|
|
1211
|
+
*
|
|
1212
|
+
* @param lastEventId The ID of the last received event for resumability
|
|
1213
|
+
* @param attemptCount Current reconnection attempt count for this specific stream
|
|
1214
|
+
*/
|
|
1215
|
+
_scheduleReconnection(options, attemptCount = 0) {
|
|
1216
|
+
const maxRetries = this._reconnectionOptions.maxRetries;
|
|
1217
|
+
if (attemptCount >= maxRetries) {
|
|
1218
|
+
this.onerror?.(new Error(`Maximum reconnection attempts (${maxRetries}) exceeded.`));
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
const delay = this._getNextReconnectionDelay(attemptCount);
|
|
1222
|
+
this._reconnectionTimeout = setTimeout(() => {
|
|
1223
|
+
this._startOrAuthSse(options).catch((error) => {
|
|
1224
|
+
this.onerror?.(new Error(`Failed to reconnect SSE stream: ${error instanceof Error ? error.message : String(error)}`));
|
|
1225
|
+
this._scheduleReconnection(options, attemptCount + 1);
|
|
1226
|
+
});
|
|
1227
|
+
}, delay);
|
|
1228
|
+
}
|
|
1229
|
+
_handleSseStream(stream, options, isReconnectable) {
|
|
1230
|
+
if (!stream) {
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
const { onresumptiontoken, replayMessageId } = options;
|
|
1234
|
+
let lastEventId;
|
|
1235
|
+
let hasPrimingEvent = false;
|
|
1236
|
+
let receivedResponse = false;
|
|
1237
|
+
const processStream = async () => {
|
|
1238
|
+
try {
|
|
1239
|
+
const reader = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream({
|
|
1240
|
+
onRetry: (retryMs) => {
|
|
1241
|
+
this._serverRetryMs = retryMs;
|
|
1242
|
+
}
|
|
1243
|
+
})).getReader();
|
|
1244
|
+
while (true) {
|
|
1245
|
+
const { value: event, done } = await reader.read();
|
|
1246
|
+
if (done) {
|
|
1247
|
+
break;
|
|
1248
|
+
}
|
|
1249
|
+
if (event.id) {
|
|
1250
|
+
lastEventId = event.id;
|
|
1251
|
+
hasPrimingEvent = true;
|
|
1252
|
+
onresumptiontoken?.(event.id);
|
|
1253
|
+
}
|
|
1254
|
+
if (!event.data) {
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
if (!event.event || event.event === "message") {
|
|
1258
|
+
try {
|
|
1259
|
+
const message = JSONRPCMessageSchema.parse(JSON.parse(event.data));
|
|
1260
|
+
if (isJSONRPCResultResponse(message)) {
|
|
1261
|
+
receivedResponse = true;
|
|
1262
|
+
if (replayMessageId !== void 0) {
|
|
1263
|
+
message.id = replayMessageId;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
this.onmessage?.(message);
|
|
1267
|
+
} catch (error) {
|
|
1268
|
+
this.onerror?.(error);
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
const canResume = isReconnectable || hasPrimingEvent;
|
|
1273
|
+
const needsReconnect = canResume && !receivedResponse;
|
|
1274
|
+
if (needsReconnect && this._abortController && !this._abortController.signal.aborted) {
|
|
1275
|
+
this._scheduleReconnection({
|
|
1276
|
+
resumptionToken: lastEventId,
|
|
1277
|
+
onresumptiontoken,
|
|
1278
|
+
replayMessageId
|
|
1279
|
+
}, 0);
|
|
1280
|
+
}
|
|
1281
|
+
} catch (error) {
|
|
1282
|
+
this.onerror?.(new Error(`SSE stream disconnected: ${error}`));
|
|
1283
|
+
const canResume = isReconnectable || hasPrimingEvent;
|
|
1284
|
+
const needsReconnect = canResume && !receivedResponse;
|
|
1285
|
+
if (needsReconnect && this._abortController && !this._abortController.signal.aborted) {
|
|
1286
|
+
try {
|
|
1287
|
+
this._scheduleReconnection({
|
|
1288
|
+
resumptionToken: lastEventId,
|
|
1289
|
+
onresumptiontoken,
|
|
1290
|
+
replayMessageId
|
|
1291
|
+
}, 0);
|
|
1292
|
+
} catch (error2) {
|
|
1293
|
+
this.onerror?.(new Error(`Failed to reconnect: ${error2 instanceof Error ? error2.message : String(error2)}`));
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
processStream();
|
|
1299
|
+
}
|
|
1300
|
+
async start() {
|
|
1301
|
+
if (this._abortController) {
|
|
1302
|
+
throw new Error("StreamableHTTPClientTransport already started! If using Client class, note that connect() calls start() automatically.");
|
|
1303
|
+
}
|
|
1304
|
+
this._abortController = new AbortController();
|
|
1305
|
+
}
|
|
1306
|
+
/**
|
|
1307
|
+
* Call this method after the user has finished authorizing via their user agent and is redirected back to the MCP client application. This will exchange the authorization code for an access token, enabling the next connection attempt to successfully auth.
|
|
1308
|
+
*/
|
|
1309
|
+
async finishAuth(authorizationCode) {
|
|
1310
|
+
if (!this._authProvider) {
|
|
1311
|
+
throw new UnauthorizedError("No auth provider");
|
|
1312
|
+
}
|
|
1313
|
+
const result = await auth(this._authProvider, {
|
|
1314
|
+
serverUrl: this._url,
|
|
1315
|
+
authorizationCode,
|
|
1316
|
+
resourceMetadataUrl: this._resourceMetadataUrl,
|
|
1317
|
+
scope: this._scope,
|
|
1318
|
+
fetchFn: this._fetchWithInit
|
|
1319
|
+
});
|
|
1320
|
+
if (result !== "AUTHORIZED") {
|
|
1321
|
+
throw new UnauthorizedError("Failed to authorize");
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
async close() {
|
|
1325
|
+
if (this._reconnectionTimeout) {
|
|
1326
|
+
clearTimeout(this._reconnectionTimeout);
|
|
1327
|
+
this._reconnectionTimeout = void 0;
|
|
1328
|
+
}
|
|
1329
|
+
this._abortController?.abort();
|
|
1330
|
+
this.onclose?.();
|
|
1331
|
+
}
|
|
1332
|
+
async send(message, options) {
|
|
1333
|
+
try {
|
|
1334
|
+
const { resumptionToken, onresumptiontoken } = options || {};
|
|
1335
|
+
if (resumptionToken) {
|
|
1336
|
+
this._startOrAuthSse({ resumptionToken, replayMessageId: isJSONRPCRequest(message) ? message.id : void 0 }).catch((err) => this.onerror?.(err));
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
const headers = await this._commonHeaders();
|
|
1340
|
+
headers.set("content-type", "application/json");
|
|
1341
|
+
headers.set("accept", "application/json, text/event-stream");
|
|
1342
|
+
const init = {
|
|
1343
|
+
...this._requestInit,
|
|
1344
|
+
method: "POST",
|
|
1345
|
+
headers,
|
|
1346
|
+
body: JSON.stringify(message),
|
|
1347
|
+
signal: this._abortController?.signal
|
|
1348
|
+
};
|
|
1349
|
+
const response = await (this._fetch ?? fetch)(this._url, init);
|
|
1350
|
+
const sessionId = response.headers.get("mcp-session-id");
|
|
1351
|
+
if (sessionId) {
|
|
1352
|
+
this._sessionId = sessionId;
|
|
1353
|
+
}
|
|
1354
|
+
if (!response.ok) {
|
|
1355
|
+
const text = await response.text().catch(() => null);
|
|
1356
|
+
if (response.status === 401 && this._authProvider) {
|
|
1357
|
+
if (this._hasCompletedAuthFlow) {
|
|
1358
|
+
throw new StreamableHTTPError(401, "Server returned 401 after successful authentication");
|
|
1359
|
+
}
|
|
1360
|
+
const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response);
|
|
1361
|
+
this._resourceMetadataUrl = resourceMetadataUrl;
|
|
1362
|
+
this._scope = scope;
|
|
1363
|
+
const result = await auth(this._authProvider, {
|
|
1364
|
+
serverUrl: this._url,
|
|
1365
|
+
resourceMetadataUrl: this._resourceMetadataUrl,
|
|
1366
|
+
scope: this._scope,
|
|
1367
|
+
fetchFn: this._fetchWithInit
|
|
1368
|
+
});
|
|
1369
|
+
if (result !== "AUTHORIZED") {
|
|
1370
|
+
throw new UnauthorizedError();
|
|
1371
|
+
}
|
|
1372
|
+
this._hasCompletedAuthFlow = true;
|
|
1373
|
+
return this.send(message);
|
|
1374
|
+
}
|
|
1375
|
+
if (response.status === 403 && this._authProvider) {
|
|
1376
|
+
const { resourceMetadataUrl, scope, error } = extractWWWAuthenticateParams(response);
|
|
1377
|
+
if (error === "insufficient_scope") {
|
|
1378
|
+
const wwwAuthHeader = response.headers.get("WWW-Authenticate");
|
|
1379
|
+
if (this._lastUpscopingHeader === wwwAuthHeader) {
|
|
1380
|
+
throw new StreamableHTTPError(403, "Server returned 403 after trying upscoping");
|
|
1381
|
+
}
|
|
1382
|
+
if (scope) {
|
|
1383
|
+
this._scope = scope;
|
|
1384
|
+
}
|
|
1385
|
+
if (resourceMetadataUrl) {
|
|
1386
|
+
this._resourceMetadataUrl = resourceMetadataUrl;
|
|
1387
|
+
}
|
|
1388
|
+
this._lastUpscopingHeader = wwwAuthHeader ?? void 0;
|
|
1389
|
+
const result = await auth(this._authProvider, {
|
|
1390
|
+
serverUrl: this._url,
|
|
1391
|
+
resourceMetadataUrl: this._resourceMetadataUrl,
|
|
1392
|
+
scope: this._scope,
|
|
1393
|
+
fetchFn: this._fetch
|
|
1394
|
+
});
|
|
1395
|
+
if (result !== "AUTHORIZED") {
|
|
1396
|
+
throw new UnauthorizedError();
|
|
1397
|
+
}
|
|
1398
|
+
return this.send(message);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
throw new StreamableHTTPError(response.status, `Error POSTing to endpoint: ${text}`);
|
|
1402
|
+
}
|
|
1403
|
+
this._hasCompletedAuthFlow = false;
|
|
1404
|
+
this._lastUpscopingHeader = void 0;
|
|
1405
|
+
if (response.status === 202) {
|
|
1406
|
+
await response.body?.cancel();
|
|
1407
|
+
if (isInitializedNotification(message)) {
|
|
1408
|
+
this._startOrAuthSse({ resumptionToken: void 0 }).catch((err) => this.onerror?.(err));
|
|
1409
|
+
}
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
const messages = Array.isArray(message) ? message : [message];
|
|
1413
|
+
const hasRequests = messages.filter((msg) => "method" in msg && "id" in msg && msg.id !== void 0).length > 0;
|
|
1414
|
+
const contentType = response.headers.get("content-type");
|
|
1415
|
+
if (hasRequests) {
|
|
1416
|
+
if (contentType?.includes("text/event-stream")) {
|
|
1417
|
+
this._handleSseStream(response.body, { onresumptiontoken }, false);
|
|
1418
|
+
} else if (contentType?.includes("application/json")) {
|
|
1419
|
+
const data = await response.json();
|
|
1420
|
+
const responseMessages = Array.isArray(data) ? data.map((msg) => JSONRPCMessageSchema.parse(msg)) : [JSONRPCMessageSchema.parse(data)];
|
|
1421
|
+
for (const msg of responseMessages) {
|
|
1422
|
+
this.onmessage?.(msg);
|
|
1423
|
+
}
|
|
1424
|
+
} else {
|
|
1425
|
+
await response.body?.cancel();
|
|
1426
|
+
throw new StreamableHTTPError(-1, `Unexpected content type: ${contentType}`);
|
|
1427
|
+
}
|
|
1428
|
+
} else {
|
|
1429
|
+
await response.body?.cancel();
|
|
1430
|
+
}
|
|
1431
|
+
} catch (error) {
|
|
1432
|
+
this.onerror?.(error);
|
|
1433
|
+
throw error;
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
get sessionId() {
|
|
1437
|
+
return this._sessionId;
|
|
1438
|
+
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Terminates the current session by sending a DELETE request to the server.
|
|
1441
|
+
*
|
|
1442
|
+
* Clients that no longer need a particular session
|
|
1443
|
+
* (e.g., because the user is leaving the client application) SHOULD send an
|
|
1444
|
+
* HTTP DELETE to the MCP endpoint with the Mcp-Session-Id header to explicitly
|
|
1445
|
+
* terminate the session.
|
|
1446
|
+
*
|
|
1447
|
+
* The server MAY respond with HTTP 405 Method Not Allowed, indicating that
|
|
1448
|
+
* the server does not allow clients to terminate sessions.
|
|
1449
|
+
*/
|
|
1450
|
+
async terminateSession() {
|
|
1451
|
+
if (!this._sessionId) {
|
|
1452
|
+
return;
|
|
1453
|
+
}
|
|
1454
|
+
try {
|
|
1455
|
+
const headers = await this._commonHeaders();
|
|
1456
|
+
const init = {
|
|
1457
|
+
...this._requestInit,
|
|
1458
|
+
method: "DELETE",
|
|
1459
|
+
headers,
|
|
1460
|
+
signal: this._abortController?.signal
|
|
1461
|
+
};
|
|
1462
|
+
const response = await (this._fetch ?? fetch)(this._url, init);
|
|
1463
|
+
await response.body?.cancel();
|
|
1464
|
+
if (!response.ok && response.status !== 405) {
|
|
1465
|
+
throw new StreamableHTTPError(response.status, `Failed to terminate session: ${response.statusText}`);
|
|
1466
|
+
}
|
|
1467
|
+
this._sessionId = void 0;
|
|
1468
|
+
} catch (error) {
|
|
1469
|
+
this.onerror?.(error);
|
|
1470
|
+
throw error;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
setProtocolVersion(version) {
|
|
1474
|
+
this._protocolVersion = version;
|
|
1475
|
+
}
|
|
1476
|
+
get protocolVersion() {
|
|
1477
|
+
return this._protocolVersion;
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* Resume an SSE stream from a previous event ID.
|
|
1481
|
+
* Opens a GET SSE connection with Last-Event-ID header to replay missed events.
|
|
1482
|
+
*
|
|
1483
|
+
* @param lastEventId The event ID to resume from
|
|
1484
|
+
* @param options Optional callback to receive new resumption tokens
|
|
1485
|
+
*/
|
|
1486
|
+
async resumeStream(lastEventId, options) {
|
|
1487
|
+
await this._startOrAuthSse({
|
|
1488
|
+
resumptionToken: lastEventId,
|
|
1489
|
+
onresumptiontoken: options?.onresumptiontoken
|
|
1490
|
+
});
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
export {
|
|
1494
|
+
StreamableHTTPClientTransport,
|
|
1495
|
+
StreamableHTTPError
|
|
1496
|
+
};
|