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