mcp-proxy 6.4.2 → 6.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/mcp-proxy.mjs +28 -6
- package/dist/bin/mcp-proxy.mjs.map +1 -1
- package/dist/index.mjs +169 -145
- package/dist/index.mjs.map +1 -1
- package/dist/{stdio-CvFTizsx.mjs → stdio-BmURZCbz.mjs} +1557 -749
- package/dist/stdio-BmURZCbz.mjs.map +1 -0
- package/jsr.json +1 -1
- package/package.json +2 -2
- package/src/JSONFilterTransform.test.ts +47 -2
- package/src/JSONFilterTransform.ts +41 -8
- package/src/bin/mcp-proxy.ts +2 -3
- package/src/fixtures/noisy-stdout-server.ts +70 -0
- package/src/proxyServer.test.ts +2 -4
- package/src/startHTTPServer.test.ts +18 -51
- package/src/startStdioServer.ts +2 -3
- package/dist/stdio-CvFTizsx.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as NEVER, S as _coercedNumber, T as AuthenticationMiddleware, _ as looseObject, a as startHTTPServer, b as string, c as LATEST_PROTOCOL_VERSION, d as
|
|
1
|
+
import { C as NEVER, S as _coercedNumber, T as AuthenticationMiddleware, _ as looseObject, a as startHTTPServer, b as string, c as LATEST_PROTOCOL_VERSION, d as isJSONRPCResultResponse, f as ZodNumber, g as literal, h as boolean, i as Client, l as isInitializedNotification, m as array, n as serializeMessage, o as proxyServer, p as any, r as Server, s as JSONRPCMessageSchema, t as ReadBuffer, u as isJSONRPCRequest, v as number$1, w as InMemoryEventStore, x as url, y as object } from "./stdio-BmURZCbz.mjs";
|
|
2
2
|
import process from "node:process";
|
|
3
3
|
|
|
4
4
|
//#region node_modules/.pnpm/zod@3.25.76/node_modules/zod/v4/classic/compat.js
|
|
@@ -354,7 +354,7 @@ function getBaseURL() {
|
|
|
354
354
|
}
|
|
355
355
|
|
|
356
356
|
//#endregion
|
|
357
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
357
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/transport.js
|
|
358
358
|
/**
|
|
359
359
|
* Normalizes HeadersInit to a plain Record<string, string> for manipulation.
|
|
360
360
|
* Handles Headers objects, arrays of tuples, and plain objects.
|
|
@@ -379,7 +379,7 @@ function createFetchWithInit(baseFetch = fetch, baseInit) {
|
|
|
379
379
|
return baseFetch(url$1, {
|
|
380
380
|
...baseInit,
|
|
381
381
|
...init,
|
|
382
|
-
headers:
|
|
382
|
+
headers: init?.headers ? {
|
|
383
383
|
...normalizeHeaders(baseInit.headers),
|
|
384
384
|
...normalizeHeaders(init.headers)
|
|
385
385
|
} : baseInit.headers
|
|
@@ -388,7 +388,7 @@ function createFetchWithInit(baseFetch = fetch, baseInit) {
|
|
|
388
388
|
}
|
|
389
389
|
|
|
390
390
|
//#endregion
|
|
391
|
-
//#region node_modules/.pnpm/pkce-challenge@5.0.
|
|
391
|
+
//#region node_modules/.pnpm/pkce-challenge@5.0.1/node_modules/pkce-challenge/dist/index.node.js
|
|
392
392
|
let crypto;
|
|
393
393
|
crypto = globalThis.crypto?.webcrypto ?? globalThis.crypto ?? import("node:crypto").then((m) => m.webcrypto);
|
|
394
394
|
/**
|
|
@@ -405,11 +405,11 @@ async function getRandomValues(size) {
|
|
|
405
405
|
*/
|
|
406
406
|
async function random(size) {
|
|
407
407
|
const mask = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";
|
|
408
|
+
const evenDistCutoff = Math.pow(2, 8) - Math.pow(2, 8) % 66;
|
|
408
409
|
let result = "";
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
result += mask[randomIndex];
|
|
410
|
+
while (result.length < size) {
|
|
411
|
+
const randomBytes = await getRandomValues(size - result.length);
|
|
412
|
+
for (const randomByte of randomBytes) if (randomByte < evenDistCutoff) result += mask[randomByte % 66];
|
|
413
413
|
}
|
|
414
414
|
return result;
|
|
415
415
|
}
|
|
@@ -443,7 +443,7 @@ async function pkceChallenge(length) {
|
|
|
443
443
|
}
|
|
444
444
|
|
|
445
445
|
//#endregion
|
|
446
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
446
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/auth.js
|
|
447
447
|
/**
|
|
448
448
|
* Reusable URL validation that disallows javascript: scheme
|
|
449
449
|
*/
|
|
@@ -627,7 +627,7 @@ const OAuthTokenRevocationRequestSchema = object({
|
|
|
627
627
|
}).strip();
|
|
628
628
|
|
|
629
629
|
//#endregion
|
|
630
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
630
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/auth-utils.js
|
|
631
631
|
/**
|
|
632
632
|
* Utilities for handling OAuth resource URIs.
|
|
633
633
|
*/
|
|
@@ -661,7 +661,7 @@ function checkResourceAllowed({ requestedResource, configuredResource }) {
|
|
|
661
661
|
}
|
|
662
662
|
|
|
663
663
|
//#endregion
|
|
664
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
664
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/server/auth/errors.js
|
|
665
665
|
/**
|
|
666
666
|
* Base class for all OAuth errors
|
|
667
667
|
*/
|
|
@@ -812,10 +812,10 @@ const OAUTH_ERRORS = {
|
|
|
812
812
|
};
|
|
813
813
|
|
|
814
814
|
//#endregion
|
|
815
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
815
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/client/auth.js
|
|
816
816
|
var UnauthorizedError = class extends Error {
|
|
817
817
|
constructor(message) {
|
|
818
|
-
super(message
|
|
818
|
+
super(message ?? "Unauthorized");
|
|
819
819
|
}
|
|
820
820
|
};
|
|
821
821
|
function isClientAuthMethod(method) {
|
|
@@ -926,44 +926,64 @@ async function parseErrorResponse(input) {
|
|
|
926
926
|
* instead of linking together the other lower-level functions in this module.
|
|
927
927
|
*/
|
|
928
928
|
async function auth(provider, options) {
|
|
929
|
-
var _a, _b;
|
|
930
929
|
try {
|
|
931
930
|
return await authInternal(provider, options);
|
|
932
931
|
} catch (error) {
|
|
933
932
|
if (error instanceof InvalidClientError || error instanceof UnauthorizedClientError) {
|
|
934
|
-
await
|
|
933
|
+
await provider.invalidateCredentials?.("all");
|
|
935
934
|
return await authInternal(provider, options);
|
|
936
935
|
} else if (error instanceof InvalidGrantError) {
|
|
937
|
-
await
|
|
936
|
+
await provider.invalidateCredentials?.("tokens");
|
|
938
937
|
return await authInternal(provider, options);
|
|
939
938
|
}
|
|
940
939
|
throw error;
|
|
941
940
|
}
|
|
942
941
|
}
|
|
943
942
|
async function authInternal(provider, { serverUrl, authorizationCode, scope, resourceMetadataUrl, fetchFn }) {
|
|
944
|
-
|
|
943
|
+
const cachedState = await provider.discoveryState?.();
|
|
945
944
|
let resourceMetadata;
|
|
946
945
|
let authorizationServerUrl;
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
946
|
+
let metadata;
|
|
947
|
+
let effectiveResourceMetadataUrl = resourceMetadataUrl;
|
|
948
|
+
if (!effectiveResourceMetadataUrl && cachedState?.resourceMetadataUrl) effectiveResourceMetadataUrl = new URL(cachedState.resourceMetadataUrl);
|
|
949
|
+
if (cachedState?.authorizationServerUrl) {
|
|
950
|
+
authorizationServerUrl = cachedState.authorizationServerUrl;
|
|
951
|
+
resourceMetadata = cachedState.resourceMetadata;
|
|
952
|
+
metadata = cachedState.authorizationServerMetadata ?? await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn });
|
|
953
|
+
if (!resourceMetadata) try {
|
|
954
|
+
resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: effectiveResourceMetadataUrl }, fetchFn);
|
|
955
|
+
} catch {}
|
|
956
|
+
if (metadata !== cachedState.authorizationServerMetadata || resourceMetadata !== cachedState.resourceMetadata) await provider.saveDiscoveryState?.({
|
|
957
|
+
authorizationServerUrl: String(authorizationServerUrl),
|
|
958
|
+
resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
|
|
959
|
+
resourceMetadata,
|
|
960
|
+
authorizationServerMetadata: metadata
|
|
961
|
+
});
|
|
962
|
+
} else {
|
|
963
|
+
const serverInfo = await discoverOAuthServerInfo(serverUrl, {
|
|
964
|
+
resourceMetadataUrl: effectiveResourceMetadataUrl,
|
|
965
|
+
fetchFn
|
|
966
|
+
});
|
|
967
|
+
authorizationServerUrl = serverInfo.authorizationServerUrl;
|
|
968
|
+
metadata = serverInfo.authorizationServerMetadata;
|
|
969
|
+
resourceMetadata = serverInfo.resourceMetadata;
|
|
970
|
+
await provider.saveDiscoveryState?.({
|
|
971
|
+
authorizationServerUrl: String(authorizationServerUrl),
|
|
972
|
+
resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
|
|
973
|
+
resourceMetadata,
|
|
974
|
+
authorizationServerMetadata: metadata
|
|
975
|
+
});
|
|
976
|
+
}
|
|
956
977
|
const resource = await selectResourceURL(serverUrl, provider, resourceMetadata);
|
|
957
|
-
const metadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn });
|
|
958
978
|
let clientInformation = await Promise.resolve(provider.clientInformation());
|
|
959
979
|
if (!clientInformation) {
|
|
960
980
|
if (authorizationCode !== void 0) throw new Error("Existing OAuth client information is required when exchanging an authorization code");
|
|
961
|
-
const supportsUrlBasedClientId =
|
|
981
|
+
const supportsUrlBasedClientId = metadata?.client_id_metadata_document_supported === true;
|
|
962
982
|
const clientMetadataUrl = provider.clientMetadataUrl;
|
|
963
983
|
if (clientMetadataUrl && !isHttpsUrl(clientMetadataUrl)) throw new InvalidClientMetadataError(`clientMetadataUrl must be a valid HTTPS URL with a non-root pathname, got: ${clientMetadataUrl}`);
|
|
964
984
|
if (supportsUrlBasedClientId && clientMetadataUrl) {
|
|
965
985
|
clientInformation = { client_id: clientMetadataUrl };
|
|
966
|
-
await
|
|
986
|
+
await provider.saveClientInformation?.(clientInformation);
|
|
967
987
|
} else {
|
|
968
988
|
if (!provider.saveClientInformation) throw new Error("OAuth client information must be saveable for dynamic registration");
|
|
969
989
|
const fullInformation = await registerClient(authorizationServerUrl, {
|
|
@@ -987,7 +1007,7 @@ async function authInternal(provider, { serverUrl, authorizationCode, scope, res
|
|
|
987
1007
|
return "AUTHORIZED";
|
|
988
1008
|
}
|
|
989
1009
|
const tokens = await provider.tokens();
|
|
990
|
-
if (tokens
|
|
1010
|
+
if (tokens?.refresh_token) try {
|
|
991
1011
|
const newTokens = await refreshAuthorization(authorizationServerUrl, {
|
|
992
1012
|
metadata,
|
|
993
1013
|
clientInformation,
|
|
@@ -1007,7 +1027,7 @@ async function authInternal(provider, { serverUrl, authorizationCode, scope, res
|
|
|
1007
1027
|
clientInformation,
|
|
1008
1028
|
state,
|
|
1009
1029
|
redirectUrl: provider.redirectUrl,
|
|
1010
|
-
scope: scope ||
|
|
1030
|
+
scope: scope || resourceMetadata?.scopes_supported?.join(" ") || provider.clientMetadata.scope,
|
|
1011
1031
|
resource
|
|
1012
1032
|
});
|
|
1013
1033
|
await provider.saveCodeVerifier(codeVerifier);
|
|
@@ -1023,13 +1043,13 @@ function isHttpsUrl(value) {
|
|
|
1023
1043
|
try {
|
|
1024
1044
|
const url$1 = new URL(value);
|
|
1025
1045
|
return url$1.protocol === "https:" && url$1.pathname !== "/";
|
|
1026
|
-
} catch
|
|
1046
|
+
} catch {
|
|
1027
1047
|
return false;
|
|
1028
1048
|
}
|
|
1029
1049
|
}
|
|
1030
1050
|
async function selectResourceURL(serverUrl, provider, resourceMetadata) {
|
|
1031
1051
|
const defaultResource = resourceUrlFromServerUrl(serverUrl);
|
|
1032
|
-
if (provider.validateResourceURL) return await provider.validateResourceURL(defaultResource, resourceMetadata
|
|
1052
|
+
if (provider.validateResourceURL) return await provider.validateResourceURL(defaultResource, resourceMetadata?.resource);
|
|
1033
1053
|
if (!resourceMetadata) return;
|
|
1034
1054
|
if (!checkResourceAllowed({
|
|
1035
1055
|
requestedResource: defaultResource,
|
|
@@ -1049,7 +1069,7 @@ function extractWWWAuthenticateParams(res) {
|
|
|
1049
1069
|
let resourceMetadataUrl;
|
|
1050
1070
|
if (resourceMetadataMatch) try {
|
|
1051
1071
|
resourceMetadataUrl = new URL(resourceMetadataMatch);
|
|
1052
|
-
} catch
|
|
1072
|
+
} catch {}
|
|
1053
1073
|
const scope = extractFieldFromWwwAuth(res, "scope") || void 0;
|
|
1054
1074
|
const error = extractFieldFromWwwAuth(res, "error") || void 0;
|
|
1055
1075
|
return {
|
|
@@ -1080,17 +1100,16 @@ function extractFieldFromWwwAuth(response, fieldName) {
|
|
|
1080
1100
|
* return `undefined`. Any other errors will be thrown as exceptions.
|
|
1081
1101
|
*/
|
|
1082
1102
|
async function discoverOAuthProtectedResourceMetadata(serverUrl, opts, fetchFn = fetch) {
|
|
1083
|
-
var _a, _b;
|
|
1084
1103
|
const response = await discoverMetadataWithFallback(serverUrl, "oauth-protected-resource", fetchFn, {
|
|
1085
|
-
protocolVersion: opts
|
|
1086
|
-
metadataUrl: opts
|
|
1104
|
+
protocolVersion: opts?.protocolVersion,
|
|
1105
|
+
metadataUrl: opts?.resourceMetadataUrl
|
|
1087
1106
|
});
|
|
1088
1107
|
if (!response || response.status === 404) {
|
|
1089
|
-
await
|
|
1108
|
+
await response?.body?.cancel();
|
|
1090
1109
|
throw new Error(`Resource server does not implement OAuth 2.0 Protected Resource Metadata.`);
|
|
1091
1110
|
}
|
|
1092
1111
|
if (!response.ok) {
|
|
1093
|
-
await
|
|
1112
|
+
await response.body?.cancel();
|
|
1094
1113
|
throw new Error(`HTTP ${response.status} trying to load well-known OAuth protected resource metadata.`);
|
|
1095
1114
|
}
|
|
1096
1115
|
return OAuthProtectedResourceMetadataSchema.parse(await response.json());
|
|
@@ -1130,18 +1149,17 @@ function shouldAttemptFallback(response, pathname) {
|
|
|
1130
1149
|
* Generic function for discovering OAuth metadata with fallback support
|
|
1131
1150
|
*/
|
|
1132
1151
|
async function discoverMetadataWithFallback(serverUrl, wellKnownType, fetchFn, opts) {
|
|
1133
|
-
var _a, _b;
|
|
1134
1152
|
const issuer = new URL(serverUrl);
|
|
1135
|
-
const protocolVersion =
|
|
1153
|
+
const protocolVersion = opts?.protocolVersion ?? LATEST_PROTOCOL_VERSION;
|
|
1136
1154
|
let url$1;
|
|
1137
|
-
if (opts
|
|
1155
|
+
if (opts?.metadataUrl) url$1 = new URL(opts.metadataUrl);
|
|
1138
1156
|
else {
|
|
1139
1157
|
const wellKnownPath = buildWellKnownPath(wellKnownType, issuer.pathname);
|
|
1140
|
-
url$1 = new URL(wellKnownPath,
|
|
1158
|
+
url$1 = new URL(wellKnownPath, opts?.metadataServerUrl ?? issuer);
|
|
1141
1159
|
url$1.search = issuer.search;
|
|
1142
1160
|
}
|
|
1143
1161
|
let response = await tryMetadataDiscovery(url$1, protocolVersion, fetchFn);
|
|
1144
|
-
if (!
|
|
1162
|
+
if (!opts?.metadataUrl && shouldAttemptFallback(response, issuer.pathname)) response = await tryMetadataDiscovery(new URL(`/.well-known/${wellKnownType}`, issuer), protocolVersion, fetchFn);
|
|
1145
1163
|
return response;
|
|
1146
1164
|
}
|
|
1147
1165
|
/**
|
|
@@ -1198,7 +1216,6 @@ function buildDiscoveryUrls(authorizationServerUrl) {
|
|
|
1198
1216
|
* @returns Promise resolving to authorization server metadata, or undefined if discovery fails
|
|
1199
1217
|
*/
|
|
1200
1218
|
async function discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn = fetch, protocolVersion = LATEST_PROTOCOL_VERSION } = {}) {
|
|
1201
|
-
var _a;
|
|
1202
1219
|
const headers = {
|
|
1203
1220
|
"MCP-Protocol-Version": protocolVersion,
|
|
1204
1221
|
Accept: "application/json"
|
|
@@ -1213,7 +1230,7 @@ async function discoverAuthorizationServerMetadata(authorizationServerUrl, { fet
|
|
|
1213
1230
|
*/
|
|
1214
1231
|
continue;
|
|
1215
1232
|
if (!response.ok) {
|
|
1216
|
-
await
|
|
1233
|
+
await response.body?.cancel();
|
|
1217
1234
|
if (response.status >= 400 && response.status < 500) continue;
|
|
1218
1235
|
throw new Error(`HTTP ${response.status} trying to load ${type === "oauth" ? "OAuth" : "OpenID provider"} metadata from ${endpointUrl}`);
|
|
1219
1236
|
}
|
|
@@ -1222,6 +1239,41 @@ async function discoverAuthorizationServerMetadata(authorizationServerUrl, { fet
|
|
|
1222
1239
|
}
|
|
1223
1240
|
}
|
|
1224
1241
|
/**
|
|
1242
|
+
* Discovers the authorization server for an MCP server following
|
|
1243
|
+
* {@link https://datatracker.ietf.org/doc/html/rfc9728 | RFC 9728} (OAuth 2.0 Protected
|
|
1244
|
+
* Resource Metadata), with fallback to treating the server URL as the
|
|
1245
|
+
* authorization server.
|
|
1246
|
+
*
|
|
1247
|
+
* This function combines two discovery steps into one call:
|
|
1248
|
+
* 1. Probes `/.well-known/oauth-protected-resource` on the MCP server to find the
|
|
1249
|
+
* authorization server URL (RFC 9728).
|
|
1250
|
+
* 2. Fetches authorization server metadata from that URL (RFC 8414 / OpenID Connect Discovery).
|
|
1251
|
+
*
|
|
1252
|
+
* Use this when you need the authorization server metadata for operations outside the
|
|
1253
|
+
* {@linkcode auth} orchestrator, such as token refresh or token revocation.
|
|
1254
|
+
*
|
|
1255
|
+
* @param serverUrl - The MCP resource server URL
|
|
1256
|
+
* @param opts - Optional configuration
|
|
1257
|
+
* @param opts.resourceMetadataUrl - Override URL for the protected resource metadata endpoint
|
|
1258
|
+
* @param opts.fetchFn - Custom fetch function for HTTP requests
|
|
1259
|
+
* @returns Authorization server URL, metadata, and resource metadata (if available)
|
|
1260
|
+
*/
|
|
1261
|
+
async function discoverOAuthServerInfo(serverUrl, opts) {
|
|
1262
|
+
let resourceMetadata;
|
|
1263
|
+
let authorizationServerUrl;
|
|
1264
|
+
try {
|
|
1265
|
+
resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: opts?.resourceMetadataUrl }, opts?.fetchFn);
|
|
1266
|
+
if (resourceMetadata.authorization_servers && resourceMetadata.authorization_servers.length > 0) authorizationServerUrl = resourceMetadata.authorization_servers[0];
|
|
1267
|
+
} catch {}
|
|
1268
|
+
if (!authorizationServerUrl) authorizationServerUrl = String(new URL("/", serverUrl));
|
|
1269
|
+
const authorizationServerMetadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn: opts?.fetchFn });
|
|
1270
|
+
return {
|
|
1271
|
+
authorizationServerUrl,
|
|
1272
|
+
authorizationServerMetadata,
|
|
1273
|
+
resourceMetadata
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1225
1277
|
* Begins the authorization flow with the given server, by generating a PKCE challenge and constructing the authorization URL.
|
|
1226
1278
|
*/
|
|
1227
1279
|
async function startAuthorization(authorizationServerUrl, { metadata, clientInformation, redirectUrl, scope, state, resource }) {
|
|
@@ -1241,7 +1293,7 @@ async function startAuthorization(authorizationServerUrl, { metadata, clientInfo
|
|
|
1241
1293
|
authorizationUrl.searchParams.set("redirect_uri", String(redirectUrl));
|
|
1242
1294
|
if (state) authorizationUrl.searchParams.set("state", state);
|
|
1243
1295
|
if (scope) authorizationUrl.searchParams.set("scope", scope);
|
|
1244
|
-
if (scope
|
|
1296
|
+
if (scope?.includes("offline_access")) authorizationUrl.searchParams.append("prompt", "consent");
|
|
1245
1297
|
if (resource) authorizationUrl.searchParams.set("resource", resource.href);
|
|
1246
1298
|
return {
|
|
1247
1299
|
authorizationUrl,
|
|
@@ -1272,16 +1324,15 @@ function prepareAuthorizationCodeRequest(authorizationCode, codeVerifier, redire
|
|
|
1272
1324
|
* Used by exchangeAuthorization, refreshAuthorization, and fetchToken.
|
|
1273
1325
|
*/
|
|
1274
1326
|
async function executeTokenRequest(authorizationServerUrl, { metadata, tokenRequestParams, clientInformation, addClientAuthentication, resource, fetchFn }) {
|
|
1275
|
-
|
|
1276
|
-
const tokenUrl = (metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint) ? new URL(metadata.token_endpoint) : new URL("/token", authorizationServerUrl);
|
|
1327
|
+
const tokenUrl = metadata?.token_endpoint ? new URL(metadata.token_endpoint) : new URL("/token", authorizationServerUrl);
|
|
1277
1328
|
const headers = new Headers({
|
|
1278
1329
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
1279
1330
|
Accept: "application/json"
|
|
1280
1331
|
});
|
|
1281
1332
|
if (resource) tokenRequestParams.set("resource", resource.href);
|
|
1282
1333
|
if (addClientAuthentication) await addClientAuthentication(headers, tokenRequestParams, tokenUrl, metadata);
|
|
1283
|
-
else if (clientInformation) applyClientAuthentication(selectClientAuthMethod(clientInformation,
|
|
1284
|
-
const response = await (fetchFn
|
|
1334
|
+
else if (clientInformation) applyClientAuthentication(selectClientAuthMethod(clientInformation, metadata?.token_endpoint_auth_methods_supported ?? []), clientInformation, headers, tokenRequestParams);
|
|
1335
|
+
const response = await (fetchFn ?? fetch)(tokenUrl, {
|
|
1285
1336
|
method: "POST",
|
|
1286
1337
|
headers,
|
|
1287
1338
|
body: tokenRequestParams
|
|
@@ -1356,7 +1407,7 @@ async function fetchToken(provider, authorizationServerUrl, { metadata, resource
|
|
|
1356
1407
|
return executeTokenRequest(authorizationServerUrl, {
|
|
1357
1408
|
metadata,
|
|
1358
1409
|
tokenRequestParams,
|
|
1359
|
-
clientInformation: clientInformation
|
|
1410
|
+
clientInformation: clientInformation ?? void 0,
|
|
1360
1411
|
addClientAuthentication: provider.addClientAuthentication,
|
|
1361
1412
|
resource,
|
|
1362
1413
|
fetchFn
|
|
@@ -1371,7 +1422,7 @@ async function registerClient(authorizationServerUrl, { metadata, clientMetadata
|
|
|
1371
1422
|
if (!metadata.registration_endpoint) throw new Error("Incompatible auth server: does not support dynamic client registration");
|
|
1372
1423
|
registrationUrl = new URL(metadata.registration_endpoint);
|
|
1373
1424
|
} else registrationUrl = new URL("/register", authorizationServerUrl);
|
|
1374
|
-
const response = await (fetchFn
|
|
1425
|
+
const response = await (fetchFn ?? fetch)(registrationUrl, {
|
|
1375
1426
|
method: "POST",
|
|
1376
1427
|
headers: { "Content-Type": "application/json" },
|
|
1377
1428
|
body: JSON.stringify(clientMetadata)
|
|
@@ -1381,7 +1432,7 @@ async function registerClient(authorizationServerUrl, { metadata, clientMetadata
|
|
|
1381
1432
|
}
|
|
1382
1433
|
|
|
1383
1434
|
//#endregion
|
|
1384
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
1435
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/client/sse.js
|
|
1385
1436
|
var SseError = class extends Error {
|
|
1386
1437
|
constructor(code, message, event) {
|
|
1387
1438
|
super(`SSE error: ${message}`);
|
|
@@ -1399,14 +1450,13 @@ var SSEClientTransport = class {
|
|
|
1399
1450
|
this._url = url$1;
|
|
1400
1451
|
this._resourceMetadataUrl = void 0;
|
|
1401
1452
|
this._scope = void 0;
|
|
1402
|
-
this._eventSourceInit = opts
|
|
1403
|
-
this._requestInit = opts
|
|
1404
|
-
this._authProvider = opts
|
|
1405
|
-
this._fetch = opts
|
|
1406
|
-
this._fetchWithInit = createFetchWithInit(opts
|
|
1453
|
+
this._eventSourceInit = opts?.eventSourceInit;
|
|
1454
|
+
this._requestInit = opts?.requestInit;
|
|
1455
|
+
this._authProvider = opts?.authProvider;
|
|
1456
|
+
this._fetch = opts?.fetch;
|
|
1457
|
+
this._fetchWithInit = createFetchWithInit(opts?.fetch, opts?.requestInit);
|
|
1407
1458
|
}
|
|
1408
1459
|
async _authThenStart() {
|
|
1409
|
-
var _a;
|
|
1410
1460
|
if (!this._authProvider) throw new UnauthorizedError("No auth provider");
|
|
1411
1461
|
let result;
|
|
1412
1462
|
try {
|
|
@@ -1417,29 +1467,27 @@ var SSEClientTransport = class {
|
|
|
1417
1467
|
fetchFn: this._fetchWithInit
|
|
1418
1468
|
});
|
|
1419
1469
|
} catch (error) {
|
|
1420
|
-
|
|
1470
|
+
this.onerror?.(error);
|
|
1421
1471
|
throw error;
|
|
1422
1472
|
}
|
|
1423
1473
|
if (result !== "AUTHORIZED") throw new UnauthorizedError();
|
|
1424
1474
|
return await this._startOrAuth();
|
|
1425
1475
|
}
|
|
1426
1476
|
async _commonHeaders() {
|
|
1427
|
-
var _a;
|
|
1428
1477
|
const headers = {};
|
|
1429
1478
|
if (this._authProvider) {
|
|
1430
1479
|
const tokens = await this._authProvider.tokens();
|
|
1431
1480
|
if (tokens) headers["Authorization"] = `Bearer ${tokens.access_token}`;
|
|
1432
1481
|
}
|
|
1433
1482
|
if (this._protocolVersion) headers["mcp-protocol-version"] = this._protocolVersion;
|
|
1434
|
-
const extraHeaders = normalizeHeaders(
|
|
1483
|
+
const extraHeaders = normalizeHeaders(this._requestInit?.headers);
|
|
1435
1484
|
return new Headers({
|
|
1436
1485
|
...headers,
|
|
1437
1486
|
...extraHeaders
|
|
1438
1487
|
});
|
|
1439
1488
|
}
|
|
1440
1489
|
_startOrAuth() {
|
|
1441
|
-
|
|
1442
|
-
const fetchImpl = (_c = (_b = (_a = this === null || this === void 0 ? void 0 : this._eventSourceInit) === null || _a === void 0 ? void 0 : _a.fetch) !== null && _b !== void 0 ? _b : this._fetch) !== null && _c !== void 0 ? _c : fetch;
|
|
1490
|
+
const fetchImpl = this?._eventSourceInit?.fetch ?? this._fetch ?? fetch;
|
|
1443
1491
|
return new Promise((resolve, reject) => {
|
|
1444
1492
|
this._eventSource = new EventSource(this._url.href, {
|
|
1445
1493
|
...this._eventSourceInit,
|
|
@@ -1460,41 +1508,38 @@ var SSEClientTransport = class {
|
|
|
1460
1508
|
});
|
|
1461
1509
|
this._abortController = new AbortController();
|
|
1462
1510
|
this._eventSource.onerror = (event) => {
|
|
1463
|
-
var _a$1;
|
|
1464
1511
|
if (event.code === 401 && this._authProvider) {
|
|
1465
1512
|
this._authThenStart().then(resolve, reject);
|
|
1466
1513
|
return;
|
|
1467
1514
|
}
|
|
1468
1515
|
const error = new SseError(event.code, event.message, event);
|
|
1469
1516
|
reject(error);
|
|
1470
|
-
|
|
1517
|
+
this.onerror?.(error);
|
|
1471
1518
|
};
|
|
1472
1519
|
this._eventSource.onopen = () => {};
|
|
1473
1520
|
this._eventSource.addEventListener("endpoint", (event) => {
|
|
1474
|
-
var _a$1;
|
|
1475
1521
|
const messageEvent = event;
|
|
1476
1522
|
try {
|
|
1477
1523
|
this._endpoint = new URL(messageEvent.data, this._url);
|
|
1478
1524
|
if (this._endpoint.origin !== this._url.origin) throw new Error(`Endpoint origin does not match connection origin: ${this._endpoint.origin}`);
|
|
1479
1525
|
} catch (error) {
|
|
1480
1526
|
reject(error);
|
|
1481
|
-
|
|
1527
|
+
this.onerror?.(error);
|
|
1482
1528
|
this.close();
|
|
1483
1529
|
return;
|
|
1484
1530
|
}
|
|
1485
1531
|
resolve();
|
|
1486
1532
|
});
|
|
1487
1533
|
this._eventSource.onmessage = (event) => {
|
|
1488
|
-
var _a$1, _b$1;
|
|
1489
1534
|
const messageEvent = event;
|
|
1490
1535
|
let message;
|
|
1491
1536
|
try {
|
|
1492
1537
|
message = JSONRPCMessageSchema.parse(JSON.parse(messageEvent.data));
|
|
1493
1538
|
} catch (error) {
|
|
1494
|
-
|
|
1539
|
+
this.onerror?.(error);
|
|
1495
1540
|
return;
|
|
1496
1541
|
}
|
|
1497
|
-
|
|
1542
|
+
this.onmessage?.(message);
|
|
1498
1543
|
};
|
|
1499
1544
|
});
|
|
1500
1545
|
}
|
|
@@ -1516,13 +1561,11 @@ var SSEClientTransport = class {
|
|
|
1516
1561
|
}) !== "AUTHORIZED") throw new UnauthorizedError("Failed to authorize");
|
|
1517
1562
|
}
|
|
1518
1563
|
async close() {
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
(_c = this.onclose) === null || _c === void 0 || _c.call(this);
|
|
1564
|
+
this._abortController?.abort();
|
|
1565
|
+
this._eventSource?.close();
|
|
1566
|
+
this.onclose?.();
|
|
1523
1567
|
}
|
|
1524
1568
|
async send(message) {
|
|
1525
|
-
var _a, _b, _c, _d;
|
|
1526
1569
|
if (!this._endpoint) throw new Error("Not connected");
|
|
1527
1570
|
try {
|
|
1528
1571
|
const headers = await this._commonHeaders();
|
|
@@ -1532,9 +1575,9 @@ var SSEClientTransport = class {
|
|
|
1532
1575
|
method: "POST",
|
|
1533
1576
|
headers,
|
|
1534
1577
|
body: JSON.stringify(message),
|
|
1535
|
-
signal:
|
|
1578
|
+
signal: this._abortController?.signal
|
|
1536
1579
|
};
|
|
1537
|
-
const response = await (
|
|
1580
|
+
const response = await (this._fetch ?? fetch)(this._endpoint, init);
|
|
1538
1581
|
if (!response.ok) {
|
|
1539
1582
|
const text = await response.text().catch(() => null);
|
|
1540
1583
|
if (response.status === 401 && this._authProvider) {
|
|
@@ -1551,9 +1594,9 @@ var SSEClientTransport = class {
|
|
|
1551
1594
|
}
|
|
1552
1595
|
throw new Error(`Error POSTing to endpoint (HTTP ${response.status}): ${text}`);
|
|
1553
1596
|
}
|
|
1554
|
-
await
|
|
1597
|
+
await response.body?.cancel();
|
|
1555
1598
|
} catch (error) {
|
|
1556
|
-
|
|
1599
|
+
this.onerror?.(error);
|
|
1557
1600
|
throw error;
|
|
1558
1601
|
}
|
|
1559
1602
|
}
|
|
@@ -1588,7 +1631,7 @@ var EventSourceParserStream = class extends TransformStream {
|
|
|
1588
1631
|
};
|
|
1589
1632
|
|
|
1590
1633
|
//#endregion
|
|
1591
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
1634
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/client/streamableHttp.js
|
|
1592
1635
|
const DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS = {
|
|
1593
1636
|
initialReconnectionDelay: 1e3,
|
|
1594
1637
|
maxReconnectionDelay: 3e4,
|
|
@@ -1608,20 +1651,18 @@ var StreamableHTTPError = class extends Error {
|
|
|
1608
1651
|
*/
|
|
1609
1652
|
var StreamableHTTPClientTransport = class {
|
|
1610
1653
|
constructor(url$1, opts) {
|
|
1611
|
-
var _a;
|
|
1612
1654
|
this._hasCompletedAuthFlow = false;
|
|
1613
1655
|
this._url = url$1;
|
|
1614
1656
|
this._resourceMetadataUrl = void 0;
|
|
1615
1657
|
this._scope = void 0;
|
|
1616
|
-
this._requestInit = opts
|
|
1617
|
-
this._authProvider = opts
|
|
1618
|
-
this._fetch = opts
|
|
1619
|
-
this._fetchWithInit = createFetchWithInit(opts
|
|
1620
|
-
this._sessionId = opts
|
|
1621
|
-
this._reconnectionOptions =
|
|
1658
|
+
this._requestInit = opts?.requestInit;
|
|
1659
|
+
this._authProvider = opts?.authProvider;
|
|
1660
|
+
this._fetch = opts?.fetch;
|
|
1661
|
+
this._fetchWithInit = createFetchWithInit(opts?.fetch, opts?.requestInit);
|
|
1662
|
+
this._sessionId = opts?.sessionId;
|
|
1663
|
+
this._reconnectionOptions = opts?.reconnectionOptions ?? DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS;
|
|
1622
1664
|
}
|
|
1623
1665
|
async _authThenStart() {
|
|
1624
|
-
var _a;
|
|
1625
1666
|
if (!this._authProvider) throw new UnauthorizedError("No auth provider");
|
|
1626
1667
|
let result;
|
|
1627
1668
|
try {
|
|
@@ -1632,14 +1673,13 @@ var StreamableHTTPClientTransport = class {
|
|
|
1632
1673
|
fetchFn: this._fetchWithInit
|
|
1633
1674
|
});
|
|
1634
1675
|
} catch (error) {
|
|
1635
|
-
|
|
1676
|
+
this.onerror?.(error);
|
|
1636
1677
|
throw error;
|
|
1637
1678
|
}
|
|
1638
1679
|
if (result !== "AUTHORIZED") throw new UnauthorizedError();
|
|
1639
1680
|
return await this._startOrAuthSse({ resumptionToken: void 0 });
|
|
1640
1681
|
}
|
|
1641
1682
|
async _commonHeaders() {
|
|
1642
|
-
var _a;
|
|
1643
1683
|
const headers = {};
|
|
1644
1684
|
if (this._authProvider) {
|
|
1645
1685
|
const tokens = await this._authProvider.tokens();
|
|
@@ -1647,33 +1687,32 @@ var StreamableHTTPClientTransport = class {
|
|
|
1647
1687
|
}
|
|
1648
1688
|
if (this._sessionId) headers["mcp-session-id"] = this._sessionId;
|
|
1649
1689
|
if (this._protocolVersion) headers["mcp-protocol-version"] = this._protocolVersion;
|
|
1650
|
-
const extraHeaders = normalizeHeaders(
|
|
1690
|
+
const extraHeaders = normalizeHeaders(this._requestInit?.headers);
|
|
1651
1691
|
return new Headers({
|
|
1652
1692
|
...headers,
|
|
1653
1693
|
...extraHeaders
|
|
1654
1694
|
});
|
|
1655
1695
|
}
|
|
1656
1696
|
async _startOrAuthSse(options) {
|
|
1657
|
-
var _a, _b, _c, _d;
|
|
1658
1697
|
const { resumptionToken } = options;
|
|
1659
1698
|
try {
|
|
1660
1699
|
const headers = await this._commonHeaders();
|
|
1661
1700
|
headers.set("Accept", "text/event-stream");
|
|
1662
1701
|
if (resumptionToken) headers.set("last-event-id", resumptionToken);
|
|
1663
|
-
const response = await (
|
|
1702
|
+
const response = await (this._fetch ?? fetch)(this._url, {
|
|
1664
1703
|
method: "GET",
|
|
1665
1704
|
headers,
|
|
1666
|
-
signal:
|
|
1705
|
+
signal: this._abortController?.signal
|
|
1667
1706
|
});
|
|
1668
1707
|
if (!response.ok) {
|
|
1669
|
-
await
|
|
1708
|
+
await response.body?.cancel();
|
|
1670
1709
|
if (response.status === 401 && this._authProvider) return await this._authThenStart();
|
|
1671
1710
|
if (response.status === 405) return;
|
|
1672
1711
|
throw new StreamableHTTPError(response.status, `Failed to open SSE stream: ${response.statusText}`);
|
|
1673
1712
|
}
|
|
1674
1713
|
this._handleSseStream(response.body, options, true);
|
|
1675
1714
|
} catch (error) {
|
|
1676
|
-
|
|
1715
|
+
this.onerror?.(error);
|
|
1677
1716
|
throw error;
|
|
1678
1717
|
}
|
|
1679
1718
|
}
|
|
@@ -1697,17 +1736,15 @@ var StreamableHTTPClientTransport = class {
|
|
|
1697
1736
|
* @param attemptCount Current reconnection attempt count for this specific stream
|
|
1698
1737
|
*/
|
|
1699
1738
|
_scheduleReconnection(options, attemptCount = 0) {
|
|
1700
|
-
var _a;
|
|
1701
1739
|
const maxRetries = this._reconnectionOptions.maxRetries;
|
|
1702
1740
|
if (attemptCount >= maxRetries) {
|
|
1703
|
-
|
|
1741
|
+
this.onerror?.(/* @__PURE__ */ new Error(`Maximum reconnection attempts (${maxRetries}) exceeded.`));
|
|
1704
1742
|
return;
|
|
1705
1743
|
}
|
|
1706
1744
|
const delay = this._getNextReconnectionDelay(attemptCount);
|
|
1707
1745
|
this._reconnectionTimeout = setTimeout(() => {
|
|
1708
1746
|
this._startOrAuthSse(options).catch((error) => {
|
|
1709
|
-
|
|
1710
|
-
(_a$1 = this.onerror) === null || _a$1 === void 0 || _a$1.call(this, /* @__PURE__ */ new Error(`Failed to reconnect SSE stream: ${error instanceof Error ? error.message : String(error)}`));
|
|
1747
|
+
this.onerror?.(/* @__PURE__ */ new Error(`Failed to reconnect SSE stream: ${error instanceof Error ? error.message : String(error)}`));
|
|
1711
1748
|
this._scheduleReconnection(options, attemptCount + 1);
|
|
1712
1749
|
});
|
|
1713
1750
|
}, delay);
|
|
@@ -1719,7 +1756,6 @@ var StreamableHTTPClientTransport = class {
|
|
|
1719
1756
|
let hasPrimingEvent = false;
|
|
1720
1757
|
let receivedResponse = false;
|
|
1721
1758
|
const processStream = async () => {
|
|
1722
|
-
var _a, _b, _c, _d;
|
|
1723
1759
|
try {
|
|
1724
1760
|
const reader = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream({ onRetry: (retryMs) => {
|
|
1725
1761
|
this._serverRetryMs = retryMs;
|
|
@@ -1730,18 +1766,18 @@ var StreamableHTTPClientTransport = class {
|
|
|
1730
1766
|
if (event.id) {
|
|
1731
1767
|
lastEventId = event.id;
|
|
1732
1768
|
hasPrimingEvent = true;
|
|
1733
|
-
onresumptiontoken
|
|
1769
|
+
onresumptiontoken?.(event.id);
|
|
1734
1770
|
}
|
|
1735
1771
|
if (!event.data) continue;
|
|
1736
1772
|
if (!event.event || event.event === "message") try {
|
|
1737
1773
|
const message = JSONRPCMessageSchema.parse(JSON.parse(event.data));
|
|
1738
|
-
if (
|
|
1774
|
+
if (isJSONRPCResultResponse(message)) {
|
|
1739
1775
|
receivedResponse = true;
|
|
1740
1776
|
if (replayMessageId !== void 0) message.id = replayMessageId;
|
|
1741
1777
|
}
|
|
1742
|
-
|
|
1778
|
+
this.onmessage?.(message);
|
|
1743
1779
|
} catch (error) {
|
|
1744
|
-
|
|
1780
|
+
this.onerror?.(error);
|
|
1745
1781
|
}
|
|
1746
1782
|
}
|
|
1747
1783
|
if ((isReconnectable || hasPrimingEvent) && !receivedResponse && this._abortController && !this._abortController.signal.aborted) this._scheduleReconnection({
|
|
@@ -1750,7 +1786,7 @@ var StreamableHTTPClientTransport = class {
|
|
|
1750
1786
|
replayMessageId
|
|
1751
1787
|
}, 0);
|
|
1752
1788
|
} catch (error) {
|
|
1753
|
-
|
|
1789
|
+
this.onerror?.(/* @__PURE__ */ new Error(`SSE stream disconnected: ${error}`));
|
|
1754
1790
|
if ((isReconnectable || hasPrimingEvent) && !receivedResponse && this._abortController && !this._abortController.signal.aborted) try {
|
|
1755
1791
|
this._scheduleReconnection({
|
|
1756
1792
|
resumptionToken: lastEventId,
|
|
@@ -1758,7 +1794,7 @@ var StreamableHTTPClientTransport = class {
|
|
|
1758
1794
|
replayMessageId
|
|
1759
1795
|
}, 0);
|
|
1760
1796
|
} catch (error$1) {
|
|
1761
|
-
|
|
1797
|
+
this.onerror?.(/* @__PURE__ */ new Error(`Failed to reconnect: ${error$1 instanceof Error ? error$1.message : String(error$1)}`));
|
|
1762
1798
|
}
|
|
1763
1799
|
}
|
|
1764
1800
|
};
|
|
@@ -1782,26 +1818,21 @@ var StreamableHTTPClientTransport = class {
|
|
|
1782
1818
|
}) !== "AUTHORIZED") throw new UnauthorizedError("Failed to authorize");
|
|
1783
1819
|
}
|
|
1784
1820
|
async close() {
|
|
1785
|
-
var _a, _b;
|
|
1786
1821
|
if (this._reconnectionTimeout) {
|
|
1787
1822
|
clearTimeout(this._reconnectionTimeout);
|
|
1788
1823
|
this._reconnectionTimeout = void 0;
|
|
1789
1824
|
}
|
|
1790
|
-
|
|
1791
|
-
|
|
1825
|
+
this._abortController?.abort();
|
|
1826
|
+
this.onclose?.();
|
|
1792
1827
|
}
|
|
1793
1828
|
async send(message, options) {
|
|
1794
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
1795
1829
|
try {
|
|
1796
1830
|
const { resumptionToken, onresumptiontoken } = options || {};
|
|
1797
1831
|
if (resumptionToken) {
|
|
1798
1832
|
this._startOrAuthSse({
|
|
1799
1833
|
resumptionToken,
|
|
1800
1834
|
replayMessageId: isJSONRPCRequest(message) ? message.id : void 0
|
|
1801
|
-
}).catch((err) =>
|
|
1802
|
-
var _a$1;
|
|
1803
|
-
return (_a$1 = this.onerror) === null || _a$1 === void 0 ? void 0 : _a$1.call(this, err);
|
|
1804
|
-
});
|
|
1835
|
+
}).catch((err) => this.onerror?.(err));
|
|
1805
1836
|
return;
|
|
1806
1837
|
}
|
|
1807
1838
|
const headers = await this._commonHeaders();
|
|
@@ -1812,9 +1843,9 @@ var StreamableHTTPClientTransport = class {
|
|
|
1812
1843
|
method: "POST",
|
|
1813
1844
|
headers,
|
|
1814
1845
|
body: JSON.stringify(message),
|
|
1815
|
-
signal:
|
|
1846
|
+
signal: this._abortController?.signal
|
|
1816
1847
|
};
|
|
1817
|
-
const response = await (
|
|
1848
|
+
const response = await (this._fetch ?? fetch)(this._url, init);
|
|
1818
1849
|
const sessionId = response.headers.get("mcp-session-id");
|
|
1819
1850
|
if (sessionId) this._sessionId = sessionId;
|
|
1820
1851
|
if (!response.ok) {
|
|
@@ -1840,7 +1871,7 @@ var StreamableHTTPClientTransport = class {
|
|
|
1840
1871
|
if (this._lastUpscopingHeader === wwwAuthHeader) throw new StreamableHTTPError(403, "Server returned 403 after trying upscoping");
|
|
1841
1872
|
if (scope) this._scope = scope;
|
|
1842
1873
|
if (resourceMetadataUrl) this._resourceMetadataUrl = resourceMetadataUrl;
|
|
1843
|
-
this._lastUpscopingHeader = wwwAuthHeader
|
|
1874
|
+
this._lastUpscopingHeader = wwwAuthHeader ?? void 0;
|
|
1844
1875
|
if (await auth(this._authProvider, {
|
|
1845
1876
|
serverUrl: this._url,
|
|
1846
1877
|
resourceMetadataUrl: this._resourceMetadataUrl,
|
|
@@ -1855,27 +1886,24 @@ var StreamableHTTPClientTransport = class {
|
|
|
1855
1886
|
this._hasCompletedAuthFlow = false;
|
|
1856
1887
|
this._lastUpscopingHeader = void 0;
|
|
1857
1888
|
if (response.status === 202) {
|
|
1858
|
-
await
|
|
1859
|
-
if (isInitializedNotification(message)) this._startOrAuthSse({ resumptionToken: void 0 }).catch((err) =>
|
|
1860
|
-
var _a$1;
|
|
1861
|
-
return (_a$1 = this.onerror) === null || _a$1 === void 0 ? void 0 : _a$1.call(this, err);
|
|
1862
|
-
});
|
|
1889
|
+
await response.body?.cancel();
|
|
1890
|
+
if (isInitializedNotification(message)) this._startOrAuthSse({ resumptionToken: void 0 }).catch((err) => this.onerror?.(err));
|
|
1863
1891
|
return;
|
|
1864
1892
|
}
|
|
1865
1893
|
const hasRequests = (Array.isArray(message) ? message : [message]).filter((msg) => "method" in msg && "id" in msg && msg.id !== void 0).length > 0;
|
|
1866
1894
|
const contentType = response.headers.get("content-type");
|
|
1867
|
-
if (hasRequests) if (contentType
|
|
1868
|
-
else if (contentType
|
|
1895
|
+
if (hasRequests) if (contentType?.includes("text/event-stream")) this._handleSseStream(response.body, { onresumptiontoken }, false);
|
|
1896
|
+
else if (contentType?.includes("application/json")) {
|
|
1869
1897
|
const data = await response.json();
|
|
1870
1898
|
const responseMessages = Array.isArray(data) ? data.map((msg) => JSONRPCMessageSchema.parse(msg)) : [JSONRPCMessageSchema.parse(data)];
|
|
1871
|
-
for (const msg of responseMessages)
|
|
1899
|
+
for (const msg of responseMessages) this.onmessage?.(msg);
|
|
1872
1900
|
} else {
|
|
1873
|
-
await
|
|
1901
|
+
await response.body?.cancel();
|
|
1874
1902
|
throw new StreamableHTTPError(-1, `Unexpected content type: ${contentType}`);
|
|
1875
1903
|
}
|
|
1876
|
-
else await
|
|
1904
|
+
else await response.body?.cancel();
|
|
1877
1905
|
} catch (error) {
|
|
1878
|
-
|
|
1906
|
+
this.onerror?.(error);
|
|
1879
1907
|
throw error;
|
|
1880
1908
|
}
|
|
1881
1909
|
}
|
|
@@ -1894,7 +1922,6 @@ var StreamableHTTPClientTransport = class {
|
|
|
1894
1922
|
* the server does not allow clients to terminate sessions.
|
|
1895
1923
|
*/
|
|
1896
1924
|
async terminateSession() {
|
|
1897
|
-
var _a, _b, _c, _d;
|
|
1898
1925
|
if (!this._sessionId) return;
|
|
1899
1926
|
try {
|
|
1900
1927
|
const headers = await this._commonHeaders();
|
|
@@ -1902,14 +1929,14 @@ var StreamableHTTPClientTransport = class {
|
|
|
1902
1929
|
...this._requestInit,
|
|
1903
1930
|
method: "DELETE",
|
|
1904
1931
|
headers,
|
|
1905
|
-
signal:
|
|
1932
|
+
signal: this._abortController?.signal
|
|
1906
1933
|
};
|
|
1907
|
-
const response = await (
|
|
1908
|
-
await
|
|
1934
|
+
const response = await (this._fetch ?? fetch)(this._url, init);
|
|
1935
|
+
await response.body?.cancel();
|
|
1909
1936
|
if (!response.ok && response.status !== 405) throw new StreamableHTTPError(response.status, `Failed to terminate session: ${response.statusText}`);
|
|
1910
1937
|
this._sessionId = void 0;
|
|
1911
1938
|
} catch (error) {
|
|
1912
|
-
|
|
1939
|
+
this.onerror?.(error);
|
|
1913
1940
|
throw error;
|
|
1914
1941
|
}
|
|
1915
1942
|
}
|
|
@@ -1929,13 +1956,13 @@ var StreamableHTTPClientTransport = class {
|
|
|
1929
1956
|
async resumeStream(lastEventId, options) {
|
|
1930
1957
|
await this._startOrAuthSse({
|
|
1931
1958
|
resumptionToken: lastEventId,
|
|
1932
|
-
onresumptiontoken: options
|
|
1959
|
+
onresumptiontoken: options?.onresumptiontoken
|
|
1933
1960
|
});
|
|
1934
1961
|
}
|
|
1935
1962
|
};
|
|
1936
1963
|
|
|
1937
1964
|
//#endregion
|
|
1938
|
-
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.
|
|
1965
|
+
//#region node_modules/.pnpm/@modelcontextprotocol+sdk@1.27.1_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/server/stdio.js
|
|
1939
1966
|
/**
|
|
1940
1967
|
* Server transport for stdio: this communicates with an MCP client by reading from the current process' stdin and writing to stdout.
|
|
1941
1968
|
*
|
|
@@ -1952,8 +1979,7 @@ var StdioServerTransport = class {
|
|
|
1952
1979
|
this.processReadBuffer();
|
|
1953
1980
|
};
|
|
1954
1981
|
this._onerror = (error) => {
|
|
1955
|
-
|
|
1956
|
-
(_a = this.onerror) === null || _a === void 0 || _a.call(this, error);
|
|
1982
|
+
this.onerror?.(error);
|
|
1957
1983
|
};
|
|
1958
1984
|
}
|
|
1959
1985
|
/**
|
|
@@ -1966,22 +1992,20 @@ var StdioServerTransport = class {
|
|
|
1966
1992
|
this._stdin.on("error", this._onerror);
|
|
1967
1993
|
}
|
|
1968
1994
|
processReadBuffer() {
|
|
1969
|
-
var _a, _b;
|
|
1970
1995
|
while (true) try {
|
|
1971
1996
|
const message = this._readBuffer.readMessage();
|
|
1972
1997
|
if (message === null) break;
|
|
1973
|
-
|
|
1998
|
+
this.onmessage?.(message);
|
|
1974
1999
|
} catch (error) {
|
|
1975
|
-
|
|
2000
|
+
this.onerror?.(error);
|
|
1976
2001
|
}
|
|
1977
2002
|
}
|
|
1978
2003
|
async close() {
|
|
1979
|
-
var _a;
|
|
1980
2004
|
this._stdin.off("data", this._ondata);
|
|
1981
2005
|
this._stdin.off("error", this._onerror);
|
|
1982
2006
|
if (this._stdin.listenerCount("data") === 0) this._stdin.pause();
|
|
1983
2007
|
this._readBuffer.clear();
|
|
1984
|
-
|
|
2008
|
+
this.onclose?.();
|
|
1985
2009
|
}
|
|
1986
2010
|
send(message) {
|
|
1987
2011
|
return new Promise((resolve) => {
|