@workglow/util 0.0.110 → 0.0.111

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/browser.js CHANGED
@@ -2569,6 +2569,450 @@ function base64ToBuf(b64) {
2569
2569
  }
2570
2570
  return buf;
2571
2571
  }
2572
+ // src/mcp/McpAuthTypes.ts
2573
+ var mcpAuthTypes = [
2574
+ "none",
2575
+ "bearer",
2576
+ "client_credentials",
2577
+ "private_key_jwt",
2578
+ "static_private_key_jwt",
2579
+ "authorization_code"
2580
+ ];
2581
+ var mcpAuthConfigSchema = {
2582
+ auth_type: {
2583
+ type: "string",
2584
+ enum: mcpAuthTypes,
2585
+ title: "Auth Type",
2586
+ description: "Authentication method for connecting to the MCP server",
2587
+ default: "none"
2588
+ },
2589
+ auth_token: {
2590
+ type: "string",
2591
+ format: "credential",
2592
+ title: "Bearer Token",
2593
+ description: "Static bearer token or API key (for bearer auth)"
2594
+ },
2595
+ auth_client_id: {
2596
+ type: "string",
2597
+ title: "Client ID",
2598
+ description: "OAuth client ID (for OAuth auth types)"
2599
+ },
2600
+ auth_client_secret: {
2601
+ type: "string",
2602
+ format: "credential",
2603
+ title: "Client Secret",
2604
+ description: "OAuth client secret (for client_credentials auth)"
2605
+ },
2606
+ auth_private_key: {
2607
+ type: "string",
2608
+ format: "credential",
2609
+ title: "Private Key",
2610
+ description: "PEM or JWK private key (for private_key_jwt auth)"
2611
+ },
2612
+ auth_algorithm: {
2613
+ type: "string",
2614
+ title: "Algorithm",
2615
+ description: "JWT signing algorithm, e.g. RS256, ES256 (for private_key_jwt auth)"
2616
+ },
2617
+ auth_jwt_bearer_assertion: {
2618
+ type: "string",
2619
+ format: "credential",
2620
+ title: "JWT Assertion",
2621
+ description: "Pre-built JWT assertion (for static_private_key_jwt auth)"
2622
+ },
2623
+ auth_redirect_url: {
2624
+ type: "string",
2625
+ format: "uri",
2626
+ title: "Redirect URL",
2627
+ description: "OAuth redirect URL (for authorization_code auth)"
2628
+ },
2629
+ auth_scope: {
2630
+ type: "string",
2631
+ title: "Scope",
2632
+ description: "OAuth scope (space-separated)"
2633
+ },
2634
+ auth_client_name: {
2635
+ type: "string",
2636
+ title: "Client Name",
2637
+ description: "Optional OAuth client display name"
2638
+ },
2639
+ auth_jwt_lifetime_seconds: {
2640
+ type: "number",
2641
+ title: "JWT Lifetime",
2642
+ description: "JWT lifetime in seconds (default: 300)",
2643
+ minimum: 1
2644
+ }
2645
+ };
2646
+ function isMcpAuthType(value) {
2647
+ return typeof value === "string" && mcpAuthTypes.includes(value);
2648
+ }
2649
+ function asNonEmptyString(value) {
2650
+ if (typeof value !== "string")
2651
+ return;
2652
+ const trimmed = value.trim();
2653
+ return trimmed === "" ? undefined : trimmed;
2654
+ }
2655
+ function asNumber(value) {
2656
+ return typeof value === "number" ? value : undefined;
2657
+ }
2658
+ function buildAuthConfig(flat) {
2659
+ const rawAuthType = flat.auth_type;
2660
+ if (!isMcpAuthType(rawAuthType) || rawAuthType === "none") {
2661
+ return;
2662
+ }
2663
+ const authType = rawAuthType;
2664
+ switch (authType) {
2665
+ case "bearer": {
2666
+ const token = asNonEmptyString(flat.auth_token);
2667
+ if (!token)
2668
+ return;
2669
+ return { type: "bearer", token };
2670
+ }
2671
+ case "client_credentials": {
2672
+ const client_id = asNonEmptyString(flat.auth_client_id);
2673
+ const client_secret = asNonEmptyString(flat.auth_client_secret);
2674
+ if (!client_id || !client_secret)
2675
+ return;
2676
+ return {
2677
+ type: "client_credentials",
2678
+ client_id,
2679
+ client_secret,
2680
+ client_name: asNonEmptyString(flat.auth_client_name),
2681
+ scope: asNonEmptyString(flat.auth_scope)
2682
+ };
2683
+ }
2684
+ case "private_key_jwt": {
2685
+ const client_id = asNonEmptyString(flat.auth_client_id);
2686
+ const private_key = asNonEmptyString(flat.auth_private_key);
2687
+ const algorithm = asNonEmptyString(flat.auth_algorithm);
2688
+ if (!client_id || !private_key || !algorithm)
2689
+ return;
2690
+ return {
2691
+ type: "private_key_jwt",
2692
+ client_id,
2693
+ private_key,
2694
+ algorithm,
2695
+ client_name: asNonEmptyString(flat.auth_client_name),
2696
+ jwt_lifetime_seconds: asNumber(flat.auth_jwt_lifetime_seconds),
2697
+ scope: asNonEmptyString(flat.auth_scope)
2698
+ };
2699
+ }
2700
+ case "static_private_key_jwt": {
2701
+ const client_id = asNonEmptyString(flat.auth_client_id);
2702
+ const jwt_bearer_assertion = asNonEmptyString(flat.auth_jwt_bearer_assertion);
2703
+ if (!client_id || !jwt_bearer_assertion)
2704
+ return;
2705
+ return {
2706
+ type: "static_private_key_jwt",
2707
+ client_id,
2708
+ jwt_bearer_assertion,
2709
+ client_name: asNonEmptyString(flat.auth_client_name),
2710
+ scope: asNonEmptyString(flat.auth_scope)
2711
+ };
2712
+ }
2713
+ case "authorization_code": {
2714
+ const client_id = asNonEmptyString(flat.auth_client_id);
2715
+ const redirect_url = asNonEmptyString(flat.auth_redirect_url);
2716
+ if (!client_id || !redirect_url)
2717
+ return;
2718
+ return {
2719
+ type: "authorization_code",
2720
+ client_id,
2721
+ client_secret: asNonEmptyString(flat.auth_client_secret),
2722
+ redirect_url,
2723
+ scope: asNonEmptyString(flat.auth_scope)
2724
+ };
2725
+ }
2726
+ default:
2727
+ return;
2728
+ }
2729
+ }
2730
+ // src/mcp/McpAuthProvider.ts
2731
+ import {
2732
+ ClientCredentialsProvider,
2733
+ PrivateKeyJwtProvider,
2734
+ StaticPrivateKeyJwtProvider,
2735
+ createPrivateKeyJwtAuth
2736
+ } from "@modelcontextprotocol/sdk/client/auth-extensions.js";
2737
+ function normalizeServerUrl(serverUrl) {
2738
+ try {
2739
+ const u = new URL(serverUrl);
2740
+ return u.origin + u.pathname.replace(/\/+$/, "");
2741
+ } catch {
2742
+ return serverUrl;
2743
+ }
2744
+ }
2745
+ function storeKey(serverUrl, suffix) {
2746
+ return `mcp:oauth:${normalizeServerUrl(serverUrl)}:${suffix}`;
2747
+ }
2748
+
2749
+ class CredentialStoreOAuthProvider {
2750
+ store;
2751
+ serverUrl;
2752
+ _clientMetadata;
2753
+ _redirectUrl;
2754
+ _initialClientInfo;
2755
+ prepareTokenRequest;
2756
+ addClientAuthentication;
2757
+ constructor(options) {
2758
+ this.store = options.store;
2759
+ this.serverUrl = options.serverUrl;
2760
+ this._clientMetadata = options.clientMetadata;
2761
+ this._redirectUrl = options.redirectUrl;
2762
+ this._initialClientInfo = options.initialClientInfo;
2763
+ if (options.prepareTokenRequest) {
2764
+ this.prepareTokenRequest = options.prepareTokenRequest;
2765
+ }
2766
+ if (options.addClientAuthentication) {
2767
+ this.addClientAuthentication = options.addClientAuthentication;
2768
+ }
2769
+ }
2770
+ get redirectUrl() {
2771
+ return this._redirectUrl;
2772
+ }
2773
+ get clientMetadata() {
2774
+ return this._clientMetadata;
2775
+ }
2776
+ async clientInformation() {
2777
+ const raw = await this.store.get(storeKey(this.serverUrl, "client_info"));
2778
+ if (!raw)
2779
+ return this._initialClientInfo;
2780
+ return JSON.parse(raw);
2781
+ }
2782
+ async saveClientInformation(info) {
2783
+ await this.store.put(storeKey(this.serverUrl, "client_info"), JSON.stringify(info));
2784
+ }
2785
+ async tokens() {
2786
+ const raw = await this.store.get(storeKey(this.serverUrl, "tokens"));
2787
+ if (!raw)
2788
+ return;
2789
+ return JSON.parse(raw);
2790
+ }
2791
+ async saveTokens(tokens) {
2792
+ const expiresAt = tokens.expires_in != null ? new Date(Date.now() + tokens.expires_in * 1000) : undefined;
2793
+ await this.store.put(storeKey(this.serverUrl, "tokens"), JSON.stringify(tokens), {
2794
+ expiresAt
2795
+ });
2796
+ }
2797
+ async redirectToAuthorization(authorizationUrl) {
2798
+ throw new Error(`MCP OAuth authorization required. ` + `Open this URL to authorize: ${authorizationUrl.toString()}`);
2799
+ }
2800
+ async saveCodeVerifier(codeVerifier) {
2801
+ await this.store.put(storeKey(this.serverUrl, "code_verifier"), codeVerifier);
2802
+ }
2803
+ async codeVerifier() {
2804
+ const v = await this.store.get(storeKey(this.serverUrl, "code_verifier"));
2805
+ if (!v)
2806
+ throw new Error("No code verifier saved for this session");
2807
+ return v;
2808
+ }
2809
+ async saveDiscoveryState(state) {
2810
+ await this.store.put(storeKey(this.serverUrl, "discovery"), JSON.stringify(state));
2811
+ }
2812
+ async discoveryState() {
2813
+ const raw = await this.store.get(storeKey(this.serverUrl, "discovery"));
2814
+ if (!raw)
2815
+ return;
2816
+ return JSON.parse(raw);
2817
+ }
2818
+ async invalidateCredentials(scope) {
2819
+ const deleteKey = async (suffix) => {
2820
+ await this.store.delete(storeKey(this.serverUrl, suffix));
2821
+ };
2822
+ switch (scope) {
2823
+ case "all":
2824
+ await deleteKey("tokens");
2825
+ await deleteKey("client_info");
2826
+ await deleteKey("code_verifier");
2827
+ await deleteKey("discovery");
2828
+ break;
2829
+ case "client":
2830
+ await deleteKey("client_info");
2831
+ break;
2832
+ case "tokens":
2833
+ await deleteKey("tokens");
2834
+ break;
2835
+ case "verifier":
2836
+ await deleteKey("code_verifier");
2837
+ break;
2838
+ case "discovery":
2839
+ await deleteKey("discovery");
2840
+ break;
2841
+ }
2842
+ }
2843
+ }
2844
+ function createAuthProvider(auth, serverUrl, credentialStore) {
2845
+ switch (auth.type) {
2846
+ case "none":
2847
+ case "bearer":
2848
+ return;
2849
+ case "client_credentials": {
2850
+ if (!credentialStore) {
2851
+ return new ClientCredentialsProvider({
2852
+ clientId: auth.client_id,
2853
+ clientSecret: auth.client_secret,
2854
+ clientName: auth.client_name,
2855
+ scope: auth.scope
2856
+ });
2857
+ }
2858
+ const prepareTokenRequest = (scope) => {
2859
+ const params = new URLSearchParams({ grant_type: "client_credentials" });
2860
+ const effectiveScope = scope ?? auth.scope;
2861
+ if (effectiveScope)
2862
+ params.set("scope", effectiveScope);
2863
+ return params;
2864
+ };
2865
+ return new CredentialStoreOAuthProvider({
2866
+ store: credentialStore,
2867
+ serverUrl,
2868
+ clientMetadata: {
2869
+ redirect_uris: [],
2870
+ grant_types: ["client_credentials"],
2871
+ token_endpoint_auth_method: "client_secret_basic",
2872
+ client_name: auth.client_name
2873
+ },
2874
+ initialClientInfo: {
2875
+ client_id: auth.client_id,
2876
+ client_secret: auth.client_secret
2877
+ },
2878
+ prepareTokenRequest
2879
+ });
2880
+ }
2881
+ case "private_key_jwt": {
2882
+ if (!credentialStore) {
2883
+ return new PrivateKeyJwtProvider({
2884
+ clientId: auth.client_id,
2885
+ privateKey: auth.private_key,
2886
+ algorithm: auth.algorithm,
2887
+ clientName: auth.client_name,
2888
+ jwtLifetimeSeconds: auth.jwt_lifetime_seconds,
2889
+ scope: auth.scope
2890
+ });
2891
+ }
2892
+ const addClientAuth = createPrivateKeyJwtAuth({
2893
+ issuer: auth.client_id,
2894
+ subject: auth.client_id,
2895
+ privateKey: auth.private_key,
2896
+ alg: auth.algorithm,
2897
+ lifetimeSeconds: auth.jwt_lifetime_seconds
2898
+ });
2899
+ const prepareTokenRequest = (scope) => {
2900
+ const params = new URLSearchParams({ grant_type: "client_credentials" });
2901
+ const effectiveScope = scope ?? auth.scope;
2902
+ if (effectiveScope)
2903
+ params.set("scope", effectiveScope);
2904
+ return params;
2905
+ };
2906
+ return new CredentialStoreOAuthProvider({
2907
+ store: credentialStore,
2908
+ serverUrl,
2909
+ clientMetadata: {
2910
+ redirect_uris: [],
2911
+ grant_types: ["client_credentials"],
2912
+ token_endpoint_auth_method: "private_key_jwt",
2913
+ client_name: auth.client_name
2914
+ },
2915
+ initialClientInfo: { client_id: auth.client_id },
2916
+ prepareTokenRequest,
2917
+ addClientAuthentication: addClientAuth
2918
+ });
2919
+ }
2920
+ case "static_private_key_jwt": {
2921
+ if (!credentialStore) {
2922
+ return new StaticPrivateKeyJwtProvider({
2923
+ clientId: auth.client_id,
2924
+ jwtBearerAssertion: auth.jwt_bearer_assertion,
2925
+ clientName: auth.client_name,
2926
+ scope: auth.scope
2927
+ });
2928
+ }
2929
+ const assertion = auth.jwt_bearer_assertion;
2930
+ const addClientAuth = (_headers, params) => {
2931
+ params.set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
2932
+ params.set("client_assertion", assertion);
2933
+ };
2934
+ const prepareTokenRequest = (scope) => {
2935
+ const params = new URLSearchParams({ grant_type: "client_credentials" });
2936
+ const effectiveScope = scope ?? auth.scope;
2937
+ if (effectiveScope)
2938
+ params.set("scope", effectiveScope);
2939
+ return params;
2940
+ };
2941
+ return new CredentialStoreOAuthProvider({
2942
+ store: credentialStore,
2943
+ serverUrl,
2944
+ clientMetadata: {
2945
+ redirect_uris: [],
2946
+ grant_types: ["client_credentials"],
2947
+ token_endpoint_auth_method: "private_key_jwt",
2948
+ client_name: auth.client_name
2949
+ },
2950
+ initialClientInfo: { client_id: auth.client_id },
2951
+ prepareTokenRequest,
2952
+ addClientAuthentication: addClientAuth
2953
+ });
2954
+ }
2955
+ case "authorization_code": {
2956
+ if (!credentialStore) {
2957
+ throw new Error("authorization_code auth requires a credential store for token persistence");
2958
+ }
2959
+ return new CredentialStoreOAuthProvider({
2960
+ store: credentialStore,
2961
+ serverUrl,
2962
+ clientMetadata: {
2963
+ redirect_uris: [auth.redirect_url],
2964
+ grant_types: ["authorization_code", "refresh_token"],
2965
+ token_endpoint_auth_method: auth.client_secret ? "client_secret_basic" : "none",
2966
+ scope: auth.scope
2967
+ },
2968
+ initialClientInfo: {
2969
+ client_id: auth.client_id,
2970
+ ...auth.client_secret ? { client_secret: auth.client_secret } : {}
2971
+ },
2972
+ redirectUrl: auth.redirect_url
2973
+ });
2974
+ }
2975
+ default:
2976
+ return;
2977
+ }
2978
+ }
2979
+ async function resolveAuthSecrets(auth, credentialStore) {
2980
+ if (auth.type === "none")
2981
+ return auth;
2982
+ const store = credentialStore ?? getGlobalCredentialStore();
2983
+ async function resolve(value) {
2984
+ if (!value)
2985
+ return value;
2986
+ const resolved = await store.get(value);
2987
+ return resolved ?? value;
2988
+ }
2989
+ switch (auth.type) {
2990
+ case "bearer":
2991
+ return { ...auth, token: await resolve(auth.token) ?? auth.token };
2992
+ case "client_credentials":
2993
+ return {
2994
+ ...auth,
2995
+ client_secret: await resolve(auth.client_secret) ?? auth.client_secret
2996
+ };
2997
+ case "private_key_jwt":
2998
+ return {
2999
+ ...auth,
3000
+ private_key: await resolve(auth.private_key) ?? auth.private_key
3001
+ };
3002
+ case "static_private_key_jwt":
3003
+ return {
3004
+ ...auth,
3005
+ jwt_bearer_assertion: await resolve(auth.jwt_bearer_assertion) ?? auth.jwt_bearer_assertion
3006
+ };
3007
+ case "authorization_code":
3008
+ return {
3009
+ ...auth,
3010
+ client_secret: await resolve(auth.client_secret)
3011
+ };
3012
+ default:
3013
+ return auth;
3014
+ }
3015
+ }
2572
3016
  // src/compress/compress.browser.ts
2573
3017
  async function compress(input, algorithm = "gzip") {
2574
3018
  const sourceBlob = new Blob([typeof input === "string" ? input : new Uint8Array(input)]);
@@ -2585,7 +3029,6 @@ async function decompress(input, algorithm = "gzip") {
2585
3029
  import { Client } from "@modelcontextprotocol/sdk/client";
2586
3030
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
2587
3031
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
2588
- var MCP_PROTOCOL_VERSION_HEADER = "2025-11-25";
2589
3032
  var mcpTransportTypes = ["streamable-http", "sse"];
2590
3033
  var mcpServerConfigSchema = {
2591
3034
  transport: {
@@ -2599,25 +3042,31 @@ var mcpServerConfigSchema = {
2599
3042
  format: "uri",
2600
3043
  title: "Server URL",
2601
3044
  description: "The URL of the MCP server (for streamable-http transport)"
2602
- }
3045
+ },
3046
+ ...mcpAuthConfigSchema
2603
3047
  };
2604
3048
  async function createMcpClient(config, signal) {
2605
3049
  let transport;
3050
+ let auth = config.auth ?? buildAuthConfig({ ...config });
3051
+ if (auth && auth.type !== "none") {
3052
+ auth = await resolveAuthSecrets(auth, getGlobalCredentialStore());
3053
+ }
3054
+ const authProvider = auth && auth.type !== "none" && auth.type !== "bearer" ? createAuthProvider(auth, config.server_url ?? "", getGlobalCredentialStore()) : undefined;
3055
+ const headers = {
3056
+ ...auth?.type === "bearer" ? { Authorization: `Bearer ${auth.token}` } : {}
3057
+ };
3058
+ const requestInit = { headers };
2606
3059
  switch (config.transport) {
2607
3060
  case "sse": {
2608
- const requestInit = {
2609
- headers: { "MCP-Protocol-Version": MCP_PROTOCOL_VERSION_HEADER }
2610
- };
2611
3061
  transport = new SSEClientTransport(new URL(config.server_url), {
3062
+ authProvider,
2612
3063
  requestInit
2613
3064
  });
2614
3065
  break;
2615
3066
  }
2616
3067
  case "streamable-http": {
2617
- const requestInit = {
2618
- headers: { "MCP-Protocol-Version": MCP_PROTOCOL_VERSION_HEADER }
2619
- };
2620
3068
  transport = new StreamableHTTPClientTransport(new URL(config.server_url), {
3069
+ authProvider,
2621
3070
  requestInit
2622
3071
  });
2623
3072
  break;
@@ -2732,6 +3181,7 @@ export {
2732
3181
  setGlobalCredentialStore,
2733
3182
  serialize,
2734
3183
  resolveCredential,
3184
+ resolveAuthSecrets,
2735
3185
  registerInputResolver,
2736
3186
  parsePartialJson,
2737
3187
  parseDataUri,
@@ -2742,6 +3192,8 @@ export {
2742
3192
  mcpTransportTypes,
2743
3193
  mcpServerConfigSchema,
2744
3194
  mcpClientFactory,
3195
+ mcpAuthTypes,
3196
+ mcpAuthConfigSchema,
2745
3197
  makeFingerprint,
2746
3198
  magnitude,
2747
3199
  jaccardSimilarity,
@@ -2763,11 +3215,13 @@ export {
2763
3215
  createTypedArrayFrom,
2764
3216
  createServiceToken,
2765
3217
  createMcpClient,
3218
+ createAuthProvider,
2766
3219
  cosineSimilarity,
2767
3220
  convertImageDataToUseableForm,
2768
3221
  compress,
2769
3222
  compileSchema,
2770
3223
  collectPropertyValues,
3224
+ buildAuthConfig,
2771
3225
  bufToBase64,
2772
3226
  base64ToBuf,
2773
3227
  areSemanticallyCompatible,
@@ -2794,6 +3248,7 @@ export {
2794
3248
  DirectedGraph,
2795
3249
  DirectedAcyclicGraph,
2796
3250
  CycleError,
3251
+ CredentialStoreOAuthProvider,
2797
3252
  Container,
2798
3253
  ConsoleLogger,
2799
3254
  ChainedCredentialStore,
@@ -2801,4 +3256,4 @@ export {
2801
3256
  BaseError
2802
3257
  };
2803
3258
 
2804
- //# debugId=1F7C429734E69AFE64756E2164756E21
3259
+ //# debugId=0564D1DAF5A6667164756E2164756E21