@axiom-lattice/gateway 2.1.34 → 2.1.35

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/index.mjs CHANGED
@@ -3544,12 +3544,441 @@ function registerMetricsServerConfigRoutes(app2) {
3544
3544
  app2.post("/api/metrics-configs/test-datasources/:datasourceId/meta", testDatasourceMetrics);
3545
3545
  }
3546
3546
 
3547
+ // src/controllers/mcp-configs.ts
3548
+ import {
3549
+ getStoreLattice as getStoreLattice8,
3550
+ mcpManager,
3551
+ toolLatticeManager as toolLatticeManager2
3552
+ } from "@axiom-lattice/core";
3553
+ import { randomUUID as randomUUID5 } from "crypto";
3554
+ function getTenantId3(request) {
3555
+ return request.headers["x-tenant-id"] || "default";
3556
+ }
3557
+ async function getMcpServerConfigList(request, reply) {
3558
+ const tenantId = getTenantId3(request);
3559
+ try {
3560
+ const storeLattice = getStoreLattice8("default", "mcp");
3561
+ const store = storeLattice.store;
3562
+ const configs = await store.getAllConfigs(tenantId);
3563
+ return {
3564
+ success: true,
3565
+ message: "MCP server configurations retrieved successfully",
3566
+ data: {
3567
+ records: configs,
3568
+ total: configs.length
3569
+ }
3570
+ };
3571
+ } catch (error) {
3572
+ console.error("Failed to get MCP server configs:", error);
3573
+ return {
3574
+ success: false,
3575
+ message: "Failed to retrieve MCP server configurations",
3576
+ data: {
3577
+ records: [],
3578
+ total: 0
3579
+ }
3580
+ };
3581
+ }
3582
+ }
3583
+ async function getMcpServerConfig(request, reply) {
3584
+ const tenantId = getTenantId3(request);
3585
+ const { key } = request.params;
3586
+ try {
3587
+ const storeLattice = getStoreLattice8("default", "mcp");
3588
+ const store = storeLattice.store;
3589
+ const config = await store.getConfigByKey(tenantId, key);
3590
+ if (!config) {
3591
+ return {
3592
+ success: false,
3593
+ message: "MCP server configuration not found"
3594
+ };
3595
+ }
3596
+ return {
3597
+ success: true,
3598
+ message: "MCP server configuration retrieved successfully",
3599
+ data: config
3600
+ };
3601
+ } catch (error) {
3602
+ console.error("Failed to get MCP server config:", error);
3603
+ return {
3604
+ success: false,
3605
+ message: "Failed to retrieve MCP server configuration"
3606
+ };
3607
+ }
3608
+ }
3609
+ async function createMcpServerConfig(request, reply) {
3610
+ const tenantId = getTenantId3(request);
3611
+ const body = request.body;
3612
+ try {
3613
+ const storeLattice = getStoreLattice8("default", "mcp");
3614
+ const store = storeLattice.store;
3615
+ const existing = await store.getConfigByKey(tenantId, body.key);
3616
+ if (existing) {
3617
+ reply.code(409);
3618
+ return {
3619
+ success: false,
3620
+ message: "MCP server configuration with this key already exists"
3621
+ };
3622
+ }
3623
+ const id = body.id || randomUUID5();
3624
+ const config = await store.createConfig(tenantId, id, body);
3625
+ try {
3626
+ await connectAndRegisterTools(config);
3627
+ await store.updateConfig(tenantId, id, { status: "connected" });
3628
+ config.status = "connected";
3629
+ } catch (error) {
3630
+ console.warn("Failed to auto-connect MCP server:", error);
3631
+ await store.updateConfig(tenantId, id, { status: "error" });
3632
+ config.status = "error";
3633
+ }
3634
+ reply.code(201);
3635
+ return {
3636
+ success: true,
3637
+ message: "MCP server configuration created successfully",
3638
+ data: config
3639
+ };
3640
+ } catch (error) {
3641
+ console.error("Failed to create MCP server config:", error);
3642
+ return {
3643
+ success: false,
3644
+ message: "Failed to create MCP server configuration"
3645
+ };
3646
+ }
3647
+ }
3648
+ async function updateMcpServerConfig(request, reply) {
3649
+ const tenantId = getTenantId3(request);
3650
+ const { key } = request.params;
3651
+ const updates = request.body;
3652
+ try {
3653
+ const storeLattice = getStoreLattice8("default", "mcp");
3654
+ const store = storeLattice.store;
3655
+ const existing = await store.getConfigByKey(tenantId, key);
3656
+ if (!existing) {
3657
+ reply.code(404);
3658
+ return {
3659
+ success: false,
3660
+ message: "MCP server configuration not found"
3661
+ };
3662
+ }
3663
+ const shouldReconnect = updates.config !== void 0;
3664
+ const updated = await store.updateConfig(tenantId, existing.id, updates);
3665
+ if (!updated) {
3666
+ return {
3667
+ success: false,
3668
+ message: "Failed to update MCP server configuration"
3669
+ };
3670
+ }
3671
+ if (shouldReconnect) {
3672
+ try {
3673
+ if (mcpManager.hasServer(key)) {
3674
+ await mcpManager.removeServer(key);
3675
+ }
3676
+ await connectAndRegisterTools(updated);
3677
+ await store.updateConfig(tenantId, existing.id, { status: "connected" });
3678
+ updated.status = "connected";
3679
+ } catch (error) {
3680
+ console.warn("Failed to reconnect MCP server:", error);
3681
+ await store.updateConfig(tenantId, existing.id, { status: "error" });
3682
+ updated.status = "error";
3683
+ }
3684
+ }
3685
+ return {
3686
+ success: true,
3687
+ message: "MCP server configuration updated successfully",
3688
+ data: updated
3689
+ };
3690
+ } catch (error) {
3691
+ console.error("Failed to update MCP server config:", error);
3692
+ return {
3693
+ success: false,
3694
+ message: "Failed to update MCP server configuration"
3695
+ };
3696
+ }
3697
+ }
3698
+ async function deleteMcpServerConfig(request, reply) {
3699
+ const tenantId = getTenantId3(request);
3700
+ const { keyOrId } = request.params;
3701
+ try {
3702
+ const storeLattice = getStoreLattice8("default", "mcp");
3703
+ const store = storeLattice.store;
3704
+ let config = await store.getConfigByKey(tenantId, keyOrId);
3705
+ let configKey = keyOrId;
3706
+ if (!config) {
3707
+ config = await store.getConfigById(tenantId, keyOrId);
3708
+ if (config) {
3709
+ configKey = config.key;
3710
+ }
3711
+ }
3712
+ if (!config) {
3713
+ reply.code(404);
3714
+ return {
3715
+ success: false,
3716
+ message: "MCP server configuration not found"
3717
+ };
3718
+ }
3719
+ try {
3720
+ if (mcpManager.hasServer(configKey)) {
3721
+ await mcpManager.removeServer(configKey);
3722
+ }
3723
+ } catch (error) {
3724
+ console.warn("Failed to remove from MCP manager:", error);
3725
+ }
3726
+ const deleted = await store.deleteConfig(tenantId, config.id);
3727
+ if (!deleted) {
3728
+ return {
3729
+ success: false,
3730
+ message: "Failed to delete MCP server configuration"
3731
+ };
3732
+ }
3733
+ return {
3734
+ success: true,
3735
+ message: "MCP server configuration deleted successfully"
3736
+ };
3737
+ } catch (error) {
3738
+ console.error("Failed to delete MCP server config:", error);
3739
+ return {
3740
+ success: false,
3741
+ message: "Failed to delete MCP server configuration"
3742
+ };
3743
+ }
3744
+ }
3745
+ async function testMcpServerConnection(request, reply) {
3746
+ const tenantId = getTenantId3(request);
3747
+ const { key } = request.params;
3748
+ try {
3749
+ const storeLattice = getStoreLattice8("default", "mcp");
3750
+ const store = storeLattice.store;
3751
+ const config = await store.getConfigByKey(tenantId, key);
3752
+ if (!config) {
3753
+ reply.code(404);
3754
+ return {
3755
+ success: false,
3756
+ message: "MCP server configuration not found"
3757
+ };
3758
+ }
3759
+ const startTime = Date.now();
3760
+ try {
3761
+ const testKey = `__test_${key}_${Date.now()}`;
3762
+ const connection = convertToConnection(config.config);
3763
+ mcpManager.addServer(testKey, connection);
3764
+ await mcpManager.connect();
3765
+ const tools = await mcpManager.getAllTools();
3766
+ const latency = Date.now() - startTime;
3767
+ await mcpManager.removeServer(testKey);
3768
+ return {
3769
+ success: true,
3770
+ message: "Connection test successful",
3771
+ data: {
3772
+ connected: true,
3773
+ latency
3774
+ }
3775
+ };
3776
+ } catch (error) {
3777
+ return {
3778
+ success: true,
3779
+ message: "Connection test failed",
3780
+ data: {
3781
+ connected: false,
3782
+ error: error instanceof Error ? error.message : "Unknown error"
3783
+ }
3784
+ };
3785
+ }
3786
+ } catch (error) {
3787
+ console.error("Failed to test MCP server connection:", error);
3788
+ return {
3789
+ success: false,
3790
+ message: "Failed to test MCP server connection",
3791
+ data: {
3792
+ connected: false,
3793
+ error: error instanceof Error ? error.message : "Unknown error"
3794
+ }
3795
+ };
3796
+ }
3797
+ }
3798
+ async function listMcpServerTools(request, reply) {
3799
+ const tenantId = getTenantId3(request);
3800
+ const { key } = request.params;
3801
+ try {
3802
+ const storeLattice = getStoreLattice8("default", "mcp");
3803
+ const store = storeLattice.store;
3804
+ const config = await store.getConfigByKey(tenantId, key);
3805
+ if (!config) {
3806
+ reply.code(404);
3807
+ return {
3808
+ success: false,
3809
+ message: "MCP server configuration not found"
3810
+ };
3811
+ }
3812
+ if (!mcpManager.hasServer(key)) {
3813
+ await connectAndRegisterTools(config);
3814
+ }
3815
+ const tools = await mcpManager.getAllTools();
3816
+ return {
3817
+ success: true,
3818
+ message: "Tools retrieved successfully",
3819
+ data: {
3820
+ tools
3821
+ }
3822
+ };
3823
+ } catch (error) {
3824
+ console.error("Failed to list MCP tools:", error);
3825
+ return {
3826
+ success: false,
3827
+ message: "Failed to retrieve tools"
3828
+ };
3829
+ }
3830
+ }
3831
+ async function connectMcpServer(request, reply) {
3832
+ const tenantId = getTenantId3(request);
3833
+ const { key } = request.params;
3834
+ try {
3835
+ const storeLattice = getStoreLattice8("default", "mcp");
3836
+ const store = storeLattice.store;
3837
+ const config = await store.getConfigByKey(tenantId, key);
3838
+ if (!config) {
3839
+ reply.code(404);
3840
+ return {
3841
+ success: false,
3842
+ message: "MCP server configuration not found"
3843
+ };
3844
+ }
3845
+ await connectAndRegisterTools(config);
3846
+ const updated = await store.updateConfig(tenantId, config.id, {
3847
+ status: "connected"
3848
+ });
3849
+ return {
3850
+ success: true,
3851
+ message: "MCP server connected successfully",
3852
+ data: updated || config
3853
+ };
3854
+ } catch (error) {
3855
+ console.error("Failed to connect MCP server:", error);
3856
+ const storeLattice = getStoreLattice8("default", "mcp");
3857
+ const store = storeLattice.store;
3858
+ const config = await store.getConfigByKey(tenantId, key);
3859
+ if (config) {
3860
+ await store.updateConfig(tenantId, config.id, { status: "error" });
3861
+ }
3862
+ return {
3863
+ success: false,
3864
+ message: `Failed to connect MCP server: ${error instanceof Error ? error.message : "Unknown error"}`
3865
+ };
3866
+ }
3867
+ }
3868
+ async function disconnectMcpServer(request, reply) {
3869
+ const tenantId = getTenantId3(request);
3870
+ const { key } = request.params;
3871
+ try {
3872
+ const storeLattice = getStoreLattice8("default", "mcp");
3873
+ const store = storeLattice.store;
3874
+ const config = await store.getConfigByKey(tenantId, key);
3875
+ if (!config) {
3876
+ reply.code(404);
3877
+ return {
3878
+ success: false,
3879
+ message: "MCP server configuration not found"
3880
+ };
3881
+ }
3882
+ if (mcpManager.hasServer(key)) {
3883
+ await mcpManager.removeServer(key);
3884
+ }
3885
+ const updated = await store.updateConfig(tenantId, config.id, {
3886
+ status: "disconnected"
3887
+ });
3888
+ return {
3889
+ success: true,
3890
+ message: "MCP server disconnected successfully",
3891
+ data: updated || config
3892
+ };
3893
+ } catch (error) {
3894
+ console.error("Failed to disconnect MCP server:", error);
3895
+ return {
3896
+ success: false,
3897
+ message: "Failed to disconnect MCP server"
3898
+ };
3899
+ }
3900
+ }
3901
+ async function testMcpServerTools(request, reply) {
3902
+ const body = request.body;
3903
+ try {
3904
+ if (!body.config) {
3905
+ reply.code(400);
3906
+ return {
3907
+ success: false,
3908
+ message: "config is required"
3909
+ };
3910
+ }
3911
+ const testKey = `__test_${Date.now()}`;
3912
+ const connection = convertToConnection(body.config);
3913
+ mcpManager.addServer(testKey, connection);
3914
+ await mcpManager.connect();
3915
+ const tools = await mcpManager.getAllTools();
3916
+ await mcpManager.removeServer(testKey);
3917
+ return {
3918
+ success: true,
3919
+ message: "Tools retrieved successfully",
3920
+ data: {
3921
+ tools
3922
+ }
3923
+ };
3924
+ } catch (error) {
3925
+ console.error("Failed to test MCP server tools:", error);
3926
+ return {
3927
+ success: false,
3928
+ message: `Failed to retrieve tools: ${error instanceof Error ? error.message : String(error)}`
3929
+ };
3930
+ }
3931
+ }
3932
+ function convertToConnection(config) {
3933
+ const baseConfig = {
3934
+ env: config.env
3935
+ };
3936
+ if (config.transport === "stdio") {
3937
+ return {
3938
+ ...baseConfig,
3939
+ transport: "stdio",
3940
+ command: config.command,
3941
+ args: config.args || []
3942
+ };
3943
+ } else {
3944
+ return {
3945
+ ...baseConfig,
3946
+ transport: config.transport === "streamable_http" ? "http" : config.transport,
3947
+ url: config.url
3948
+ };
3949
+ }
3950
+ }
3951
+ async function connectAndRegisterTools(config) {
3952
+ const connection = convertToConnection(config.config);
3953
+ mcpManager.addServer(config.key, connection);
3954
+ await mcpManager.connect();
3955
+ const allTools = await mcpManager.getAllTools();
3956
+ const selectedTools = allTools.filter(
3957
+ (tool) => config.selectedTools.includes(tool.name)
3958
+ );
3959
+ for (const tool of selectedTools) {
3960
+ toolLatticeManager2.registerExistingTool(tool.name, tool);
3961
+ }
3962
+ }
3963
+ function registerMcpServerConfigRoutes(app2) {
3964
+ app2.get("/api/mcp-servers", getMcpServerConfigList);
3965
+ app2.get("/api/mcp-servers/:key", getMcpServerConfig);
3966
+ app2.post("/api/mcp-servers", createMcpServerConfig);
3967
+ app2.put("/api/mcp-servers/:key", updateMcpServerConfig);
3968
+ app2.delete("/api/mcp-servers/:keyOrId", deleteMcpServerConfig);
3969
+ app2.post("/api/mcp-servers/:key/test", testMcpServerConnection);
3970
+ app2.get("/api/mcp-servers/:key/tools", listMcpServerTools);
3971
+ app2.post("/api/mcp-servers/:key/connect", connectMcpServer);
3972
+ app2.post("/api/mcp-servers/:key/disconnect", disconnectMcpServer);
3973
+ app2.post("/api/mcp-servers/test-tools", testMcpServerTools);
3974
+ }
3975
+
3547
3976
  // src/controllers/users.ts
3548
- import { getStoreLattice as getStoreLattice8 } from "@axiom-lattice/core";
3977
+ import { getStoreLattice as getStoreLattice9 } from "@axiom-lattice/core";
3549
3978
  import { v4 as uuidv42 } from "uuid";
3550
3979
  var UsersController = class {
3551
3980
  constructor() {
3552
- this.userStore = getStoreLattice8("default", "user").store;
3981
+ this.userStore = getStoreLattice9("default", "user").store;
3553
3982
  }
3554
3983
  async listUsers(request, reply) {
3555
3984
  const { email } = request.query;
@@ -3627,11 +4056,11 @@ function registerUserRoutes(app2) {
3627
4056
  }
3628
4057
 
3629
4058
  // src/controllers/tenants.ts
3630
- import { getStoreLattice as getStoreLattice9 } from "@axiom-lattice/core";
4059
+ import { getStoreLattice as getStoreLattice10 } from "@axiom-lattice/core";
3631
4060
  import { v4 as uuidv43 } from "uuid";
3632
4061
  var TenantsController = class {
3633
4062
  constructor() {
3634
- this.tenantStore = getStoreLattice9("default", "tenant").store;
4063
+ this.tenantStore = getStoreLattice10("default", "tenant").store;
3635
4064
  }
3636
4065
  // ==================== Tenant CRUD ====================
3637
4066
  async listTenants(request, reply) {
@@ -3695,7 +4124,7 @@ function registerTenantRoutes(app2) {
3695
4124
  }
3696
4125
 
3697
4126
  // src/controllers/auth.ts
3698
- import { getStoreLattice as getStoreLattice10 } from "@axiom-lattice/core";
4127
+ import { getStoreLattice as getStoreLattice11 } from "@axiom-lattice/core";
3699
4128
  import { v4 as uuidv44 } from "uuid";
3700
4129
  var defaultAuthConfig = {
3701
4130
  autoApproveUsers: true,
@@ -3704,9 +4133,9 @@ var defaultAuthConfig = {
3704
4133
  };
3705
4134
  var AuthController = class {
3706
4135
  constructor(config = {}) {
3707
- this.userStore = getStoreLattice10("default", "user").store;
3708
- this.tenantStore = getStoreLattice10("default", "tenant").store;
3709
- this.userTenantLinkStore = getStoreLattice10("default", "userTenantLink").store;
4136
+ this.userStore = getStoreLattice11("default", "user").store;
4137
+ this.tenantStore = getStoreLattice11("default", "tenant").store;
4138
+ this.userTenantLinkStore = getStoreLattice11("default", "userTenantLink").store;
3710
4139
  this.config = { ...defaultAuthConfig, ...config };
3711
4140
  }
3712
4141
  async register(request, reply) {
@@ -4143,6 +4572,7 @@ var registerLatticeRoutes = (app2) => {
4143
4572
  registerWorkspaceRoutes(app2);
4144
4573
  registerDatabaseConfigRoutes(app2);
4145
4574
  registerMetricsServerConfigRoutes(app2);
4575
+ registerMcpServerConfigRoutes(app2);
4146
4576
  registerUserRoutes(app2);
4147
4577
  registerTenantRoutes(app2);
4148
4578
  registerAuthRoutes(app2, {