@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/node.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.node.ts
2573
3017
  import { promisify } from "util";
2574
3018
  import zlib from "zlib";
@@ -2623,12 +3067,24 @@ var mcpServerConfigSchema = {
2623
3067
  additionalProperties: { type: "string" },
2624
3068
  title: "Environment",
2625
3069
  description: "Environment variables (for stdio transport)"
2626
- }
3070
+ },
3071
+ ...mcpAuthConfigSchema
2627
3072
  };
2628
3073
  async function createMcpClient(config, signal) {
2629
3074
  let transport;
3075
+ let auth = config.auth ?? buildAuthConfig({ ...config });
3076
+ if (auth && auth.type !== "none") {
3077
+ auth = await resolveAuthSecrets(auth, getGlobalCredentialStore());
3078
+ }
3079
+ const authProvider = auth && auth.type !== "none" && auth.type !== "bearer" ? createAuthProvider(auth, config.server_url ?? "", getGlobalCredentialStore()) : undefined;
3080
+ const headers = {
3081
+ ...auth?.type === "bearer" ? { Authorization: `Bearer ${auth.token}` } : {}
3082
+ };
2630
3083
  switch (config.transport) {
2631
3084
  case "stdio":
3085
+ if (auth && auth.type !== "none") {
3086
+ getLogger().warn("MCP auth is not supported for stdio transport; auth config ignored. " + "Use env vars to pass credentials to stdio servers.");
3087
+ }
2632
3088
  transport = new StdioClientTransport({
2633
3089
  command: config.command,
2634
3090
  args: config.args,
@@ -2636,11 +3092,17 @@ async function createMcpClient(config, signal) {
2636
3092
  });
2637
3093
  break;
2638
3094
  case "sse": {
2639
- transport = new SSEClientTransport(new URL(config.server_url));
3095
+ transport = new SSEClientTransport(new URL(config.server_url), {
3096
+ authProvider,
3097
+ requestInit: { headers }
3098
+ });
2640
3099
  break;
2641
3100
  }
2642
3101
  case "streamable-http": {
2643
- transport = new StreamableHTTPClientTransport(new URL(config.server_url));
3102
+ transport = new StreamableHTTPClientTransport(new URL(config.server_url), {
3103
+ authProvider,
3104
+ requestInit: { headers }
3105
+ });
2644
3106
  break;
2645
3107
  }
2646
3108
  default:
@@ -2737,6 +3199,7 @@ export {
2737
3199
  setGlobalCredentialStore,
2738
3200
  serialize,
2739
3201
  resolveCredential,
3202
+ resolveAuthSecrets,
2740
3203
  registerInputResolver,
2741
3204
  parsePartialJson,
2742
3205
  parseDataUri,
@@ -2747,6 +3210,8 @@ export {
2747
3210
  mcpTransportTypes,
2748
3211
  mcpServerConfigSchema,
2749
3212
  mcpClientFactory,
3213
+ mcpAuthTypes,
3214
+ mcpAuthConfigSchema,
2750
3215
  makeFingerprint,
2751
3216
  magnitude,
2752
3217
  jaccardSimilarity,
@@ -2768,11 +3233,13 @@ export {
2768
3233
  createTypedArrayFrom,
2769
3234
  createServiceToken,
2770
3235
  createMcpClient,
3236
+ createAuthProvider,
2771
3237
  cosineSimilarity,
2772
3238
  convertImageDataToUseableForm,
2773
3239
  compress,
2774
3240
  compileSchema,
2775
3241
  collectPropertyValues,
3242
+ buildAuthConfig,
2776
3243
  bufToBase64,
2777
3244
  base64ToBuf,
2778
3245
  areSemanticallyCompatible,
@@ -2799,6 +3266,7 @@ export {
2799
3266
  DirectedGraph,
2800
3267
  DirectedAcyclicGraph,
2801
3268
  CycleError,
3269
+ CredentialStoreOAuthProvider,
2802
3270
  Container,
2803
3271
  ConsoleLogger,
2804
3272
  ChainedCredentialStore,
@@ -2806,4 +3274,4 @@ export {
2806
3274
  BaseError
2807
3275
  };
2808
3276
 
2809
- //# debugId=AF341740F172F37064756E2164756E21
3277
+ //# debugId=31EC196D59B2C57A64756E2164756E21